* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecmascript/builtins/builtins_boolean.h"
#include "ecmascript/js_primitive_ref.h"
namespace panda::ecmascript::builtins {
JSTaggedValue BuiltinsBoolean::BooleanConstructor(EcmaRuntimeCallInfo *argv)
{
BUILTINS_ENTRY_DEBUG_LOG();
ASSERT(argv);
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, Boolean, Constructor);
[[maybe_unused]] EcmaHandleScope handleScope(thread);
bool boolValue = GetCallArg(argv, 0)->ToBoolean();
JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
if (newTarget->IsUndefined()) {
return GetTaggedBoolean(boolValue);
}
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<JSFunction> ctor = JSHandle<JSFunction>(GetConstructor(argv));
JSHandle<JSObject> result = factory->NewJSObjectByConstructor(ctor, newTarget);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
JSTaggedValue objValue = boolValue ? JSTaggedValue::True() : JSTaggedValue::False();
JSPrimitiveRef::Cast(*result)->SetValue(thread, objValue);
return result.GetTaggedValue();
}
JSTaggedValue BuiltinsBoolean::ThisBooleanValue(JSThread *thread, JSTaggedValue value)
{
BUILTINS_API_TRACE(thread, Boolean, ThisBooleanValue);
if (value.IsBoolean()) {
return value;
}
if (value.IsJSPrimitiveRef()) {
JSTaggedValue primitive = JSPrimitiveRef::Cast(value.GetTaggedObject())->GetValue(thread);
if (primitive.IsBoolean()) {
return primitive;
}
}
[[maybe_unused]] EcmaHandleScope handleScope(thread);
THROW_TYPE_ERROR_AND_RETURN(thread, "the type can not convert to BooleanValue", JSTaggedValue::Exception());
}
JSTaggedValue BuiltinsBoolean::BooleanPrototypeToString(EcmaRuntimeCallInfo *argv)
{
ASSERT(argv);
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, Boolean, BooleanPrototypeToString);
[[maybe_unused]] EcmaHandleScope handleScope(thread);
JSTaggedValue thisValueToBoolean = BuiltinsBoolean::ThisBooleanValue(thread, GetThis(argv).GetTaggedValue());
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
return thisValueToBoolean.IsTrue() ? GetTaggedString(thread, "true") : GetTaggedString(thread, "false");
}
JSTaggedValue BuiltinsBoolean::BooleanPrototypeValueOf(EcmaRuntimeCallInfo *argv)
{
ASSERT(argv);
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, Boolean, BooleanPrototypeValueOf);
return BuiltinsBoolean::ThisBooleanValue(thread, GetThis(argv).GetTaggedValue());
}
}