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