1/*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2008, 2016 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21#include "config.h"
22#include "BooleanConstructor.h"
23
24#include "BooleanPrototype.h"
25#include "JSGlobalObject.h"
26#include "JSCInlines.h"
27
28namespace JSC {
29
30STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(BooleanConstructor);
31
32const ClassInfo BooleanConstructor::s_info = { "Function", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(BooleanConstructor) };
33
34// ECMA 15.6.1
35static EncodedJSValue JSC_HOST_CALL callBooleanConstructor(JSGlobalObject* globalObject, CallFrame* callFrame)
36{
37 return JSValue::encode(jsBoolean(callFrame->argument(0).toBoolean(globalObject)));
38}
39
40// ECMA 15.6.2
41static EncodedJSValue JSC_HOST_CALL constructWithBooleanConstructor(JSGlobalObject* globalObject, CallFrame* callFrame)
42{
43 VM& vm = globalObject->vm();
44 auto scope = DECLARE_THROW_SCOPE(vm);
45 JSValue boolean = jsBoolean(callFrame->argument(0).toBoolean(globalObject));
46 Structure* booleanStructure = InternalFunction::createSubclassStructure(globalObject, callFrame->jsCallee(), callFrame->newTarget(), globalObject->booleanObjectStructure());
47 RETURN_IF_EXCEPTION(scope, encodedJSValue());
48 BooleanObject* obj = BooleanObject::create(vm, booleanStructure);
49 obj->setInternalValue(vm, boolean);
50 return JSValue::encode(obj);
51}
52
53BooleanConstructor::BooleanConstructor(VM& vm, Structure* structure)
54 : InternalFunction(vm, structure, callBooleanConstructor, constructWithBooleanConstructor)
55{
56}
57
58void BooleanConstructor::finishCreation(VM& vm, BooleanPrototype* booleanPrototype)
59{
60 Base::finishCreation(vm, vm.propertyNames->Boolean.string(), NameAdditionMode::WithoutStructureTransition);
61 putDirectWithoutTransition(vm, vm.propertyNames->prototype, booleanPrototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
62 putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum);
63}
64
65JSObject* constructBooleanFromImmediateBoolean(JSGlobalObject* globalObject, JSValue immediateBooleanValue)
66{
67 VM& vm = globalObject->vm();
68 BooleanObject* obj = BooleanObject::create(vm, globalObject->booleanObjectStructure());
69 obj->setInternalValue(vm, immediateBooleanValue);
70 return obj;
71}
72
73} // namespace JSC
74