escargot/src/builtins/BuiltinBoolean.cpp
2023-08-18 18:36:46 +09:00

103 lines
5.9 KiB
C++

/*
* Copyright (c) 2016-present Samsung Electronics Co., Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#include "Escargot.h"
#include "runtime/GlobalObject.h"
#include "runtime/Context.h"
#include "runtime/BooleanObject.h"
#include "runtime/NativeFunctionObject.h"
namespace Escargot {
static Value builtinBooleanConstructor(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
bool primitiveVal = argv[0].toBoolean(state);
if (!newTarget.hasValue()) {
return Value(primitiveVal);
} else {
Object* proto = Object::getPrototypeFromConstructor(state, newTarget.value(), [](ExecutionState& state, Context* constructorRealm) -> Object* {
return constructorRealm->globalObject()->booleanPrototype();
});
RETURN_VALUE_IF_PENDING_EXCEPTION
BooleanObject* boolObj = new BooleanObject(state, proto, primitiveVal);
return boolObj;
}
}
static Value builtinBooleanValueOf(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
if (thisValue.isBoolean()) {
return Value(thisValue);
} else if (thisValue.isObject() && thisValue.asObject()->isBooleanObject()) {
return Value(thisValue.asPointerValue()->asBooleanObject()->primitiveValue());
}
THROW_BUILTIN_ERROR_RETURN_VALUE(state, ErrorCode::TypeError, ErrorObject::Messages::GlobalObject_ThisNotBoolean);
RELEASE_ASSERT_NOT_REACHED();
}
static Value builtinBooleanToString(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
if (thisValue.isBoolean()) {
return Value(thisValue.toString(state));
} else if (thisValue.isObject() && thisValue.asObject()->isBooleanObject()) {
return Value(thisValue.asPointerValue()->asBooleanObject()->primitiveValue()).toString(state);
}
THROW_BUILTIN_ERROR_RETURN_VALUE(state, ErrorCode::TypeError, ErrorObject::Messages::GlobalObject_ThisNotBoolean);
RELEASE_ASSERT_NOT_REACHED();
}
void GlobalObject::initializeBoolean(ExecutionState& state)
{
ObjectPropertyNativeGetterSetterData* nativeData = new ObjectPropertyNativeGetterSetterData(true, false, true,
[](ExecutionState& state, Object* self, const Value& receiver, const EncodedValue& privateDataFromObjectPrivateArea) -> Value {
ASSERT(self->isGlobalObject());
return self->asGlobalObject()->boolean();
},
nullptr);
defineNativeDataAccessorProperty(state, ObjectPropertyName(state.context()->staticStrings().Boolean), nativeData, Value(Value::EmptyValue));
}
void GlobalObject::installBoolean(ExecutionState& state)
{
const StaticStrings* strings = &state.context()->staticStrings();
m_boolean = new NativeFunctionObject(state, NativeFunctionInfo(strings->Boolean, builtinBooleanConstructor, 1), NativeFunctionObject::__ForBuiltinConstructor__);
m_boolean->setGlobalIntrinsicObject(state);
m_booleanPrototype = new BooleanObject(state, m_objectPrototype, false);
m_booleanPrototype->setGlobalIntrinsicObject(state, true);
m_booleanPrototype->directDefineOwnProperty(state, ObjectPropertyName(strings->constructor), ObjectPropertyDescriptor(m_boolean, (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectPropertyDescriptor::ConfigurablePresent)));
// $19.3.3.2 Boolean.prototype.toString
m_booleanPrototype->directDefineOwnProperty(state, ObjectPropertyName(strings->toString),
ObjectPropertyDescriptor(new NativeFunctionObject(state, NativeFunctionInfo(strings->toString, builtinBooleanToString, 0, NativeFunctionInfo::Strict)),
(ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectPropertyDescriptor::ConfigurablePresent)));
// $19.3.3.3 Boolean.prototype.valueOf
m_booleanPrototype->directDefineOwnProperty(state, ObjectPropertyName(strings->valueOf),
ObjectPropertyDescriptor(new NativeFunctionObject(state, NativeFunctionInfo(strings->valueOf, builtinBooleanValueOf, 0, NativeFunctionInfo::Strict)),
(ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectPropertyDescriptor::ConfigurablePresent)));
m_boolean->setFunctionPrototype(state, m_booleanPrototype);
m_booleanProxyObject = new BooleanObject(state);
redefineOwnProperty(state, ObjectPropertyName(strings->Boolean),
ObjectPropertyDescriptor(m_boolean, (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectPropertyDescriptor::ConfigurablePresent)));
}
} // namespace Escargot