• 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 #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 CjsModuleCacheTest : 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: PutIfAbsent
54  * @tc.desc: Call "PutIfAbsent" function, check whether the module is loaded successfully into the CjsModuleCache.
55  * by checking returned value.
56  * @tc.type: FUNC
57  * @tc.require:
58  */
HWTEST_F_L0(CjsModuleCacheTest,PutIfAbsent)59 HWTEST_F_L0(CjsModuleCacheTest, PutIfAbsent)
60 {
61     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
62     auto vm = thread->GetEcmaVM();
63     JSHandle<CjsModuleCache> cache = CjsModuleCache::Create(thread, CjsModuleCache::DEAULT_DICTIONART_CAPACITY);
64 
65     JSHandle<CjsModule> module = factory->NewCjsModule();
66     JSHandle<JSTaggedValue> fileName(factory->NewFromUtf8("ark/js_runtime/test.js"));
67     JSHandle<JSTaggedValue> dirName(factory->NewFromUtf8("ark/js_runtime"));
68     CjsModule::InitializeModule(thread, module, fileName, dirName);
69     JSHandle<CjsModuleCache> newCacheTested = CjsModuleCache::PutIfAbsentAndReset(thread, cache,
70                                               JSHandle<JSTaggedValue>::Cast(fileName),
71                                               JSHandle<JSTaggedValue>(module));
72     JSHandle<CjsModule> moduleTested = JSHandle<CjsModule>(thread,
73                                            newCacheTested->GetModule(fileName.GetTaggedValue()));
74 
75     EXPECT_TRUE(newCacheTested->ContainsModule(fileName.GetTaggedValue()));
76     JSHandle<EcmaString> fileNameTested(thread, moduleTested->GetFilename());
77     EXPECT_EQ(EcmaStringAccessor::Compare(vm, fileNameTested, JSHandle<EcmaString>(fileName)), 0);
78 }
79 
80 /**
81  * @tc.name: ResetModule
82  * @tc.desc: Call "ResetModule" function,check whether the same-name module is replaced.
83  * @tc.type: FUNC
84  * @tc.require:
85  */
HWTEST_F_L0(CjsModuleCacheTest,ResetModule)86 HWTEST_F_L0(CjsModuleCacheTest, ResetModule)
87 {
88     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
89     auto vm = thread->GetEcmaVM();
90     JSHandle<CjsModuleCache> cache = CjsModuleCache::Create(thread, CjsModuleCache::DEAULT_DICTIONART_CAPACITY);
91     JSHandle<CjsModule> module = factory->NewCjsModule();
92     JSHandle<CjsModule> moduleWithNewExp = factory->NewCjsModule();
93     JSHandle<JSTaggedValue> fileName(factory->NewFromUtf8("ark/js_runtime/test.js"));
94     JSHandle<JSTaggedValue> dirName(factory->NewFromUtf8("./ark/js_runtime"));
95     JSHandle<EcmaString> test(factory->NewFromUtf8("test"));
96     CjsModule::InitializeModule(thread, module, fileName, dirName);
97     CjsModule::InitializeModule(thread, moduleWithNewExp, fileName, dirName);
98     moduleWithNewExp->SetExports(thread, test);
99     JSHandle<CjsModuleCache> newCache = CjsModuleCache::PutIfAbsentAndReset(thread, cache,
100                                         JSHandle<JSTaggedValue>::Cast(fileName),
101                                         JSHandle<JSTaggedValue>(module));
102     JSHandle<CjsModuleCache> newCacheTested = CjsModuleCache::ResetModule(thread, newCache, fileName,
103                                                   JSHandle<JSTaggedValue>::Cast(moduleWithNewExp));
104     JSHandle<CjsModule> moduleTested = JSHandle<CjsModule>(thread,
105                                            newCacheTested->GetModule(fileName.GetTaggedValue()));
106 
107     JSHandle<EcmaString> exportsTested(thread, moduleTested->GetExports());
108     EXPECT_EQ(EcmaStringAccessor::Compare(vm, exportsTested, test), 0);
109 }
110 } // namespace panda::test
111