Bumping manifests a=b2g-bump
[gecko.git] / js / src / jsbool.cpp
blob9da805e0955dc4608cfe1f170fff21a90d621171
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sts=4 et sw=4 tw=99:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /*
8 * JS boolean implementation.
9 */
11 #include "jsboolinlines.h"
13 #include "jsapi.h"
14 #include "jsatom.h"
15 #include "jscntxt.h"
16 #include "jsobj.h"
17 #include "jstypes.h"
19 #include "vm/GlobalObject.h"
20 #include "vm/ProxyObject.h"
21 #include "vm/StringBuffer.h"
23 #include "vm/BooleanObject-inl.h"
25 using namespace js;
26 using namespace js::types;
28 const Class BooleanObject::class_ = {
29 "Boolean",
30 JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_HAS_CACHED_PROTO(JSProto_Boolean)
33 MOZ_ALWAYS_INLINE bool
34 IsBoolean(HandleValue v)
36 return v.isBoolean() || (v.isObject() && v.toObject().is<BooleanObject>());
39 #if JS_HAS_TOSOURCE
40 MOZ_ALWAYS_INLINE bool
41 bool_toSource_impl(JSContext* cx, CallArgs args)
43 HandleValue thisv = args.thisv();
44 MOZ_ASSERT(IsBoolean(thisv));
46 bool b = thisv.isBoolean() ? thisv.toBoolean() : thisv.toObject().as<BooleanObject>().unbox();
48 StringBuffer sb(cx);
49 if (!sb.append("(new Boolean(") || !BooleanToStringBuffer(b, sb) || !sb.append("))"))
50 return false;
52 JSString* str = sb.finishString();
53 if (!str)
54 return false;
55 args.rval().setString(str);
56 return true;
59 static bool
60 bool_toSource(JSContext* cx, unsigned argc, Value* vp)
62 CallArgs args = CallArgsFromVp(argc, vp);
63 return CallNonGenericMethod<IsBoolean, bool_toSource_impl>(cx, args);
65 #endif
67 MOZ_ALWAYS_INLINE bool
68 bool_toString_impl(JSContext* cx, CallArgs args)
70 HandleValue thisv = args.thisv();
71 MOZ_ASSERT(IsBoolean(thisv));
73 bool b = thisv.isBoolean() ? thisv.toBoolean() : thisv.toObject().as<BooleanObject>().unbox();
74 args.rval().setString(js_BooleanToString(cx, b));
75 return true;
78 static bool
79 bool_toString(JSContext* cx, unsigned argc, Value* vp)
81 CallArgs args = CallArgsFromVp(argc, vp);
82 return CallNonGenericMethod<IsBoolean, bool_toString_impl>(cx, args);
85 MOZ_ALWAYS_INLINE bool
86 bool_valueOf_impl(JSContext* cx, CallArgs args)
88 HandleValue thisv = args.thisv();
89 MOZ_ASSERT(IsBoolean(thisv));
91 bool b = thisv.isBoolean() ? thisv.toBoolean() : thisv.toObject().as<BooleanObject>().unbox();
92 args.rval().setBoolean(b);
93 return true;
96 static bool
97 bool_valueOf(JSContext* cx, unsigned argc, Value* vp)
99 CallArgs args = CallArgsFromVp(argc, vp);
100 return CallNonGenericMethod<IsBoolean, bool_valueOf_impl>(cx, args);
103 static const JSFunctionSpec boolean_methods[] = {
104 #if JS_HAS_TOSOURCE
105 JS_FN(js_toSource_str, bool_toSource, 0, 0),
106 #endif
107 JS_FN(js_toString_str, bool_toString, 0, 0),
108 JS_FN(js_valueOf_str, bool_valueOf, 0, 0),
109 JS_FS_END
112 static bool
113 Boolean(JSContext* cx, unsigned argc, Value* vp)
115 CallArgs args = CallArgsFromVp(argc, vp);
117 bool b = args.length() != 0 ? JS::ToBoolean(args[0]) : false;
119 if (args.isConstructing()) {
120 JSObject* obj = BooleanObject::create(cx, b);
121 if (!obj)
122 return false;
123 args.rval().setObject(*obj);
124 } else {
125 args.rval().setBoolean(b);
127 return true;
130 JSObject*
131 js_InitBooleanClass(JSContext* cx, HandleObject obj)
133 MOZ_ASSERT(obj->isNative());
135 Rooted<GlobalObject*> global(cx, &obj->as<GlobalObject>());
137 Rooted<BooleanObject*> booleanProto(cx, global->createBlankPrototype<BooleanObject>(cx));
138 if (!booleanProto)
139 return nullptr;
140 booleanProto->setFixedSlot(BooleanObject::PRIMITIVE_VALUE_SLOT, BooleanValue(false));
142 RootedFunction ctor(cx, global->createConstructor(cx, Boolean, cx->names().Boolean, 1));
143 if (!ctor)
144 return nullptr;
146 if (!LinkConstructorAndPrototype(cx, ctor, booleanProto))
147 return nullptr;
149 if (!DefinePropertiesAndFunctions(cx, booleanProto, nullptr, boolean_methods))
150 return nullptr;
152 if (!GlobalObject::initBuiltinConstructor(cx, global, JSProto_Boolean, ctor, booleanProto))
153 return nullptr;
155 return booleanProto;
158 JSString*
159 js_BooleanToString(ExclusiveContext* cx, bool b)
161 return b ? cx->names().true_ : cx->names().false_;
164 JS_PUBLIC_API(bool)
165 js::ToBooleanSlow(HandleValue v)
167 if (v.isString())
168 return v.toString()->length() != 0;
170 MOZ_ASSERT(v.isObject());
171 return !EmulatesUndefined(&v.toObject());