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/global_env.h"
17 #include "ecmascript/interpreter/slow_runtime_stub.h"
18 #include "ecmascript/require/js_cjs_module.h"
19 #include "ecmascript/require/js_cjs_module_cache.h"
20 #include "ecmascript/require/js_require_manager.h"
21 #include "ecmascript/tests/test_helper.h"
22
23 using namespace panda::ecmascript;
24 namespace panda::test {
25 class CjsModuleTest : public testing::Test {
26 public:
SetUpTestCase()27 static void SetUpTestCase()
28 {
29 GTEST_LOG_(INFO) << "SetUpTestCase";
30 }
31
TearDownTestCase()32 static void TearDownTestCase()
33 {
34 GTEST_LOG_(INFO) << "TearDownCase";
35 }
36
SetUp()37 void SetUp() override
38 {
39 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
40 }
41
TearDown()42 void TearDown() override
43 {
44 TestHelper::DestroyEcmaVMWithScope(instance, scope);
45 }
46
47 EcmaVM *instance {nullptr};
48 EcmaHandleScope *scope {nullptr};
49 JSThread *thread {nullptr};
50 };
51
52 /**
53 * @tc.name: SearchFromModuleCache
54 * @tc.desc: Call "SearchFromModuleCache" function: return the corresponding module.exports,
55 * if Module(store in GlobalEnv) contains the module which named "xxx". Else, return Hole.
56 * by checking returned value.
57 * @tc.type: FUNC
58 * @tc.require:
59 */
HWTEST_F_L0(CjsModuleTest,SearchFromModuleCache)60 HWTEST_F_L0(CjsModuleTest, SearchFromModuleCache)
61 {
62 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
63 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
64 auto vm = thread->GetEcmaVM();
65 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
66
67 JSHandle<CjsModuleCache> cache = CjsModuleCache::Create(thread, CjsModuleCache::DEAULT_DICTIONART_CAPACITY);
68 JSHandle<CjsModule> module1 = factory->NewCjsModule();
69 JSHandle<CjsModule> module2 = factory->NewCjsModule();
70 JSHandle<JSTaggedValue> fileName1(factory->NewFromUtf8("ark/js_runtime/test1.js"));
71 JSHandle<JSTaggedValue> fileName2(factory->NewFromUtf8("ark/js_runtime/test2.js"));
72 JSHandle<JSTaggedValue> fileName3(factory->NewFromUtf8("ark/js_runtime/test3.js"));
73 JSHandle<EcmaString> exports(factory->NewFromUtf8("export string"));
74 JSHandle<JSTaggedValue> dirName(factory->NewFromUtf8("ark/js_runtime"));
75 CjsModule::InitializeModule(thread, module1, fileName1, dirName);
76 CjsModule::InitializeModule(thread, module2, fileName2, dirName);
77 JSHandle<JSTaggedValue> exportsName = globalConst->GetHandledCjsExportsString();
78 SlowRuntimeStub::StObjByName(thread, module1.GetTaggedValue(), exportsName.GetTaggedValue(),
79 exports.GetTaggedValue());
80 SlowRuntimeStub::StObjByName(thread, module2.GetTaggedValue(), exportsName.GetTaggedValue(),
81 exports.GetTaggedValue());
82
83 cache = CjsModuleCache::PutIfAbsentAndReset(thread, cache, fileName1, JSHandle<JSTaggedValue>(module1));
84 cache = CjsModuleCache::PutIfAbsentAndReset(thread, cache, fileName2, JSHandle<JSTaggedValue>(module2));
85 JSHandle<JSTaggedValue> moduleObj(env->GetCjsModuleFunction());
86 JSHandle<JSTaggedValue> cacheName = globalConst->GetHandledCjsCacheString();
87 SlowRuntimeStub::StObjByName(thread, moduleObj.GetTaggedValue(), cacheName.GetTaggedValue(),
88 cache.GetTaggedValue());
89 JSHandle<JSTaggedValue> test1 = CjsModule::SearchFromModuleCache(thread, fileName1);
90 EXPECT_TRUE(test1->IsString());
91 EXPECT_EQ(EcmaStringAccessor::Compare(vm, JSHandle<EcmaString>(exports), JSHandle<EcmaString>(test1)), 0);
92
93 JSHandle<JSTaggedValue> test2 = CjsModule::SearchFromModuleCache(thread, fileName3);
94 EXPECT_TRUE(test2->IsHole());
95 }
96
97 /**
98 * @tc.name: PutIntoCache
99 * @tc.desc: Call "PutIntoCache" function, put module into Module(store in GlobalEnv).
100 * @tc.type: FUNC
101 * @tc.require:
102 */
HWTEST_F_L0(CjsModuleTest,PutIntoCache)103 HWTEST_F_L0(CjsModuleTest, PutIntoCache)
104 {
105 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
106 auto *globalConst = thread->GlobalConstants();
107 auto vm = thread->GetEcmaVM();
108
109 JSHandle<CjsModule> module = factory->NewCjsModule();
110 JSHandle<JSTaggedValue> fileName(factory->NewFromUtf8("ark/js_runtime/test.js"));
111 JSHandle<JSTaggedValue> dirName(factory->NewFromUtf8("./ark/js_runtime"));
112 JSHandle<EcmaString> exports(factory->NewFromUtf8("test"));
113 CjsModule::InitializeModule(thread, module, fileName, dirName);
114
115 JSHandle<JSTaggedValue> exportsName = globalConst->GetHandledCjsExportsString();
116 SlowRuntimeStub::StObjByName(thread, module.GetTaggedValue(), exportsName.GetTaggedValue(),
117 exports.GetTaggedValue());
118 CjsModule::PutIntoCache(thread, module, fileName);
119 JSHandle<JSTaggedValue> test = CjsModule::SearchFromModuleCache(thread, fileName);
120 EXPECT_EQ(EcmaStringAccessor::Compare(vm, exports, JSHandle<EcmaString>(test)), 0);
121 }
122 } // namespace panda::test
123