From 539a9e00351f6bd942e3b67242e91186c5c55e7b Mon Sep 17 00:00:00 2001 From: "zhangyafei.echo" Date: Wed, 26 Apr 2023 19:23:13 +0800 Subject: [PATCH] Description:JsEnvironment support timer. Sig:SIG_ApplicationFramework Feature or BugFix: Feature Binary Source: No Signed-off-by: zhangyafei.echo Change-Id: I1b74939ac8d8c47cc256edf016f611ab2754ed1a --- frameworks/native/runtime/js_runtime.cpp | 8 ++++++- frameworks/native/runtime/js_timer.cpp | 2 +- frameworks/native/runtime/js_timer.h | 2 +- .../runtime/ohos_js_environment_impl.cpp | 14 ++++++++++-- .../native/runtime/ohos_js_environment_impl.h | 2 +- .../inner_api/runtime/include/js_runtime.h | 1 + .../js_environment/src/js_environment.cpp | 7 +++++- .../inner_api/js_environment_impl.h | 4 +++- .../js_environment_test.cpp | 22 +++++++++++++++++++ .../runtime_test/ohos_js_environment_test.cpp | 15 ++++++++++--- 10 files changed, 66 insertions(+), 11 deletions(-) diff --git a/frameworks/native/runtime/js_runtime.cpp b/frameworks/native/runtime/js_runtime.cpp index 18aa5b702a..5ebef9299b 100644 --- a/frameworks/native/runtime/js_runtime.cpp +++ b/frameworks/native/runtime/js_runtime.cpp @@ -515,7 +515,7 @@ bool JsRuntime::Initialize(const Options& options) if (options.isUnique) { HILOG_INFO("Not supported TimerModule when form render"); } else { - InitTimerModule(*nativeEngine, *globalObj); + InitTimerModule(); } InitWorkerModule(*nativeEngine, codePath_, options.isDebugVersion, options.isBundle); @@ -1075,5 +1075,11 @@ void JsRuntime::FreeNativeReference(std::unique_ptr uniqueNativ work = nullptr; } } + +void JsRuntime::InitTimerModule() +{ + CHECK_POINTER(jsEnv_); + jsEnv_->InitTimerModule(); +} } // namespace AbilityRuntime } // namespace OHOS diff --git a/frameworks/native/runtime/js_timer.cpp b/frameworks/native/runtime/js_timer.cpp index bba2777336..d187d18fe3 100644 --- a/frameworks/native/runtime/js_timer.cpp +++ b/frameworks/native/runtime/js_timer.cpp @@ -182,7 +182,7 @@ NativeValue* StopTimeoutOrInterval(NativeEngine* engine, NativeCallbackInfo* inf } } -void InitTimerModule(NativeEngine& engine, NativeObject& globalObject) +void InitTimer(NativeEngine& engine, NativeObject& globalObject) { HILOG_DEBUG("InitTimerModule begin."); const char *moduleName = "JsTimer"; diff --git a/frameworks/native/runtime/js_timer.h b/frameworks/native/runtime/js_timer.h index cab14cbc3b..a91069656b 100644 --- a/frameworks/native/runtime/js_timer.h +++ b/frameworks/native/runtime/js_timer.h @@ -20,7 +20,7 @@ namespace OHOS { namespace AbilityRuntime { -void InitTimerModule(NativeEngine& engine, NativeObject& globalObject); +void InitTimer(NativeEngine& engine, NativeObject& globalObject); } // namespace AbilityRuntime } // namespace OHOS diff --git a/frameworks/native/runtime/ohos_js_environment_impl.cpp b/frameworks/native/runtime/ohos_js_environment_impl.cpp index d762569f87..d04009fddd 100644 --- a/frameworks/native/runtime/ohos_js_environment_impl.cpp +++ b/frameworks/native/runtime/ohos_js_environment_impl.cpp @@ -16,6 +16,9 @@ #include "ohos_js_environment_impl.h" #include "hilog_wrapper.h" +#include "js_runtime_utils.h" +#include "js_timer.h" +#include "js_utils.h" namespace OHOS { namespace AbilityRuntime { @@ -39,9 +42,16 @@ void OHOSJsEnvironmentImpl::RemoveTask(const std::string& name) HILOG_DEBUG("called"); } -void OHOSJsEnvironmentImpl::InitTimerModule() +void OHOSJsEnvironmentImpl::InitTimerModule(NativeEngine* engine) { - HILOG_DEBUG("called"); + HILOG_DEBUG("Init timer."); + CHECK_POINTER(engine); + + HandleScope handleScope(*engine); + NativeObject* globalObj = ConvertNativeValueTo(engine->GetGlobal()); + CHECK_POINTER(globalObj); + + InitTimer(*engine, *globalObj); } void OHOSJsEnvironmentImpl::InitConsoleLogModule() diff --git a/frameworks/native/runtime/ohos_js_environment_impl.h b/frameworks/native/runtime/ohos_js_environment_impl.h index a404e85d4a..9d107a2a4f 100644 --- a/frameworks/native/runtime/ohos_js_environment_impl.h +++ b/frameworks/native/runtime/ohos_js_environment_impl.h @@ -29,7 +29,7 @@ public: void RemoveTask(const std::string& name) override; - void InitTimerModule() override; + void InitTimerModule(NativeEngine* engine) override; void InitConsoleLogModule() override; diff --git a/interfaces/inner_api/runtime/include/js_runtime.h b/interfaces/inner_api/runtime/include/js_runtime.h index 3ba0421bdc..5430cf2d3b 100644 --- a/interfaces/inner_api/runtime/include/js_runtime.h +++ b/interfaces/inner_api/runtime/include/js_runtime.h @@ -141,6 +141,7 @@ private: inline bool IsUseAbilityRuntime(const Options& options) const; void FreeNativeReference(std::unique_ptr uniqueNativeRef, std::shared_ptr&& sharedNativeRef); + void InitTimerModule(); }; } // namespace AbilityRuntime } // namespace OHOS diff --git a/js_environment/frameworks/js_environment/src/js_environment.cpp b/js_environment/frameworks/js_environment/src/js_environment.cpp index 09053acb56..5e1d86ead6 100644 --- a/js_environment/frameworks/js_environment/src/js_environment.cpp +++ b/js_environment/frameworks/js_environment/src/js_environment.cpp @@ -67,8 +67,13 @@ void JsEnvironment::StopDebugger() void JsEnvironment::InitTimerModule() { + if (engine_ == nullptr) { + JSENV_LOG_E("Invalid native engine."); + return; + } + if (impl_ != nullptr) { - impl_->InitTimerModule(); + impl_->InitTimerModule(engine_); } } diff --git a/js_environment/interfaces/inner_api/js_environment_impl.h b/js_environment/interfaces/inner_api/js_environment_impl.h index b620ff5aca..ba57977ebf 100644 --- a/js_environment/interfaces/inner_api/js_environment_impl.h +++ b/js_environment/interfaces/inner_api/js_environment_impl.h @@ -18,6 +18,8 @@ #include +#include "native_engine/native_engine.h" + namespace OHOS { namespace JsEnv { class JsEnvironmentImpl { @@ -29,7 +31,7 @@ public: virtual void RemoveTask(const std::string& name) = 0; - virtual void InitTimerModule() = 0; + virtual void InitTimerModule(NativeEngine* engine) = 0; virtual void InitConsoleLogModule() = 0; diff --git a/js_environment/test/unittest/js_environment_test/js_environment_test.cpp b/js_environment/test/unittest/js_environment_test/js_environment_test.cpp index 888428611b..15b790fa5b 100644 --- a/js_environment/test/unittest/js_environment_test/js_environment_test.cpp +++ b/js_environment/test/unittest/js_environment_test/js_environment_test.cpp @@ -150,5 +150,27 @@ HWTEST_F(JsEnvironmentTest, LoadScript_0300, TestSize.Level0) EXPECT_EQ(jsEnv->LoadScript("/system/etc/strip.native.min.abc"), true); } + +/** + * @tc.name: JsEnvInitTimerModule_0100 + * @tc.desc: Initialize timer module. + * @tc.type: FUNC + * @tc.require: issueI6Z5M5 + */ +HWTEST_F(JsEnvironmentTest, JsEnvInitTimerModule_0100, TestSize.Level0) +{ + auto jsEnv = std::make_shared(std::make_unique()); + ASSERT_NE(jsEnv, nullptr); + + // Init timer module when native engine is invalid. + jsEnv->InitTimerModule(); + + panda::RuntimeOption pandaOption; + auto ret = jsEnv->Initialize(pandaOption, static_cast(this)); + ASSERT_EQ(ret, true); + + // Init timer module when native engine has created. + jsEnv->InitTimerModule(); +} } // namespace JsEnv } // namespace OHOS diff --git a/test/unittest/runtime_test/ohos_js_environment_test.cpp b/test/unittest/runtime_test/ohos_js_environment_test.cpp index 2deeb93952..3b07b7af8b 100644 --- a/test/unittest/runtime_test/ohos_js_environment_test.cpp +++ b/test/unittest/runtime_test/ohos_js_environment_test.cpp @@ -20,6 +20,7 @@ #include #include "hilog_wrapper.h" +#include "js_runtime.h" using namespace testing; using namespace testing::ext; @@ -70,14 +71,22 @@ HWTEST_F(OHOSJsEnvironmentTest, PostTask_0100, TestSize.Level0) * @tc.name: InitTimerModule_0100 * @tc.desc: Js environment init timer. * @tc.type: FUNC - * @tc.require: issueI6KODF + * @tc.require: issueI6Z5M5 */ HWTEST_F(OHOSJsEnvironmentTest, InitTimerModule_0100, TestSize.Level0) { - auto jsEnvImpl = std::make_shared(); + auto jsEnvImpl = std::make_unique(); ASSERT_NE(jsEnvImpl, nullptr); - jsEnvImpl->InitTimerModule(); + // Init timer module when native engine is invalid. + jsEnvImpl->InitTimerModule(nullptr); + + AbilityRuntime::Runtime::Options options; + auto jsRuntime = AbilityRuntime::JsRuntime::Create(options); + ASSERT_NE(jsRuntime, nullptr); + + // Init timer module when native engine has created. + jsEnvImpl->InitTimerModule(jsRuntime->GetNativeEnginePointer()); } /**