!164 add the necessary methods for ark worker

Merge pull request !164 from yaojian16/master
This commit is contained in:
openharmony_ci 2021-10-09 13:14:06 +00:00 committed by Gitee
commit 46c12a7e79
9 changed files with 333 additions and 8 deletions

View File

@ -70,7 +70,7 @@ if (is_standard_system) {
} else {
ace_engine_part = "ace_engine_full"
hilog_deps = [ "hilog:libhilog" ]
worker_deps = [ "jsapi:worker_init" ]
worker_deps = [ "jsapi_worker:worker_init" ]
ark_runtime_path = "//ark/runtime"
}

View File

@ -31,6 +31,8 @@ void JsClickFunction::Execute()
V8DeclarativeEngineInstance::TriggerPageUpdate();
#elif USE_QUICKJS_ENGINE
QJSDeclarativeEngineInstance::TriggerPageUpdate(QJSContext::Current());
#elif USE_ARK_ENGINE
JsiDeclarativeEngineInstance::TriggerPageUpdate(JsiDeclarativeEngineInstance::GetJsRuntime());
#endif
}

View File

@ -17,6 +17,8 @@
#include <unistd.h>
#include "worker_init.h"
#include "base/i18n/localization.h"
#include "base/log/ace_trace.h"
#include "base/log/event_report.h"
@ -405,6 +407,93 @@ void JsiDeclarativeEngineInstance::InitConsoleModule()
global->SetProperty(runtime_, "aceConsole", aceConsoleObj);
}
std::string GetLogContent(NativeEngine* nativeEngine, NativeCallbackInfo* info)
{
std::string content;
for (size_t i = 0; i < info->argc; ++i) {
if (info->argv[i]->TypeOf() != NATIVE_STRING) {
LOGE("argv is not NativeString");
continue;
}
auto nativeString = reinterpret_cast<NativeString*>(info->argv[i]->GetInterface(NativeString::INTERFACE_ID));
size_t bufferSize = nativeString->GetLength();
size_t strLength = 0;
char* buffer = new char[bufferSize + 1] { 0 };
nativeString->GetCString(buffer, bufferSize + 1, &strLength);
content.append(buffer);
delete[] buffer;
}
return content;
}
NativeValue* AppLogPrint(NativeEngine* nativeEngine, NativeCallbackInfo* info, JsLogLevel level)
{
// Should have at least 1 parameters.
if (info->argc == 0) {
LOGE("the arg is error");
return nativeEngine->CreateUndefined();
}
std::string content = GetLogContent(nativeEngine, info);
switch (level) {
case JsLogLevel::DEBUG:
APP_LOGD("app Log: %{public}s", content.c_str());
break;
case JsLogLevel::INFO:
APP_LOGI("app Log: %{public}s", content.c_str());
break;
case JsLogLevel::WARNING:
APP_LOGW("app Log: %{public}s", content.c_str());
break;
case JsLogLevel::ERROR:
APP_LOGE("app Log: %{public}s", content.c_str());
break;
}
return nativeEngine->CreateUndefined();
}
NativeValue* AppDebugLogPrint(NativeEngine* nativeEngine, NativeCallbackInfo* info)
{
return AppLogPrint(nativeEngine, info, JsLogLevel::DEBUG);
}
NativeValue* AppInfoLogPrint(NativeEngine* nativeEngine, NativeCallbackInfo* info)
{
return AppLogPrint(nativeEngine, info, JsLogLevel::INFO);
}
NativeValue* AppWarnLogPrint(NativeEngine* nativeEngine, NativeCallbackInfo* info)
{
return AppLogPrint(nativeEngine, info, JsLogLevel::WARNING);
}
NativeValue* AppErrorLogPrint(NativeEngine* nativeEngine, NativeCallbackInfo* info)
{
return AppLogPrint(nativeEngine, info, JsLogLevel::ERROR);
}
void JsiDeclarativeEngineInstance::InitConsoleModule(ArkNativeEngine* engine)
{
ACE_SCOPED_TRACE("JsiDeclarativeEngineInstance::RegisterConsoleModule");
LOGD("JsiDeclarativeEngineInstance RegisterConsoleModule to nativeEngine");
NativeValue* global = engine->GetGlobal();
if (global->TypeOf() != NATIVE_OBJECT) {
LOGE("global is not NativeObject");
return;
}
auto nativeGlobal = reinterpret_cast<NativeObject*>(global->GetInterface(NativeObject::INTERFACE_ID));
// app log method
NativeValue* console = engine->CreateObject();
auto consoleObj = reinterpret_cast<NativeObject*>(console->GetInterface(NativeObject::INTERFACE_ID));
consoleObj->SetProperty("log", engine->CreateFunction("log", strlen("log"), AppDebugLogPrint, nullptr));
consoleObj->SetProperty("debug", engine->CreateFunction("debug", strlen("debug"), AppDebugLogPrint, nullptr));
consoleObj->SetProperty("info", engine->CreateFunction("info", strlen("info"), AppInfoLogPrint, nullptr));
consoleObj->SetProperty("warn", engine->CreateFunction("warn", strlen("warn"), AppWarnLogPrint, nullptr));
consoleObj->SetProperty("error", engine->CreateFunction("error", strlen("error"), AppErrorLogPrint, nullptr));
nativeGlobal->SetProperty("console", console);
}
void JsiDeclarativeEngineInstance::InitPerfUtilModule()
{
ACE_SCOPED_TRACE("JsiDeclarativeEngineInstance::InitPerfUtilModule");
@ -611,7 +700,82 @@ bool JsiDeclarativeEngine::Initialize(const RefPtr<FrontendDelegate>& delegate)
LOGI("JsiDeclarativeEngine Initialize");
ACE_DCHECK(delegate);
engineInstance_ = AceType::MakeRefPtr<JsiDeclarativeEngineInstance>(delegate, instanceId_);
return engineInstance_->InitJsEnv(IsDebugVersion() && NeedDebugBreakPoint());
bool result = engineInstance_->InitJsEnv(IsDebugVersion() && NeedDebugBreakPoint());
if (!result) {
LOGE("JsiDeclarativeEngine Initialize, init js env failed");
return false;
}
auto runtime = engineInstance_->GetJsRuntime();
auto vm = std::static_pointer_cast<ArkJSRuntime>(runtime)->GetEcmaVm();
if (vm == nullptr) {
LOGE("JsiDeclarativeEngine Initialize, vm is null");
return false;
}
nativeEngine_ = new ArkNativeEngine(const_cast<EcmaVM*>(vm), static_cast<void*>(this));
ACE_DCHECK(delegate);
delegate->AddTaskObserver([nativeEngine = nativeEngine_]() {
nativeEngine->Loop(LOOP_NOWAIT);
});
RegisterWorker();
return result;
}
void JsiDeclarativeEngine::RegisterInitWorkerFunc()
{
auto weakInstance = AceType::WeakClaim(AceType::RawPtr(engineInstance_));
auto&& initWorkerFunc = [weakInstance](NativeEngine* nativeEngine) {
LOGI("WorkerCore RegisterInitWorkerFunc called");
if (nativeEngine == nullptr) {
LOGE("nativeEngine is nullptr");
return;
}
auto arkNativeEngine = static_cast<ArkNativeEngine*>(nativeEngine);
if (arkNativeEngine == nullptr) {
LOGE("arkNativeEngine is nullptr");
return;
}
auto instance = weakInstance.Upgrade();
if (instance == nullptr) {
LOGE("instance is nullptr");
return;
}
instance->InitConsoleModule(arkNativeEngine);
std::vector<uint8_t> buffer((uint8_t *)_binary_jsEnumStyle_abc_start, (uint8_t *)_binary_jsEnumStyle_abc_end);
auto stateMgmtResult = arkNativeEngine->RunBufferScript(buffer);
if (stateMgmtResult == nullptr) {
LOGE("init worker error");
}
};
OHOS::CCRuntime::Worker::WorkerCore::RegisterInitWorkerFunc(initWorkerFunc);
}
void JsiDeclarativeEngine::RegisterAssetFunc()
{
auto weakDelegate = AceType::WeakClaim(AceType::RawPtr(engineInstance_->GetDelegate()));
auto&& assetFunc = [weakDelegate](const std::string& uri, std::vector<uint8_t>& content) {
LOGI("WorkerCore RegisterAssetFunc called");
auto delegate = weakDelegate.Upgrade();
if (delegate == nullptr) {
LOGE("delegate is nullptr");
return;
}
size_t index = uri.find_last_of(".");
if (index == std::string::npos) {
LOGE("invalid uri");
} else {
delegate->GetResourceData(uri.substr(0, index) + ".abc", content);
}
};
OHOS::CCRuntime::Worker::WorkerCore::RegisterAssetFunc(assetFunc);
}
void JsiDeclarativeEngine::RegisterWorker()
{
RegisterInitWorkerFunc();
RegisterAssetFunc();
}
void JsiDeclarativeEngine::LoadJs(const std::string& url, const RefPtr<JsAcePage>& page, bool isMainPage)

