1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/builtins/builtins_boolean.h"
17
18 #include "ecmascript/builtins/builtins_errors.h"
19 #include "ecmascript/ecma_macros.h"
20 #include "ecmascript/global_env.h"
21 #include "ecmascript/js_primitive_ref.h"
22
23 namespace panda::ecmascript::builtins {
24 // ecma 19.3.1.1 Boolean(value)
BooleanConstructor(EcmaRuntimeCallInfo * argv)25 JSTaggedValue BuiltinsBoolean::BooleanConstructor(EcmaRuntimeCallInfo *argv)
26 {
27 BUILTINS_ENTRY_DEBUG_LOG();
28 ASSERT(argv);
29 JSThread *thread = argv->GetThread();
30 BUILTINS_API_TRACE(thread, Boolean, Constructor);
31 [[maybe_unused]] EcmaHandleScope handleScope(thread);
32 // 1. Let b be ToBoolean(value).
33 bool boolValue = GetCallArg(argv, 0)->ToBoolean();
34 // 2. If NewTarget is undefined, return b.
35 JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
36 if (newTarget->IsUndefined()) {
37 return GetTaggedBoolean(boolValue);
38 }
39 // 3. Let O be OrdinaryCreateFromConstructor(NewTarget, "%BooleanPrototype%", [[BooleanData]] ).
40 // 5. Set the value of O's [[BooleanData]] internal slot to b.
41 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
42 JSHandle<JSFunction> ctor = JSHandle<JSFunction>(GetConstructor(argv));
43 JSHandle<JSObject> result = factory->NewJSObjectByConstructor(ctor, newTarget);
44 // 4. ReturnIfAbrupt(O).
45 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
46 JSTaggedValue objValue = boolValue ? JSTaggedValue::True() : JSTaggedValue::False();
47 JSPrimitiveRef::Cast(*result)->SetValue(thread, objValue);
48 // 6. Return O.
49 return result.GetTaggedValue();
50 }
51
52 // ecma 19.3.3 abstract operation thisBooleanValue(value)
ThisBooleanValue(JSThread * thread,JSTaggedValue value)53 JSTaggedValue BuiltinsBoolean::ThisBooleanValue(JSThread *thread, JSTaggedValue value)
54 {
55 BUILTINS_API_TRACE(thread, Boolean, ThisBooleanValue);
56 // 1. If Type(value) is Boolean, return value
57 if (value.IsBoolean()) {
58 return value;
59 }
60 // 2. If Type(value) is Object and value has a [[BooleanData]] internal slot, then
61 if (value.IsJSPrimitiveRef()) {
62 JSTaggedValue primitive = JSPrimitiveRef::Cast(value.GetTaggedObject())->GetValue();
63 // a. Assert: value's [[BooleanData]] internal slot is a Boolean value.
64 if (primitive.IsBoolean()) {
65 // b. Return the value of value's [[BooleanData]] internal slot.
66 return primitive;
67 }
68 }
69 [[maybe_unused]] EcmaHandleScope handleScope(thread);
70 // 3. Throw a TypeError exception.
71 THROW_TYPE_ERROR_AND_RETURN(thread, "the type can not convert to BooleanValue", JSTaggedValue::Exception());
72 }
73
74 // ecma 19.3.3.2 Boolean.prototype.toString ()
BooleanPrototypeToString(EcmaRuntimeCallInfo * argv)75 JSTaggedValue BuiltinsBoolean::BooleanPrototypeToString(EcmaRuntimeCallInfo *argv)
76 {
77 ASSERT(argv);
78 JSThread *thread = argv->GetThread();
79 BUILTINS_API_TRACE(thread, Boolean, BooleanPrototypeToString);
80 [[maybe_unused]] EcmaHandleScope handleScope(thread);
81 // 1. Let b be thisBooleanValue(this value).
82 JSTaggedValue thisValueToBoolean = BuiltinsBoolean::ThisBooleanValue(thread, GetThis(argv).GetTaggedValue());
83 // 2. ReturnIfAbrupt(b)
84 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
85
86 // 3. If b is true, return "true"; else return "false".
87 return thisValueToBoolean.IsTrue() ? GetTaggedString(thread, "true") : GetTaggedString(thread, "false");
88 }
89
90 // ecma 19.3.3.3 Boolean.prototype.valueOf ()
BooleanPrototypeValueOf(EcmaRuntimeCallInfo * argv)91 JSTaggedValue BuiltinsBoolean::BooleanPrototypeValueOf(EcmaRuntimeCallInfo *argv)
92 {
93 ASSERT(argv);
94 JSThread *thread = argv->GetThread();
95 BUILTINS_API_TRACE(thread, Boolean, BooleanPrototypeValueOf);
96 // 1. Return thisBooleanValue(this value).
97 return BuiltinsBoolean::ThisBooleanValue(thread, GetThis(argv).GetTaggedValue());
98 }
99 } // namespace panda::ecmascript::builtins
100