Description:Support pa engine use loop initialized in JsEnv.

Sig:SIG_ApplicationFramework
Feature or BugFix: Feature
Binary Source: No

Signed-off-by: zhangyafei.echo <zhangyafei12@huawei.com>
Change-Id: Ic3f2481bf56d224309125ae99b2a558eeeb1f4d9
This commit is contained in:
zhangyafei.echo 2023-06-09 14:10:58 +08:00
parent 0fa67dc775
commit 8549e82342
10 changed files with 175 additions and 47 deletions

View File

@ -133,12 +133,12 @@ std::atomic<bool> JsRuntime::hasInstance(false);
JsRuntime::JsRuntime()
{
HILOG_DEBUG("JsRuntime costructor.");
HILOG_INFO("JsRuntime costructor.");
}
JsRuntime::~JsRuntime()
{
HILOG_DEBUG("JsRuntime destructor.");
HILOG_INFO("JsRuntime destructor.");
Deinitialize();
StopDebugMode();
}
@ -178,14 +178,11 @@ void JsRuntime::StartDebugMode(bool needBreakPoint)
}
HILOG_INFO("Ark VM is starting debug mode [%{public}s]", needBreakPoint ? "break" : "normal");
auto debuggerPostTask = [jsEnv = jsEnv_](std::function<void()>&& task) {
jsEnv->PostTask(task);
};
StartDebuggerInWorkerModule();
HdcRegister::Get().StartHdcRegister(bundleName_);
ConnectServerManager::Get().StartConnectServer(bundleName_);
ConnectServerManager::Get().AddInstance(instanceId_);
debugMode_ = StartDebugger(needBreakPoint, instanceId_, debuggerPostTask);
debugMode_ = StartDebugger(needBreakPoint, instanceId_);
}
void JsRuntime::StopDebugMode()
@ -202,19 +199,15 @@ void JsRuntime::InitConsoleModule()
jsEnv_->InitConsoleModule();
}
bool JsRuntime::StartDebugger(bool needBreakPoint, const DebuggerPostTask& debuggerPostTask)
{
return StartDebugger(needBreakPoint, gettid(), debuggerPostTask);
}
bool JsRuntime::StartDebugger(bool needBreakPoint, uint32_t instanceId, const DebuggerPostTask& debuggerPostTask)
bool JsRuntime::StartDebugger(bool needBreakPoint, uint32_t instanceId)
{
CHECK_POINTER_AND_RETURN(jsEnv_, false);
return jsEnv_->StartDebugger(ARK_DEBUGGER_LIB_PATH, needBreakPoint, instanceId, debuggerPostTask);
return jsEnv_->StartDebugger(ARK_DEBUGGER_LIB_PATH, needBreakPoint, instanceId);
}
void JsRuntime::StopDebugger()
{
CHECK_POINTER(jsEnv_);
jsEnv_->StopDebugger();
}
@ -518,6 +511,7 @@ bool JsRuntime::Initialize(const Options& options)
isBundle_ = options.isBundle;
bundleName_ = options.bundleName;
codePath_ = options.codePath;
ReInitJsEnvImpl(options);
if (!options.hapPath.empty()) {
bool newCreate = false;
@ -538,11 +532,6 @@ bool JsRuntime::Initialize(const Options& options)
panda::JSNApi::SetBundleName(vm, options.bundleName);
panda::JSNApi::SetHostResolveBufferTracker(vm, JsModuleReader(options.bundleName, options.hapPath));
isModular = !panda::JSNApi::IsBundle(vm);
if (!InitLoop(options.eventRunner)) {
HILOG_ERROR("Initialize loop failed.");
return false;
}
}
}
@ -561,6 +550,11 @@ bool JsRuntime::Initialize(const Options& options)
}
InitWorkerModule(options);
if (!InitLoop()) {
HILOG_ERROR("Initialize loop failed.");
return false;
}
}
preloaded_ = options.preload;
@ -598,7 +592,7 @@ bool JsRuntime::CreateJsEnv(const Options& options)
}
OHOSJsEnvLogger::RegisterJsEnvLogger();
jsEnv_ = std::make_shared<JsEnv::JsEnvironment>(std::make_unique<OHOSJsEnvironmentImpl>());
jsEnv_ = std::make_shared<JsEnv::JsEnvironment>(std::make_unique<OHOSJsEnvironmentImpl>(options.eventRunner));
if (jsEnv_ == nullptr || !jsEnv_->Initialize(pandaOption, static_cast<void*>(this))) {
HILOG_ERROR("Initialize js environment failed.");
return false;
@ -644,10 +638,10 @@ void JsRuntime::DoCleanWorkAfterStageCleaned()
PostTask(gcTask, "ability_destruct_gc", TRIGGER_GC_AFTER_CLEAR_STAGE_MS);
}
bool JsRuntime::InitLoop(const std::shared_ptr<AppExecFwk::EventRunner>& eventRunner)
bool JsRuntime::InitLoop()
{
CHECK_POINTER_AND_RETURN(jsEnv_, false);
return jsEnv_->InitLoop(eventRunner);
return jsEnv_->InitLoop();
}
void JsRuntime::SetAppLibPath(const AppLibPathMap& appLibPaths, const bool& isSystemApp)
@ -917,6 +911,12 @@ void JsRuntime::PostTask(const std::function<void()>& task, const std::string& n
jsEnv_->PostTask(task, name, delayTime);
}
void JsRuntime::PostSyncTask(const std::function<void()>& task, const std::string& name)
{
CHECK_POINTER(jsEnv_);
jsEnv_->PostSyncTask(task, name);
}
void JsRuntime::RemoveTask(const std::string& name)
{
CHECK_POINTER(jsEnv_);
@ -1151,5 +1151,11 @@ void JsRuntime::InitWorkerModule(const Options& options)
}
jsEnv_->InitWorkerModule(workerInfo);
}
void JsRuntime::ReInitJsEnvImpl(const Options& options)
{
CHECK_POINTER(jsEnv_);
jsEnv_->ReInitJsEnvImpl(std::make_unique<OHOSJsEnvironmentImpl>(options.eventRunner));
}
} // namespace AbilityRuntime
} // namespace OHOS