View File

@ -30,6 +30,7 @@
#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_runtime.h"
#include "frameworks/bridge/js_frontend/engine/common/js_engine.h"
#include "frameworks/bridge/js_frontend/js_ace_page.h"
#include "native_engine/impl/ark/ark_native_engine.h"
namespace OHOS::Ace::Framework {
@ -47,6 +48,9 @@ public:
bool FireJsEvent(const std::string& eventStr);
// add Console object to worker
void InitConsoleModule(ArkNativeEngine* engine);
static void RootViewHandle(const shared_ptr<JsRuntime>& runtime, panda::Local<panda::ObjectRef> value);
void DestroyRootViewHandle(int32_t pageId);
void DestroyAllRootViewHandle();
@ -204,6 +208,10 @@ private:
void TimerCallJs(const std::string& callbackId) const;
void RegisterWorker();
void RegisterInitWorkerFunc();
void RegisterAssetFunc();
RefPtr<JsiDeclarativeEngineInstance> engineInstance_;
int32_t instanceId_ = 0;

View File

@ -26,6 +26,7 @@
#include "base/log/event_report.h"
#include "base/log/log.h"
#include "frameworks/bridge/declarative_frontend/engine/quickjs/modules/qjs_module_manager.h"
#include "frameworks/bridge/declarative_frontend/engine/quickjs/qjs_helpers.h"
#include "frameworks/bridge/js_frontend/engine/common/js_constants.h"
#include "frameworks/bridge/js_frontend/engine/quickjs/qjs_utils.h"

View File

@ -58,6 +58,7 @@ template("js_engine_ark") {
defines += [ "APP_USE_ARM" ]
}
deps += [ "//foundation/ace/napi:ace_napi_ark" ]
external_deps = worker_deps
}
}

