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 ASSERT(argv);
28 BUILTINS_API_TRACE(argv->GetThread(), Boolean, Constructor);
29 JSThread *thread = argv->GetThread();
30 [[maybe_unused]] EcmaHandleScope handleScope(thread);
31 // 1. Let b be ToBoolean(value).
32 bool boolValue = GetCallArg(argv, 0)->ToBoolean();
33 // 2. If NewTarget is undefined, return b.
34 JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
35 if (newTarget->IsUndefined()) {
36 return GetTaggedBoolean(boolValue);
37 }
38 // 3. Let O be OrdinaryCreateFromConstructor(NewTarget, "%BooleanPrototype%", [[BooleanData]] ).
39 // 5. Set the value of O's [[BooleanData]] internal slot to b.
40 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
41 JSHandle<JSFunction> ctor = JSHandle<JSFunction>(GetConstructor(argv));
42 JSHandle<JSObject> result = factory->NewJSObjectByConstructor(ctor, newTarget);
43 JSTaggedValue objValue = boolValue ? JSTaggedValue::True() : JSTaggedValue::False();
44 JSPrimitiveRef::Cast(*result)->SetValue(thread, objValue);
45 // 4. ReturnIfAbrupt(O).
46 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
47 // 6. Return O.
48 return result.GetTaggedValue();
49 }
50
51 // ecma 19.3.3 abstract operation thisBooleanValue(value)
ThisBooleanValue(JSThread * thread,JSTaggedValue value)52 JSTaggedValue BuiltinsBoolean::ThisBooleanValue(JSThread *thread, JSTaggedValue value)
53 {
54 BUILTINS_API_TRACE(thread, Boolean, ThisBooleanValue);
55 // 1. If Type(value) is Boolean, return value
56 if (value.IsBoolean()) {
57 return value == JSTaggedValue::True() ? GetTaggedBoolean(true) : GetTaggedBoolean(false);
58 }
59 // 2. If Type(value) is Object and value has a [[BooleanData]] internal slot, then
60 if (value.IsJSPrimitiveRef()) {
61 JSTaggedValue primitive = JSPrimitiveRef::Cast(value.GetTaggedObject())->GetValue();
62 // a. Assert: value's [[BooleanData]] internal slot is a Boolean value.
63 if (primitive.IsBoolean()) {
64 // b. Return the value of value's [[BooleanData]] internal slot.
65 return primitive == JSTaggedValue::True() ? GetTaggedBoolean(true) : GetTaggedBoolean(false);
66 }
67 }
68 [[maybe_unused]] EcmaHandleScope handleScope(thread);
69 // 3. Throw a TypeError exception.
70 THROW_TYPE_ERROR_AND_RETURN(thread, "the type can not convert to BooleanValue", JSTaggedValue::Exception());
71 }
72
73 // ecma 19.3.3.2 Boolean.prototype.toString ()
BooleanPrototypeToString(EcmaRuntimeCallInfo * argv)74 JSTaggedValue BuiltinsBoolean::BooleanPrototypeToString(EcmaRuntimeCallInfo *argv)
75 {
76 ASSERT(argv);
77 JSThread *thread = argv->GetThread();
78 [[maybe_unused]] EcmaHandleScope handleScope(thread);
79 // 1. Let b be thisBooleanValue(this value).
80 JSTaggedValue thisValueToBoolean = BuiltinsBoolean::ThisBooleanValue(thread, GetThis(argv).GetTaggedValue());
81 // 2. ReturnIfAbrupt(b)
82 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
83
84 // 3. If b is true, return "true"; else return "false".
85 return thisValueToBoolean.IsTrue() ? GetTaggedString(thread, "true") : GetTaggedString(thread, "false");
86 }
87
88 // ecma 19.3.3.3 Boolean.prototype.valueOf ()
BooleanPrototypeValueOf(EcmaRuntimeCallInfo * argv)89 JSTaggedValue BuiltinsBoolean::BooleanPrototypeValueOf(EcmaRuntimeCallInfo *argv)
90 {
91 ASSERT(argv);
92 // 1. Return thisBooleanValue(this value).
93 return BuiltinsBoolean::ThisBooleanValue(argv->GetThread(), GetThis(argv).GetTaggedValue());
94 }
95 } // namespace panda::ecmascript::builtins
96