View File

@ -29,6 +29,15 @@ OHOSJsEnvironmentImpl::OHOSJsEnvironmentImpl()
HILOG_DEBUG("called");
}
OHOSJsEnvironmentImpl::OHOSJsEnvironmentImpl(const std::shared_ptr<AppExecFwk::EventRunner>& eventRunner)
{
HILOG_INFO("called");
if (eventRunner != nullptr) {
HILOG_DEBUG("Create event handler.");
eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(eventRunner);
}
}
OHOSJsEnvironmentImpl::~OHOSJsEnvironmentImpl()
{
HILOG_DEBUG("called");
@ -42,6 +51,14 @@ void OHOSJsEnvironmentImpl::PostTask(const std::function<void()>& task, const st
}
}
void OHOSJsEnvironmentImpl::PostSyncTask(const std::function<void()>& task, const std::string& name)
{
HILOG_DEBUG("Post sync task");
if (eventHandler_ != nullptr) {
eventHandler_->PostSyncTask(task, name);
}
}
void OHOSJsEnvironmentImpl::RemoveTask(const std::string& name)
{
HILOG_DEBUG("called");
@ -66,10 +83,9 @@ void OHOSJsEnvironmentImpl::InitConsoleModule(NativeEngine* engine)
JsSysModule::Console::InitConsoleModule(reinterpret_cast<napi_env>(engine));
}
bool OHOSJsEnvironmentImpl::InitLoop(NativeEngine* engine, const std::shared_ptr<AppExecFwk::EventRunner>& eventRunner)
bool OHOSJsEnvironmentImpl::InitLoop(NativeEngine* engine)
{
HILOG_DEBUG("called");
eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(eventRunner);
auto uvLoop = engine->GetUVLoop();
auto fd = uvLoop != nullptr ? uv_backend_fd(uvLoop) : -1;
if (fd < 0) {
@ -78,8 +94,11 @@ bool OHOSJsEnvironmentImpl::InitLoop(NativeEngine* engine, const std::shared_ptr
}
uv_run(uvLoop, UV_RUN_NOWAIT);
uint32_t events = AppExecFwk::FILE_DESCRIPTOR_INPUT_EVENT | AppExecFwk::FILE_DESCRIPTOR_OUTPUT_EVENT;
eventHandler_->AddFileDescriptorListener(fd, events, std::make_shared<OHOSLoopHandler>(uvLoop));
if (eventHandler_ != nullptr) {
uint32_t events = AppExecFwk::FILE_DESCRIPTOR_INPUT_EVENT | AppExecFwk::FILE_DESCRIPTOR_OUTPUT_EVENT;
eventHandler_->AddFileDescriptorListener(fd, events, std::make_shared<OHOSLoopHandler>(uvLoop));
}
return true;
}

View File

@ -23,17 +23,20 @@ namespace AbilityRuntime {
class OHOSJsEnvironmentImpl : public JsEnv::JsEnvironmentImpl {
public:
OHOSJsEnvironmentImpl();
explicit OHOSJsEnvironmentImpl(const std::shared_ptr<AppExecFwk::EventRunner>& eventRunner);
~OHOSJsEnvironmentImpl() override;
void PostTask(const std::function<void()>& task, const std::string& name, int64_t delayTime) override;
void PostSyncTask(const std::function<void()>& task, const std::string& name) override;
void RemoveTask(const std::string& name) override;
void InitTimerModule(NativeEngine* engine) override;
void InitConsoleModule(NativeEngine* engine) override;
bool InitLoop(NativeEngine* engine, const std::shared_ptr<AppExecFwk::EventRunner>& eventRunner) override;
bool InitLoop(NativeEngine* engine) override;
void DeInitLoop(NativeEngine* engine) override;

View File

@ -80,6 +80,7 @@ public:
std::unique_ptr<NativeReference> LoadSystemModule(
const std::string& moduleName, NativeValue* const* argv = nullptr, size_t argc = 0);
void PostTask(const std::function<void()>& task, const std::string& name, int64_t delayTime);
void PostSyncTask(const std::function<void()>& task, const std::string& name);
void RemoveTask(const std::string& name);
void DumpHeapSnapshot(bool isPrivate) override;
bool BuildJsStackInfoList(uint32_t tid, std::vector<JsFrames>& jsFrames) override;
@ -97,10 +98,7 @@ public:
bool NotifyHotReloadPage() override;
void RegisterUncaughtExceptionHandler(JsEnv::UncaughtExceptionInfo uncaughtExceptionInfo);
bool LoadScript(const std::string& path, std::vector<uint8_t>* buffer = nullptr, bool isBundle = false);
bool StartDebugMode(const std::string& bundleName, bool needBreakPoint, uint32_t instanceId,
const DebuggerPostTask& debuggerPostTask = {});
bool StartDebugger(bool needBreakPoint, const DebuggerPostTask& debuggerPostTask = {});
bool StartDebugger(bool needBreakPoint, uint32_t instanceId, const DebuggerPostTask& debuggerPostTask = {});
bool StartDebugger(bool needBreakPoint, uint32_t instanceId);
void StopDebugger();
bool LoadScript(const std::string& path, uint8_t *buffer, size_t len, bool isBundle);
@ -145,13 +143,14 @@ private:
private:
bool CreateJsEnv(const Options& options);
void PreloadAce(const Options& options);
bool InitLoop(const std::shared_ptr<AppExecFwk::EventRunner>& eventRunner);
bool InitLoop();
inline bool IsUseAbilityRuntime(const Options& options) const;
void FreeNativeReference(std::unique_ptr<NativeReference> uniqueNativeRef,
std::shared_ptr<NativeReference>&& sharedNativeRef);
void InitConsoleModule();
void InitTimerModule();
void InitWorkerModule(const Options& options);
void ReInitJsEnvImpl(const Options& options);
};
} // namespace AbilityRuntime
} // namespace OHOS

