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 : #include "vm/StringBuffer.h"
58 :
59 : #include "jsinferinlines.h"
60 : #include "jsobjinlines.h"
61 :
62 : #include "vm/BooleanObject-inl.h"
63 : #include "vm/MethodGuard-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 108 : bool_toSource(JSContext *cx, unsigned argc, Value *vp)
82 : {
83 108 : CallArgs args = CallArgsFromVp(argc, vp);
84 :
85 : bool b, ok;
86 108 : if (!BoxedPrimitiveMethodGuard(cx, args, bool_toSource, &b, &ok))
87 63 : return ok;
88 :
89 90 : StringBuffer sb(cx);
90 45 : if (!sb.append("(new Boolean(") || !BooleanToStringBuffer(cx, b, sb) || !sb.append("))"))
91 0 : return false;
92 :
93 45 : JSString *str = sb.finishString();
94 45 : if (!str)
95 0 : return false;
96 45 : args.rval().setString(str);
97 45 : return true;
98 : }
99 : #endif
100 :
101 : static JSBool
102 99 : bool_toString(JSContext *cx, unsigned argc, Value *vp)
103 : {
104 99 : CallArgs args = CallArgsFromVp(argc, vp);
105 :
106 : bool b, ok;
107 99 : if (!BoxedPrimitiveMethodGuard<bool>(cx, args, bool_toString, &b, &ok))
108 72 : return ok;
109 :
110 27 : args.rval().setString(cx->runtime->atomState.booleanAtoms[b ? 1 : 0]);
111 27 : 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 333 : Boolean(JSContext *cx, unsigned argc, Value *vp)
138 : {
139 333 : CallArgs args = CallArgsFromVp(argc, vp);
140 :
141 333 : bool b = args.length() != 0 ? js_ValueToBoolean(args[0]) : false;
142 :
143 333 : 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 45 : args.rval().setBoolean(b);
150 : }
151 333 : return true;
152 : }
153 :
154 : JSObject *
155 754 : js_InitBooleanClass(JSContext *cx, JSObject *obj)
156 : {
157 754 : JS_ASSERT(obj->isNative());
158 :
159 754 : GlobalObject *global = &obj->asGlobal();
160 :
161 754 : JSObject *booleanProto = global->createBlankPrototype(cx, &BooleanClass);
162 754 : if (!booleanProto)
163 0 : return NULL;
164 754 : booleanProto->setFixedSlot(BooleanObject::PRIMITIVE_VALUE_SLOT, BooleanValue(false));
165 :
166 754 : JSFunction *ctor = global->createConstructor(cx, Boolean, CLASS_ATOM(cx, Boolean), 1);
167 754 : if (!ctor)
168 0 : return NULL;
169 :
170 754 : if (!LinkConstructorAndPrototype(cx, ctor, booleanProto))
171 0 : return NULL;
172 :
173 754 : if (!DefinePropertiesAndBrand(cx, booleanProto, NULL, boolean_methods))
174 0 : return NULL;
175 :
176 754 : if (!DefineConstructorAndPrototype(cx, global, JSProto_Boolean, ctor, booleanProto))
177 0 : return NULL;
178 :
179 754 : return booleanProto;
180 : }
181 :
182 : JSString *
183 3069 : js_BooleanToString(JSContext *cx, JSBool b)
184 : {
185 3069 : return cx->runtime->atomState.booleanAtoms[b ? 1 : 0];
186 : }
187 :
188 : namespace js {
189 :
190 : bool
191 9 : BooleanGetPrimitiveValueSlow(JSContext *cx, JSObject &obj, Value *vp)
192 : {
193 9 : JS_ASSERT(ObjectClassIs(obj, ESClass_Boolean, cx));
194 9 : JS_ASSERT(obj.isProxy());
195 :
196 : /*
197 : * To respect the proxy abstraction, we can't simply unwrap and call
198 : * getPrimitiveThis on the wrapped object. All we know is that obj says
199 : * its [[Class]] is "Boolean". Boolean.prototype.valueOf is specified to
200 : * return the [[PrimitiveValue]] internal property, so call that instead.
201 : */
202 18 : InvokeArgsGuard ag;
203 9 : if (!cx->stack.pushInvokeArgs(cx, 0, &ag))
204 0 : return false;
205 9 : ag.calleev().setUndefined();
206 9 : ag.thisv().setObject(obj);
207 9 : if (!GetProxyHandler(&obj)->nativeCall(cx, &obj, &BooleanClass, bool_valueOf, ag))
208 0 : return false;
209 9 : *vp = ag.rval();
210 9 : return true;
211 : }
212 :
213 : } /* namespace js */
214 :
215 : JSBool
216 7240051 : js_ValueToBoolean(const Value &v)
217 : {
218 7240051 : if (v.isInt32())
219 4856313 : return v.toInt32() != 0;
220 2383738 : if (v.isString())
221 222346 : return v.toString()->length() != 0;
222 2161392 : if (v.isObject())
223 729968 : return JS_TRUE;
224 1431424 : if (v.isNullOrUndefined())
225 235004 : return JS_FALSE;
226 1196420 : if (v.isDouble()) {
227 : double d;
228 :
229 869647 : d = v.toDouble();
230 869647 : return !JSDOUBLE_IS_NaN(d) && d != 0;
231 : }
232 326773 : JS_ASSERT(v.isBoolean());
233 326773 : return v.toBoolean();
234 : }
|