diff --git a/ecmascript/builtins/builtins_global.cpp b/ecmascript/builtins/builtins_global.cpp index b7ddbce6bde37ca106267aa4ee5576eb65bdb612..3a47f6f84cb64c8d546d765fb027a61a3a679521 100644 --- a/ecmascript/builtins/builtins_global.cpp +++ b/ecmascript/builtins/builtins_global.cpp @@ -731,7 +731,7 @@ JSTaggedValue BuiltinsGlobal::LoadNativeModule(EcmaRuntimeCallInfo *msg) CString abcFilePath = fileName.c_str(); if (moduleName.size() != 0) { curJsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile( - thread, abcFilePath, requestPath, false, false, ExecuteTypes::STATIC); + thread, abcFilePath, requestPath, false, ExecuteTypes::STATIC); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); if (curJsPandaFile == nullptr) { errorMsg = "Load native module failed, filename '" + abcFilePath + diff --git a/ecmascript/builtins/builtins_promise_job.cpp b/ecmascript/builtins/builtins_promise_job.cpp index d8de2bf3b6b877f81c10505dba2ddfe541972d89..d705932ca9e317724f7ed32fb5cc17b2a1ba5711 100644 --- a/ecmascript/builtins/builtins_promise_job.cpp +++ b/ecmascript/builtins/builtins_promise_job.cpp @@ -158,7 +158,7 @@ JSTaggedValue BuiltinsPromiseJob::DynamicImportJob(EcmaRuntimeCallInfo *argv) if (!recordName->IsUndefined()) { recordNameStr = ModulePathHelper::Utf8ConvertToString(recordName.GetTaggedValue()); curJsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile( - thread, fileName, recordNameStr.c_str(), false, false, ExecuteTypes::STATIC); + thread, fileName, recordNameStr.c_str(), false, ExecuteTypes::STATIC); RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, CatchException(thread, reject)); if (curJsPandaFile == nullptr) { LOG_FULL(FATAL) << "Load current file's panda file failed. Current file is " << recordNameStr; @@ -194,7 +194,7 @@ JSTaggedValue BuiltinsPromiseJob::DynamicImportJob(EcmaRuntimeCallInfo *argv) moduleName = entryPoint; } std::shared_ptr jsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile( - thread, fileName, entryPoint, false, false, ExecuteTypes::STATIC); + thread, fileName, entryPoint, false, ExecuteTypes::STATIC); RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, CatchException(thread, reject)); if (jsPandaFile == nullptr) { LOG_FULL(FATAL) << "Load current file's panda file failed. Current file is " << fileName; diff --git a/ecmascript/compiler/aot_file/tests/aot_file_test.cpp b/ecmascript/compiler/aot_file/tests/aot_file_test.cpp index dc311d04c365686af65f833f47ea1089eb67ca4c..cdc950e9c81b27dd17b4154e412c037eaf52c8c3 100644 --- a/ecmascript/compiler/aot_file/tests/aot_file_test.cpp +++ b/ecmascript/compiler/aot_file/tests/aot_file_test.cpp @@ -316,7 +316,7 @@ protected: // Ensure AOT file exists ASSERT_TRUE(AOTFileManager::AOTFileExist(testAotPath_, AOTFileManager::FILE_EXTENSION_AN)); std::shared_ptr jsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile( - vm->GetJSThread(), CString(testAbcPath_.c_str()), testRecordName_, false, false, ExecuteTypes::STATIC); + vm->GetJSThread(), CString(testAbcPath_.c_str()), testRecordName_, false, ExecuteTypes::STATIC); ASSERT_TRUE(jsPandaFile != nullptr); // Perform binding vm->GetAOTFileManager()->BindPreloadedPandaFilesInAotFile(testRecordName_); @@ -368,7 +368,6 @@ protected: CString(testAbcPath.c_str()), testRecordName, false, - false, ExecuteTypes::STATIC); ASSERT_TRUE(jsPandaFile.get() != nullptr); // Test command-line mode branch @@ -401,7 +400,6 @@ protected: CString(testAbcPath.c_str()), testRecordName, false, - false, ExecuteTypes::STATIC); ASSERT_TRUE(jsPandaFile.get() != nullptr); // Verify WasAOTOutputFileSet is false diff --git a/ecmascript/ecma_vm.h b/ecmascript/ecma_vm.h index 68c438a26dadb3ede5a6e5ec8bc81d7fbb00a3fb..f944146181defb7fd4e2e15d133f229b6d74c437 100644 --- a/ecmascript/ecma_vm.h +++ b/ecmascript/ecma_vm.h @@ -128,7 +128,7 @@ using SourceMapCallback = std::function; using ResolveBufferCallback = - std::function; + std::function; using TimerCallbackFunc = void (*)(void *data); using TimerTaskCallback = void* (*)(EcmaVM *vm, void *data, TimerCallbackFunc func, uint64_t timeout, bool repeat); using CancelTimerCallback = void (*)(void *timerCallbackInfo); @@ -556,6 +556,16 @@ public: return resolveBufferCallback_; } + void SetResolveBufferCallbackForHybridApp(ResolveBufferCallback cb) + { + resolveBufferCallbackForHybridApp_ = cb; + } + + ResolveBufferCallback GetResolveBufferCallbackForHybridApp() const + { + return resolveBufferCallbackForHybridApp_; + } + void SetTimerTaskCallback(TimerTaskCallback callback) { timerTaskCallback_ = callback; @@ -1439,6 +1449,7 @@ private: // resolve path to get abc's buffer ResolveBufferCallback resolveBufferCallback_ {nullptr}; + ResolveBufferCallback resolveBufferCallbackForHybridApp_ {nullptr}; // set timer task to execute callback on time TimerTaskCallback timerTaskCallback_ {nullptr}; diff --git a/ecmascript/jspandafile/js_pandafile_executor.cpp b/ecmascript/jspandafile/js_pandafile_executor.cpp index 128e69084a0930dfa5619dbedf38cf0cb5c1d01b..7e15c9abd7f57c21848031a20fb578bc1a980c96 100644 --- a/ecmascript/jspandafile/js_pandafile_executor.cpp +++ b/ecmascript/jspandafile/js_pandafile_executor.cpp @@ -33,7 +33,7 @@ Expected JSPandaFileExecutor::ExecuteFromFile(JSThread *thr EcmaVM *vm = thread->GetEcmaVM(); std::shared_ptr jsPandaFile = - JSPandaFileManager::GetInstance()->LoadJSPandaFile(thread, name, entry, needUpdate, false, executeType); + JSPandaFileManager::GetInstance()->LoadJSPandaFile(thread, name, entry, needUpdate, executeType); RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, Unexpected(false)); if (jsPandaFile == nullptr) { #ifdef FUZZ_TEST @@ -429,7 +429,7 @@ Expected JSPandaFileExecutor::LazyExecuteModule( std::shared_ptr jsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile( - thread, filename, recordName, false, false, ExecuteTypes::STATIC); + thread, filename, recordName, false, ExecuteTypes::STATIC); RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, Unexpected(false)); if (jsPandaFile == nullptr) { #ifdef FUZZ_TEST @@ -476,7 +476,7 @@ int JSPandaFileExecutor::ExecuteAbcFileWithSingletonPatternFlag(JSThread *thread { CString abcFilePath = ModulePathHelper::ConcatPandaFilePath(moduleName); std::shared_ptr jsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile( - thread, abcFilePath, entry, false, false, ExecuteTypes::STATIC); + thread, abcFilePath, entry, false, ExecuteTypes::STATIC); RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, ROUTE_URI_ERROR); if (jsPandaFile == nullptr) { LOG_ECMA(ERROR) << "When the route jump, loading panda file failed. Current file is " << abcFilePath; @@ -519,7 +519,7 @@ bool JSPandaFileExecutor::IsExecuteModuleInAbcFile(JSThread *thread, [[maybe_unu return false; } std::shared_ptr jsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile( - thread, abcFilePath, entry, false, false, ExecuteTypes::STATIC); + thread, abcFilePath, entry, false, ExecuteTypes::STATIC); RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false); if (jsPandaFile == nullptr) { LOG_ECMA(ERROR) << "When checking if module is in abc file, loading panda file failed. Current file is " << diff --git a/ecmascript/jspandafile/js_pandafile_manager.cpp b/ecmascript/jspandafile/js_pandafile_manager.cpp index 30feebd395522d7b281be174fd60069d70d3dba5..7bc3bb51cf231296f9eb1fa74e33ed29b5252e4e 100644 --- a/ecmascript/jspandafile/js_pandafile_manager.cpp +++ b/ecmascript/jspandafile/js_pandafile_manager.cpp @@ -49,8 +49,9 @@ JSPandaFileManager::~JSPandaFileManager() * Typically return nullptr for JSPandafile load fail. Throw cppcrash if load hsp failed. * Specifically, return jscrash if napi load hsp failed. */ +template std::shared_ptr JSPandaFileManager::LoadJSPandaFile(JSThread *thread, const CString &filename, - std::string_view entryPoint, bool needUpdate, bool isHybrid, const ExecuteTypes &executeType) + std::string_view entryPoint, bool needUpdate, const ExecuteTypes &executeType) { { LockHolder lock(jsPandaFileLock_); @@ -79,6 +80,9 @@ std::shared_ptr JSPandaFileManager::LoadJSPandaFile(JSThread *threa if (!vm->IsBundlePack() && moduleManager->GetExecuteMode() == ModuleExecuteMode::ExecuteBufferMode && !vm->IsRestrictedWorkerThread()) { ResolveBufferCallback resolveBufferCallback = vm->GetResolveBufferCallback(); + if constexpr (isHybrid == ForHybridApp::Hybrid) { + resolveBufferCallback = vm->GetResolveBufferCallbackForHybridApp(); + } if (resolveBufferCallback == nullptr) { LoadJSPandaFileFailLog("[ArkRuntime Log] Importing shared package is not supported in the Previewer."); LOG_FULL(FATAL) << "resolveBufferCallback is nullptr"; @@ -97,7 +101,7 @@ std::shared_ptr JSPandaFileManager::LoadJSPandaFile(JSThread *threa uint8_t *data = nullptr; size_t dataSize = 0; std::string errorMsg; - bool getBuffer = resolveBufferCallback(hspPath, isHybrid, &data, &dataSize, errorMsg); + bool getBuffer = resolveBufferCallback(hspPath, &data, &dataSize, errorMsg); if (!getBuffer) { LoadJSPandaFileFailLog("[ArkRuntime Log] Importing shared package in the Previewer."); CString msg = "resolveBufferCallback get hsp buffer failed, hsp path:" + filename + @@ -130,6 +134,10 @@ std::shared_ptr JSPandaFileManager::LoadJSPandaFile(JSThread *threa #endif return jsPandaFile; } +template std::shared_ptr JSPandaFileManager::LoadJSPandaFile(JSThread *thread, + const CString &filename, std::string_view entryPoint, bool needUpdate, const ExecuteTypes &executeType); +template std::shared_ptr JSPandaFileManager::LoadJSPandaFile(JSThread *thread, + const CString &filename, std::string_view entryPoint, bool needUpdate, const ExecuteTypes &executeType); // The security interface needs to be modified accordingly. std::shared_ptr JSPandaFileManager::LoadJSPandaFile(JSThread *thread, const CString &filename, @@ -552,7 +560,8 @@ std::shared_ptr JSPandaFileManager::GenerateJSPandaFile(JSThread *t /* * Check whether the file path can be loaded into pandafile, excluding bundle packaging and decompression paths */ -bool JSPandaFileManager::CheckFilePath(JSThread *thread, const CString &fileName, bool isHybrid) +template +bool JSPandaFileManager::CheckFilePath(JSThread *thread, const CString &fileName) { std::shared_ptr jsPandaFile = FindJSPandaFileUnlocked(fileName); if (jsPandaFile != nullptr) { @@ -562,6 +571,9 @@ bool JSPandaFileManager::CheckFilePath(JSThread *thread, const CString &fileName ModuleManager *moduleManager = thread->GetModuleManager(); if (!vm->IsBundlePack() && moduleManager->GetExecuteMode() == ModuleExecuteMode::ExecuteBufferMode) { ResolveBufferCallback resolveBufferCallback = vm->GetResolveBufferCallback(); + if constexpr (isHybrid == ForHybridApp::Hybrid) { + resolveBufferCallback = vm->GetResolveBufferCallbackForHybridApp(); + } if (resolveBufferCallback == nullptr) { LOG_FULL(ERROR) << "When checking file path, resolveBufferCallback is nullptr"; return false; @@ -569,8 +581,7 @@ bool JSPandaFileManager::CheckFilePath(JSThread *thread, const CString &fileName uint8_t *data = nullptr; size_t dataSize = 0; std::string errorMsg; - bool getBuffer = - resolveBufferCallback(ModulePathHelper::ParseHapPath(fileName), isHybrid, &data, &dataSize, errorMsg); + bool getBuffer = resolveBufferCallback(ModulePathHelper::ParseHapPath(fileName), &data, &dataSize, errorMsg); if (!getBuffer) { LOG_FULL(ERROR) << "When checking file path, resolveBufferCallback get buffer failed, errorMsg = " << errorMsg; @@ -580,6 +591,9 @@ bool JSPandaFileManager::CheckFilePath(JSThread *thread, const CString &fileName return true; } +template bool JSPandaFileManager::CheckFilePath(JSThread *thread, const CString &fileName); +template bool JSPandaFileManager::CheckFilePath(JSThread *thread, const CString &fileName); + std::shared_ptr JSPandaFileManager::GenerateJSPandafileFromBufferCache( JSThread *thread, const CString &filename, std::string_view entryPoint) { diff --git a/ecmascript/jspandafile/js_pandafile_manager.h b/ecmascript/jspandafile/js_pandafile_manager.h index 734a0f34d07d3f3a56c3320656ee2f1e8d1b0f9b..bbdb83cd38294cc99c6af3a1711accdb41260aaa 100644 --- a/ecmascript/jspandafile/js_pandafile_manager.h +++ b/ecmascript/jspandafile/js_pandafile_manager.h @@ -33,8 +33,9 @@ public: JSHandle GenerateProgram(EcmaVM *vm, const JSPandaFile *jsPandaFile, std::string_view entryPoint); + template std::shared_ptr LoadJSPandaFile(JSThread *thread, const CString &filename, std::string_view entryPoint, - bool needUpdate = false, bool isHybrid = false, + bool needUpdate = false, const ExecuteTypes &executeType = ExecuteTypes::STATIC); std::shared_ptr LoadJSPandaFile(JSThread *thread, const CString &filename, std::string_view entryPoint, @@ -58,8 +59,9 @@ public: DebugInfoExtractor *GetJSPtExtractorAndExtract(const JSPandaFile *jsPandaFile); DebugInfoExtractor *CpuProfilerGetJSPtExtractor(const JSPandaFile *jsPandaFile); - - bool CheckFilePath(JSThread *thread, const CString &fileName, bool isHybrid = false); + + template + bool CheckFilePath(JSThread *thread, const CString &fileName); // for debugger template diff --git a/ecmascript/jspandafile/tests/js_pandafile_manager_test.cpp b/ecmascript/jspandafile/tests/js_pandafile_manager_test.cpp index 1f369a8356e3e22c15579f67f2d18936b9ef9968..f1eecb2bc66b297c4dc98fd46712fd9a4f069e8a 100644 --- a/ecmascript/jspandafile/tests/js_pandafile_manager_test.cpp +++ b/ecmascript/jspandafile/tests/js_pandafile_manager_test.cpp @@ -166,7 +166,7 @@ HWTEST_F_L0(JSPandaFileManagerTest, MultiEcmaVM_Add_Find_Remove_JSPandaFile) JSThread *thread1; TestHelper::CreateEcmaVMWithScope(instance1, thread1, scope1); std::shared_ptr loadedPf1 = pfManager->LoadJSPandaFile( - thread1, filename1, JSPandaFile::ENTRY_MAIN_FUNCTION, false, false, ExecuteTypes::STATIC); + thread1, filename1, JSPandaFile::ENTRY_MAIN_FUNCTION, false, ExecuteTypes::STATIC); EXPECT_TRUE(pf1 == loadedPf1); EXPECT_TRUE(instance1->GetJSThread()->GetEcmaVM()->HasCachedConstpool(pf1.get())); TestHelper::DestroyEcmaVMWithScope(instance1, scope1); // Remove 'instance1' when ecmaVM destruct. diff --git a/ecmascript/module/js_module_source_text.cpp b/ecmascript/module/js_module_source_text.cpp index 5e71844da8fab8ed251710704933765c271a8836..c739f52308cdb4b283e91a6b1883971c0114d541 100644 --- a/ecmascript/module/js_module_source_text.cpp +++ b/ecmascript/module/js_module_source_text.cpp @@ -1186,7 +1186,7 @@ Expected SourceTextModule::ModuleExecution(JSThread *thread JSPandaFileManager::GetInstance()->LoadJSPandaFile(thread, moduleFilenameStr, entryPoint, buffer, size); } else { jsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile( - thread, moduleFilenameStr, entryPoint, false, false, executeType); + thread, moduleFilenameStr, entryPoint, false, executeType); } RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, Unexpected(false)); if (jsPandaFile == nullptr) { // LCOV_EXCL_BR_LINE @@ -1728,7 +1728,7 @@ void SourceTextModule::ExecuteAsyncModule(JSThread *thread, const JSHandleLoadJSPandaFile(thread, moduleFilenameStr, entryPoint, buffer, size); } else { jsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile( - thread, moduleFilenameStr, entryPoint, false, false, executeType); + thread, moduleFilenameStr, entryPoint, false, executeType); } RETURN_IF_ABRUPT_COMPLETION(thread); if (jsPandaFile == nullptr) { // LCOV_EXCL_BR_LINE diff --git a/ecmascript/module/module_resolver.cpp b/ecmascript/module/module_resolver.cpp index 355886d392834b6f307602032f594533bb801071..69ded0c7d75dce271a6cff0babdc5202f71124f0 100644 --- a/ecmascript/module/module_resolver.cpp +++ b/ecmascript/module/module_resolver.cpp @@ -43,7 +43,7 @@ JSHandle ModuleResolver::HostResolveImportedModule(JSThread* thre { if (jsPandaFile == nullptr) { std::shared_ptr file = - JSPandaFileManager::GetInstance()->LoadJSPandaFile(thread, fileName, recordName, false, false, executeType); + JSPandaFileManager::GetInstance()->LoadJSPandaFile(thread, fileName, recordName, false, executeType); RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread); if (file == nullptr) { CString msg = "Load file with filename '" + fileName + "' failed, recordName '" + recordName + "'"; @@ -103,7 +103,7 @@ JSHandle ModuleResolver::HostResolveImportedModuleWithMerge(JSThr } CString recordName = module->GetEcmaModuleRecordNameString(); std::shared_ptr pandaFile = - JSPandaFileManager::GetInstance()->LoadJSPandaFile(thread, baseFilename, recordName, false, false, executeType); + JSPandaFileManager::GetInstance()->LoadJSPandaFile(thread, baseFilename, recordName, false, executeType); RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread); if (pandaFile == nullptr) { // LCOV_EXCL_BR_LINE LOG_FULL(FATAL) << "Load current file's panda file failed. Current file is " << baseFilename; @@ -182,7 +182,8 @@ JSHandle ModuleResolver::HostResolveImportedModuleForHotReload(JS const ExecuteTypes &executeType) { std::shared_ptr jsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile( - thread, moduleFileName, recordName, false, false, executeType); + thread, moduleFileName, recordName, false, executeType); + RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread); if (jsPandaFile == nullptr) { // LCOV_EXCL_BR_LINE LOG_FULL(FATAL) << "Load current file's panda file failed. Current file is " << moduleFileName; } @@ -209,7 +210,7 @@ JSHandle ModuleResolver::HostResolveImportedModuleWithMerge(JSThr { if (jsPandaFile == nullptr) { std::shared_ptr file = JSPandaFileManager::GetInstance()->LoadJSPandaFile( - thread, moduleFileName, recordName, false, false, executeType); + thread, moduleFileName, recordName, false, executeType); RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread); if (file == nullptr) { CString msg = "Load file with filename '" + moduleFileName + "' failed, recordName '" + recordName + "'"; @@ -257,7 +258,7 @@ JSHandle ModuleResolver::HostResolveImportedModuleBundlePack(JSTh // Can not use jsPandaFile from js_pandafile_executor, need to construct with JSPandaFile::ENTRY_MAIN_FUNCTION std::shared_ptr jsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile( - thread, referencingModule, JSPandaFile::ENTRY_MAIN_FUNCTION, false, false, executeType); + thread, referencingModule, JSPandaFile::ENTRY_MAIN_FUNCTION, false, executeType); RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread); if (jsPandaFile == nullptr) { // LCOV_EXCL_BR_LINE LOG_FULL(FATAL) << "Load current file's panda file failed. Current file is " << referencingModule; @@ -278,7 +279,7 @@ JSHandle ModuleResolver::HostResolveImportedModuleBundlePack(JSTh return module; } std::shared_ptr pandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile( - thread, moduleFileName, JSPandaFile::ENTRY_MAIN_FUNCTION, false, false, executeType); + thread, moduleFileName, JSPandaFile::ENTRY_MAIN_FUNCTION, false, executeType); RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread); if (pandaFile == nullptr) { // LCOV_EXCL_BR_LINE LOG_FULL(FATAL) << "Load current file's panda file failed. Current file is " << referencingModule; diff --git a/ecmascript/module/napi_module_loader.cpp b/ecmascript/module/napi_module_loader.cpp index 9ffb7ec70298c75fe758b3ca6f67137bfd5612a4..70839d928f903192b66ee4611ce8f3137a169843 100644 --- a/ecmascript/module/napi_module_loader.cpp +++ b/ecmascript/module/napi_module_loader.cpp @@ -21,17 +21,17 @@ #include "ecmascript/jspandafile/js_pandafile_executor.h" namespace panda::ecmascript { +template JSHandle NapiModuleLoader::LoadModuleNameSpaceWithModuleInfo(EcmaVM *vm, CString &requestPath, - CString &modulePath, CString &abcFilePath, - bool isHybrid) + CString &modulePath, CString &abcFilePath) { LOG_ECMA(INFO) << "NapiModuleLoader::LoadModuleNameSpaceWithModuleInfo requestPath:" << requestPath << "," << "modulePath:" << modulePath; JSThread *thread = vm->GetJSThread(); std::shared_ptr curJsPandaFile; if (!modulePath.empty()) { - curJsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile(thread, abcFilePath, requestPath, false, - isHybrid, ExecuteTypes::NAPI); + curJsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile(thread, + abcFilePath, requestPath, false, ExecuteTypes::NAPI); RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread); if (curJsPandaFile == nullptr) { LOG_FULL(FATAL) << "Load current file's panda file failed. Current file is " << abcFilePath; @@ -47,10 +47,14 @@ JSHandle NapiModuleLoader::LoadModuleNameSpaceWithModuleInfo(Ecma curJsPandaFile.get()); return nameSp; } +template JSHandle NapiModuleLoader::LoadModuleNameSpaceWithModuleInfo(EcmaVM *vm, + CString &requestPath, CString &modulePath, CString &abcFilePath); +template JSHandle NapiModuleLoader::LoadModuleNameSpaceWithModuleInfo(EcmaVM *vm, + CString &requestPath, CString &modulePath, CString &abcFilePath); +template JSHandle NapiModuleLoader::LoadModuleNameSpace(EcmaVM *vm, CString requestPath, - const CString &moduleName, CString &abcFilePath, - bool isHybrid) + const CString &moduleName, CString &abcFilePath) { JSThread *thread = vm->GetJSThread(); CString path = base::ConcatToCString(vm->GetBundleName(), PathHelper::SLASH_TAG); @@ -60,20 +64,25 @@ JSHandle NapiModuleLoader::LoadModuleNameSpace(EcmaVM *vm, CStrin path += moduleName; CString recordNameStr = ModulePathHelper::TranslateNapiFileRequestPath(thread, path, requestPath); LOG_ECMA(DEBUG) << "NapiModuleLoader::LoadFilePathWithinModule: Concated recordName " << recordNameStr; - return LoadModuleNameSpaceFromFile(thread, recordNameStr, abcFilePath); + return LoadModuleNameSpaceFromFile(thread, recordNameStr, abcFilePath); } CString abcModuleName = ModulePathHelper::GetModuleNameWithBaseFile(abcFilePath); CString srcPrefix = base::ConcatToCString(abcModuleName, ModulePathHelper::PHYCICAL_FILE_PATH.data()); path += abcModuleName; // RequestPath starts with moduleName/src/main/xxx if (StringHelper::StringStartWith(requestPath, srcPrefix)) { - return LoadFilePathWithinModule(thread, abcFilePath, srcPrefix, requestPath, path, isHybrid); + return LoadFilePathWithinModule(thread, abcFilePath, srcPrefix, requestPath, path); } - return LoadModuleNameSpaceWithModuleInfo(vm, requestPath, path, abcFilePath, isHybrid); + return LoadModuleNameSpaceWithModuleInfo(vm, requestPath, path, abcFilePath); } -JSHandle NapiModuleLoader::LoadModuleNameSpace(EcmaVM *vm, CString requestPath, CString modulePath, - bool isHybrid) +template JSHandle NapiModuleLoader::LoadModuleNameSpace(EcmaVM *vm, + CString requestPath, const CString& moduleName, CString& abcFilePath); +template JSHandle NapiModuleLoader::LoadModuleNameSpace(EcmaVM *vm, + CString requestPath, const CString& moduleName, CString& abcFilePath); + +template +JSHandle NapiModuleLoader::LoadModuleNameSpace(EcmaVM *vm, CString requestPath, CString modulePath) { JSThread *thread = vm->GetJSThread(); CString moduleName = ModulePathHelper::GetModuleNameWithPath(modulePath); @@ -81,26 +90,35 @@ JSHandle NapiModuleLoader::LoadModuleNameSpace(EcmaVM *vm, CStrin CString srcPrefix = base::ConcatToCString(moduleName, ModulePathHelper::PHYCICAL_FILE_PATH.data()); // RequestPath starts with moduleName/src/main/xxx if (StringHelper::StringStartWith(requestPath, srcPrefix)) { - return LoadFilePathWithinModule(thread, abcFilePath, srcPrefix, requestPath, modulePath, isHybrid); + return LoadFilePathWithinModule(thread, abcFilePath, srcPrefix, requestPath, modulePath); } - return LoadModuleNameSpaceWithModuleInfo(vm, requestPath, modulePath, abcFilePath, isHybrid); + return LoadModuleNameSpaceWithModuleInfo(vm, requestPath, modulePath, abcFilePath); } +template JSHandle NapiModuleLoader::LoadModuleNameSpace(EcmaVM *vm, + CString requestPath, CString modulePath); +template JSHandle NapiModuleLoader::LoadModuleNameSpace(EcmaVM *vm, + CString requestPath, CString modulePath); +template JSHandle NapiModuleLoader::LoadFilePathWithinModule(JSThread *thread, const CString& abcFilePath, - const CString& srcPrefix, const CString& requestPath, const CString& modulePath, bool isHybrid) + const CString& srcPrefix, const CString& requestPath, const CString& modulePath) { if (requestPath.size() > srcPrefix.size()) { // Sub after moduleName/src/main/ CString fileName = requestPath.substr(srcPrefix.size() + 1); CString recordNameStr = ModulePathHelper::TranslateNapiFileRequestPath(thread, modulePath, fileName); LOG_ECMA(DEBUG) << "NapiModuleLoader::LoadFilePathWithinModule: Concated recordName " << recordNameStr; - return LoadModuleNameSpaceFromFile(thread, recordNameStr, abcFilePath, isHybrid); + return LoadModuleNameSpaceFromFile(thread, recordNameStr, abcFilePath); } else { CString msg = "cannot find record '" + requestPath + "' in basefileName " + abcFilePath + "," + "from napi load module"; THROW_NEW_ERROR_AND_RETURN_HANDLE(thread, ErrorType::REFERENCE_ERROR, JSTaggedValue, msg.c_str()); } } +template JSHandle NapiModuleLoader::LoadFilePathWithinModule(JSThread *thread, + const CString& abcFilePath, const CString& srcPrefix, const CString& requestPath, const CString& modulePath); +template JSHandle NapiModuleLoader::LoadFilePathWithinModule(JSThread *thread, + const CString& abcFilePath, const CString& srcPrefix, const CString& requestPath, const CString& modulePath); JSHandle NapiModuleLoader::LoadModuleNameSpaceWithPath(JSThread *thread, CString &abcFilePath, const CString &requestPath, const CString &modulePath, const JSPandaFile *pandaFile) @@ -116,11 +134,12 @@ JSHandle NapiModuleLoader::LoadModuleNameSpaceWithPath(JSThread * return LoadModuleNameSpaceFromFile(thread, entryPoint, abcFilePath); } +template JSHandle NapiModuleLoader::LoadModuleNameSpaceFromFile( - JSThread *thread, const CString &entryPoint, const CString &abcFilePath, bool isHybrid) + JSThread *thread, const CString &entryPoint, const CString &abcFilePath) { - std::shared_ptr jsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile( - thread, abcFilePath, entryPoint, false, isHybrid, ExecuteTypes::NAPI); + std::shared_ptr jsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile( + thread, abcFilePath, entryPoint, false, ExecuteTypes::NAPI); RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread); if (jsPandaFile == nullptr) { LOG_FULL(FATAL) << "Load current file's panda file failed. Current file is " << abcFilePath; @@ -142,6 +161,10 @@ JSHandle NapiModuleLoader::LoadModuleNameSpaceFromFile( return ecmascript::NapiModuleLoader::GetModuleNameSpace( thread, entryPoint, abcFilePath); } +template JSHandle NapiModuleLoader::LoadModuleNameSpaceFromFile( + JSThread *thread, const CString &entryPoint, const CString &abcFilePath); +template JSHandle NapiModuleLoader::LoadModuleNameSpaceFromFile( + JSThread *thread, const CString &entryPoint, const CString &abcFilePath); JSHandle NapiModuleLoader::GetModuleNameSpace(JSThread *thread, const CString &entryPoint, const CString &abcFilePath) diff --git a/ecmascript/module/napi_module_loader.h b/ecmascript/module/napi_module_loader.h index 05f523e4eb3e538474ed1a8a9a1ccdb467810409..fe702b5f2ac63d4e612468cb4e2d6e148e1d0fe6 100644 --- a/ecmascript/module/napi_module_loader.h +++ b/ecmascript/module/napi_module_loader.h @@ -20,28 +20,33 @@ namespace panda::ecmascript { class NapiModuleLoader { public: + template static JSHandle LoadModuleNameSpace(EcmaVM *vm, CString requestPath, const CString &moduleName, - CString &abcFilePath, bool isHybrid = false); + CString &abcFilePath); + template static JSHandle LoadModuleNameSpace(EcmaVM *vm, CString requestPath, - CString modulePath, bool isHybrid = false); + CString modulePath); static JSHandle GetModuleNameSpace(JSThread *thread, const CString &entryPoint, const CString &abcFilePath); + + template static JSHandle LoadModuleNameSpaceFromFile(JSThread *thread, const CString &entryPoint, - const CString &abcFilePath, bool isHybrid = false); + const CString &abcFilePath); private: + template static JSHandle LoadModuleNameSpaceWithModuleInfo( EcmaVM *vm, CString &requestPath, CString &modulePath, - CString &abcFilePath, bool isHybrid = false); + CString &abcFilePath); static JSHandle LoadModuleNameSpaceWithPath( JSThread *thread, CString &abcFilePath, const CString &requestPath, const CString &modulePath, const JSPandaFile *pandaFile); - + + template static JSHandle LoadFilePathWithinModule( JSThread *thread, const CString &abcFilePath, const CString &srcPrefix, - const CString &requestPath, const CString &modulePath, - bool isHybrid = false); + const CString &requestPath, const CString &modulePath); }; } diff --git a/ecmascript/napi/include/jsnapi_expo.h b/ecmascript/napi/include/jsnapi_expo.h index f5d78d309553b7f95172f3123791bc26194af8c8..108959737f15691dd650557b56bd1973a5cdfd28 100644 --- a/ecmascript/napi/include/jsnapi_expo.h +++ b/ecmascript/napi/include/jsnapi_expo.h @@ -90,6 +90,11 @@ enum class QueueType : uint8_t { } } // namespace ecmascript +enum class ForHybridApp { + Normal, + Hybrid +}; + struct HmsMap { std::string originalPath; std::string targetPath; @@ -1709,8 +1714,13 @@ public: static Local GetExportObjectFromOhmUrl(EcmaVM *vm, const std::string &ohmUrl, const std::string &key); static Local ExecuteNativeModule(EcmaVM *vm, const std::string &key); static Local GetModuleNameSpaceFromFile(EcmaVM *vm, const std::string &file); + template static Local GetModuleNameSpaceWithModuleInfo(EcmaVM *vm, const std::string &file, - const std::string &module_path, bool isHybrid = false); + const std::string &module_path); + static Local GetModuleNameSpaceWithModuleInfoForNormalApp(EcmaVM *vm, const std::string &file, + const std::string &module_path); + static Local GetModuleNameSpaceWithModuleInfoForHybridApp(EcmaVM *vm, const std::string &file, + const std::string &module_path); static Local GetModuleNameSpaceWithPath(const EcmaVM *vm, const char *path); static std::pair ResolveOhmUrl(std::string ohmUrl); @@ -1800,11 +1810,13 @@ public: static void SetCancelTimerCallback(EcmaVM *vm, CancelTimerCallback callback); static void NotifyEnvInitialized(EcmaVM *vm); static void SetReleaseSecureMemCallback(EcmaVM *vm, ReleaseSecureMemCallback releaseSecureMemFunc); - static void SetHostResolveBufferTracker(EcmaVM *vm, std::function cb); static void PandaFileSerialize(const EcmaVM *vm); static void ModuleSerialize(const EcmaVM *vm); static void ModuleDeserialize(EcmaVM *vm, const uint32_t appVersion); + static void SetHostResolveBufferTrackerForHybridApp(EcmaVM *vm, std::function cb); static void SetUnloadNativeModuleCallback(EcmaVM *vm, const std::function &cb); static void SetNativePtrGetter(EcmaVM *vm, void* cb); static void SetSourceMapCallback(EcmaVM *vm, SourceMapCallback cb); diff --git a/ecmascript/napi/jsnapi_expo.cpp b/ecmascript/napi/jsnapi_expo.cpp index 3a308fb385c7bdc550676e8499299edf7134b5f7..75ad5a5717fcca00cfb277931fb83cf0480bbfc1 100644 --- a/ecmascript/napi/jsnapi_expo.cpp +++ b/ecmascript/napi/jsnapi_expo.cpp @@ -4691,6 +4691,7 @@ void JSNApi::SynchronizVMInfo(EcmaVM *vm, const EcmaVM *hostVM) hostVM->GetAssociatedJSThread()->GetModuleManager(); vmModuleManager->SetExecuteMode(hostVMModuleManager->GetExecuteMode()); vm->SetResolveBufferCallback(hostVM->GetResolveBufferCallback()); + vm->SetResolveBufferCallbackForHybridApp(hostVM->GetResolveBufferCallbackForHybridApp()); } bool JSNApi::IsProfiling(EcmaVM *vm) @@ -6189,11 +6190,18 @@ void JSNApi::ModuleDeserialize(EcmaVM *vm, const uint32_t appVersion) } void JSNApi::SetHostResolveBufferTracker(EcmaVM *vm, - std::function cb) + std::function cb) { vm->SetResolveBufferCallback(cb); } +void JSNApi::SetHostResolveBufferTrackerForHybridApp(EcmaVM *vm, + std::function cb) +{ + vm->SetResolveBufferCallbackForHybridApp(cb); +} + + void JSNApi::SetSearchHapPathTracker(EcmaVM *vm, std::function cb) { @@ -6411,7 +6419,7 @@ Local JSNApi::GetExportObject(EcmaVM *vm, const std::string &file, co ModulePathHelper::ParseAbcPathAndOhmUrl(vm, entry, name, entry); std::shared_ptr jsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile( - thread, name, entry.c_str(), false, false, ecmascript::ExecuteTypes::STATIC); + thread, name, entry.c_str(), false, ecmascript::ExecuteTypes::STATIC); if (jsPandaFile == nullptr) { JSHandle exportObj(thread, JSTaggedValue::Null()); return JSNApiHelper::ToLocal(exportObj); @@ -6500,17 +6508,34 @@ Local JSNApi::GetModuleNameSpaceFromFile(EcmaVM *vm, const std::strin return JSNApiHelper::ToLocal(moduleNamespace); } + +template Local JSNApi::GetModuleNameSpaceWithModuleInfo(EcmaVM *vm, const std::string &file, - const std::string &module_path, bool isHybrid) + const std::string &module_path) { CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, JSValueRef::Undefined(vm)); ecmascript::ThreadManagedScope managedScope(thread); ecmascript::CString requestPath = file.c_str(); ecmascript::CString modulePath = module_path.c_str(); JSHandle nameSp = - ecmascript::NapiModuleLoader::LoadModuleNameSpace(vm, requestPath, modulePath, isHybrid); + ecmascript::NapiModuleLoader::LoadModuleNameSpace(vm, requestPath, modulePath); return JSNApiHelper::ToLocal(nameSp); } +template Local JSNApi::GetModuleNameSpaceWithModuleInfo(EcmaVM *vm, + const std::string &file, const std::string &module_path); +template Local JSNApi::GetModuleNameSpaceWithModuleInfo(EcmaVM *vm, + const std::string &file, const std::string &module_path); + +Local JSNApi::GetModuleNameSpaceWithModuleInfoForNormalApp(EcmaVM *vm, const std::string &file, + const std::string &module_path) +{ + return JSNApi::GetModuleNameSpaceWithModuleInfo(vm, file, module_path); +} +Local JSNApi::GetModuleNameSpaceWithModuleInfoForHybridApp(EcmaVM *vm, const std::string &file, + const std::string &module_path) +{ + return JSNApi::GetModuleNameSpaceWithModuleInfo(vm, file, module_path); +} Local JSNApi::GetModuleNameSpaceWithPath(const EcmaVM *vm, const char *path) { diff --git a/ecmascript/napi/test/ffi_workload.cpp b/ecmascript/napi/test/ffi_workload.cpp index cb40ea24c250f945ad5e666e6447f065e7002c33..e4d5ad2112098e0e617aa7616334f6faa9465220 100644 --- a/ecmascript/napi/test/ffi_workload.cpp +++ b/ecmascript/napi/test/ffi_workload.cpp @@ -3536,9 +3536,8 @@ HWTEST_F_L0(JSNApiSplTest, JSNApi_SetHostResolveBufferTracker) { LocalScope scope(vm_); CalculateForTime(); - std::function - cb = [](const std::string &inputPath, bool isHybrid, - uint8_t **buff, size_t *buffSize, std::string &errorMsg) -> bool { + std::function cb = + [](const std::string &inputPath, uint8_t **buff, size_t *buffSize, std::string &errorMsg) -> bool { if (inputPath.empty() || buff == nullptr || buffSize == nullptr) { return false; } diff --git a/ecmascript/napi/test/jsnapi_third_tests.cpp b/ecmascript/napi/test/jsnapi_third_tests.cpp index 23fc335d448b91546db1171939981116d23063bd..160c3cc6ae77a63e68ccc5151a163f2ef0e51c41 100644 --- a/ecmascript/napi/test/jsnapi_third_tests.cpp +++ b/ecmascript/napi/test/jsnapi_third_tests.cpp @@ -697,7 +697,7 @@ HWTEST_F_L0(JSNApiTests, SetHostResolveBufferTracker) { LocalScope scope(vm_); JSNApi::SetHostResolveBufferTracker(vm_, - [&](std::string, bool, uint8_t **, size_t *, std::string &) -> bool { return true; }); + [&](std::string, uint8_t **, size_t *, std::string &) -> bool { return true; }); } /* diff --git a/ecmascript/patch/quick_fix_manager.cpp b/ecmascript/patch/quick_fix_manager.cpp index aac16b7587abc840bd953f43ef40a8bb8766e7c4..0c03de0f2fb1eec1827a5db01cb74865e0d56f55 100644 --- a/ecmascript/patch/quick_fix_manager.cpp +++ b/ecmascript/patch/quick_fix_manager.cpp @@ -87,7 +87,7 @@ PatchErrorCode QuickFixManager::LoadPatch(JSThread *thread, const std::string &p } std::shared_ptr baseFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile( - thread, baseFileName.c_str(), "", false, false, ExecuteTypes::STATIC); + thread, baseFileName.c_str(), "", false, ExecuteTypes::STATIC); RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, PatchErrorCode::FILE_NOT_FOUND); if (baseFile == nullptr) { LOG_ECMA(ERROR) << "find base jsPandafile failed"; @@ -96,7 +96,7 @@ PatchErrorCode QuickFixManager::LoadPatch(JSThread *thread, const std::string &p // The entry point is not work for merge abc. std::shared_ptr patchFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile( - thread, patchFileName.c_str(), "", false, false, ExecuteTypes::STATIC); + thread, patchFileName.c_str(), "", false, ExecuteTypes::STATIC); RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, PatchErrorCode::FILE_NOT_FOUND); if (patchFile == nullptr) { LOG_ECMA(ERROR) << "load patch jsPandafile failed"; diff --git a/ecmascript/require/js_cjs_module.cpp b/ecmascript/require/js_cjs_module.cpp index cbd78169ae0a7afd64da4b3ba71c6e07d7184e12..f535398f65c6ab6187a38338e7a46b164ac18ac5 100644 --- a/ecmascript/require/js_cjs_module.cpp +++ b/ecmascript/require/js_cjs_module.cpp @@ -160,7 +160,7 @@ void CjsModule::RequireExecution(JSThread *thread, const CString &mergedFilename { std::shared_ptr jsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile( - thread, mergedFilename, requestEntryPoint, false, false, ExecuteTypes::STATIC); + thread, mergedFilename, requestEntryPoint, false, ExecuteTypes::STATIC); RETURN_IF_ABRUPT_COMPLETION(thread); if (jsPandaFile == nullptr) { LOG_FULL(FATAL) << "Load current file's panda file failed. Current file is " << mergedFilename; diff --git a/test/fuzztest/jsnapisethostpromiserejectiontracker_fuzzer/jsnapisethostpromiserejectiontracker_fuzzer.cpp b/test/fuzztest/jsnapisethostpromiserejectiontracker_fuzzer/jsnapisethostpromiserejectiontracker_fuzzer.cpp index 79f69541de0d03c28d5a6729c963746ae7ab4584..206f697429903645ff3a93dcd8a26ebeea5a11c7 100644 --- a/test/fuzztest/jsnapisethostpromiserejectiontracker_fuzzer/jsnapisethostpromiserejectiontracker_fuzzer.cpp +++ b/test/fuzztest/jsnapisethostpromiserejectiontracker_fuzzer/jsnapisethostpromiserejectiontracker_fuzzer.cpp @@ -56,7 +56,7 @@ namespace OHOS { uint8_t* ptr = nullptr; ptr = const_cast(data); JSNApi::SetHostResolveBufferTracker(vm, - [&](std::string, bool, uint8_t **, size_t *, std::string &) -> bool { return true; }); + [&](std::string, uint8_t **, size_t *, std::string &) -> bool { return true; }); JSNApi::DestroyJSVM(vm); }