View File

@ -35,12 +35,12 @@ static panda::DFXJSNApi::ProfilerType ConvertProfilerType(JsEnvironment::PROFILE
JsEnvironment::JsEnvironment(std::unique_ptr<JsEnvironmentImpl> impl) : impl_(std::move(impl))
{
JSENV_LOG_D("Js environment costructor.");
JSENV_LOG_I("Js environment costructor.");
}
JsEnvironment::~JsEnvironment()
{
JSENV_LOG_D("Js environment destructor.");
JSENV_LOG_I("Js environment destructor.");
if (engine_ != nullptr) {
engine_->DeleteEngine();
@ -100,6 +100,13 @@ void JsEnvironment::PostTask(const std::function<void()>& task, const std::strin
}
}
void JsEnvironment::PostSyncTask(const std::function<void()>& task, const std::string& name)
{
if (impl_ != nullptr) {
impl_->PostSyncTask(task, name);
}
}
void JsEnvironment::RemoveTask(const std::string& name)
{
if (impl_ != nullptr) {
@ -145,11 +152,18 @@ bool JsEnvironment::LoadScript(const std::string& path, std::vector<uint8_t>* bu
return engine_->RunScriptBuffer(path.c_str(), *buffer, isBundle) != nullptr;
}
bool JsEnvironment::StartDebugger(const char* libraryPath, bool needBreakPoint, uint32_t instanceId,
const DebuggerPostTask& debuggerPostTask)
bool JsEnvironment::StartDebugger(const char* libraryPath, bool needBreakPoint, uint32_t instanceId)
{
if (vm_ != nullptr) {
panda::JSNApi::DebugOption debugOption = {libraryPath, needBreakPoint};
auto debuggerPostTask = [weak = weak_from_this()](std::function<void()>&& task) {
auto jsEnv = weak.lock();
if (jsEnv == nullptr) {
JSENV_LOG_E("JsEnv is invalid.");
return;
}
jsEnv->PostTask(task);
};
return panda::JSNApi::StartDebugger(vm_, debugOption, instanceId, debuggerPostTask);
}
return false;
@ -174,7 +188,7 @@ void JsEnvironment::InitConsoleModule()
}
}
bool JsEnvironment::InitLoop(const std::shared_ptr<AppExecFwk::EventRunner>& eventRunner)
bool JsEnvironment::InitLoop()
{
if (engine_ == nullptr) {
JSENV_LOG_E("Invalid Native Engine.");
@ -182,7 +196,7 @@ bool JsEnvironment::InitLoop(const std::shared_ptr<AppExecFwk::EventRunner>& eve
}
if (impl_ != nullptr) {
impl_->InitLoop(engine_, eventRunner);
impl_->InitLoop(engine_);
}
return true;
}
@ -219,5 +233,11 @@ void JsEnvironment::StartProfiler(const char* libraryPath, uint32_t instanceId,
panda::DFXJSNApi::StartProfiler(vm_, option, instanceId, debuggerPostTask);
}
void JsEnvironment::ReInitJsEnvImpl(std::unique_ptr<JsEnvironmentImpl> impl)
{
JSENV_LOG_I("ReInit jsenv impl.");
impl_ = std::move(impl);
}
} // namespace JsEnv
} // namespace OHOS

View File

@ -27,7 +27,7 @@
namespace OHOS {
namespace JsEnv {
class JsEnvironmentImpl;
class JsEnvironment final {
class JsEnvironment final : public std::enable_shared_from_this<JsEnvironment> {
public:
JsEnvironment() {}
explicit JsEnvironment(std::unique_ptr<JsEnvironmentImpl> impl);
@ -60,26 +60,30 @@ public:
void PostTask(const std::function<void()>& task, const std::string& name = "", int64_t delayTime = 0);
void PostSyncTask(const std::function<void()>& task, const std::string& name = "");
void RemoveTask(const std::string& name);
void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo uncaughtExceptionInfo);
bool LoadScript(const std::string& path, std::vector<uint8_t>* buffer = nullptr, bool isBundle = false);
bool StartDebugger(const char* libraryPath, bool needBreakPoint, uint32_t instanceId,
const DebuggerPostTask& debuggerPostTask = {});
bool StartDebugger(const char* libraryPath, bool needBreakPoint, uint32_t instanceId);
void StopDebugger();
void InitConsoleModule();
bool InitLoop(const std::shared_ptr<AppExecFwk::EventRunner>& eventRunner);
bool InitLoop();
void DeInitLoop();
void StopDebugger();
bool LoadScript(const std::string& path, uint8_t *buffer, size_t len, bool isBundle);
void StartProfiler(const char* libraryPath, uint32_t instanceId, PROFILERTYPE profiler, int32_t interval,
const DebuggerPostTask &debuggerPostTask = {});
void ReInitJsEnvImpl(std::unique_ptr<JsEnvironmentImpl> impl);
private:
std::unique_ptr<JsEnvironmentImpl> impl_ = nullptr;
NativeEngine* engine_ = nullptr;

View File

@ -40,13 +40,15 @@ public:
virtual void PostTask(const std::function<void()>& task, const std::string& name, int64_t delayTime) = 0;
virtual void PostSyncTask(const std::function<void()>& task, const std::string& name) = 0;
virtual void RemoveTask(const std::string& name) = 0;
virtual void InitTimerModule(NativeEngine* engine) = 0;
virtual void InitConsoleModule(NativeEngine *engine) = 0;
virtual bool InitLoop(NativeEngine *engine, const std::shared_ptr<AppExecFwk::EventRunner>& eventRunner) = 0;
virtual bool InitLoop(NativeEngine *engine) = 0;
virtual void DeInitLoop(NativeEngine *engine) = 0;

View File

@ -260,8 +260,7 @@ HWTEST_F(JsEnvironmentTest, StartDebugger_0100, TestSize.Level0)
const char* libraryPath = "LIBRARYPATH";
bool needBreakPoint = true;
uint32_t instanceId = 10;
DebuggerPostTask debuggerPostTask = {};
bool result = jsEnv->StartDebugger(libraryPath, needBreakPoint, instanceId, debuggerPostTask);
bool result = jsEnv->StartDebugger(libraryPath, needBreakPoint, instanceId);
EXPECT_EQ(result, false);
}
@ -336,5 +335,31 @@ HWTEST_F(JsEnvironmentTest, StartProfiler_0200, TestSize.Level1)
jsEnv->StartProfiler(libraryPath, 0, JsEnvironment::PROFILERTYPE::PROFILERTYPE_HEAP, 0, debuggerPostTask);
ASSERT_NE(jsEnv->GetVM(), nullptr);
}
/**
* @tc.name: PostSyncTask_0100
* @tc.desc: Js environment post sync task.
* @tc.type: FUNC
* @tc.require: issueI7C87T
*/
HWTEST_F(JsEnvironmentTest, PostSyncTask_0100, TestSize.Level0)
{
auto runner = AppExecFwk::EventRunner::Create("TASK_RUNNER");
ASSERT_NE(runner, nullptr);
auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>(runner));
ASSERT_NE(jsEnv, nullptr);
panda::RuntimeOption pandaOption;
ASSERT_EQ(jsEnv->Initialize(pandaOption, static_cast<void*>(this)), true);
ASSERT_EQ(jsEnv->InitLoop(), true);
std::string taskName = "syncTask001";
bool taskExecuted = false;
auto task = [taskName, &taskExecuted]() {
JSENV_LOG_I("%{public}s called.", taskName.c_str());
taskExecuted = true;
};
jsEnv->PostSyncTask(task, taskName);
EXPECT_EQ(taskExecuted, true);
}
} // namespace JsEnv
} // namespace OHOS

