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 "jstypes.h"
44 : #include "jsutil.h"
45 : #include "jsapi.h"
46 : #include "jsatom.h"
47 : #include "jsbool.h"
48 : #include "jscntxt.h"
49 : #include "jsinfer.h"
50 : #include "jsversion.h"
51 : #include "jslock.h"
52 : #include "jsnum.h"
53 : #include "jsobj.h"
54 : #include "jsstr.h"
55 :
56 : #include "vm/GlobalObject.h"
57 :
58 : #include "jsinferinlines.h"
59 : #include "jsobjinlines.h"
60 :
61 : #include "vm/BooleanObject-inl.h"
62 : #include "vm/MethodGuard-inl.h"
63 : #include "vm/StringBuffer-inl.h"
64 :
65 : using namespace js;
66 : using namespace js::types;
67 :
68 : Class js::BooleanClass = {
69 : "Boolean",
70 : JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_HAS_CACHED_PROTO(JSProto_Boolean), JS_PropertyStub, /* addProperty */
71 : JS_PropertyStub, /* delProperty */
72 : JS_PropertyStub, /* getProperty */
73 : JS_StrictPropertyStub, /* setProperty */
74 : JS_EnumerateStub,
75 : JS_ResolveStub,
76 : JS_ConvertStub
77 : };
78 :
79 : #if JS_HAS_TOSOURCE
80 : static JSBool
81 110 : bool_toSource(JSContext *cx, unsigned argc, Value *vp)
82 : {
83 110 : CallArgs args = CallArgsFromVp(argc, vp);
84 :
85 : bool b, ok;
86 110 : if (!BoxedPrimitiveMethodGuard(cx, args, bool_toSource, &b, &ok))
87 63 : return ok;
88 :
89 94 : StringBuffer sb(cx);
90 47 : if (!sb.append("(new Boolean(") || !BooleanToStringBuffer(cx, b, sb) || !sb.append("))"))
91 0 : return false;
92 :
93 47 : JSString *str = sb.finishString();
94 47 : if (!str)
95 0 : return false;
96 47 : args.rval().setString(str);
97 47 : return true;
98 : }
99 : #endif
100 :
101 : static JSBool
102 111 : bool_toString(JSContext *cx, unsigned argc, Value *vp)
103 : {
104 111 : CallArgs args = CallArgsFromVp(argc, vp);
105 :
106 : bool b, ok;
107 111 : if (!BoxedPrimitiveMethodGuard<bool>(cx, args, bool_toString, &b, &ok))
108 72 : return ok;
109 :
110 39 : args.rval().setString(cx->runtime->atomState.booleanAtoms[b ? 1 : 0]);
111 39 : return true;
112 : }
113 :
114 : static JSBool
115 216 : bool_valueOf(JSContext *cx, unsigned argc, Value *vp)
116 : {
117 216 : CallArgs args = CallArgsFromVp(argc, vp);
118 :
119 : bool b, ok;
120 216 : if (!BoxedPrimitiveMethodGuard(cx, args, bool_valueOf, &b, &ok))
121 63 : return ok;
122 :
123 153 : args.rval().setBoolean(b);
124 153 : return true;
125 : }
126 :
127 : static JSFunctionSpec boolean_methods[] = {
128 : #if JS_HAS_TOSOURCE
129 : JS_FN(js_toSource_str, bool_toSource, 0, 0),
130 : #endif
131 : JS_FN(js_toString_str, bool_toString, 0, 0),
132 : JS_FN(js_valueOf_str, bool_valueOf, 0, 0),
133 : JS_FS_END
134 : };
135 :
136 : static JSBool
137 345 : Boolean(JSContext *cx, unsigned argc, Value *vp)
138 : {
139 345 : CallArgs args = CallArgsFromVp(argc, vp);
140 :
141 345 : bool b = args.length() != 0 ? js_ValueToBoolean(args[0]) : false;
142 :
143 345 : if (IsConstructing(vp)) {
144 288 : JSObject *obj = BooleanObject::create(cx, b);
145 288 : if (!obj)
146 0 : return false;
147 288 : args.rval().setObject(*obj);
148 : } else {
149 57 : args.rval().setBoolean(b);
150 : }
151 345 : return true;
152 : }
153 :
154 : JSObject *
155 1058 : js_InitBooleanClass(JSContext *cx, JSObject *obj)
156 : {
157 1058 : JS_ASSERT(obj->isNative());
158 :
159 1058 : GlobalObject *global = &obj->asGlobal();
160 :
161 1058 : JSObject *booleanProto = global->createBlankPrototype(cx, &BooleanClass);
162 1058 : if (!booleanProto)
163 0 : return NULL;
164 1058 : booleanProto->setFixedSlot(BooleanObject::PRIMITIVE_VALUE_SLOT, BooleanValue(false));
165 :
166 : JSFunction *ctor = global->createConstructor(cx, Boolean, &BooleanClass,
167 1058 : CLASS_ATOM(cx, Boolean), 1);
168 1058 : if (!ctor)
169 0 : return NULL;
170 :
171 1058 : if (!LinkConstructorAndPrototype(cx, ctor, booleanProto))
172 0 : return NULL;
173 :
174 1058 : if (!DefinePropertiesAndBrand(cx, booleanProto, NULL, boolean_methods))
175 0 : return NULL;
176 :
177 1058 : if (!DefineConstructorAndPrototype(cx, global, JSProto_Boolean, ctor, booleanProto))
178 0 : return NULL;
179 :
180 1058 : return booleanProto;
181 : }
182 :
183 : JSString *
184 121861 : js_BooleanToString(JSContext *cx, JSBool b)
185 : {
186 121861 : return cx->runtime->atomState.booleanAtoms[b ? 1 : 0];
187 : }
188 :
189 : namespace js {
190 :
191 : bool
192 9 : BooleanGetPrimitiveValueSlow(JSContext *cx, JSObject &obj, Value *vp)
193 : {
194 9 : JS_ASSERT(ObjectClassIs(obj, ESClass_Boolean, cx));
195 9 : JS_ASSERT(obj.isProxy());
196 :
197 : /*
198 : * To respect the proxy abstraction, we can't simply unwrap and call
199 : * getPrimitiveThis on the wrapped object. All we know is that obj says
200 : * its [[Class]] is "Boolean". Boolean.prototype.valueOf is specified to
201 : * return the [[PrimitiveValue]] internal property, so call that instead.
202 : */
203 18 : InvokeArgsGuard ag;
204 9 : if (!cx->stack.pushInvokeArgs(cx, 0, &ag))
205 0 : return false;
206 9 : ag.calleev().setUndefined();
207 9 : ag.thisv().setObject(obj);
208 9 : if (!GetProxyHandler(&obj)->nativeCall(cx, &obj, &BooleanClass, bool_valueOf, ag))
209 0 : return false;
210 9 : *vp = ag.rval();
211 9 : return true;
212 : }
213 :
214 : } /* namespace js */
215 :
216 : JSBool
217 10656554 : js_ValueToBoolean(const Value &v)
218 : {
219 10656554 : if (v.isInt32())
220 5050419 : return v.toInt32() != 0;
221 5606135 : if (v.isString())
222 478847 : return v.toString()->length() != 0;
223 5127288 : if (v.isObject())
224 1089661 : return JS_TRUE;
225 4037627 : if (v.isNullOrUndefined())
226 1124234 : return JS_FALSE;
227 2913393 : if (v.isDouble()) {
228 : double d;
229 :
230 880478 : d = v.toDouble();
231 880478 : return !JSDOUBLE_IS_NaN(d) && d != 0;
232 : }
233 2032915 : JS_ASSERT(v.isBoolean());
234 2032915 : return v.toBoolean();
235 : }
|