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/base/builtins_base.h"
17 #include "ecmascript/ecma_string.h"
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/interpreter/interpreter.h"
21 #include "ecmascript/interpreter/slow_runtime_stub.h"
22 #include "ecmascript/object_factory.h"
23 #include "ecmascript/tests/test_helper.h"
24
25 using namespace panda::ecmascript;
26 namespace panda::test {
27 class SlowRuntimeStubTest : public testing::Test {
28 public:
SetUpTestCase()29 static void SetUpTestCase()
30 {
31 GTEST_LOG_(INFO) << "SetUpTestCase";
32 }
33
TearDownTestCase()34 static void TearDownTestCase()
35 {
36 GTEST_LOG_(INFO) << "TearDownCase";
37 }
38
SetUp()39 void SetUp() override
40 {
41 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
42 }
43
TearDown()44 void TearDown() override
45 {
46 TestHelper::DestroyEcmaVMWithScope(instance, scope);
47 }
48
49 EcmaVM *instance {nullptr};
50 ecmascript::EcmaHandleScope *scope {nullptr};
51 JSThread *thread {nullptr};
52 };
53
JSObjectTestCreate(JSThread * thread)54 static JSFunction *JSObjectTestCreate(JSThread *thread)
55 {
56 EcmaVM *ecmaVM = thread->GetEcmaVM();
57 JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
58 return globalEnv->GetObjectFunction().GetObject<JSFunction>();
59 }
60
HWTEST_F_L0(SlowRuntimeStubTest,StPatchVar)61 HWTEST_F_L0(SlowRuntimeStubTest, StPatchVar)
62 {
63 JSTaggedValue result = SlowRuntimeStub::StPatchVar(thread, 0, JSTaggedValue::True());
64 ASSERT_TRUE(result.IsTrue());
65 }
66
HWTEST_F_L0(SlowRuntimeStubTest,LdPatchVar)67 HWTEST_F_L0(SlowRuntimeStubTest, LdPatchVar)
68 {
69 JSTaggedValue result = SlowRuntimeStub::StPatchVar(thread, 0, JSTaggedValue::False());
70 ASSERT_TRUE(result.IsTrue());
71 JSTaggedValue res = SlowRuntimeStub::LdPatchVar(thread, 0);
72 ASSERT_TRUE(res.IsFalse());
73 }
74
HWTEST_F_L0(SlowRuntimeStubTest,StObjByIndex)75 HWTEST_F_L0(SlowRuntimeStubTest, StObjByIndex)
76 {
77 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
78 // 1. handler has no "GetPrototypeOf"
79 JSHandle<JSTaggedValue> hclass(thread, JSObjectTestCreate(thread));
80 JSHandle<JSTaggedValue> proto(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(hclass), hclass));
81 JSTaggedValue result = SlowRuntimeStub::StObjByIndex(thread, proto.GetTaggedValue(), 0, JSTaggedValue::False());
82 ASSERT_TRUE(result.IsTrue());
83 }
84
HWTEST_F_L0(SlowRuntimeStubTest,LdObjByIndex)85 HWTEST_F_L0(SlowRuntimeStubTest, LdObjByIndex)
86 {
87 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
88 // 1. handler has no "GetPrototypeOf"
89 JSHandle<JSTaggedValue> hclass(thread, JSObjectTestCreate(thread));
90 JSHandle<JSTaggedValue> proto(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(hclass), hclass));
91 JSTaggedValue result = SlowRuntimeStub::StObjByIndex(thread, proto.GetTaggedValue(), 0, JSTaggedValue::False());
92 ASSERT_TRUE(result.IsTrue());
93 JSTaggedValue res = SlowRuntimeStub::LdObjByIndex(thread, proto.GetTaggedValue(), 0, false, JSTaggedValue::False());
94 ASSERT_TRUE(res.IsFalse());
95 }
96
HWTEST_F_L0(SlowRuntimeStubTest,ThrowTypeError)97 HWTEST_F_L0(SlowRuntimeStubTest, ThrowTypeError)
98 {
99 JSTaggedValue result = SlowRuntimeStub::ThrowTypeError(thread, "Typeerror");
100 ASSERT_TRUE(result.IsException());
101 }
102 } // namespace panda::test
103