View File

@ -652,7 +652,7 @@ HWTEST_F(JsRuntimeTest, StopDebugger_0100, TestSize.Level0)
auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
ASSERT_NE(jsRuntime, nullptr);
jsRuntime->StopDebugger();
HILOG_INFO("StopDebugger end");
}
@ -751,5 +751,26 @@ HWTEST_F(JsRuntimeTest, JsRuntimeStopDebuggerTest_0100, TestSize.Level0)
jsRuntime->StopDebugger();
}
/**
* @tc.name: PostSyncTask_0100
* @tc.desc: Js runtime post sync task.
* @tc.type: FUNC
* @tc.require: issueI7C87T
*/
HWTEST_F(JsRuntimeTest, PostSyncTask_0100, TestSize.Level0)
{
auto jsRuntime = AbilityRuntime::JsRuntime::Create(options_);
ASSERT_NE(jsRuntime, nullptr);
std::string taskName = "syncTask001";
bool taskExecuted = false;
auto task = [taskName, &taskExecuted]() {
HILOG_INFO("%{public}s called.", taskName.c_str());
taskExecuted = true;
};
jsRuntime->PostSyncTask(task, taskName);
EXPECT_EQ(taskExecuted, true);
}
} // namespace AbilityRuntime
} // namespace OHOS

