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