/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "ecmascript/interpreter/slow_runtime_stub.h" #include "ecmascript/require/js_cjs_module.h" #include "ecmascript/require/js_cjs_module_cache.h" #include "ecmascript/require/js_require_manager.h" #include "ecmascript/tests/test_helper.h" #include "ecmascript/global_env.h" using namespace panda::ecmascript; namespace panda::test { class CjsModuleTest : public testing::Test { public: static void SetUpTestCase() { GTEST_LOG_(INFO) << "SetUpTestCase"; } static void TearDownTestCase() { GTEST_LOG_(INFO) << "TearDownCase"; } void SetUp() override { TestHelper::CreateEcmaVMWithScope(instance, thread, scope); } void TearDown() override { TestHelper::DestroyEcmaVMWithScope(instance, scope); } EcmaVM *instance {nullptr}; EcmaHandleScope *scope {nullptr}; JSThread *thread {nullptr}; }; /** * @tc.name: SearchFromModuleCache * @tc.desc: Call "SearchFromModuleCache" function: return the corresponding module.exports, * if Module(store in GlobalEnv) contains the module which named "xxx". Else, return Hole. * by checking returned value. * @tc.type: FUNC * @tc.require: */ HWTEST_F_L0(CjsModuleTest, SearchFromModuleCache) { JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); const GlobalEnvConstants *globalConst = thread->GlobalConstants(); JSHandle cache = CjsModuleCache::Create(thread, CjsModuleCache::DEAULT_DICTIONART_CAPACITY); JSHandle module1 = factory->NewCjsModule(); JSHandle module2 = factory->NewCjsModule(); JSHandle fileName1(factory->NewFromUtf8("ark/js_runtime/test1.js")); JSHandle fileName2(factory->NewFromUtf8("ark/js_runtime/test2.js")); JSHandle fileName3(factory->NewFromUtf8("ark/js_runtime/test3.js")); JSHandle exports(factory->NewFromUtf8("export string")); JSHandle dirName(factory->NewFromUtf8("ark/js_runtime")); CjsModule::InitializeModule(thread, module1, fileName1, dirName); CjsModule::InitializeModule(thread, module2, fileName2, dirName); JSHandle exportsName = globalConst->GetHandledCjsExportsString(); SlowRuntimeStub::StObjByName(thread, module1.GetTaggedValue(), exportsName.GetTaggedValue(), exports.GetTaggedValue()); SlowRuntimeStub::StObjByName(thread, module2.GetTaggedValue(), exportsName.GetTaggedValue(), exports.GetTaggedValue()); cache = CjsModuleCache::PutIfAbsentAndReset(thread, cache, fileName1, JSHandle(module1)); cache = CjsModuleCache::PutIfAbsentAndReset(thread, cache, fileName2, JSHandle(module2)); JSHandle moduleObj(env->GetCjsModuleFunction()); JSHandle cacheName = globalConst->GetHandledCjsCacheString(); SlowRuntimeStub::StObjByName(thread, moduleObj.GetTaggedValue(), cacheName.GetTaggedValue(), cache.GetTaggedValue()); JSHandle test1 = CjsModule::SearchFromModuleCache(thread, fileName1); EXPECT_TRUE(test1->IsString()); EcmaString *test1Str = EcmaString::Cast(test1.GetTaggedValue().GetTaggedObject()); EXPECT_EQ(EcmaStringAccessor::Compare(*exports, test1Str), 0); JSHandle test2 = CjsModule::SearchFromModuleCache(thread, fileName3); EXPECT_TRUE(test2->IsHole()); } /** * @tc.name: PutIntoCache * @tc.desc: Call "PutIntoCache" function, put module into Module(store in GlobalEnv). * @tc.type: FUNC * @tc.require: */ HWTEST_F_L0(CjsModuleTest, PutIntoCache) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); auto *globalConst = thread->GlobalConstants(); JSHandle module = factory->NewCjsModule(); JSHandle fileName(factory->NewFromUtf8("ark/js_runtime/test.js")); JSHandle dirName(factory->NewFromUtf8("./ark/js_runtime")); JSHandle exports(factory->NewFromUtf8("test")); CjsModule::InitializeModule(thread, module, fileName, dirName); JSHandle exportsName = globalConst->GetHandledCjsExportsString(); SlowRuntimeStub::StObjByName(thread, module.GetTaggedValue(), exportsName.GetTaggedValue(), exports.GetTaggedValue()); CjsModule::PutIntoCache(thread, module, fileName); JSHandle test = CjsModule::SearchFromModuleCache(thread, fileName); EXPECT_EQ(EcmaStringAccessor::Compare(*exports, EcmaString::Cast(test.GetTaggedValue().GetTaggedObject())), 0); } } // namespace panda::test