View File

@ -67,6 +67,35 @@ HWTEST_F(OHOSJsEnvironmentTest, PostTask_0100, TestSize.Level0)
jsEnvImpl->RemoveTask(taskName);
}
/**
* @tc.name: PostSyncTask_0100
* @tc.desc: Js environment post sync task.
* @tc.type: FUNC
* @tc.require: issueI7C87T
*/
HWTEST_F(OHOSJsEnvironmentTest, PostSyncTask_0100, TestSize.Level0)
{
auto runner = AppExecFwk::EventRunner::Create("TASK_RUNNER");
ASSERT_NE(runner, nullptr);
auto jsEnvImpl = std::make_shared<OHOSJsEnvironmentImpl>(runner);
ASSERT_NE(jsEnvImpl, nullptr);
AbilityRuntime::Runtime::Options options;
auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
ASSERT_NE(jsRuntime, nullptr);
auto ret = jsEnvImpl->InitLoop(jsRuntime->GetNativeEnginePointer());
ASSERT_EQ(ret, true);
std::string taskName = "syncTask001";
bool taskExecuted = false;
auto task = [taskName, &taskExecuted]() {
HILOG_INFO("%{public}s called.", taskName.c_str());
taskExecuted = true;
};
jsEnvImpl->PostSyncTask(task, taskName);
EXPECT_EQ(taskExecuted, true);
}
/**
* @tc.name: InitTimerModule_0100
* @tc.desc: Js environment init timer.