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