1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3
* ***** BEGIN LICENSE BLOCK *****
4
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
16
* The Original Code is Mozilla Communicator client code, released
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.
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.
38
* ***** END LICENSE BLOCK ***** */
41
* JS boolean implementation.
45
#include "jsutil.h" /* Added by JSIFY */
57
JSClass js_BooleanClass = {
60
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
61
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
62
JSCLASS_NO_OPTIONAL_MEMBERS
69
bool_toSource(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
76
if (!JS_InstanceOf(cx, obj, &js_BooleanClass, argv))
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))",
83
js_boolean_str[JSVAL_TO_BOOLEAN(v) ? 1 : 0]);
84
str = JS_NewStringCopyZ(cx, buf);
87
*rval = STRING_TO_JSVAL(str);
93
bool_toString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
100
if (!JS_InstanceOf(cx, obj, &js_BooleanClass, argv))
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);
109
*rval = STRING_TO_JSVAL(str);
114
bool_valueOf(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
116
if (!JS_InstanceOf(cx, obj, &js_BooleanClass, argv))
118
*rval = OBJ_GET_SLOT(cx, obj, JSSLOT_PRIVATE);
122
static JSFunctionSpec boolean_methods[] = {
124
{js_toSource_str, bool_toSource, 0,0,0},
126
{js_toString_str, bool_toString, 0,0,0},
127
{js_valueOf_str, bool_valueOf, 0,0,0},
132
Boolean(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
138
if (!js_ValueToBoolean(cx, argv[0], &b))
140
bval = BOOLEAN_TO_JSVAL(b);
144
if (!(cx->fp->flags & JSFRAME_CONSTRUCTING)) {
148
OBJ_SET_SLOT(cx, obj, JSSLOT_PRIVATE, bval);
153
js_InitBooleanClass(JSContext *cx, JSObject *obj)
157
proto = JS_InitClass(cx, obj, NULL, &js_BooleanClass, Boolean, 1,
158
NULL, boolean_methods, NULL, NULL);
161
OBJ_SET_SLOT(cx, proto, JSSLOT_PRIVATE, JSVAL_FALSE);
166
js_BooleanToObject(JSContext *cx, JSBool b)
170
obj = js_NewObject(cx, &js_BooleanClass, NULL, NULL);
173
OBJ_SET_SLOT(cx, obj, JSSLOT_PRIVATE, BOOLEAN_TO_JSVAL(b));
178
js_BooleanToString(JSContext *cx, JSBool b)
180
return ATOM_TO_STRING(cx->runtime->atomState.booleanAtoms[b ? 1 : 0]);
184
js_ValueToBoolean(JSContext *cx, jsval v, JSBool *bp)
189
if (JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v)) {
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))
195
if (!JSVAL_IS_BOOLEAN(v))
196
v = JSVAL_TRUE; /* non-null object is true */
197
b = JSVAL_TO_BOOLEAN(v);
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;
209
JS_ASSERT(JSVAL_IS_BOOLEAN(v));
210
b = JSVAL_TO_BOOLEAN(v);