/* * Copyright (c) 2021 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/require/js_require_manager.h" #include "ecmascript/ecma_vm.h" #include "ecmascript/global_env.h" #include "ecmascript/interpreter/slow_runtime_stub.h" #include "ecmascript/object_factory.h" #include "ecmascript/ecma_string.h" namespace panda::ecmascript { void RequireManager::InitializeCommonJS(JSThread *thread, CJSInfo cjsInfo) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); const GlobalEnvConstants *globalConst = thread->GlobalConstants(); JSHandle module = cjsInfo.moduleHdl; JSHandle require = cjsInfo.requireHdl; JSHandle exports = cjsInfo.exportsHdl; JSHandle filename = cjsInfo.filenameHdl; JSHandle dirname = cjsInfo.dirnameHdl; // Set module.exports ---> exports JSHandle exportsKey = globalConst->GetHandledCjsExportsString(); SlowRuntimeStub::StObjByName(thread, module.GetTaggedValue(), exportsKey.GetTaggedValue(), exports.GetTaggedValue()); // initialize module CjsModule::InitializeModule(thread, module, filename, dirname); // Set this.module ---> this.require.parent JSHandle parentKey(factory->NewFromASCII("parent")); SlowRuntimeStub::StObjByName(thread, require.GetTaggedValue(), parentKey.GetTaggedValue(), module.GetTaggedValue()); // cache ----> Set Module._cache JSHandle cacheKey = globalConst->GetHandledCjsCacheString(); JSHandle moduleObj(thread->GetEcmaVM()->GetGlobalEnv()->GetCjsModuleFunction()); JSTaggedValue modCache = SlowRuntimeStub::LdObjByName(thread, moduleObj.GetTaggedValue(), cacheKey.GetTaggedValue(), false, JSTaggedValue::Undefined()); JSHandle moduleCache = JSHandle(thread, modCache); JSHandle newCache = CjsModuleCache::PutIfAbsentAndReset(thread, moduleCache, JSHandle::Cast(filename), JSHandle(module)); SlowRuntimeStub::StObjByName(thread, moduleObj.GetTaggedValue(), cacheKey.GetTaggedValue(), newCache.GetTaggedValue()); } void RequireManager::CollectExecutedExp(JSThread *thread, CJSInfo cjsInfo) { const GlobalEnvConstants *globalConst = thread->GlobalConstants(); JSHandle module = cjsInfo.moduleHdl; JSHandle filename = cjsInfo.filenameHdl; // get Module from global env JSHandle moduleObj(thread->GetEcmaVM()->GetGlobalEnv()->GetCjsModuleFunction()); JSHandle cacheKey = globalConst->GetHandledCjsCacheString(); JSTaggedValue executedCacheVal = SlowRuntimeStub::LdObjByName(thread, moduleObj.GetTaggedValue(), cacheKey.GetTaggedValue(), false, JSTaggedValue::Undefined()); JSHandle executedCache = JSHandle(thread, executedCacheVal); JSHandle resetCache = CjsModuleCache::ResetModule(thread, executedCache, filename, JSHandle::Cast(module)); SlowRuntimeStub::StObjByName(thread, moduleObj.GetTaggedValue(), cacheKey.GetTaggedValue(), resetCache.GetTaggedValue()); } } // namespace panda::ecmascript