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/js_tagged_value-inl.h"
19 #include "ecmascript/require/js_cjs_module.h"
20 #include "ecmascript/require/js_cjs_module_cache.h"
21 #include "ecmascript/require/js_require_manager.h"
22 #include "ecmascript/tests/test_helper.h"
23 #include "ecmascript/global_env.h"
24
25 using namespace panda::ecmascript;
26 namespace panda::test {
27 class CjsManagerTest : 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 EcmaHandleScope *scope {nullptr};
51 JSThread *thread {nullptr};
52 };
53
54 /**
55 * @tc.name: InitializeCommonJS
56 * @tc.desc: Call "InitializeCommonJS" function, connect relationship between objects.
57 * @tc.type: FUNC
58 * @tc.require:
59 */
HWTEST_F_L0(CjsManagerTest,InitializeCommonJS)60 HWTEST_F_L0(CjsManagerTest, InitializeCommonJS)
61 {
62 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
63 auto *globalConst = thread->GlobalConstants();
64
65 JSHandle<CjsModule> module = factory->NewCjsModule();
66 JSHandle<JSTaggedValue> require = thread->GetEcmaVM()->GetGlobalEnv()->GetCjsRequireFunction();
67 JSHandle<CjsExports> exports = factory->NewCjsExports();
68 JSHandle<JSTaggedValue> fileName(factory->NewFromUtf8("ark/js_runtime/test.js"));
69 JSHandle<JSTaggedValue> dirName(factory->NewFromUtf8("ark/js_runtime"));
70 CJSInfo cjsInfo(module, require, exports, fileName, dirName);
71 RequireManager::InitializeCommonJS(thread, cjsInfo);
72
73 JSHandle<JSTaggedValue> exportsKey = globalConst->GetHandledCjsExportsString();
74 JSTaggedValue exportsVal =
75 SlowRuntimeStub::LdObjByName(thread, module.GetTaggedValue(),
76 exportsKey.GetTaggedValue(), false, JSTaggedValue::Undefined());
77
78 EXPECT_EQ(JSTaggedValue::SameValue(exportsVal, JSTaggedValue::Hole()), false);
79 }
80
81 /**
82 * @tc.name: CollectExecutedExp
83 * @tc.desc: Call "CollectExecutedExp" function,check whether the same-name module is replaced.
84 * @tc.type: FUNC
85 * @tc.require:
86 */
HWTEST_F_L0(CjsManagerTest,CollectExecutedExp)87 HWTEST_F_L0(CjsManagerTest, CollectExecutedExp)
88 {
89 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
90 auto *globalConst = thread->GlobalConstants();
91
92 JSHandle<CjsModule> module1 = factory->NewCjsModule();
93 JSHandle<JSTaggedValue> require = thread->GetEcmaVM()->GetGlobalEnv()->GetCjsRequireFunction();
94 JSHandle<CjsExports> exports = factory->NewCjsExports();
95 JSHandle<JSTaggedValue> fileName(factory->NewFromUtf8("ark/js_runtime/test.js"));
96 JSHandle<JSTaggedValue> dirName(factory->NewFromUtf8("ark/js_runtime"));
97 CJSInfo cjsInfo1(module1, require, exports, fileName, dirName);
98 RequireManager::InitializeCommonJS(thread, cjsInfo1);
99
100 JSHandle<CjsModule> module2 = factory->NewCjsModule();
101 JSHandle<EcmaString> exportsStr(factory->NewFromUtf8("test"));
102 CjsModule::InitializeModule(thread, module2, fileName, dirName);
103 JSHandle<JSTaggedValue> exportsName = globalConst->GetHandledCjsExportsString();
104 SlowRuntimeStub::StObjByName(thread, module2.GetTaggedValue(), exportsName.GetTaggedValue(),
105 exportsStr.GetTaggedValue());
106
107 CJSInfo cjsInfo2(module2, require, exports, fileName, dirName);
108 RequireManager::CollectExecutedExp(thread, cjsInfo2);
109 JSHandle<JSTaggedValue> test = CjsModule::SearchFromModuleCache(thread, fileName);
110
111 EcmaString *exportsTested = EcmaString::Cast(test->GetTaggedObject());
112 EXPECT_EQ(EcmaStringAccessor::Compare(exportsTested, *exportsStr), 0);
113 }
114 } // namespace panda::test
115