From 2900a1cf42449c4e062eba27a08ac6815aaeb01a Mon Sep 17 00:00:00 2001 From: rentangyu Date: Thu, 6 Aug 2026 16:03:00 +0800 Subject: [PATCH] jsruntime Use Case Supplement Issue:https://gitcode.com/openharmony/ability_ability_runtime/issues/15874?ref=&did=4225421#tid-4225421 Signed-off-by: rentangyu AI[0%] Human Fixed[0%] Human[100%] AI Adopted[0%] --- frameworks/native/runtime/js_runtime.cpp | 10 +- .../inner_api/runtime/include/js_runtime.h | 5 +- .../unittest/runtime_test/js_runtime_test.cpp | 99 +++++++++++++++++++ 3 files changed, 106 insertions(+), 8 deletions(-) diff --git a/frameworks/native/runtime/js_runtime.cpp b/frameworks/native/runtime/js_runtime.cpp index 764c6612ef..05e633441f 100755 --- a/frameworks/native/runtime/js_runtime.cpp +++ b/frameworks/native/runtime/js_runtime.cpp @@ -483,7 +483,7 @@ bool JsRuntime::LoadRepairPatch(const std::string& hqfFile, const std::string& h auto vm = GetEcmaVm(); CHECK_POINTER_AND_RETURN(vm, false); - InitSourceMap(hqfFile); + HqfInitSourceMap(hqfFile); std::string patchFile; auto hqfSafeData = GetSafeData(hqfFile, patchFile); @@ -788,7 +788,7 @@ bool JsRuntime::Initialize(const Options& options) } if (!options.preload) { - SourceMapInit(options.bundleName); + InitSourceMap(options.bundleName); if (options.isUnique) { TAG_LOGD(AAFwkTag::JSRUNTIME, "Not supported TimerModule when form render"); @@ -1004,7 +1004,7 @@ void JsRuntime::CreatePluginDefaultNamespace(const std::string &lddictionaries) moduleManager->InheritNamespaceEachOther(currentNamespace, pluginDefaultNamespace); } -void JsRuntime::SourceMapInit(const std::string bundleName) +void JsRuntime::InitSourceMap(const std::string bundleName) { HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); CHECK_POINTER(jsEnv_); @@ -1026,7 +1026,7 @@ void JsRuntime::SourceMapInit(const std::string bundleName) ffrt::submit(init, {}, {}, ffrt::task_attr().qos(ffrt::qos_user_initiated)); } -void JsRuntime::InitSourceMap(const std::string hqfFilePath) +void JsRuntime::HqfInitSourceMap(const std::string hqfFilePath) { std::string patchSoureMapFile; std::vector soureMapBuffer; @@ -1686,7 +1686,7 @@ void JsRuntime::RegisterQuickFixQueryFunc(const std::mapsecond)); - InitSourceMap(hqfFile); + HqfInitSourceMap(hqfFile); } panda::JSNApi::RegisterQuickFixQueryFunc(vm, JsQuickfixCallback(moduleAndPath)); } diff --git a/interfaces/inner_api/runtime/include/js_runtime.h b/interfaces/inner_api/runtime/include/js_runtime.h index d2f6975ffd..bba8ab49e0 100755 --- a/interfaces/inner_api/runtime/include/js_runtime.h +++ b/interfaces/inner_api/runtime/include/js_runtime.h @@ -45,7 +45,6 @@ class FileMapper; namespace JsEnv { class JsEnvironment; -class SourceMapOperator; struct ErrorObject; struct UncaughtExceptionInfo; using UncatchableTask = std::function GetSafeData(const std::string& path, std::string& fileFullName); - void SourceMapInit(const std::string bundleName); - void InitSourceMap(const std::string hqfFilePath); + void InitSourceMap(const std::string bundleName); + void HqfInitSourceMap(const std::string hqfFilePath); void FreeNativeReference(std::unique_ptr reference); void FreeNativeReference(std::shared_ptr&& reference); void StartProfiler(const DebugOption debugOption) override; diff --git a/test/unittest/runtime_test/js_runtime_test.cpp b/test/unittest/runtime_test/js_runtime_test.cpp index a913a6261d..41f2d98f31 100755 --- a/test/unittest/runtime_test/js_runtime_test.cpp +++ b/test/unittest/runtime_test/js_runtime_test.cpp @@ -2387,5 +2387,104 @@ HWTEST_F(JsRuntimeTest, LoadSystemModule_0200, TestSize.Level1) TAG_LOGI(AAFwkTag::TEST, "LoadSystemModule_0200 end"); } + +/** + * @tc.name: InitSourceMap_0100 + * @tc.desc: JsRuntime test for InitSourceMap with jsEnv_ nullptr. + * @tc.type: FUNC + */ +HWTEST_F(JsRuntimeTest, InitSourceMap_0100, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "InitSourceMap_0100 start"); + + auto jsRuntime = std::make_unique(); + ASSERT_NE(jsRuntime, nullptr); + jsRuntime->jsEnv_ = nullptr; + std::string bundleName = TEST_BUNDLE_NAME; + jsRuntime->InitSourceMap(bundleName); + // jsEnv_ is nullptr, CHECK_POINTER returns early, no crash + jsRuntime.reset(); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + TAG_LOGI(AAFwkTag::TEST, "InitSourceMap_0100 end"); +} + +/** + * @tc.name: InitSourceMap_0200 + * @tc.desc: JsRuntime test for InitSourceMap with empty bundleName. + * @tc.type: FUNC + */ +HWTEST_F(JsRuntimeTest, InitSourceMap_0200, TestSize.Level2) +{ + TAG_LOGI(AAFwkTag::TEST, "InitSourceMap_0200 start"); + + auto jsRuntime = std::make_unique(); + ASSERT_NE(jsRuntime, nullptr); + jsRuntime->jsEnv_ = std::make_shared(); + std::string bundleName = ""; + jsRuntime->InitSourceMap(bundleName); + // empty bundleName, GetHapPathList returns empty, no crash + jsRuntime.reset(); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + TAG_LOGI(AAFwkTag::TEST, "InitSourceMap_0200 end"); +} + +/** + * @tc.name: InitSourceMap_0300 + * @tc.desc: JsRuntime test for InitSourceMap with valid bundleName. + * @tc.type: FUNC + */ +HWTEST_F(JsRuntimeTest, InitSourceMap_0300, TestSize.Level2) +{ + TAG_LOGI(AAFwkTag::TEST, "InitSourceMap_0300 start"); + + auto jsRuntime = std::make_unique(); + ASSERT_NE(jsRuntime, nullptr); + jsRuntime->jsEnv_ = std::make_shared(); + std::string bundleName = TEST_BUNDLE_NAME; + jsRuntime->InitSourceMap(bundleName); + // valid bundleName but no real hap files in test env, ffrt task runs safely + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + jsRuntime.reset(); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + TAG_LOGI(AAFwkTag::TEST, "InitSourceMap_0300 end"); +} + +/** + * @tc.name: HqfInitSourceMap_0100 + * @tc.desc: JsRuntime test for HqfInitSourceMap with invalid hqfFilePath. + * @tc.type: FUNC + */ +HWTEST_F(JsRuntimeTest, HqfInitSourceMap_0100, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "HqfInitSourceMap_0100 start"); + + auto jsRuntime = std::make_unique(); + ASSERT_NE(jsRuntime, nullptr); + std::string hqfFilePath = "/invalid/path/test.hqf"; + jsRuntime->HqfInitSourceMap(hqfFilePath); + // GetFileBuffer fails with invalid path, returns early, no crash + jsRuntime.reset(); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + TAG_LOGI(AAFwkTag::TEST, "HqfInitSourceMap_0100 end"); +} + +/** + * @tc.name: HqfInitSourceMap_0200 + * @tc.desc: JsRuntime test for HqfInitSourceMap with empty hqfFilePath. + * @tc.type: FUNC + */ +HWTEST_F(JsRuntimeTest, HqfInitSourceMap_0200, TestSize.Level2) +{ + TAG_LOGI(AAFwkTag::TEST, "HqfInitSourceMap_0200 start"); + + auto jsRuntime = std::make_unique(); + ASSERT_NE(jsRuntime, nullptr); + std::string hqfFilePath = ""; + jsRuntime->HqfInitSourceMap(hqfFilePath); + // empty path, GetFileBuffer fails, returns early, no crash + jsRuntime.reset(); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + TAG_LOGI(AAFwkTag::TEST, "HqfInitSourceMap_0200 end"); +} } // namespace AbilityRuntime } // namespace OHOS \ No newline at end of file