1/* 2 * Copyright (c) 2023-2024 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/ecma_context.h" 17#include "ecmascript/module/js_dynamic_import.h" 18#include "ecmascript/base/path_helper.h" 19#include "ecmascript/builtins/builtins_promise_job.h" 20#include "ecmascript/interpreter/interpreter.h" 21#include "ecmascript/js_function.h" 22#include "ecmascript/module/js_module_deregister.h" 23#include "ecmascript/module/js_module_manager.h" 24#include "ecmascript/module/module_data_extractor.h" 25#include "ecmascript/module/module_path_helper.h" 26 27namespace panda::ecmascript { 28using PathHelper = base::PathHelper; 29using BuiltinsPromiseJob = builtins::BuiltinsPromiseJob; 30 31JSTaggedValue DynamicImport::ExecuteNativeOrJsonModule(JSThread *thread, const CString &specifierString, 32 ModuleTypes moduleType, 33 JSHandle<JSPromiseReactionsFunction> resolve, 34 JSHandle<JSPromiseReactionsFunction> reject, 35 const JSPandaFile *jsPandaFile) 36{ 37 ModuleManager *moduleManager = thread->GetCurrentEcmaContext()->GetModuleManager(); 38 JSMutableHandle<JSTaggedValue> requiredModule(thread, thread->GlobalConstants()->GetUndefined()); 39 // IsInstantiatedModule is for lazy module to execute 40 if (moduleManager->IsLocalModuleLoaded(specifierString) && 41 (!moduleManager->IsLocalModuleInstantiated(specifierString))) { 42 ModuleDeregister::ReviseLoadedModuleCount(thread, specifierString); 43 JSHandle<SourceTextModule> moduleRecord = 44 moduleManager->HostGetImportedModule(specifierString); 45 requiredModule.Update(moduleRecord); 46 } else { 47 JSHandle<SourceTextModule> moduleRecord(thread, thread->GlobalConstants()->GetUndefined()); 48 if (moduleType != ModuleTypes::JSON_MODULE) { 49 // nativeModule 50 JSHandle<JSTaggedValue> nativeModuleHld = 51 moduleManager->ResolveNativeModule(specifierString, "", moduleType); 52 moduleRecord = JSHandle<SourceTextModule>::Cast(nativeModuleHld); 53 if (!SourceTextModule::LoadNativeModule(thread, moduleRecord, moduleType)) { 54 LOG_FULL(ERROR) << " dynamically loading native module" << specifierString << " failed"; 55 } 56 } else { 57 // json 58 moduleRecord = JSHandle<SourceTextModule>::Cast(ModuleDataExtractor::ParseJsonModule( 59 thread, jsPandaFile, jsPandaFile->GetJSPandaFileDesc(), specifierString)); 60 } 61 moduleRecord->SetStatus(ModuleStatus::EVALUATED); 62 moduleRecord->SetLoadingTypes(LoadingTypes::DYNAMITC_MODULE); 63 moduleRecord->SetRegisterCounts(1); 64 thread->GetEcmaVM()->PushToDeregisterModuleList(specifierString); 65 requiredModule.Update(moduleRecord); 66 } 67 68 JSHandle<JSTaggedValue> moduleNamespace = SourceTextModule::GetModuleNamespace(thread, 69 JSHandle<SourceTextModule>(requiredModule)); 70 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, BuiltinsPromiseJob::CatchException(thread, reject)); 71 JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined(); 72 EcmaRuntimeCallInfo *info = 73 EcmaInterpreter::NewRuntimeCallInfo(thread, 74 JSHandle<JSTaggedValue>(resolve), 75 undefined, undefined, 1); 76 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, BuiltinsPromiseJob::CatchException(thread, reject)); 77 info->SetCallArg(moduleNamespace.GetTaggedValue()); 78 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 79 return JSFunction::Call(info); 80} 81} // namespace panda::ecmascript 82