• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <thread>
17 
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/ic/invoke_cache.h"
20 #include "ecmascript/interpreter/interpreter-inl.h"
21 #include "ecmascript/object_factory.h"
22 #include "ecmascript/tests/test_helper.h"
23 
24 using namespace panda::ecmascript;
25 namespace panda::test {
26 class ICInvokeTest : public testing::Test {
27 public:
SetUpTestCase()28     static void SetUpTestCase()
29     {
30         GTEST_LOG_(INFO) << "SetUpTestCase";
31     }
32 
TearDownTestCase()33     static void TearDownTestCase()
34     {
35         GTEST_LOG_(INFO) << "TearDownCase";
36     }
37 
SetUp()38     void SetUp() override
39     {
40         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
41         ecmaVm = EcmaVM::Cast(instance);
42     }
43 
TearDown()44     void TearDown() override
45     {
46         TestHelper::DestroyEcmaVMWithScope(instance, scope);
47     }
48 
49     PandaVM *instance {nullptr};
50     EcmaVM *ecmaVm = nullptr;
51     EcmaHandleScope *scope {nullptr};
52     JSThread *thread {nullptr};
53 };
54 
HWTEST_F_L0(ICInvokeTest,SetMonoConstuctCacheSlot)55 HWTEST_F_L0(ICInvokeTest, SetMonoConstuctCacheSlot)
56 {
57     auto globalEnv = ecmaVm->GetGlobalEnv();
58     auto factory = ecmaVm->GetFactory();
59     JSHandle<JSFunction> func = factory->NewJSFunction(globalEnv);
60     func.GetTaggedValue().GetTaggedObject()->GetClass()->SetClassConstructor(true);
61 
62     JSHandle<TaggedArray> array = factory->NewTaggedArray(10);
63     uint32_t slotId = 5;
64     bool setResult = InvokeCache::SetMonoConstuctCacheSlot(
65         thread, static_cast<ProfileTypeInfo *>(*array), slotId, func.GetTaggedValue(), JSTaggedValue(123));
66     ASSERT_TRUE(setResult);
67     ASSERT_EQ(array->Get(thread, slotId), func.GetTaggedValue());
68     ASSERT_EQ(array->Get(thread, slotId + 1), JSTaggedValue(123));
69 }
70 
HWTEST_F_L0(ICInvokeTest,SetPolyConstuctCacheSlot)71 HWTEST_F_L0(ICInvokeTest, SetPolyConstuctCacheSlot)
72 {
73     auto globalEnv = ecmaVm->GetGlobalEnv();
74     auto factory = ecmaVm->GetFactory();
75     JSHandle<TaggedArray> array1 = factory->NewTaggedArray(3);
76     JSHandle<TaggedArray> array2 = factory->NewTaggedArray(3);
77 
78     JSHandle<JSFunction> func0 = factory->NewJSFunction(globalEnv);
79     func0.GetTaggedValue().GetTaggedObject()->GetClass()->SetClassConstructor(true);
80     array1->Set(thread, 0, func0.GetTaggedValue());
81     array2->Set(thread, 0, JSTaggedValue(123));
82     JSHandle<JSFunction> func1 = factory->NewJSFunction(globalEnv);
83     func1.GetTaggedValue().GetTaggedObject()->GetClass()->SetClassConstructor(true);
84     array1->Set(thread, 1, func1.GetTaggedValue());
85     array2->Set(thread, 1, JSTaggedValue(456));
86     JSHandle<JSFunction> func2 = factory->NewJSFunction(globalEnv);
87     func2.GetTaggedValue().GetTaggedObject()->GetClass()->SetClassConstructor(true);
88     array1->Set(thread, 2, func2.GetTaggedValue());
89     array2->Set(thread, 2, JSTaggedValue(789));
90 
91     JSHandle<TaggedArray> array = factory->NewTaggedArray(10);
92     uint32_t slotId = 5;
93     bool setResult = InvokeCache::SetPolyConstuctCacheSlot(
94         thread, static_cast<ProfileTypeInfo *>(*array), slotId, 3, array1.GetTaggedValue(), array2.GetTaggedValue());
95     ASSERT_TRUE(setResult);
96     JSTaggedValue slot = array->Get(thread, slotId);
97     ASSERT_TRUE(slot.IsTaggedArray());
98     JSHandle<TaggedArray> slotArray(thread, slot);
99     ASSERT_EQ(slotArray->Get(thread, 0), func0.GetTaggedValue());
100     ASSERT_EQ(slotArray->Get(thread, 1), JSTaggedValue(123));
101     ASSERT_EQ(slotArray->Get(thread, 2), func1.GetTaggedValue());
102     ASSERT_EQ(slotArray->Get(thread, 3), JSTaggedValue(456));
103     ASSERT_EQ(slotArray->Get(thread, 4), func2.GetTaggedValue());
104     ASSERT_EQ(slotArray->Get(thread, 5), JSTaggedValue(789));
105     ASSERT_EQ(array->Get(thread, slotId + 1), JSTaggedValue::Hole());
106 }
107 }