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