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 JSClass js_BooleanClass = {
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, &js_BooleanClass, 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 js_BooleanClass.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, &js_BooleanClass, 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, &js_BooleanClass, 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 static JSBool
132 Boolean(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
133 {
134 JSBool b;
135 jsval bval;
137 if (argc != 0) {
138 if (!js_ValueToBoolean(cx, argv[0], &b))
139 return JS_FALSE;
140 bval = BOOLEAN_TO_JSVAL(b);
141 } else {
142 bval = JSVAL_FALSE;
143 }
144 if (!(cx->fp->flags & JSFRAME_CONSTRUCTING)) {
145 *rval = bval;
146 return JS_TRUE;
147 }
148 OBJ_SET_SLOT(cx, obj, JSSLOT_PRIVATE, bval);
149 return JS_TRUE;
150 }
152 JSObject *
153 js_InitBooleanClass(JSContext *cx, JSObject *obj)
154 {
155 JSObject *proto;
157 proto = JS_InitClass(cx, obj, NULL, &js_BooleanClass, Boolean, 1,
158 NULL, boolean_methods, NULL, NULL);
159 if (!proto)
160 return NULL;
161 OBJ_SET_SLOT(cx, proto, JSSLOT_PRIVATE, JSVAL_FALSE);
162 return proto;
163 }
165 JSObject *
166 js_BooleanToObject(JSContext *cx, JSBool b)
167 {
168 JSObject *obj;
170 obj = js_NewObject(cx, &js_BooleanClass, NULL, NULL);
171 if (!obj)
172 return NULL;
173 OBJ_SET_SLOT(cx, obj, JSSLOT_PRIVATE, BOOLEAN_TO_JSVAL(b));
174 return obj;
175 }
177 JSString *
178 js_BooleanToString(JSContext *cx, JSBool b)
179 {
180 return ATOM_TO_STRING(cx->runtime->atomState.booleanAtoms[b ? 1 : 0]);
181 }
183 JSBool
184 js_ValueToBoolean(JSContext *cx, jsval v, JSBool *bp)
185 {
186 JSBool b;
187 jsdouble d;
189 if (JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v)) {
190 b = JS_FALSE;
191 } else if (JSVAL_IS_OBJECT(v)) {
192 if (!JS_VERSION_IS_ECMA(cx)) {
193 if (!OBJ_DEFAULT_VALUE(cx, JSVAL_TO_OBJECT(v), JSTYPE_BOOLEAN, &v))
194 return JS_FALSE;
195 if (!JSVAL_IS_BOOLEAN(v))
196 v = JSVAL_TRUE; /* non-null object is true */
197 b = JSVAL_TO_BOOLEAN(v);
198 } else {
199 b = JS_TRUE;
200 }
201 } else if (JSVAL_IS_STRING(v)) {
202 b = JSSTRING_LENGTH(JSVAL_TO_STRING(v)) ? JS_TRUE : JS_FALSE;
203 } else if (JSVAL_IS_INT(v)) {
204 b = JSVAL_TO_INT(v) ? JS_TRUE : JS_FALSE;
205 } else if (JSVAL_IS_DOUBLE(v)) {
206 d = *JSVAL_TO_DOUBLE(v);
207 b = (!JSDOUBLE_IS_NaN(d) && d != 0) ? JS_TRUE : JS_FALSE;
208 } else {
209 JS_ASSERT(JSVAL_IS_BOOLEAN(v));
210 b = JSVAL_TO_BOOLEAN(v);
211 }
213 *bp = b;
214 return JS_TRUE;
215 }