• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 namespace panda::ecmascript {
24 using PathHelper = base::PathHelper;
25 using BuiltinsPromiseJob = builtins::BuiltinsPromiseJob;
26 
ExecuteNativeModule(JSThread * thread,JSHandle<EcmaString> specifierString,ModuleTypes moduleType,JSHandle<JSPromiseReactionsFunction> resolve,JSHandle<JSPromiseReactionsFunction> reject)27 JSTaggedValue DynamicImport::ExecuteNativeModule(JSThread *thread, JSHandle<EcmaString> specifierString,
28     ModuleTypes moduleType, JSHandle<JSPromiseReactionsFunction> resolve, JSHandle<JSPromiseReactionsFunction> reject)
29 {
30     ModuleManager *moduleManager = thread->GetCurrentEcmaContext()->GetModuleManager();
31     JSMutableHandle<JSTaggedValue> requiredModule(thread, thread->GlobalConstants()->GetUndefined());
32     if (moduleManager->IsImportedModuleLoaded(specifierString.GetTaggedValue())) {
33         ModuleDeregister::ReviseLoadedModuleCount(thread, specifierString.GetTaggedValue());
34         JSHandle<SourceTextModule> moduleRecord =
35             moduleManager->HostGetImportedModule(specifierString.GetTaggedValue());
36         requiredModule.Update(moduleRecord);
37     } else {
38         CString requestPath = ConvertToString(specifierString.GetTaggedValue());
39         CString entryPoint = PathHelper::GetStrippedModuleName(requestPath);
40         JSHandle<JSTaggedValue> nativeModuleHld = moduleManager->ResolveNativeModule(requestPath, moduleType);
41         JSHandle<SourceTextModule> nativeModule = JSHandle<SourceTextModule>::Cast(nativeModuleHld);
42 
43         if (!SourceTextModule::LoadNativeModule(thread, nativeModule, JSHandle<JSTaggedValue>(specifierString),
44             moduleType)) {
45             LOG_FULL(ERROR) << " dynamically loading native module" << requestPath << " failed";
46         }
47 
48         // initialize native module
49         nativeModule->SetStatus(ModuleStatus::EVALUATED);
50         nativeModule->SetLoadingTypes(LoadingTypes::DYNAMITC_MODULE);
51         nativeModule->SetRegisterCounts(1);
52         thread->GetEcmaVM()->PushToDeregisterModuleList(requestPath);
53         requiredModule.Update(nativeModule);
54     }
55 
56     JSHandle<JSTaggedValue> moduleNamespace = SourceTextModule::GetModuleNamespace(thread,
57         JSHandle<SourceTextModule>(requiredModule));
58     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, BuiltinsPromiseJob::CatchException(thread, reject));
59     JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
60     EcmaRuntimeCallInfo *info =
61         EcmaInterpreter::NewRuntimeCallInfo(thread,
62                                             JSHandle<JSTaggedValue>(resolve),
63                                             undefined, undefined, 1);
64     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, BuiltinsPromiseJob::CatchException(thread, reject));
65     info->SetCallArg(moduleNamespace.GetTaggedValue());
66     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
67     return JSFunction::Call(info);
68 }
69 
70 }  // namespace panda::ecmascript
71