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/. */
8 * JS boolean implementation.
11 #include "jsboolinlines.h"
19 #include "vm/GlobalObject.h"
20 #include "vm/ProxyObject.h"
21 #include "vm/StringBuffer.h"
23 #include "vm/BooleanObject-inl.h"
26 using namespace js::types
;
28 const Class
BooleanObject::class_
= {
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
>());
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();
49 if (!sb
.append("(new Boolean(") || !BooleanToStringBuffer(b
, sb
) || !sb
.append("))"))
52 JSString
* str
= sb
.finishString();
55 args
.rval().setString(str
);
60 bool_toSource(JSContext
* cx
, unsigned argc
, Value
* vp
)
62 CallArgs args
= CallArgsFromVp(argc
, vp
);
63 return CallNonGenericMethod
<IsBoolean
, bool_toSource_impl
>(cx
, args
);
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
));
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
);
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
[] = {
105 JS_FN(js_toSource_str
, bool_toSource
, 0, 0),
107 JS_FN(js_toString_str
, bool_toString
, 0, 0),
108 JS_FN(js_valueOf_str
, bool_valueOf
, 0, 0),
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
);
123 args
.rval().setObject(*obj
);
125 args
.rval().setBoolean(b
);
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
));
140 booleanProto
->setFixedSlot(BooleanObject::PRIMITIVE_VALUE_SLOT
, BooleanValue(false));
142 RootedFunction
ctor(cx
, global
->createConstructor(cx
, Boolean
, cx
->names().Boolean
, 1));
146 if (!LinkConstructorAndPrototype(cx
, ctor
, booleanProto
))
149 if (!DefinePropertiesAndFunctions(cx
, booleanProto
, nullptr, boolean_methods
))
152 if (!GlobalObject::initBuiltinConstructor(cx
, global
, JSProto_Boolean
, ctor
, booleanProto
))
159 js_BooleanToString(ExclusiveContext
* cx
, bool b
)
161 return b
? cx
->names().true_
: cx
->names().false_
;
165 js::ToBooleanSlow(HandleValue v
)
168 return v
.toString()->length() != 0;
170 MOZ_ASSERT(v
.isObject());
171 return !EmulatesUndefined(&v
.toObject());