From 5f8de423f190bbb79a62f804151bc24824fa32d8 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Fri, 2 Feb 2018 04:16:08 -0500 Subject: Add m-esr52 at 52.6.0 --- js/src/jsbool.cpp | 177 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 js/src/jsbool.cpp (limited to 'js/src/jsbool.cpp') diff --git a/js/src/jsbool.cpp b/js/src/jsbool.cpp new file mode 100644 index 0000000000..c8109b02c2 --- /dev/null +++ b/js/src/jsbool.cpp @@ -0,0 +1,177 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * vim: set ts=8 sts=4 et sw=4 tw=99: + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/* + * JS boolean implementation. + */ + +#include "jsboolinlines.h" + +#include "jsapi.h" +#include "jsatom.h" +#include "jscntxt.h" +#include "jsobj.h" +#include "jstypes.h" + +#include "vm/GlobalObject.h" +#include "vm/ProxyObject.h" +#include "vm/StringBuffer.h" + +#include "vm/BooleanObject-inl.h" + +using namespace js; + +const Class BooleanObject::class_ = { + "Boolean", + JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_HAS_CACHED_PROTO(JSProto_Boolean) +}; + +MOZ_ALWAYS_INLINE bool +IsBoolean(HandleValue v) +{ + return v.isBoolean() || (v.isObject() && v.toObject().is()); +} + +#if JS_HAS_TOSOURCE +MOZ_ALWAYS_INLINE bool +bool_toSource_impl(JSContext* cx, const CallArgs& args) +{ + HandleValue thisv = args.thisv(); + MOZ_ASSERT(IsBoolean(thisv)); + + bool b = thisv.isBoolean() ? thisv.toBoolean() : thisv.toObject().as().unbox(); + + StringBuffer sb(cx); + if (!sb.append("(new Boolean(") || !BooleanToStringBuffer(b, sb) || !sb.append("))")) + return false; + + JSString* str = sb.finishString(); + if (!str) + return false; + args.rval().setString(str); + return true; +} + +static bool +bool_toSource(JSContext* cx, unsigned argc, Value* vp) +{ + CallArgs args = CallArgsFromVp(argc, vp); + return CallNonGenericMethod(cx, args); +} +#endif + +MOZ_ALWAYS_INLINE bool +bool_toString_impl(JSContext* cx, const CallArgs& args) +{ + HandleValue thisv = args.thisv(); + MOZ_ASSERT(IsBoolean(thisv)); + + bool b = thisv.isBoolean() ? thisv.toBoolean() : thisv.toObject().as().unbox(); + args.rval().setString(BooleanToString(cx, b)); + return true; +} + +static bool +bool_toString(JSContext* cx, unsigned argc, Value* vp) +{ + CallArgs args = CallArgsFromVp(argc, vp); + return CallNonGenericMethod(cx, args); +} + +MOZ_ALWAYS_INLINE bool +bool_valueOf_impl(JSContext* cx, const CallArgs& args) +{ + HandleValue thisv = args.thisv(); + MOZ_ASSERT(IsBoolean(thisv)); + + bool b = thisv.isBoolean() ? thisv.toBoolean() : thisv.toObject().as().unbox(); + args.rval().setBoolean(b); + return true; +} + +static bool +bool_valueOf(JSContext* cx, unsigned argc, Value* vp) +{ + CallArgs args = CallArgsFromVp(argc, vp); + return CallNonGenericMethod(cx, args); +} + +static const JSFunctionSpec boolean_methods[] = { +#if JS_HAS_TOSOURCE + JS_FN(js_toSource_str, bool_toSource, 0, 0), +#endif + JS_FN(js_toString_str, bool_toString, 0, 0), + JS_FN(js_valueOf_str, bool_valueOf, 0, 0), + JS_FS_END +}; + +static bool +Boolean(JSContext* cx, unsigned argc, Value* vp) +{ + CallArgs args = CallArgsFromVp(argc, vp); + + bool b = args.length() != 0 ? JS::ToBoolean(args[0]) : false; + + if (args.isConstructing()) { + RootedObject newTarget (cx, &args.newTarget().toObject()); + RootedObject proto(cx); + + if (!GetPrototypeFromConstructor(cx, newTarget, &proto)) + return false; + + JSObject* obj = BooleanObject::create(cx, b, proto); + if (!obj) + return false; + args.rval().setObject(*obj); + } else { + args.rval().setBoolean(b); + } + return true; +} + +JSObject* +js::InitBooleanClass(JSContext* cx, HandleObject obj) +{ + MOZ_ASSERT(obj->isNative()); + + Rooted global(cx, &obj->as()); + + Rooted booleanProto(cx, global->createBlankPrototype(cx)); + if (!booleanProto) + return nullptr; + booleanProto->setFixedSlot(BooleanObject::PRIMITIVE_VALUE_SLOT, BooleanValue(false)); + + RootedFunction ctor(cx, global->createConstructor(cx, Boolean, cx->names().Boolean, 1)); + if (!ctor) + return nullptr; + + if (!LinkConstructorAndPrototype(cx, ctor, booleanProto)) + return nullptr; + + if (!DefinePropertiesAndFunctions(cx, booleanProto, nullptr, boolean_methods)) + return nullptr; + + if (!GlobalObject::initBuiltinConstructor(cx, global, JSProto_Boolean, ctor, booleanProto)) + return nullptr; + + return booleanProto; +} + +JSString* +js::BooleanToString(ExclusiveContext* cx, bool b) +{ + return b ? cx->names().true_ : cx->names().false_; +} + +JS_PUBLIC_API(bool) +js::ToBooleanSlow(HandleValue v) +{ + if (v.isString()) + return v.toString()->length() != 0; + + MOZ_ASSERT(v.isObject()); + return !EmulatesUndefined(&v.toObject()); +} -- cgit v1.2.3