PageRenderTime 52ms CodeModel.GetById 37ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/js/src/jsbool.cpp

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