1 /*
2 * Copyright (c) 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/napi_module_loader.h"
17 #include "ecmascript/module/module_path_helper.h"
18 #include "ecmascript/module/js_module_manager.h"
19 #include "ecmascript/jspandafile/js_pandafile_manager.h"
20 #include "ecmascript/jspandafile/js_pandafile_executor.h"
21
22 namespace panda::ecmascript {
LoadModuleNameSpaceWithModuleInfo(EcmaVM * vm,CString & requestPath,CString & modulePath)23 JSHandle<JSTaggedValue> NapiModuleLoader::LoadModuleNameSpaceWithModuleInfo(EcmaVM *vm, CString &requestPath,
24 CString &modulePath)
25 {
26 LOG_ECMA(DEBUG) << "NapiModuleLoader::LoadModuleNameSpaceWithModuleInfo requestPath:" << requestPath <<
27 "," << "modulePath:" << modulePath;
28 CString moduleStr = ModulePathHelper::GetModuleNameWithPath(modulePath);
29 CString abcFilePath = ModulePathHelper::ConcatPandaFilePath(moduleStr);
30 JSThread *thread = vm->GetJSThread();
31 std::shared_ptr<JSPandaFile> curJsPandaFile;
32 if (modulePath.size() != 0) {
33 bool isValid = JSPandaFileManager::GetInstance()->CheckFilePath(thread, abcFilePath);
34 if (!isValid) {
35 CString msg = "Load file with filename '" + abcFilePath +
36 "' failed, module name '" + requestPath + "'" + ", from napi load module";
37 THROW_NEW_ERROR_AND_RETURN_HANDLE(thread, ErrorType::REFERENCE_ERROR, JSTaggedValue, msg.c_str());
38 }
39 curJsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile(thread, abcFilePath, requestPath);
40 if (vm->IsNormalizedOhmUrlPack()) {
41 ModulePathHelper::TranslateExpressionToNormalized(thread, curJsPandaFile.get(), abcFilePath, "",
42 requestPath);
43 } else if (ModulePathHelper::NeedTranstale(requestPath)) {
44 ModulePathHelper::TranstaleExpressionInput(curJsPandaFile.get(), requestPath);
45 }
46 }
47 JSHandle<JSTaggedValue> nameSp = LoadModuleNameSpaceWithPath(thread, abcFilePath, requestPath, modulePath,
48 curJsPandaFile.get());
49 return nameSp;
50 }
51
LoadModuleNameSpaceWithPath(JSThread * thread,CString & abcFilePath,CString & requestPath,CString & modulePath,const JSPandaFile * pandaFile)52 JSHandle<JSTaggedValue> NapiModuleLoader::LoadModuleNameSpaceWithPath(JSThread *thread, CString &abcFilePath,
53 CString &requestPath, CString &modulePath, const JSPandaFile *pandaFile)
54 {
55 auto [isNative, moduleType] = SourceTextModule::CheckNativeModule(requestPath);
56 ModuleManager *moduleManager = thread->GetCurrentEcmaContext()->GetModuleManager();
57 if (isNative) {
58 JSHandle<JSTaggedValue> moduleHandle = moduleManager->LoadNativeModule(thread, requestPath);
59 return moduleHandle;
60 }
61 CString entryPoint = ModulePathHelper::ConcatFileNameWithMerge(thread, pandaFile,
62 abcFilePath, modulePath, requestPath);
63 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
64
65 bool isValid = JSPandaFileManager::GetInstance()->CheckFilePath(thread, abcFilePath);
66 if (!isValid) {
67 CString msg = "Load file with filename '" + abcFilePath +
68 "' failed, module name '" + requestPath + "'" + ", from napi load module";
69 THROW_NEW_ERROR_AND_RETURN_HANDLE(thread, ErrorType::REFERENCE_ERROR, JSTaggedValue, msg.c_str());
70 }
71 std::shared_ptr<JSPandaFile> jsPandaFile =
72 JSPandaFileManager::GetInstance()->LoadJSPandaFile(thread, abcFilePath, entryPoint);
73
74 JSRecordInfo *recordInfo = nullptr;
75 bool hasRecord = jsPandaFile->CheckAndGetRecordInfo(entryPoint, &recordInfo);
76 if (!hasRecord) {
77 LOG_FULL(ERROR) << "cannot find record '" << entryPoint <<"' in basefileName " << abcFilePath << ","
78 << "from napi load module";
79 CString msg = "cannot find record '" + entryPoint + "' in basefileName " + abcFilePath + "," +
80 "from napi load module";
81 THROW_NEW_ERROR_AND_RETURN_HANDLE(thread, ErrorType::REFERENCE_ERROR, JSTaggedValue, msg.c_str());
82 }
83 // IsInstantiatedModule is for lazy module to execute
84 if (!moduleManager->IsLocalModuleLoaded(entryPoint) || moduleManager->IsLocalModuleInstantiated(entryPoint)) {
85 if (!JSPandaFileExecutor::ExecuteFromAbcFile(thread, abcFilePath, entryPoint.c_str(), false, true)) {
86 CString msg = "Cannot execute request from napi load module : " + entryPoint +
87 ", from napi load module";
88 THROW_NEW_ERROR_AND_RETURN_HANDLE(thread, ErrorType::REFERENCE_ERROR, JSTaggedValue, msg.c_str());
89 }
90 }
91 JSHandle<SourceTextModule> moduleRecord = moduleManager->HostGetImportedModule(entryPoint);
92 JSHandle<JSTaggedValue> nameSp = SourceTextModule::GetModuleNamespace(thread, moduleRecord);
93 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
94 return nameSp;
95 }
96 }