• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 #include "ecmascript/global_env.h"
23 
24 using namespace panda::ecmascript;
25 namespace panda::test {
26 class CjsModuleTest : 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     }
42 
TearDown()43     void TearDown() override
44     {
45         TestHelper::DestroyEcmaVMWithScope(instance, scope);
46     }
47 
48     EcmaVM *instance {nullptr};
49     EcmaHandleScope *scope {nullptr};
50     JSThread *thread {nullptr};
51 };
52 
53 /**
54  * @tc.name: SearchFromModuleCache
55  * @tc.desc: Call "SearchFromModuleCache" function: return the corresponding module.exports,
56  * if Module(store in GlobalEnv) contains the module which named "xxx". Else, return Hole.
57  * by checking returned value.
58  * @tc.type: FUNC
59  * @tc.require:
60  */
HWTEST_F_L0(CjsModuleTest,SearchFromModuleCache)61 HWTEST_F_L0(CjsModuleTest, SearchFromModuleCache)
62 {
63     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
64     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
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     EcmaString *test1Str = EcmaString::Cast(test1.GetTaggedValue().GetTaggedObject());
92     EXPECT_EQ(EcmaStringAccessor::Compare(*exports, test1Str), 0);
93 
94     JSHandle<JSTaggedValue> test2 = CjsModule::SearchFromModuleCache(thread, fileName3);
95     EXPECT_TRUE(test2->IsHole());
96 }
97 
98 /**
99  * @tc.name: PutIntoCache
100  * @tc.desc: Call "PutIntoCache" function, put module into Module(store in GlobalEnv).
101  * @tc.type: FUNC
102  * @tc.require:
103  */
HWTEST_F_L0(CjsModuleTest,PutIntoCache)104 HWTEST_F_L0(CjsModuleTest, PutIntoCache)
105 {
106     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
107     auto *globalConst = thread->GlobalConstants();
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(*exports,
121         EcmaString::Cast(test.GetTaggedValue().GetTaggedObject())), 0);
122 }
123 } // namespace panda::test
124