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/module/js_shared_module_manager.h"
20 #include "ecmascript/jspandafile/js_pandafile_manager.h"
21 #include "ecmascript/jspandafile/js_pandafile_executor.h"
22
23 namespace panda::ecmascript {
24 template<ForHybridApp isHybrid>
LoadModuleNameSpaceWithModuleInfo(EcmaVM * vm,CString & requestPath,CString & modulePath,CString & abcFilePath)25 JSHandle<JSTaggedValue> NapiModuleLoader::LoadModuleNameSpaceWithModuleInfo(EcmaVM *vm, CString &requestPath,
26 CString &modulePath, CString &abcFilePath)
27 {
28 LOG_ECMA(INFO) << "NapiModuleLoader::LoadModuleNameSpaceWithModuleInfo requestPath:" << requestPath <<
29 "," << "modulePath:" << modulePath;
30 JSThread *thread = vm->GetJSThread();
31 std::shared_ptr<JSPandaFile> curJsPandaFile;
32 if (!modulePath.empty()) {
33 curJsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile<isHybrid>(thread,
34 abcFilePath, requestPath, false, ExecuteTypes::NAPI);
35 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
36 if (curJsPandaFile == nullptr) {
37 LOG_FULL(FATAL) << "Load current file's panda file failed. Current file is " << abcFilePath;
38 }
39 if (vm->IsNormalizedOhmUrlPack()) {
40 ModulePathHelper::TranslateExpressionToNormalized(thread, curJsPandaFile.get(), abcFilePath, "",
41 requestPath);
42 } else if (ModulePathHelper::NeedTranstale(requestPath)) {
43 ModulePathHelper::TranstaleExpressionInput(curJsPandaFile.get(), requestPath);
44 }
45 }
46 JSHandle<JSTaggedValue> nameSp = LoadModuleNameSpaceWithPath(thread, abcFilePath, requestPath, modulePath,
47 curJsPandaFile.get());
48 return nameSp;
49 }
50 template JSHandle<JSTaggedValue> NapiModuleLoader::LoadModuleNameSpaceWithModuleInfo<ForHybridApp::Normal>(EcmaVM *vm,
51 CString &requestPath, CString &modulePath, CString &abcFilePath);
52 template JSHandle<JSTaggedValue> NapiModuleLoader::LoadModuleNameSpaceWithModuleInfo<ForHybridApp::Hybrid>(EcmaVM *vm,
53 CString &requestPath, CString &modulePath, CString &abcFilePath);
54
55 template<ForHybridApp isHybrid>
LoadModuleNameSpace(EcmaVM * vm,CString requestPath,const CString & moduleName,CString & abcFilePath)56 JSHandle<JSTaggedValue> NapiModuleLoader::LoadModuleNameSpace(EcmaVM *vm, CString requestPath,
57 const CString &moduleName, CString &abcFilePath)
58 {
59 JSThread *thread = vm->GetJSThread();
60 CString path = base::ConcatToCString(vm->GetBundleName(), PathHelper::SLASH_TAG);
61 // RequestPath starts with ets/xxx
62
63 if (StringHelper::StringStartWith(requestPath, ModulePathHelper::PREFIX_ETS)) {
64 path += moduleName;
65 CString recordNameStr = ModulePathHelper::TranslateNapiFileRequestPath(thread, path, requestPath);
66 LOG_ECMA(DEBUG) << "NapiModuleLoader::LoadFilePathWithinModule: Concated recordName " << recordNameStr;
67 return LoadModuleNameSpaceFromFile<isHybrid>(thread, recordNameStr, abcFilePath);
68 }
69 CString abcModuleName = ModulePathHelper::GetModuleNameWithBaseFile(abcFilePath);
70 CString srcPrefix = base::ConcatToCString(abcModuleName, ModulePathHelper::PHYCICAL_FILE_PATH.data());
71 path += abcModuleName;
72 // RequestPath starts with moduleName/src/main/xxx
73 if (StringHelper::StringStartWith(requestPath, srcPrefix)) {
74 return LoadFilePathWithinModule<isHybrid>(thread, abcFilePath, srcPrefix, requestPath, path);
75 }
76 return LoadModuleNameSpaceWithModuleInfo<isHybrid>(vm, requestPath, path, abcFilePath);
77 }
78
79 template JSHandle<JSTaggedValue> NapiModuleLoader::LoadModuleNameSpace<ForHybridApp::Normal>(EcmaVM *vm,
80 CString requestPath, const CString& moduleName, CString& abcFilePath);
81 template JSHandle<JSTaggedValue> NapiModuleLoader::LoadModuleNameSpace<ForHybridApp::Hybrid>(EcmaVM *vm,
82 CString requestPath, const CString& moduleName, CString& abcFilePath);
83
84 template<ForHybridApp isHybrid>
LoadModuleNameSpace(EcmaVM * vm,CString requestPath,CString modulePath)85 JSHandle<JSTaggedValue> NapiModuleLoader::LoadModuleNameSpace(EcmaVM *vm, CString requestPath, CString modulePath)
86 {
87 JSThread *thread = vm->GetJSThread();
88 CString moduleName = ModulePathHelper::GetModuleNameWithPath(modulePath);
89 CString abcFilePath = ModulePathHelper::ConcatPandaFilePath(moduleName);
90 CString srcPrefix = base::ConcatToCString(moduleName, ModulePathHelper::PHYCICAL_FILE_PATH.data());
91 // RequestPath starts with moduleName/src/main/xxx
92 if (StringHelper::StringStartWith(requestPath, srcPrefix)) {
93 return LoadFilePathWithinModule<isHybrid>(thread, abcFilePath, srcPrefix, requestPath, modulePath);
94 }
95 return LoadModuleNameSpaceWithModuleInfo<isHybrid>(vm, requestPath, modulePath, abcFilePath);
96 }
97 template JSHandle<JSTaggedValue> NapiModuleLoader::LoadModuleNameSpace<ForHybridApp::Normal>(EcmaVM *vm,
98 CString requestPath, CString modulePath);
99 template JSHandle<JSTaggedValue> NapiModuleLoader::LoadModuleNameSpace<ForHybridApp::Hybrid>(EcmaVM *vm,
100 CString requestPath, CString modulePath);
101
102 template<ForHybridApp isHybrid>
LoadFilePathWithinModule(JSThread * thread,const CString & abcFilePath,const CString & srcPrefix,const CString & requestPath,const CString & modulePath)103 JSHandle<JSTaggedValue> NapiModuleLoader::LoadFilePathWithinModule(JSThread *thread, const CString& abcFilePath,
104 const CString& srcPrefix, const CString& requestPath, const CString& modulePath)
105 {
106 if (requestPath.size() > srcPrefix.size()) {
107 // Sub after moduleName/src/main/
108 CString fileName = requestPath.substr(srcPrefix.size() + 1);
109 CString recordNameStr = ModulePathHelper::TranslateNapiFileRequestPath(thread, modulePath, fileName);
110 LOG_ECMA(DEBUG) << "NapiModuleLoader::LoadFilePathWithinModule: Concated recordName " << recordNameStr;
111 return LoadModuleNameSpaceFromFile<isHybrid>(thread, recordNameStr, abcFilePath);
112 } else {
113 CString msg = "cannot find record '" + requestPath + "' in basefileName " + abcFilePath + "," +
114 "from napi load module";
115 THROW_NEW_ERROR_AND_RETURN_HANDLE(thread, ErrorType::REFERENCE_ERROR, JSTaggedValue, msg.c_str());
116 }
117 }
118 template JSHandle<JSTaggedValue> NapiModuleLoader::LoadFilePathWithinModule<ForHybridApp::Normal>(JSThread *thread,
119 const CString& abcFilePath, const CString& srcPrefix, const CString& requestPath, const CString& modulePath);
120 template JSHandle<JSTaggedValue> NapiModuleLoader::LoadFilePathWithinModule<ForHybridApp::Hybrid>(JSThread *thread,
121 const CString& abcFilePath, const CString& srcPrefix, const CString& requestPath, const CString& modulePath);
122
LoadModuleNameSpaceWithPath(JSThread * thread,CString & abcFilePath,const CString & requestPath,const CString & modulePath,const JSPandaFile * pandaFile)123 JSHandle<JSTaggedValue> NapiModuleLoader::LoadModuleNameSpaceWithPath(JSThread *thread, CString &abcFilePath,
124 const CString &requestPath, const CString &modulePath, const JSPandaFile *pandaFile)
125 {
126 ModuleManager *moduleManager = thread->GetModuleManager();
127 if (SourceTextModule::IsNativeModule(requestPath)) {
128 JSHandle<JSTaggedValue> moduleHandle = moduleManager->LoadNativeModule(thread, requestPath);
129 return moduleHandle;
130 }
131 CString entryPoint = ModulePathHelper::ConcatFileNameWithMerge(thread, pandaFile,
132 abcFilePath, modulePath, requestPath);
133 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
134 return LoadModuleNameSpaceFromFile(thread, entryPoint, abcFilePath);
135 }
136
137 template<ForHybridApp isHybrid>
LoadModuleNameSpaceFromFile(JSThread * thread,const CString & entryPoint,const CString & abcFilePath)138 JSHandle<JSTaggedValue> NapiModuleLoader::LoadModuleNameSpaceFromFile(
139 JSThread *thread, const CString &entryPoint, const CString &abcFilePath)
140 {
141 std::shared_ptr<JSPandaFile> jsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile<isHybrid>(
142 thread, abcFilePath, entryPoint, false, ExecuteTypes::NAPI);
143 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
144 if (jsPandaFile == nullptr) {
145 LOG_FULL(FATAL) << "Load current file's panda file failed. Current file is " << abcFilePath;
146 }
147 JSRecordInfo *recordInfo = jsPandaFile->CheckAndGetRecordInfo(entryPoint);
148 if (recordInfo == nullptr) {
149 LOG_FULL(ERROR) << "cannot find record '" << entryPoint <<"' in basefileName " << abcFilePath << ","
150 << "from napi load module";
151 CString msg = "cannot find record '" + entryPoint + "' in basefileName " + abcFilePath + "," +
152 "from napi load module";
153 THROW_NEW_ERROR_AND_RETURN_HANDLE(thread, ErrorType::REFERENCE_ERROR, JSTaggedValue, msg.c_str());
154 }
155
156 if (jsPandaFile->IsSharedModule(recordInfo)) {
157 LockHolder lock(SharedModuleManager::GetInstance()->GetSharedMutex());
158 return ecmascript::NapiModuleLoader::GetModuleNameSpace(
159 thread, entryPoint, abcFilePath);
160 }
161 return ecmascript::NapiModuleLoader::GetModuleNameSpace(
162 thread, entryPoint, abcFilePath);
163 }
164 template JSHandle<JSTaggedValue> NapiModuleLoader::LoadModuleNameSpaceFromFile<ForHybridApp::Normal>(
165 JSThread *thread, const CString &entryPoint, const CString &abcFilePath);
166 template JSHandle<JSTaggedValue> NapiModuleLoader::LoadModuleNameSpaceFromFile<ForHybridApp::Hybrid>(
167 JSThread *thread, const CString &entryPoint, const CString &abcFilePath);
168
GetModuleNameSpace(JSThread * thread,const CString & entryPoint,const CString & abcFilePath)169 JSHandle<JSTaggedValue> NapiModuleLoader::GetModuleNameSpace(JSThread *thread, const CString &entryPoint,
170 const CString &abcFilePath)
171 {
172 ModuleManager *moduleManager = thread->GetModuleManager();
173 // IsInstantiatedModule is for lazy module to execute
174 if (moduleManager->NeedExecuteModule(entryPoint)) {
175 if (!JSPandaFileExecutor::ExecuteFromAbcFile(
176 thread, abcFilePath, entryPoint, false, ExecuteTypes::NAPI)) {
177 CString msg = "Cannot execute request from napi load module : " + entryPoint +
178 ", from napi load module";
179 THROW_NEW_ERROR_AND_RETURN_HANDLE(thread, ErrorType::REFERENCE_ERROR, JSTaggedValue, msg.c_str());
180 }
181 }
182
183 JSHandle<SourceTextModule> moduleRecord = moduleManager->GetImportedModule(entryPoint);
184 auto &options = const_cast<EcmaVM *>(thread->GetEcmaVM())->GetJSOptions();
185 if (options.EnableModuleException()) {
186 moduleRecord->CheckAndThrowModuleError(thread);
187 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
188 }
189 JSHandle<JSTaggedValue> nameSp = SourceTextModule::GetModuleNamespace(thread, moduleRecord);
190 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
191 return nameSp;
192 }
193 }