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