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 ***** */
39
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"
56
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 };
64
65 #if JS_HAS_TOSOURCE
66 #include "jsprf.h"
67
68 static JSBool
69 bool_toSource(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
70       jsval *rval)
71 0 {
72 0     jsval v;
73 0     char buf[32];
74 0     JSString *str;
75
76 0     if (!JS_InstanceOf(cx, obj, &boolean_class, argv))
77 0 return JS_FALSE;
78 0     v = OBJ_GET_SLOT(cx, obj, JSSLOT_PRIVATE);
79 0     if (!JSVAL_IS_BOOLEAN(v))
80 0 return js_obj_toSource(cx, obj, argc, argv, rval);
81 0     JS_snprintf(buf, sizeof buf, "(new %s(%s))",
82 boolean_class.name,
83 js_boolean_str[JSVAL_TO_BOOLEAN(v) ? 1 : 0]);
84 0     str = JS_NewStringCopyZ(cx, buf);
85 0     if (!str)
86 0 return JS_FALSE;
87 0     *rval = STRING_TO_JSVAL(str);
88 0     return JS_TRUE;
89 }
90 #endif
91
92 static JSBool
93 bool_toString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
94       jsval *rval)
95 0 {
96 0     jsval v;
97 0     JSAtom *atom;
98 0     JSString *str;
99
100 0     if (!JS_InstanceOf(cx, obj, &boolean_class, argv))
101 0 return JS_FALSE;
102 0     v = OBJ_GET_SLOT(cx, obj, JSSLOT_PRIVATE);
103 0     if (!JSVAL_IS_BOOLEAN(v))
104 0 return js_obj_toString(cx, obj, argc, argv, rval);
105 0     atom = cx->runtime->atomState.booleanAtoms[JSVAL_TO_BOOLEAN(v) ? 1 : 0];
106 0     str = ATOM_TO_STRING(atom);
107 0     if (!str)
108 0 return JS_FALSE;
109 0     *rval = STRING_TO_JSVAL(str);
110 0     return JS_TRUE;
111 }
112
113 static JSBool
114 bool_valueOf(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
115 0 {
116 0     if (!JS_InstanceOf(cx, obj, &boolean_class, argv))
117 0 return JS_FALSE;
118 0     *rval = OBJ_GET_SLOT(cx, obj, JSSLOT_PRIVATE);
119 0     return JS_TRUE;
120 }
121
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 };
130
131 #ifdef XP_MAC
132 #undef Boolean
133 #define Boolean js_Boolean
134 #endif
135
136 static JSBool
137 Boolean(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
138 0 {
139 0     JSBool b;
140 0     jsval bval;
141
142 0     if (argc != 0) {
143 0 if (!js_ValueToBoolean(cx, argv[0], &b))
144 0     return JS_FALSE;
145 0 bval = BOOLEAN_TO_JSVAL(b);
146     } else {
147 0 bval = JSVAL_FALSE;
148     }
149 0     if (!(cx->fp->flags & JSFRAME_CONSTRUCTING)) {
150 0 *rval = bval;
151 0 return JS_TRUE;
152     }
153 0     OBJ_SET_SLOT(cx, obj, JSSLOT_PRIVATE, bval);
154 0     return JS_TRUE;
155 }
156
157 JSObject *
158 js_InitBooleanClass(JSContext *cx, JSObject *obj)
159 17 {
160 17     JSObject *proto;
161
162 17     proto = JS_InitClass(cx, obj, NULL, &boolean_class, Boolean, 1,
163 NULL, boolean_methods, NULL, NULL);
164 17     if (!proto)
165 0 return NULL;
166 17     OBJ_SET_SLOT(cx, proto, JSSLOT_PRIVATE, JSVAL_FALSE);
167 17     return proto;
168 }
169
170 JSObject *
171 js_BooleanToObject(JSContext *cx, JSBool b)
172 0 {
173 0     JSObject *obj;
174
175 0     obj = js_NewObject(cx, &boolean_class, NULL, NULL);
176 0     if (!obj)
177 0 return NULL;
178 0     OBJ_SET_SLOT(cx, obj, JSSLOT_PRIVATE, BOOLEAN_TO_JSVAL(b));
179 0     return obj;
180 }
181
182 JSString *
183 js_BooleanToString(JSContext *cx, JSBool b)
184 0 {
185 0     return ATOM_TO_STRING(cx->runtime->atomState.booleanAtoms[b ? 1 : 0]);
186 }
187
188 JSBool
189 js_ValueToBoolean(JSContext *cx, jsval v, JSBool *bp)
190 0 {
191 0     JSBool b;
192 0     jsdouble d;
193
194 #if defined _MSC_VER && _MSC_VER <= 800
195     /* MSVC1.5 coredumps */
196     if (!bp)
197 return JS_TRUE;
198     /* This should be an if-else chain, but MSVC1.5 crashes if it is. */
199 #define ELSE
200 #else
201 #define ELSE else
202 #endif
203
204 0     if (JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v)) {
205 /* Must return early to avoid falling thru to JSVAL_IS_OBJECT case. */
206 0 *bp = JS_FALSE;
207 0 return JS_TRUE;
208     }
209 0     if (JSVAL_IS_OBJECT(v)) {
210 0 if (!JSVERSION_IS_ECMA(cx->version)) {
211 0     if (!OBJ_DEFAULT_VALUE(cx, JSVAL_TO_OBJECT(v), JSTYPE_BOOLEAN, &v))
212 0 return JS_FALSE;
213 0     if (!JSVAL_IS_BOOLEAN(v))
214 0 v = JSVAL_TRUE; /* non-null object is true */
215 0     b = JSVAL_TO_BOOLEAN(v);
216 } else {
217 0     b = JS_TRUE;
218 }
219     } ELSE
220 0     if (JSVAL_IS_STRING(v)) {
221 0 b = JSSTRING_LENGTH(JSVAL_TO_STRING(v)) ? JS_TRUE : JS_FALSE;
222     } ELSE
223 0     if (JSVAL_IS_INT(v)) {
224 0 b = JSVAL_TO_INT(v) ? JS_TRUE : JS_FALSE;
225     } ELSE
226 0     if (JSVAL_IS_DOUBLE(v)) {
227 0 d = *JSVAL_TO_DOUBLE(v);
228 0 b = (!JSDOUBLE_IS_NaN(d) && d != 0) ? JS_TRUE : JS_FALSE;
229     } ELSE
230 #if defined _MSC_VER && _MSC_VER <= 800
231     if (JSVAL_IS_BOOLEAN(v)) {
232 b = JSVAL_TO_BOOLEAN(v);
233     }
234 #else
235     {
236 0         JS_ASSERT(JSVAL_IS_BOOLEAN(v));
237 0 b = JSVAL_TO_BOOLEAN(v);
238     }
239 #endif
240
241 #undef ELSE
242 0     *bp = b;
243 0     return JS_TRUE;