1 /*
2 * Copyright (c) 2022 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/base/builtins_base.h"
17 #include "ecmascript/ecma_runtime_call_info.h"
18 #include "ecmascript/ecma_string.h"
19 #include "ecmascript/ecma_vm.h"
20 #include "ecmascript/global_env.h"
21 #include "ecmascript/interpreter/interpreter.h"
22 #include "ecmascript/js_function.h"
23 #include "ecmascript/js_handle.h"
24 #include "ecmascript/js_hclass.h"
25 #include "ecmascript/js_proxy.h"
26 #include "ecmascript/js_tagged_value-inl.h"
27 #include "ecmascript/object_factory.h"
28 #include "ecmascript/tests/test_helper.h"
29
30 using namespace panda::ecmascript;
31 namespace panda::test {
32 class RuntimeStubsTest : public testing::Test {
33 public:
SetUpTestCase()34 static void SetUpTestCase()
35 {
36 GTEST_LOG_(INFO) << "SetUpTestCase";
37 }
38
TearDownTestCase()39 static void TearDownTestCase()
40 {
41 GTEST_LOG_(INFO) << "TearDownCase";
42 }
43
SetUp()44 void SetUp() override
45 {
46 TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
47 }
48
TearDown()49 void TearDown() override
50 {
51 TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
52 }
53
54 EcmaHandleScope *scope {nullptr};
55 JSThread *thread {nullptr};
56 EcmaVM *ecmaVm = nullptr;
57 };
58
JSObjectTestCreate(JSThread * thread)59 static JSFunction *JSObjectTestCreate(JSThread *thread)
60 {
61 EcmaVM *ecmaVM = thread->GetEcmaVM();
62 JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
63 return globalEnv->GetObjectFunction().GetObject<JSFunction>();
64 }
65
HWTEST_F_L0(RuntimeStubsTest,GetSymbolFunction)66 HWTEST_F_L0(RuntimeStubsTest, GetSymbolFunction)
67 {
68 auto glue = thread->GetGlueAddr();
69 JSTaggedValue result =
70 JSTaggedValue(RuntimeStubs::GetSymbolFunction(glue, 0, reinterpret_cast<uintptr_t>(nullptr)));
71 EcmaVM *ecmaVm = thread->GetEcmaVM();
72 JSHandle<GlobalEnv> globalEnv = ecmaVm->GetGlobalEnv();
73 JSTaggedValue symbol = globalEnv->GetSymbolFunction().GetTaggedValue();
74 ASSERT_TRUE(result == symbol);
75 }
76
HWTEST_F_L0(RuntimeStubsTest,CallGetPrototype)77 HWTEST_F_L0(RuntimeStubsTest, CallGetPrototype)
78 {
79 auto glue = thread->GetGlueAddr();
80 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
81 JSHandle<JSTaggedValue> hclass(thread, JSObjectTestCreate(thread));
82 JSHandle<JSTaggedValue> proto(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(hclass), hclass));
83 JSHandle<JSTaggedValue> targetHandle(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(hclass), hclass));
84 EXPECT_TRUE(targetHandle->IsECMAObject());
85 JSObject::SetPrototype(thread, JSHandle<JSObject>(targetHandle), proto);
86
87 JSHandle<JSTaggedValue> handlerHandle(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(hclass), hclass));
88 EXPECT_TRUE(handlerHandle->IsECMAObject());
89 JSHandle<JSProxy> proxyHandle = JSProxy::ProxyCreate(thread, targetHandle, handlerHandle);
90
91 JSProxy *proxy = *proxyHandle;
92 JSTaggedValue result =
93 JSTaggedValue(RuntimeStubs::CallGetPrototype(glue, 1, reinterpret_cast<uintptr_t>(&proxy)));
94
95 ASSERT_TRUE(JSTaggedValue::SameValue(result, proto.GetTaggedValue()));
96 }
97
HWTEST_F_L0(RuntimeStubsTest,GetTaggedArrayPtrTest)98 HWTEST_F_L0(RuntimeStubsTest, GetTaggedArrayPtrTest)
99 {
100 auto glue = thread->GetGlueAddr();
101 JSTaggedType arr = JSTaggedValue::VALUE_UNDEFINED;
102 JSTaggedValue result =
103 JSTaggedValue(RuntimeStubs::GetTaggedArrayPtrTest(glue, 1, reinterpret_cast<uintptr_t>(&arr)));
104 ASSERT_TRUE(result.IsTaggedArray());
105 }
106
HWTEST_F_L0(RuntimeStubsTest,ThrowDeleteSuperProperty)107 HWTEST_F_L0(RuntimeStubsTest, ThrowDeleteSuperProperty)
108 {
109 auto glue = thread->GetGlueAddr();
110 JSTaggedValue result =
111 JSTaggedValue(RuntimeStubs::ThrowDeleteSuperProperty(glue, 0, reinterpret_cast<uintptr_t>(nullptr)));
112 ASSERT_TRUE(result.IsHole());
113 }
114
HWTEST_F_L0(RuntimeStubsTest,ThrowNonConstructorException)115 HWTEST_F_L0(RuntimeStubsTest, ThrowNonConstructorException)
116 {
117 auto glue = thread->GetGlueAddr();
118 JSTaggedValue result =
119 JSTaggedValue(RuntimeStubs::ThrowNonConstructorException(glue, 0, reinterpret_cast<uintptr_t>(nullptr)));
120 ASSERT_TRUE(result.IsException());
121 }
122 } // namespace panda::test
123