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