View File

@ -20,7 +20,8 @@
#include <unistd.h>
#include "bridge/js_frontend/engine/jsi/ark_js_runtime.h"
#include "native_engine/impl/ark/ark_native_engine.h"
#include "bridge/js_frontend/engine/jsi/ark_js_value.h"
#include "worker_init.h"
#include "base/i18n/localization.h"
#include "base/log/ace_trace.h"
@ -1418,7 +1419,7 @@ void ShowActionMenu(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsVal
engine->CallJs(complete, R"({"errMsg":"enableAlertBeforeBackPage:buttons is invalid"})");
} else {
std::string callBackStr = arg->ToString(runtime);
// Get callbackId and clear redundant symbols
// Get callbackId and clear redundant symbols, 2 is available min string length
if (callBackStr.size() > 2 && callBackStr.front() == '\"' && callBackStr.back() == '\"') {
callBackStr = callBackStr.substr(1, callBackStr.size() - 2);
engine->CallJs(callBackStr,
@ -1496,7 +1497,7 @@ shared_ptr<JsValue> EnableAlertBeforeBackPage(const shared_ptr<JsRuntime>& runti
} else {
LOGE("enableAlertBeforeBackPage message is null");
std::string callBackStr = arg->ToString(runtime);
// Get callbackId and clear redundant symbols
// Get callbackId and clear redundant symbols, 2 is available min string length
if (callBackStr.size() > 2 && callBackStr.front() == '\"' && callBackStr.back() == '\"') {
callBackStr = callBackStr.substr(1, callBackStr.size() - 2);
engine->CallJs(callBackStr,
@ -1560,7 +1561,7 @@ shared_ptr<JsValue> DisableAlertBeforeBackPage(const shared_ptr<JsRuntime>& runt
}
std::string callBackStr = arg->ToString(runtime);
// Get callbackId and clear redundant symbols
// Get callbackId and clear redundant symbols, 2 is available min string length
if (callBackStr.size() > 2 && callBackStr.front() == '\"' && callBackStr.back() == '\"') {
callBackStr = callBackStr.substr(1, callBackStr.size() - 2);
engine->CallJs(callBackStr, R"({"arguments":[{"errMsg":"disableAlertBeforeBackPage:ok"}],"method":"success"})");
@ -2478,6 +2479,93 @@ void JsiEngineInstance::RegisterConsoleModule()
global->SetProperty(runtime_, "aceConsole", aceConsoleObj);
}
std::string GetLogContent(NativeEngine* nativeEngine, NativeCallbackInfo* info)
{
std::string content;
for (size_t i = 0; i < info->argc; ++i) {
if (info->argv[i]->TypeOf() != NATIVE_STRING) {
LOGE("argv is not NativeString");
continue;
}
auto nativeString = reinterpret_cast<NativeString*>(info->argv[i]->GetInterface(NativeString::INTERFACE_ID));
size_t bufferSize = nativeString->GetLength();
size_t strLength = 0;
char* buffer = new char[bufferSize + 1] { 0 };
nativeString->GetCString(buffer, bufferSize + 1, &strLength);
content.append(buffer);
delete[] buffer;
}
return content;
}
NativeValue* AppLogPrint(NativeEngine* nativeEngine, NativeCallbackInfo* info, JsLogLevel level)
{
// Should have at least 1 parameters.
if (info->argc == 0) {
LOGE("the arg is error");
return nativeEngine->CreateUndefined();
}
std::string content = GetLogContent(nativeEngine, info);
switch (level) {
case JsLogLevel::DEBUG:
APP_LOGD("app Log: %{public}s", content.c_str());
break;
case JsLogLevel::INFO:
APP_LOGI("app Log: %{public}s", content.c_str());
break;
case JsLogLevel::WARNING:
APP_LOGW("app Log: %{public}s", content.c_str());
break;
case JsLogLevel::ERROR:
APP_LOGE("app Log: %{public}s", content.c_str());
break;
}
return nativeEngine->CreateUndefined();
}
NativeValue* AppDebugLogPrint(NativeEngine* nativeEngine, NativeCallbackInfo* info)
{
return AppLogPrint(nativeEngine, info, JsLogLevel::DEBUG);
}
NativeValue* AppInfoLogPrint(NativeEngine* nativeEngine, NativeCallbackInfo* info)
{
return AppLogPrint(nativeEngine, info, JsLogLevel::INFO);
}
NativeValue* AppWarnLogPrint(NativeEngine* nativeEngine, NativeCallbackInfo* info)
{
return AppLogPrint(nativeEngine, info, JsLogLevel::WARNING);
}
NativeValue* AppErrorLogPrint(NativeEngine* nativeEngine, NativeCallbackInfo* info)
{
return AppLogPrint(nativeEngine, info, JsLogLevel::ERROR);
}
void JsiEngineInstance::RegisterConsoleModule(ArkNativeEngine* engine)
{
ACE_SCOPED_TRACE("JsiEngineInstance::RegisterConsoleModule");
LOGD("JsiEngineInstance RegisterConsoleModule to nativeEngine");
NativeValue* global = engine->GetGlobal();
if (global->TypeOf() != NATIVE_OBJECT) {
LOGE("global is not NativeObject");
return;
}
auto nativeGlobal = reinterpret_cast<NativeObject*>(global->GetInterface(NativeObject::INTERFACE_ID));
// app log method
NativeValue* console = engine->CreateObject();
auto consoleObj = reinterpret_cast<NativeObject*>(console->GetInterface(NativeObject::INTERFACE_ID));
consoleObj->SetProperty("log", engine->CreateFunction("log", strlen("log"), AppDebugLogPrint, nullptr));
consoleObj->SetProperty("debug", engine->CreateFunction("debug", strlen("debug"), AppDebugLogPrint, nullptr));
consoleObj->SetProperty("info", engine->CreateFunction("info", strlen("info"), AppInfoLogPrint, nullptr));
consoleObj->SetProperty("warn", engine->CreateFunction("warn", strlen("warn"), AppWarnLogPrint, nullptr));
consoleObj->SetProperty("error", engine->CreateFunction("error", strlen("error"), AppErrorLogPrint, nullptr));
nativeGlobal->SetProperty("console", console);
}
void JsiEngineInstance::RegisterPerfUtilModule()
{
ACE_SCOPED_TRACE("JsiEngine::RegisterPerfUtilModule");
@ -2636,13 +2724,68 @@ bool JsiEngine::Initialize(const RefPtr<FrontendDelegate>& delegate)
}
nativeEngine_ = new ArkNativeEngine(const_cast<EcmaVM*>(vm), static_cast<void*>(this));
ACE_DCHECK(delegate);
delegate->AddTaskObserver([nativeEngine = nativeEngine_](){
delegate->AddTaskObserver([nativeEngine = nativeEngine_]() {
nativeEngine->Loop(LOOP_NOWAIT);
});
RegisterWorker();
return true;
}
void JsiEngine::RegisterInitWorkerFunc()
{
auto weakInstance = AceType::WeakClaim(AceType::RawPtr(engineInstance_));
auto&& initWorkerFunc = [weakInstance](NativeEngine* nativeEngine) {
LOGI("WorkerCore RegisterInitWorkerFunc called");
if (nativeEngine == nullptr) {
LOGE("nativeEngine is nullptr");
return;
}
auto arkNativeEngine = static_cast<ArkNativeEngine*>(nativeEngine);
if (arkNativeEngine == nullptr) {
LOGE("arkNativeEngine is nullptr");
return;
}
auto instance = weakInstance.Upgrade();
if (instance == nullptr) {
LOGE("instance is nullptr");
return;
}
instance->RegisterConsoleModule(arkNativeEngine);
// load jsfwk
if (!arkNativeEngine->ExecuteJsBin("/system/etc/strip.native.min.abc")) {
LOGE("Failed to load js framework!");
}
};
OHOS::CCRuntime::Worker::WorkerCore::RegisterInitWorkerFunc(initWorkerFunc);
}
void JsiEngine::RegisterAssetFunc()
{
auto weakDelegate = AceType::WeakClaim(AceType::RawPtr(engineInstance_->GetDelegate()));
auto&& assetFunc = [weakDelegate](const std::string& uri, std::vector<uint8_t>& content) {
LOGI("WorkerCore RegisterAssetFunc called");
auto delegate = weakDelegate.Upgrade();
if (delegate == nullptr) {
LOGE("delegate is nullptr");
return;
}
size_t index = uri.find_last_of(".");
if (index == std::string::npos) {
LOGE("invalid uri");
} else {
delegate->GetResourceData(uri.substr(0, index) + ".abc", content);
}
};
OHOS::CCRuntime::Worker::WorkerCore::RegisterAssetFunc(assetFunc);
}
void JsiEngine::RegisterWorker()
{
RegisterInitWorkerFunc();
RegisterAssetFunc();
}
JsiEngine::~JsiEngine()
{
engineInstance_->GetFrontendDelegate()->RemoveTaskObserver();

View File

@ -26,6 +26,7 @@
#include "frameworks/bridge/js_frontend/engine/jsi/jsi_utils.h"
#include "frameworks/bridge/js_frontend/frontend_delegate.h"
#include "frameworks/bridge/js_frontend/js_ace_page.h"
#include "native_engine/impl/ark/ark_native_engine.h"
namespace OHOS::Ace::Framework {
class JsiEngineInstance final : public AceType, public JsEngineInstance {
@ -47,6 +48,8 @@ public:
void CallJs(const std::string& callbackId, const std::string& args, bool keepAlive = false, bool isGlobal = false);
shared_ptr<JsRuntime> GetJsRuntime() const;
bool InitJsEnv(bool debugger_mode);
// add Console object to worker
void RegisterConsoleModule(ArkNativeEngine* engine);
void FlushCommandBuffer(void* context, const std::string& command) override;
@ -106,6 +109,9 @@ public:
private:
void GetLoadOptions(std::string& optionStr, bool isMainPage, bool hasAppCode);
void RegisterWorker();
void RegisterInitWorkerFunc();
void RegisterAssetFunc();
int32_t instanceId_ = 0;
RefPtr<JsiEngineInstance> engineInstance_;

View File

@ -39,7 +39,7 @@ bool ParseFileUri(const RefPtr<AssetManager>& assetManager, const std::string& f
fileName = fileUri;
} else {
fileName = fileUri.substr(slashPos + 1);
filePath = fileUri.substr(0, slashPos);
filePath = fileUri.substr(0, slashPos + 1);
}
if (StartWith(filePath, SLASHSTR)) {