1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
18 *
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
40 /*
41 * JS boolean implementation.
42 */
43 #include "jsstddef.h"
44 #include "jstypes.h"
45 #include "jsutil.h" /* Added by JSIFY */
46 #include "jsapi.h"
47 #include "jsatom.h"
48 #include "jsbool.h"
49 #include "jscntxt.h"
50 #include "jsconfig.h"
51 #include "jsinterp.h"
52 #include "jslock.h"
53 #include "jsnum.h"
54 #include "jsobj.h"
55 #include "jsstr.h"
57 static JSClass boolean_class = {
58 "Boolean",
59 JSCLASS_HAS_PRIVATE,
60 JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
61 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
62 JSCLASS_NO_OPTIONAL_MEMBERS
63 };
65 #if JS_HAS_TOSOURCE
66 #include "jsprf.h"
68 static JSBool
69 bool_toSource(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
70 jsval *rval)
71 {
72 jsval v;
73 char buf[32];
74 JSString *str;
76 if (!JS_InstanceOf(cx, obj, &boolean_class, argv))
77 return JS_FALSE;
78 v = OBJ_GET_SLOT(cx, obj, JSSLOT_PRIVATE);
79 if (!JSVAL_IS_BOOLEAN(v))
80 return js_obj_toSource(cx, obj, argc, argv, rval);
81 JS_snprintf(buf, sizeof buf, "(new %s(%s))",
82 boolean_class.name,
83 js_boolean_str[JSVAL_TO_BOOLEAN(v) ? 1 : 0]);
84 str = JS_NewStringCopyZ(cx, buf);
85 if (!str)
86 return JS_FALSE;
87 *rval = STRING_TO_JSVAL(str);
88 return JS_TRUE;
89 }
90 #endif
92 static JSBool
93 bool_toString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
94 jsval *rval)
95 {
96 jsval v;
97 JSAtom *atom;
98 JSString *str;
100 if (!JS_InstanceOf(cx, obj, &boolean_class, argv))
101 return JS_FALSE;
102 v = OBJ_GET_SLOT(cx, obj, JSSLOT_PRIVATE);
103 if (!JSVAL_IS_BOOLEAN(v))
104 return js_obj_toString(cx, obj, argc, argv, rval);
105 atom = cx->runtime->atomState.booleanAtoms[JSVAL_TO_BOOLEAN(v) ? 1 : 0];
106 str = ATOM_TO_STRING(atom);
107 if (!str)
108 return JS_FALSE;
109 *rval = STRING_TO_JSVAL(str);
110 return JS_TRUE;
111 }
113 static JSBool
114 bool_valueOf(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
115 {
116 if (!JS_InstanceOf(cx, obj, &boolean_class, argv))
117 return JS_FALSE;
118 *rval = OBJ_GET_SLOT(cx, obj, JSSLOT_PRIVATE);
119 return JS_TRUE;
120 }
122 static JSFunctionSpec boolean_methods[] = {
123 #if JS_HAS_TOSOURCE
124 {js_toSource_str, bool_toSource, 0,0,0},
125 #endif
126 {js_toString_str, bool_toString, 0,0,0},
127 {js_valueOf_str, bool_valueOf, 0,0,0},
128 {0,0,0,0,0}
129 };
131 #ifdef XP_MAC
132 #undef Boolean
133 #define Boolean js_Boolean
134 #endif
136 static JSBool
137 Boolean(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
138 {
139 JSBool b;
140 jsval bval;
142 if (argc != 0) {
143 if (!js_ValueToBoolean(cx, argv[0], &b))
144 return JS_FALSE;
145 bval = BOOLEAN_TO_JSVAL(b);
146 } else {
147 bval = JSVAL_FALSE;
148 }
149 if (!(cx->fp->flags & JSFRAME_CONSTRUCTING)) {
150 *rval = bval;
151 return JS_TRUE;
152 }
153 OBJ_SET_SLOT(cx, obj, JSSLOT_PRIVATE, bval);
154 return JS_TRUE;
155 }
157 JSObject *
158 js_InitBooleanClass(JSContext *cx, JSObject *obj)
159 {
160 JSObject *proto;
162 proto = JS_InitClass(cx, obj, NULL, &boolean_class, Boolean, 1,
163 NULL, boolean_methods, NULL, NULL);
164 if (!proto)
165 return NULL;
166 OBJ_SET_SLOT(cx, proto, JSSLOT_PRIVATE, JSVAL_FALSE);
167 return proto;
168 }
170 JSObject *
171 js_BooleanToObject(JSContext *cx, JSBool b)
172 {
173 JSObject *obj;
175 obj = js_NewObject(cx, &boolean_class, NULL, NULL);
176 if (!obj)
177 return NULL;
178 OBJ_SET_SLOT(cx, obj, JSSLOT_PRIVATE, BOOLEAN_TO_JSVAL(b));
179 return obj;
180 }
182 JSString *
183 js_BooleanToString(JSContext *cx, JSBool b)
184 {
185 return ATOM_TO_STRING(cx->runtime->atomState.booleanAtoms[b ? 1 : 0]);
186 }
188 JSBool
189 js_ValueToBoolean(JSContext *cx, jsval v, JSBool *bp)
190 {
191 JSBool b;
192 jsdouble d;
194 if (JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v)) {
195 b = JS_FALSE;
196 } else if (JSVAL_IS_OBJECT(v)) {
197 if (!JSVERSION_IS_ECMA(cx->version)) {
198 if (!OBJ_DEFAULT_VALUE(cx, JSVAL_TO_OBJECT(v), JSTYPE_BOOLEAN, &v))
199 return JS_FALSE;
200 if (!JSVAL_IS_BOOLEAN(v))
201 v = JSVAL_TRUE; /* non-null object is true */
202 b = JSVAL_TO_BOOLEAN(v);
203 } else {
204 b = JS_TRUE;
205 }
206 } else if (JSVAL_IS_STRING(v)) {
207 b = JSSTRING_LENGTH(JSVAL_TO_STRING(v)) ? JS_TRUE : JS_FALSE;
208 } else if (JSVAL_IS_INT(v)) {
209 b = JSVAL_TO_INT(v) ? JS_TRUE : JS_FALSE;
210 } else if (JSVAL_IS_DOUBLE(v)) {
211 d = *JSVAL_TO_DOUBLE(v);
212 b = (!JSDOUBLE_IS_NaN(d) && d != 0) ? JS_TRUE : JS_FALSE;
213 } else {
214 JS_ASSERT(JSVAL_IS_BOOLEAN(v));
215 b = JSVAL_TO_BOOLEAN(v);
216 }
218 *bp = b;
219 return JS_TRUE;
220 }