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/. */
6
7 /*
8 * JS boolean implementation.
9 */
10
11 #include "jsboolinlines.h"
12
13 #include "jsapi.h"
14 #include "jsatom.h"
15 #include "jscntxt.h"
16 #include "jsobj.h"
17 #include "jstypes.h"
18
19 #include "vm/GlobalObject.h"
20 #include "vm/ProxyObject.h"
21 #include "vm/StringBuffer.h"
22
23 #include "vm/BooleanObject-inl.h"
24
25 using namespace js;
26
27 const Class BooleanObject::class_ = {
28 "Boolean",
29 JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_HAS_CACHED_PROTO(JSProto_Boolean)
30 };
31
32 MOZ_ALWAYS_INLINE bool
IsBoolean(HandleValue v)33 IsBoolean(HandleValue v)
34 {
35 return v.isBoolean() || (v.isObject() && v.toObject().is<BooleanObject>());
36 }
37
38 #if JS_HAS_TOSOURCE
39 MOZ_ALWAYS_INLINE bool
bool_toSource_impl(JSContext * cx,const CallArgs & args)40 bool_toSource_impl(JSContext* cx, const CallArgs& args)
41 {
42 HandleValue thisv = args.thisv();
43 MOZ_ASSERT(IsBoolean(thisv));
44
45 bool b = thisv.isBoolean() ? thisv.toBoolean() : thisv.toObject().as<BooleanObject>().unbox();
46
47 StringBuffer sb(cx);
48 if (!sb.append("(new Boolean(") || !BooleanToStringBuffer(b, sb) || !sb.append("))"))
49 return false;
50
51 JSString* str = sb.finishString();
52 if (!str)
53 return false;
54 args.rval().setString(str);
55 return true;
56 }
57
58 static bool
bool_toSource(JSContext * cx,unsigned argc,Value * vp)59 bool_toSource(JSContext* cx, unsigned argc, Value* vp)
60 {
61 CallArgs args = CallArgsFromVp(argc, vp);
62 return CallNonGenericMethod<IsBoolean, bool_toSource_impl>(cx, args);
63 }
64 #endif
65
66 MOZ_ALWAYS_INLINE bool
bool_toString_impl(JSContext * cx,const CallArgs & args)67 bool_toString_impl(JSContext* cx, const CallArgs& args)
68 {
69 HandleValue thisv = args.thisv();
70 MOZ_ASSERT(IsBoolean(thisv));
71
72 bool b = thisv.isBoolean() ? thisv.toBoolean() : thisv.toObject().as<BooleanObject>().unbox();
73 args.rval().setString(BooleanToString(cx, b));
74 return true;
75 }
76
77 static bool
bool_toString(JSContext * cx,unsigned argc,Value * vp)78 bool_toString(JSContext* cx, unsigned argc, Value* vp)
79 {
80 CallArgs args = CallArgsFromVp(argc, vp);
81 return CallNonGenericMethod<IsBoolean, bool_toString_impl>(cx, args);
82 }
83
84 MOZ_ALWAYS_INLINE bool
bool_valueOf_impl(JSContext * cx,const CallArgs & args)85 bool_valueOf_impl(JSContext* cx, const CallArgs& args)
86 {
87 HandleValue thisv = args.thisv();
88 MOZ_ASSERT(IsBoolean(thisv));
89
90 bool b = thisv.isBoolean() ? thisv.toBoolean() : thisv.toObject().as<BooleanObject>().unbox();
91 args.rval().setBoolean(b);
92 return true;
93 }
94
95 static bool
bool_valueOf(JSContext * cx,unsigned argc,Value * vp)96 bool_valueOf(JSContext* cx, unsigned argc, Value* vp)
97 {
98 CallArgs args = CallArgsFromVp(argc, vp);
99 return CallNonGenericMethod<IsBoolean, bool_valueOf_impl>(cx, args);
100 }
101
102 static const JSFunctionSpec boolean_methods[] = {
103 #if JS_HAS_TOSOURCE
104 JS_FN(js_toSource_str, bool_toSource, 0, 0),
105 #endif
106 JS_FN(js_toString_str, bool_toString, 0, 0),
107 JS_FN(js_valueOf_str, bool_valueOf, 0, 0),
108 JS_FS_END
109 };
110
111 static bool
Boolean(JSContext * cx,unsigned argc,Value * vp)112 Boolean(JSContext* cx, unsigned argc, Value* vp)
113 {
114 CallArgs args = CallArgsFromVp(argc, vp);
115
116 bool b = args.length() != 0 ? JS::ToBoolean(args[0]) : false;
117
118 if (args.isConstructing()) {
119 RootedObject newTarget (cx, &args.newTarget().toObject());
120 RootedObject proto(cx);
121
122 if (!GetPrototypeFromConstructor(cx, newTarget, &proto))
123 return false;
124
125 JSObject* obj = BooleanObject::create(cx, b, proto);
126 if (!obj)
127 return false;
128 args.rval().setObject(*obj);
129 } else {
130 args.rval().setBoolean(b);
131 }
132 return true;
133 }
134
135 JSObject*
InitBooleanClass(JSContext * cx,HandleObject obj)136 js::InitBooleanClass(JSContext* cx, HandleObject obj)
137 {
138 MOZ_ASSERT(obj->isNative());
139
140 Rooted<GlobalObject*> global(cx, &obj->as<GlobalObject>());
141
142 Rooted<BooleanObject*> booleanProto(cx, global->createBlankPrototype<BooleanObject>(cx));
143 if (!booleanProto)
144 return nullptr;
145 booleanProto->setFixedSlot(BooleanObject::PRIMITIVE_VALUE_SLOT, BooleanValue(false));
146
147 RootedFunction ctor(cx, global->createConstructor(cx, Boolean, cx->names().Boolean, 1));
148 if (!ctor)
149 return nullptr;
150
151 if (!LinkConstructorAndPrototype(cx, ctor, booleanProto))
152 return nullptr;
153
154 if (!DefinePropertiesAndFunctions(cx, booleanProto, nullptr, boolean_methods))
155 return nullptr;
156
157 if (!GlobalObject::initBuiltinConstructor(cx, global, JSProto_Boolean, ctor, booleanProto))
158 return nullptr;
159
160 return booleanProto;
161 }
162
163 JSString*
BooleanToString(ExclusiveContext * cx,bool b)164 js::BooleanToString(ExclusiveContext* cx, bool b)
165 {
166 return b ? cx->names().true_ : cx->names().false_;
167 }
168
JS_PUBLIC_API(bool)169 JS_PUBLIC_API(bool)
170 js::ToBooleanSlow(HandleValue v)
171 {
172 if (v.isString())
173 return v.toString()->length() != 0;
174
175 MOZ_ASSERT(v.isObject());
176 return !EmulatesUndefined(&v.toObject());
177 }
178