From f2bd0993d070599a8bc8929517c1032766dac692 Mon Sep 17 00:00:00 2001 From: panzhenyu1 Date: Mon, 28 Mar 2022 15:07:17 +0800 Subject: [PATCH] Add support for Ark engine as built-in JS engine for previewer. Add support for Ark engine as built-in JS engine for previewer. Note, This patch won't enable Ark engine on previewer currently but offer an option to enable Ark in the future patch. relevant issue: #I4ZE7F:Jsi engine adapt to windows platform and add support for Previewer Signed-off-by: panzhenyu1 Change-Id: Ic36079145f2724cda32b79005952f576221313d6 --- .../pa_engine/engine/jsi/jsi_pa_engine.cpp | 218 +---------- .../pa_engine/engine/jsi/jsi_pa_utils.cpp | 2 +- adapter/preview/build/config_js_engine.gni | 17 + adapter/preview/build/config_mac.gni | 39 +- adapter/preview/build/config_windows.gni | 39 +- adapter/preview/entrance/ace_container.cpp | 9 +- adapter/preview/entrance/samples/BUILD.gn | 151 +++++--- build/ace_lib.gni | 11 +- .../bridge/declarative_frontend/BUILD.gn | 4 +- .../declarative_frontend.cpp | 10 - .../declarative_frontend.h | 1 - .../declarative_frontend/engine/jsi/BUILD.gn | 26 +- .../engine/jsi/ark/ark_js_runtime.cpp | 224 ----------- .../engine/jsi/ark/ark_js_runtime.h | 116 ------ .../engine/jsi/ark/ark_js_value.cpp | 328 ---------------- .../engine/jsi/ark/ark_js_value.h | 101 ----- .../engine/jsi/ark/include/js_runtime.h | 88 ----- .../engine/jsi/ark/include/js_value.h | 83 ----- .../engine/jsi/js_converter.cpp | 2 +- .../engine/jsi/jsi_declarative_engine.cpp | 349 ++++-------------- .../engine/jsi/jsi_declarative_engine.h | 10 +- .../jsi/jsi_declarative_group_js_bridge.cpp | 8 + .../jsi/jsi_declarative_group_js_bridge.h | 8 + .../engine/jsi/jsi_declarative_utils.cpp | 2 +- .../engine/jsi/jsi_types.cpp | 2 +- .../engine/jsi/jsi_types.inl | 2 +- .../engine/jsi/jsi_value_conversions.h | 2 +- .../engine/jsi/modules/jsi_app_module.h | 4 +- .../engine/jsi/modules/jsi_context_module.h | 4 +- .../engine/jsi/modules/jsi_curves_module.h | 4 +- .../engine/jsi/modules/jsi_matrix4_module.h | 4 +- .../engine/jsi/modules/jsi_module_manager.h | 4 +- .../engine/jsi/modules/jsi_router_module.cpp | 21 +- .../engine/jsi/modules/jsi_router_module.h | 4 +- .../engine/jsi/modules/jsi_syscap_module.h | 4 +- .../engine/jsi/modules/jsi_timer_module.h | 4 +- .../engine/quickjs/qjs_declarative_engine.h | 2 +- .../js_frontend/engine/common/js_engine.h | 8 + .../bridge/js_frontend/engine/jsi/BUILD.gn | 26 +- .../js_frontend/engine/jsi/ark_js_runtime.cpp | 20 +- .../js_frontend/engine/jsi/ark_js_runtime.h | 7 +- .../js_frontend/engine/jsi/jsi_base_utils.cpp | 267 +++++++++++++- .../js_frontend/engine/jsi/jsi_base_utils.h | 39 +- .../engine/jsi/jsi_canvas_bridge.cpp | 2 + .../js_frontend/engine/jsi/jsi_engine.cpp | 287 +------------- .../engine/jsi/jsi_group_js_bridge.cpp | 8 + .../engine/jsi/jsi_group_js_bridge.h | 9 + .../js_frontend/engine/jsi/jsi_utils.cpp | 2 +- .../engine/jsi/jsi_utils_windows.cpp | 50 +++ 49 files changed, 784 insertions(+), 1848 deletions(-) create mode 100644 adapter/preview/build/config_js_engine.gni delete mode 100644 frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_runtime.cpp delete mode 100644 frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_runtime.h delete mode 100644 frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_value.cpp delete mode 100644 frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_value.h delete mode 100644 frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_runtime.h delete mode 100644 frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_value.h create mode 100644 frameworks/bridge/js_frontend/engine/jsi/jsi_utils_windows.cpp diff --git a/adapter/ohos/entrance/pa_engine/engine/jsi/jsi_pa_engine.cpp b/adapter/ohos/entrance/pa_engine/engine/jsi/jsi_pa_engine.cpp index 31862e85..4f6b317e 100755 --- a/adapter/ohos/entrance/pa_engine/engine/jsi/jsi_pa_engine.cpp +++ b/adapter/ohos/entrance/pa_engine/engine/jsi/jsi_pa_engine.cpp @@ -76,145 +76,6 @@ bool UnwrapRawImageDataMap(NativeEngine* engine, NativeValue* argv, std::map& params) -{ - std::string ret; - if (params.empty()) { - return ret; - } - std::string formatStr = params[0]; - uint32_t size = params.size(); - uint32_t len = formatStr.size(); - uint32_t pos = 0; - uint32_t count = 1; - for (; pos < len; ++pos) { - if (count >= size) { - break; - } - if (formatStr[pos] == '%') { - if (pos + 1 >= len) { - break; - } - switch (formatStr[pos + 1]) { - case 's': - case 'j': - case 'd': - case 'O': - case 'o': - case 'i': - case 'f': - case 'c': - ret += params[count++]; - ++pos; - break; - case '%': - ret += formatStr[pos]; - ++pos; - break; - default: - ret += formatStr[pos]; - break; - } - } else { - ret += formatStr[pos]; - } - } - if (pos < len) { - ret += formatStr.substr(pos, len - pos); - } - return ret; -} - -std::string GetLogContent( - const shared_ptr& runtime, const std::vector>& argv, int32_t argc) -{ - if (argc == 1) { - return argv[0]->ToString(runtime); - } - std::vector params; - for (int32_t i = 0; i < argc; ++i) { - params.emplace_back(argv[i]->ToString(runtime)); - } - return ParseLogContent(params); -} - -shared_ptr AppLogPrint( - const shared_ptr& runtime, JsLogLevel level, const std::vector>& argv, int32_t argc) -{ - // Should have at least 1 parameters. - if (argc == 0) { - LOGE("the arg is error"); - return runtime->NewUndefined(); - } - std::string content = GetLogContent(runtime, argv, argc); - 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 runtime->NewUndefined(); -} - -// native implementation for js function: console.debug() -shared_ptr AppDebugLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return AppLogPrint(runtime, JsLogLevel::DEBUG, argv, argc); -} - -// native implementation for js function: console.info() -shared_ptr AppInfoLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return AppLogPrint(runtime, JsLogLevel::INFO, argv, argc); -} - -// native implementation for js function: console.warn() -shared_ptr AppWarnLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return AppLogPrint(runtime, JsLogLevel::WARNING, argv, argc); -} - -// native implementation for js function: console.error() -shared_ptr AppErrorLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return AppLogPrint(runtime, JsLogLevel::ERROR, argv, argc); -} - // native implementation for js function: Particle.onCreateFinish() shared_ptr JsOnCreateFinish(const shared_ptr& runtime, const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) @@ -337,79 +198,14 @@ void JsiPaEngineInstance::RegisterConsoleModule() // app log method shared_ptr consoleObj = runtime_->NewObject(); - consoleObj->SetProperty(runtime_, "log", runtime_->NewFunction(AppDebugLogPrint)); - consoleObj->SetProperty(runtime_, "debug", runtime_->NewFunction(AppDebugLogPrint)); - consoleObj->SetProperty(runtime_, "info", runtime_->NewFunction(AppInfoLogPrint)); - consoleObj->SetProperty(runtime_, "warn", runtime_->NewFunction(AppWarnLogPrint)); - consoleObj->SetProperty(runtime_, "error", runtime_->NewFunction(AppErrorLogPrint)); + consoleObj->SetProperty(runtime_, "log", runtime_->NewFunction(JsiBaseUtils::AppDebugLogPrint)); + consoleObj->SetProperty(runtime_, "debug", runtime_->NewFunction(JsiBaseUtils::AppDebugLogPrint)); + consoleObj->SetProperty(runtime_, "info", runtime_->NewFunction(JsiBaseUtils::AppInfoLogPrint)); + consoleObj->SetProperty(runtime_, "warn", runtime_->NewFunction(JsiBaseUtils::AppWarnLogPrint)); + consoleObj->SetProperty(runtime_, "error", runtime_->NewFunction(JsiBaseUtils::AppErrorLogPrint)); global->SetProperty(runtime_, "console", consoleObj); } -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(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 JsiPaEngineInstance::RegisterConsoleModule(ArkNativeEngine* engine) { ACE_SCOPED_TRACE("JsiEngineInstance::RegisterConsoleModule"); @@ -617,7 +413,9 @@ JsiPaEngine::~JsiPaEngine() UnloadLibrary(); engineInstance_->GetDelegate()->RemoveTaskObserver(); if (nativeEngine_ != nullptr) { +#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) nativeEngine_->CancelCheckUVLoop(); +#endif delete nativeEngine_; } } @@ -716,7 +514,9 @@ bool JsiPaEngine::Initialize(const RefPtr& delegate) }); JsBackendTimerModule::GetInstance()->InitTimerModule(nativeEngine_, delegate); SetPostTask(nativeEngine_); +#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) nativeEngine_->CheckUVLoop(); +#endif if (delegate && delegate->GetAssetManager()) { std::string packagePath = delegate->GetAssetManager()->GetLibPath(); if (!packagePath.empty()) { diff --git a/adapter/ohos/entrance/pa_engine/engine/jsi/jsi_pa_utils.cpp b/adapter/ohos/entrance/pa_engine/engine/jsi/jsi_pa_utils.cpp index 97bfeb19..a6c0b160 100644 --- a/adapter/ohos/entrance/pa_engine/engine/jsi/jsi_pa_utils.cpp +++ b/adapter/ohos/entrance/pa_engine/engine/jsi/jsi_pa_utils.cpp @@ -16,7 +16,7 @@ #include "frameworks/bridge/js_frontend/engine/jsi/jsi_base_utils.h" namespace OHOS::Ace::Framework { -int32_t GetLineOffset() +int32_t GetLineOffset(const AceType *data) { const int32_t offset = 14; return offset; diff --git a/adapter/preview/build/config_js_engine.gni b/adapter/preview/build/config_js_engine.gni new file mode 100644 index 00000000..eff780bb --- /dev/null +++ b/adapter/preview/build/config_js_engine.gni @@ -0,0 +1,17 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +declare_args() { + # Enable Ark as Previewer JS engine, set it 'false' to use QuickJS instead + enable_ark_preview = false +} diff --git a/adapter/preview/build/config_mac.gni b/adapter/preview/build/config_mac.gni index 3a7454c0..e616bdc8 100644 --- a/adapter/preview/build/config_mac.gni +++ b/adapter/preview/build/config_mac.gni @@ -11,28 +11,45 @@ # See the License for the specific language governing permissions and # limitations under the License. +import("config_js_engine.gni") + +# js engine common configs +js_engines = [] +js_pa_support = false +use_build_in_js_engine = true + +if (enable_ark_preview) { + ark_engine = { + engine_name = "ark" + engine_path = "jsi" + engine_defines = [ "USE_ARK_ENGINE" ] + } + js_engines += [ ark_engine ] + build_for_preview = true + xcomponent_components_support = true + use_external_icu = "shared" +} else { + qjs_engine = { + engine_name = "qjs" + engine_path = "quickjs" + engine_defines = [ "USE_QUICKJS_ENGINE" ] + } + js_engines += [ qjs_engine ] + xcomponent_components_support = false +} + +# windows platform defines and configs defines = [ "MAC_PLATFORM", "UNICODE", "SK_BUILD_FONT_MGR_FOR_PREVIEW_MAC", ] - -js_engines = [] -qjs_engine = { - engine_name = "qjs" - engine_path = "quickjs" - engine_defines = [ "USE_QUICKJS_ENGINE" ] -} -js_engines += [ qjs_engine ] -use_build_in_js_engine = true use_curl_download = true accessibility_support = true rich_components_support = true advance_components_support = false form_components_support = true -xcomponent_components_support = false plugin_components_support = false -js_pa_support = false connect_server_support = false enable_rosen_backend = false enable_standard_input = false diff --git a/adapter/preview/build/config_windows.gni b/adapter/preview/build/config_windows.gni index 6cb23ebb..ecddbaa0 100644 --- a/adapter/preview/build/config_windows.gni +++ b/adapter/preview/build/config_windows.gni @@ -11,6 +11,34 @@ # See the License for the specific language governing permissions and # limitations under the License. +import("config_js_engine.gni") + +# js engine common configs +js_engines = [] +js_pa_support = false +use_build_in_js_engine = true + +if (enable_ark_preview) { + ark_engine = { + engine_name = "ark" + engine_path = "jsi" + engine_defines = [ "USE_ARK_ENGINE" ] + } + js_engines += [ ark_engine ] + build_for_preview = true + xcomponent_components_support = true + use_external_icu = "shared" +} else { + qjs_engine = { + engine_name = "qjs" + engine_path = "quickjs" + engine_defines = [ "USE_QUICKJS_ENGINE" ] + } + js_engines += [ qjs_engine ] + xcomponent_components_support = false +} + +# windows platform defines and configs defines = [ "WINDOWS_PLATFORM", "_USE_MATH_DEFINES", @@ -20,23 +48,12 @@ defines = [ "SK_BUILD_FOR_WIN", "SK_BUILD_FONT_MGR_FOR_PREVIEW_WIN", ] - -js_engines = [] -qjs_engine = { - engine_name = "qjs" - engine_path = "quickjs" - engine_defines = [ "USE_QUICKJS_ENGINE" ] -} -js_engines += [ qjs_engine ] -use_build_in_js_engine = true use_curl_download = true accessibility_support = true rich_components_support = true advance_components_support = false form_components_support = true plugin_components_support = false -xcomponent_components_support = false -js_pa_support = false connect_server_support = false enable_rosen_backend = false enable_standard_input = false diff --git a/adapter/preview/entrance/ace_container.cpp b/adapter/preview/entrance/ace_container.cpp index d4ad1532..173a1eea 100644 --- a/adapter/preview/entrance/ace_container.cpp +++ b/adapter/preview/entrance/ace_container.cpp @@ -827,11 +827,16 @@ void AceContainer::LoadDocument(const std::string& url, const std::string& compo LOGE("frontend is null, AceContainer::LoadDocument failed"); return; } + auto jsEngine = frontend->GetJsEngine(); + if (!jsEngine) { + LOGE("jsEngine is null, AceContainer::LoadDocument failed"); + return; + } std::string dstUrl = url + COMPONENT_PREVIEW + componentName; taskExecutor_->PostTask( - [front = frontend, componentName, dstUrl]() { + [front = frontend, componentName, dstUrl, jsEngine]() { front->SetPagePath(dstUrl); - front->ReplaceJSContent(dstUrl, componentName); + jsEngine->ReplaceJSContent(dstUrl, componentName); }, TaskExecutor::TaskType::JS); } diff --git a/adapter/preview/entrance/samples/BUILD.gn b/adapter/preview/entrance/samples/BUILD.gn index e6fc9ce0..5b879215 100644 --- a/adapter/preview/entrance/samples/BUILD.gn +++ b/adapter/preview/entrance/samples/BUILD.gn @@ -13,6 +13,7 @@ import("//build/ohos.gni") import("//foundation/ace/ace_engine/ace_config.gni") +import("//foundation/ace/ace_engine/adapter/preview/build/config_js_engine.gni") config("ace_pc_preview_config") { cflags_cc = [ @@ -39,12 +40,19 @@ template("ace_test") { deps = [ ":copy_napi_modules_shared_library_for_test(${current_toolchain})", - ":copy_napi_shared_library_for_test(${current_toolchain})", ":copy_resource_dynamic_library(${current_toolchain})", "$ace_napi:ace_napi(${current_toolchain})", "$ace_napi/sample/native_module_demo:demo(${current_toolchain})", "$ace_root/adapter/preview/build:libace_engine_$platform(${current_toolchain})", ] + if (enable_ark_preview) { + deps += [ + ":copy_ark_shared_library(${current_toolchain})", + ":copy_napi_shared_library(${current_toolchain})", + ] + } else { + deps += [ ":copy_napi_shared_library_for_test(${current_toolchain})" ] + } if (platform == "windows") { libs = [ "pthread" ] } @@ -84,64 +92,73 @@ foreach(item, ace_platforms) { } } -ohos_copy("copy_resource_dynamic_library") { - if (use_mac) { - if (is_standard_system) { - resource_manager_library = get_label_info( - "//base/global/resmgr_standard/frameworks/resmgr:global_resmgr_mac", - "root_out_dir") + "/global/resmgr_standard/libglobal_resmgr_mac.dylib" - deps = [ "//base/global/resmgr_standard/frameworks/resmgr:global_resmgr_mac(${current_toolchain})" ] - sources = [ resource_manager_library ] - } else { - sources = [ - "//prebuilts/ace-toolkit/preview/rich/lib/mac/tv/libhmicuuc.dylib", - "//prebuilts/ace-toolkit/preview/rich/lib/mac/tv/libresourcemanager_mac.dylib", - ] - } - outputs = - [ root_build_dir + "/clang_x64/common/common/{{source_file_part}}" ] - } else { - if (is_standard_system) { - resource_manager_library = get_label_info( - "//base/global/resmgr_standard/frameworks/resmgr:global_resmgr_win", - "root_out_dir") + "/global/resmgr_standard/libglobal_resmgr_win.dll" - deps = [ "//base/global/resmgr_standard/frameworks/resmgr:global_resmgr_win(${current_toolchain})" ] - sources = [ resource_manager_library ] - } else { - sources = [ - "//prebuilts/ace-toolkit/preview/rich/lib/windows/tv/libhmicuuc.dll", - "//prebuilts/ace-toolkit/preview/rich/lib/windows/tv/libresourcemanager_win.dll", - ] - } +if (enable_ark_preview) { + ohos_copy("copy_ark_shared_library") { + sources = [] + deps = [] + ark_core_shared_library = + get_label_info("//ark/runtime_core/libpandabase:libarkbase", + "root_out_dir") + "/ark/ark" + sources += [ + "$ark_core_shared_library/libarkbase.dll", + "$ark_core_shared_library/libarkfile.dll", + "$ark_core_shared_library/libarkziparchive.dll", + ] + deps += [ + "//ark/runtime_core/libpandabase:libarkbase(${current_toolchain})", + "//ark/runtime_core/libpandafile:libarkfile(${current_toolchain})", + "//ark/runtime_core/libziparchive:libarkziparchive(${current_toolchain})", + ] + ark_js_shared_library = + get_label_info("//ark/js_runtime:libark_jsruntime", "root_out_dir") + + "/ark/ark_js_runtime" + sources += [ "$ark_js_shared_library/libark_jsruntime.dll" ] + deps += [ "//ark/js_runtime:libark_jsruntime" ] outputs = [ root_build_dir + "/mingw_x86_64/common/common/{{source_file_part}}" ] } -} - -ohos_copy("copy_napi_shared_library_for_test") { - shared_library_path = - get_label_info("$ace_napi:ace_napi(${current_toolchain})", "root_out_dir") - deps = [ - "$ace_napi:ace_napi(${current_toolchain})", - "$ace_napi:ace_napi_quickjs(${current_toolchain})", - ] - if (use_mac) { - sources = [ - "$shared_library_path/ace/napi/libace_napi.dylib", - "$shared_library_path/ace/napi/libace_napi_quickjs.dylib", - "$shared_library_path/ace/napi/libuv.dylib", - ] - outputs = - [ root_build_dir + "/clang_x64/common/common/{{source_file_part}}" ] - } else { + ohos_copy("copy_napi_shared_library") { + shared_library_path = + get_label_info("$ace_napi:ace_napi(${current_toolchain})", + "root_out_dir") sources = [ "$shared_library_path/ace/napi/libace_napi.dll", - "$shared_library_path/ace/napi/libace_napi_quickjs.dll", - "$shared_library_path/ace/napi/libuv.dll", + "$shared_library_path/ace/napi/libace_napi_ark.dll", + ] + deps = [ + "$ace_napi:ace_napi(${current_toolchain})", + "$ace_napi:ace_napi_ark(${current_toolchain})", ] outputs = [ root_build_dir + "/mingw_x86_64/common/common/{{source_file_part}}" ] } +} else { + ohos_copy("copy_napi_shared_library_for_test") { + shared_library_path = + get_label_info("$ace_napi:ace_napi(${current_toolchain})", + "root_out_dir") + deps = [ + "$ace_napi:ace_napi(${current_toolchain})", + "$ace_napi:ace_napi_quickjs(${current_toolchain})", + ] + if (use_mac) { + sources = [ + "$shared_library_path/ace/napi/libace_napi.dylib", + "$shared_library_path/ace/napi/libace_napi_quickjs.dylib", + "$shared_library_path/ace/napi/libuv.dylib", + ] + outputs = + [ root_build_dir + "/clang_x64/common/common/{{source_file_part}}" ] + } else { + sources = [ + "$shared_library_path/ace/napi/libace_napi.dll", + "$shared_library_path/ace/napi/libace_napi_quickjs.dll", + "$shared_library_path/ace/napi/libuv.dll", + ] + outputs = [ root_build_dir + + "/mingw_x86_64/common/common/{{source_file_part}}" ] + } + } } ohos_copy("copy_napi_modules_shared_library_for_test") { @@ -191,3 +208,37 @@ ohos_copy("copy_napi_modules_shared_library_for_test") { "/mingw_x86_64/common/common/module/{{source_file_part}}" ] } } + +ohos_copy("copy_resource_dynamic_library") { + if (use_mac) { + if (is_standard_system) { + resource_manager_library = get_label_info( + "//base/global/resmgr_standard/frameworks/resmgr:global_resmgr_mac", + "root_out_dir") + "/global/resmgr_standard/libglobal_resmgr_mac.dylib" + deps = [ "//base/global/resmgr_standard/frameworks/resmgr:global_resmgr_mac(${current_toolchain})" ] + sources = [ resource_manager_library ] + } else { + sources = [ + "//prebuilts/ace-toolkit/preview/rich/lib/mac/tv/libhmicuuc.dylib", + "//prebuilts/ace-toolkit/preview/rich/lib/mac/tv/libresourcemanager_mac.dylib", + ] + } + outputs = + [ root_build_dir + "/clang_x64/common/common/{{source_file_part}}" ] + } else { + if (is_standard_system) { + resource_manager_library = get_label_info( + "//base/global/resmgr_standard/frameworks/resmgr:global_resmgr_win", + "root_out_dir") + "/global/resmgr_standard/libglobal_resmgr_win.dll" + deps = [ "//base/global/resmgr_standard/frameworks/resmgr:global_resmgr_win(${current_toolchain})" ] + sources = [ resource_manager_library ] + } else { + sources = [ + "//prebuilts/ace-toolkit/preview/rich/lib/windows/tv/libhmicuuc.dll", + "//prebuilts/ace-toolkit/preview/rich/lib/windows/tv/libresourcemanager_win.dll", + ] + } + outputs = + [ root_build_dir + "/mingw_x86_64/common/common/{{source_file_part}}" ] + } +} diff --git a/build/ace_lib.gni b/build/ace_lib.gni index 8a626f6f..fa6d07a4 100644 --- a/build/ace_lib.gni +++ b/build/ace_lib.gni @@ -29,7 +29,7 @@ template("libace_static") { deps += config.platform_deps } - # only qjs support build-in + # build-in qjs engine if (defined(config.use_build_in_js_engine) && config.use_build_in_js_engine && defined(config.qjs_engine)) { if (defined(use_js_debug) && use_js_debug) { @@ -47,6 +47,15 @@ template("libace_static") { } } } + + # build-in ark js engine for preview + if (defined(config.use_build_in_js_engine) && + config.use_build_in_js_engine && defined(config.ark_engine)) { + deps += [ + "$ace_root/frameworks/bridge/declarative_frontend:declarative_js_engine_ark_$platform", + "$ace_root/frameworks/bridge/js_frontend/engine:js_engine_ark_$platform", + ] + } part_name = ace_engine_part } } diff --git a/frameworks/bridge/declarative_frontend/BUILD.gn b/frameworks/bridge/declarative_frontend/BUILD.gn index c712adfe..6690f7e0 100644 --- a/frameworks/bridge/declarative_frontend/BUILD.gn +++ b/frameworks/bridge/declarative_frontend/BUILD.gn @@ -90,7 +90,9 @@ template("declarative_js_engine") { part_name = ace_engine_part defines += invoker.defines if (target_cpu == "arm64") { - defines += [ "_ARM64_" ] + if (!is_mingw) { + defines += [ "_ARM64_" ] + } } deps = [] diff --git a/frameworks/bridge/declarative_frontend/declarative_frontend.cpp b/frameworks/bridge/declarative_frontend/declarative_frontend.cpp index 394b9af5..78554e8b 100644 --- a/frameworks/bridge/declarative_frontend/declarative_frontend.cpp +++ b/frameworks/bridge/declarative_frontend/declarative_frontend.cpp @@ -568,16 +568,6 @@ void DeclarativeFrontend::TransferJsResponseDataPreview(int callbackId, int32_t { delegate_->TransferJsResponseDataPreview(callbackId, code, responseData); } - -void DeclarativeFrontend::ReplaceJSContent(const std::string& url, const std::string componentName) const -{ - auto jsEngineInstance = AceType::DynamicCast(jsEngine_); - if (!jsEngineInstance) { - LOGE("jsEngineInstance is null"); - return; - } - jsEngineInstance->ReplaceJSContent(url, componentName); -} #endif void DeclarativeFrontend::TransferJsPluginGetError(int callbackId, int32_t errorCode, std::string&& errorMessage) const diff --git a/frameworks/bridge/declarative_frontend/declarative_frontend.h b/frameworks/bridge/declarative_frontend/declarative_frontend.h index 0a231f61..f3c1053c 100644 --- a/frameworks/bridge/declarative_frontend/declarative_frontend.h +++ b/frameworks/bridge/declarative_frontend/declarative_frontend.h @@ -87,7 +87,6 @@ public: } void TransferJsResponseDataPreview(int32_t callbackId, int32_t code, ResponseData responseData) const; - void ReplaceJSContent(const std::string& url, const std::string componentName) const; #endif void TransferJsPluginGetError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override; void TransferJsEventData(int32_t callbackId, int32_t code, std::vector&& data) const override; diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/BUILD.gn b/frameworks/bridge/declarative_frontend/engine/jsi/BUILD.gn index 2061abf3..04b5e305 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/BUILD.gn +++ b/frameworks/bridge/declarative_frontend/engine/jsi/BUILD.gn @@ -42,7 +42,7 @@ ts2abc_gen_abc("gen_jsEnumStyle_abc") { gen_obj("abc_proxyclass") { input = base_output_path + "/stateMgmt.abc" - if (use_mac) { + if (use_mac || use_mingw_win) { abcproxyclass_obj_path = base_output_path + "/js_proxy_class.c" } output = abcproxyclass_obj_path @@ -75,13 +75,9 @@ template("declarative_js_engine_ark") { public_configs = [ "//ark/js_runtime:ark_jsruntime_public_config" ] sources = [ - "$ace_root/frameworks/bridge/js_frontend/engine/jsi/jsi_base_utils.cpp", - "ark/ark_js_runtime.cpp", - "ark/ark_js_value.cpp", "js_converter.cpp", "jsi_declarative_engine.cpp", "jsi_declarative_group_js_bridge.cpp", - "jsi_declarative_utils.cpp", "jsi_object_template.cpp", "jsi_types.cpp", "jsi_view_register.cpp", @@ -95,16 +91,28 @@ template("declarative_js_engine_ark") { "modules/jsi_timer_module.cpp", ] + if (!defined(config.build_for_preview) || !config.build_for_preview) { + sources += [ + "$ace_root/frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.cpp", + "$ace_root/frameworks/bridge/js_frontend/engine/jsi/ark_js_value.cpp", + "$ace_root/frameworks/bridge/js_frontend/engine/jsi/jsi_base_utils.cpp", + "jsi_declarative_utils.cpp", + ] + } + deps = [ "//ark/js_runtime:libark_jsruntime" ] # if napi support deps += [ "//foundation/ace/napi:ace_napi_ark" ] public_deps = [ "//foundation/ace/napi:ace_napi" ] - deps += [ - ":gen_obj_src_abc_enum_style", - ":gen_obj_src_abc_proxyclass", - ] + # do not support windows platform + if (!is_mingw) { + deps += [ + ":gen_obj_src_abc_enum_style", + ":gen_obj_src_abc_proxyclass", + ] + } } } diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_runtime.cpp b/frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_runtime.cpp deleted file mode 100644 index 2e175301..00000000 --- a/frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_runtime.cpp +++ /dev/null @@ -1,224 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_runtime.h" - -#include "base/log/log.h" -#include "base/utils/system_properties.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_value.h" - -// NOLINTNEXTLINE(readability-identifier-naming) -namespace OHOS::Ace::Framework { -static constexpr auto PANDA_MAIN_FUNCTION = "_GLOBAL::func_main_0"; - -Local FunctionCallback(EcmaVM *vm, Local thisValue, - const Local argument[], // NOLINT(modernize-avoid-c-arrays) - int32_t length, void *data) -{ - auto package = reinterpret_cast(data); - if (package == nullptr) { - return JSValueRef::Undefined(vm); - } - return package->Callback(thisValue, argument, length); -} - -bool ArkJSRuntime::Initialize(const std::string &libraryPath, bool isDebugMode) -{ - RuntimeOption option; - option.SetGcType(RuntimeOption::GC_TYPE::GEN_GC); - option.SetArkProperties(SystemProperties::GetArkProperties()); - option.SetAsmInterOption(SystemProperties::GetAsmInterOption()); - const int64_t poolSize = 0x10000000; // 256M - option.SetGcPoolSize(poolSize); - option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); - option.SetLogBufPrint(print_); - option.SetDebuggerLibraryPath(libraryPath); - libPath_ = libraryPath; - isDebugMode_ = isDebugMode; - - vm_ = JSNApi::CreateJSVM(option); - return vm_ != nullptr; -} - -bool ArkJSRuntime::InitializeFromExistVM(EcmaVM* vm) -{ - vm_ = vm; - usingExistVM_ = true; - LOGI("InitializeFromExistVM %{public}p", vm); - return vm_ != nullptr; -} - -void ArkJSRuntime::Reset() -{ - if (vm_ != nullptr) { - if (!usingExistVM_) { - JSNApi::StopDebugger(libPath_.c_str()); - JSNApi::DestroyJSVM(vm_); - } - vm_ = nullptr; - } - for (auto data : dataList_) { - delete data; - } - dataList_.clear(); -} - -void ArkJSRuntime::SetLogPrint(LOG_PRINT out) -{ - print_ = out; -} - -shared_ptr ArkJSRuntime::EvaluateJsCode([[maybe_unused]] const std::string &src) -{ - return NewUndefined(); -} - -bool ArkJSRuntime::EvaluateJsCode(const uint8_t *buffer, int32_t size) -{ - JSExecutionScope executionScope(vm_); - LocalScope scope(vm_); - bool ret = JSNApi::Execute(vm_, buffer, size, PANDA_MAIN_FUNCTION); - HandleUncaughtException(); - return ret; -} - -bool ArkJSRuntime::ExecuteJsBin(const std::string &fileName) -{ - JSExecutionScope executionScope(vm_); - static bool debugFlag = true; - if (debugFlag && !libPath_.empty()) { - JSNApi::StartDebugger(libPath_.c_str(), vm_, isDebugMode_); - debugFlag = false; - } - LocalScope scope(vm_); - bool ret = JSNApi::Execute(vm_, fileName, PANDA_MAIN_FUNCTION); - HandleUncaughtException(); - return ret; -} - -shared_ptr ArkJSRuntime::GetGlobal() -{ - LocalScope scope(vm_); - return std::make_shared(shared_from_this(), JSNApi::GetGlobalObject(vm_)); -} - -void ArkJSRuntime::RunGC() -{ - JSExecutionScope executionScope(vm_); - LocalScope scope(vm_); - JSNApi::TriggerGC(vm_); -} - -shared_ptr ArkJSRuntime::NewInt32(int32_t value) -{ - LocalScope scope(vm_); - return std::make_shared(shared_from_this(), IntegerRef::New(vm_, value)); -} - -shared_ptr ArkJSRuntime::NewBoolean(bool value) -{ - LocalScope scope(vm_); - return std::make_shared(shared_from_this(), BooleanRef::New(vm_, value)); -} - -shared_ptr ArkJSRuntime::NewNumber(double d) -{ - LocalScope scope(vm_); - return std::make_shared(shared_from_this(), NumberRef::New(vm_, d)); -} - -shared_ptr ArkJSRuntime::NewNull() -{ - LocalScope scope(vm_); - return std::make_shared(shared_from_this(), JSValueRef::Null(vm_)); -} - -shared_ptr ArkJSRuntime::NewUndefined() -{ - LocalScope scope(vm_); - return std::make_shared(shared_from_this(), JSValueRef::Undefined(vm_)); -} - -shared_ptr ArkJSRuntime::NewString(const std::string &str) -{ - LocalScope scope(vm_); - return std::make_shared(shared_from_this(), StringRef::NewFromUtf8(vm_, str.c_str())); -} - -shared_ptr ArkJSRuntime::ParseJson(const std::string &str) -{ - LocalScope scope(vm_); - Local string = StringRef::NewFromUtf8(vm_, str.c_str()); - return std::make_shared(shared_from_this(), JSON::Parse(vm_, string)); -} - -shared_ptr ArkJSRuntime::NewObject() -{ - LocalScope scope(vm_); - return std::make_shared(shared_from_this(), ObjectRef::New(vm_)); -} - -shared_ptr ArkJSRuntime::NewArray() -{ - LocalScope scope(vm_); - return std::make_shared(shared_from_this(), ArrayRef::New(vm_)); -} - -shared_ptr ArkJSRuntime::NewFunction(RegisterFunctionType func) -{ - LocalScope scope(vm_); - auto data = new PandaFunctionData(shared_from_this(), func); - dataList_.emplace_back(data); - return std::make_shared(shared_from_this(), FunctionRef::New(vm_, FunctionCallback, data)); -} - -shared_ptr ArkJSRuntime::NewNativePointer(void *ptr) -{ - LocalScope scope(vm_); - return std::make_shared(shared_from_this(), NativePointerRef::New(vm_, ptr)); -} - -void ArkJSRuntime::RegisterUncaughtExceptionHandler(UncaughtExceptionCallback callback) -{ - JSNApi::EnableUserUncaughtErrorHandler(vm_); - uncaughtErrorHandler_ = callback; -} - -void ArkJSRuntime::HandleUncaughtException() -{ - Local exception = JSNApi::GetAndClearUncaughtException(vm_); - if (!exception.IsEmpty() && !exception->IsHole() && uncaughtErrorHandler_ != nullptr) { - shared_ptr errorPtr = - std::static_pointer_cast(std::make_shared(shared_from_this(), exception)); - uncaughtErrorHandler_(errorPtr, shared_from_this()); - } -} - -Local PandaFunctionData::Callback(const Local &thisValue, - const Local argument[], // NOLINT(modernize-avoid-c-arrays) - int32_t length) const -{ - EscapeLocalScope scope(runtime_->GetEcmaVm()); - shared_ptr thisPtr = std::static_pointer_cast(std::make_shared(runtime_, thisValue)); - - std::vector> argv; - argv.reserve(length); - for (int i = 0; i < length; ++i) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - argv.emplace_back(std::static_pointer_cast(std::make_shared(runtime_, argument[i]))); - } - shared_ptr result = func_(runtime_, thisPtr, argv, length); - return scope.Escape(std::static_pointer_cast(result)->GetValue(runtime_)); -} -} // namespace OHOS::Ace::Framework diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_runtime.h b/frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_runtime.h deleted file mode 100644 index 379db4f9..00000000 --- a/frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_runtime.h +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JSI_ARK_ARK_JS_RUNTIME_H -#define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JSI_ARK_ARK_JS_RUNTIME_H - -#include - -#include "ecmascript/napi/include/jsnapi.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_runtime.h" - -namespace panda::ecmascript { -class EcmaVM; -} // namespace panda::ecmascript - -// NOLINTNEXTLINE(readability-identifier-naming) -namespace OHOS::Ace::Framework { -using panda::ArrayRef; -using panda::BooleanRef; -using panda::EscapeLocalScope; -using panda::FunctionRef; -using panda::Global; -using panda::IntegerRef; -using panda::JSExecutionScope; -using panda::JSNApi; -using panda::JSON; -using panda::JSValueRef; -using panda::Local; -using panda::LocalScope; -using panda::NativePointerRef; -using panda::NumberRef; -using panda::ObjectRef; -using panda::RuntimeOption; -using panda::StringRef; -using panda::ecmascript::EcmaVM; -class PandaFunctionData; - -// NOLINTNEXTLINE(fuchsia-multiple-inheritance) -class ArkJSRuntime final : public JsRuntime, public std::enable_shared_from_this { -public: - bool StartDebugger(const char *libraryPath, EcmaVM *vm) const; - bool Initialize(const std::string &libraryPath, bool isDebugMode) override; - bool InitializeFromExistVM(EcmaVM* vm); - void Reset() override; - void SetLogPrint(LOG_PRINT out) override; - shared_ptr EvaluateJsCode(const std::string &src) override; - bool EvaluateJsCode(const uint8_t *buffer, int32_t size) override; - bool ExecuteJsBin(const std::string &fileName) override; - shared_ptr GetGlobal() override; - void RunGC() override; - - shared_ptr NewNumber(double d) override; - shared_ptr NewInt32(int32_t value) override; - shared_ptr NewBoolean(bool value) override; - shared_ptr NewNull() override; - shared_ptr NewUndefined() override; - shared_ptr NewString(const std::string &str) override; - shared_ptr ParseJson(const std::string &str) override; - shared_ptr NewObject() override; - shared_ptr NewArray() override; - shared_ptr NewFunction(RegisterFunctionType func) override; - shared_ptr NewNativePointer(void *ptr) override; - void RegisterUncaughtExceptionHandler(UncaughtExceptionCallback callback) override; - void HandleUncaughtException() override; - - const EcmaVM *GetEcmaVm() const - { - return vm_; - } - -private: - EcmaVM *vm_ = nullptr; - std::vector dataList_; - LOG_PRINT print_ { nullptr }; - UncaughtExceptionCallback uncaughtErrorHandler_ { nullptr }; - std::string libPath_ {}; - bool usingExistVM_ = false; - bool isDebugMode_ = true; -}; - -class PandaFunctionData { -public: - PandaFunctionData(shared_ptr runtime, RegisterFunctionType func) - : runtime_(std::move(runtime)), func_(std::move(func)) - { - } - - ~PandaFunctionData() = default; - - NO_COPY_SEMANTIC(PandaFunctionData); - NO_MOVE_SEMANTIC(PandaFunctionData); - -private: - Local Callback(const Local &thisValue, - const Local argument[], // NOLINT(modernize-avoid-c-arrays) - int32_t length) const; - shared_ptr runtime_; - RegisterFunctionType func_; - friend Local FunctionCallback(EcmaVM *vm, Local thisValue, - const Local argument[], // NOLINT(modernize-avoid-c-arrays) - int32_t length, void *data); -}; -} // namespace OHOS::Ace::Framework -#endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JSI_ARK_ARK_JS_RUNTIME_H diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_value.cpp b/frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_value.cpp deleted file mode 100644 index 063c29d5..00000000 --- a/frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_value.cpp +++ /dev/null @@ -1,328 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_value.h" - -#include - -// NOLINTNEXTLINE(readability-identifier-naming) -namespace OHOS::Ace::Framework { -int32_t ArkJSValue::ToInt32(shared_ptr runtime) -{ - shared_ptr pandaRuntime = std::static_pointer_cast(runtime); - LocalScope scope(pandaRuntime->GetEcmaVm()); - if (value_.CheckException()) { - return 0; - } - return value_->Int32Value(pandaRuntime->GetEcmaVm()); -} - -double ArkJSValue::ToDouble(shared_ptr runtime) -{ - shared_ptr pandaRuntime = std::static_pointer_cast(runtime); - LocalScope scope(pandaRuntime->GetEcmaVm()); - if (value_.CheckException()) { - return 0; - } - Local number = value_->ToNumber(pandaRuntime->GetEcmaVm()); - if (!number.CheckException()) { - return number->Value(); - } - return 0; -} - -std::string ArkJSValue::ToString(shared_ptr runtime) -{ - shared_ptr pandaRuntime = std::static_pointer_cast(runtime); - LocalScope scope(pandaRuntime->GetEcmaVm()); - if (value_.CheckException()) { - return ""; - } - Local string = value_->ToString(pandaRuntime->GetEcmaVm()); - if (!string.CheckException()) { - return string->ToString(); - } - return ""; -} - -bool ArkJSValue::ToBoolean(shared_ptr runtime) -{ - shared_ptr pandaRuntime = std::static_pointer_cast(runtime); - LocalScope scope(pandaRuntime->GetEcmaVm()); - return !value_.CheckException() && value_->BooleaValue(); -} - -bool ArkJSValue::IsUndefined([[maybe_unused]] shared_ptr runtime) -{ - return !value_.IsEmpty() && value_->IsUndefined(); -} - -bool ArkJSValue::IsNull([[maybe_unused]] shared_ptr runtime) -{ - return !value_.IsEmpty() && value_->IsNull(); -} - -bool ArkJSValue::IsBoolean([[maybe_unused]] shared_ptr runtime) -{ - return !value_.IsEmpty() && value_->IsBoolean(); -} - -bool ArkJSValue::IsInt32([[maybe_unused]] shared_ptr runtime) -{ - return !value_.IsEmpty() && value_->IsInt(); -} - -bool ArkJSValue::WithinInt32([[maybe_unused]] shared_ptr runtime) -{ - return !value_.IsEmpty() && value_->WithinInt32(); -} - -bool ArkJSValue::IsString([[maybe_unused]] shared_ptr runtime) -{ - return !value_.IsEmpty() && value_->IsString(); -} - -bool ArkJSValue::IsNumber([[maybe_unused]] shared_ptr runtime) -{ - return !value_.IsEmpty() && value_->IsNumber(); -} - -bool ArkJSValue::IsObject([[maybe_unused]] shared_ptr runtime) -{ - return !value_.IsEmpty() && value_->IsObject(); -} - -bool ArkJSValue::IsArray([[maybe_unused]] shared_ptr runtime) -{ - shared_ptr pandaRuntime = std::static_pointer_cast(runtime); - return !value_.IsEmpty() && value_->IsArray(pandaRuntime->GetEcmaVm()); -} - -bool ArkJSValue::IsFunction([[maybe_unused]] shared_ptr runtime) -{ - return !value_.IsEmpty() && value_->IsFunction(); -} - -// NOLINTNEXTLINE(performance-unnecessary-value-param) -bool ArkJSValue::IsException([[maybe_unused]] shared_ptr runtime) -{ - return value_.IsEmpty() || value_->IsException(); -} - -shared_ptr ArkJSValue::Call(shared_ptr runtime, shared_ptr thisObj, - std::vector> argv, int32_t argc) -{ - shared_ptr pandaRuntime = std::static_pointer_cast(runtime); - JSExecutionScope executionScope(pandaRuntime->GetEcmaVm()); - LocalScope scope(pandaRuntime->GetEcmaVm()); - if (!IsFunction(pandaRuntime)) { - return std::make_shared(pandaRuntime, JSValueRef::Exception(pandaRuntime->GetEcmaVm())); - } - std::vector> arguments; - arguments.reserve(argc); - for (const shared_ptr &arg : argv) { - arguments.emplace_back(std::static_pointer_cast(arg)->GetValue(pandaRuntime)); - } - Local thisValue = std::static_pointer_cast(thisObj)->GetValue(pandaRuntime); - Local function(GetValue(pandaRuntime)); - Local result = function->Call(pandaRuntime->GetEcmaVm(), thisValue, arguments.data(), argc); - runtime->HandleUncaughtException(); - return std::make_shared(pandaRuntime, result); -} - -bool ArkJSValue::GetPropertyNames(shared_ptr runtime, shared_ptr &propName, int32_t &len) -{ - shared_ptr pandaRuntime = std::static_pointer_cast(runtime); - LocalScope scope(pandaRuntime->GetEcmaVm()); - if (value_.CheckException()) { - return false; - } - Local obj = value_->ToObject(pandaRuntime->GetEcmaVm()); - if (obj.CheckException()) { - return false; - } - Local names = obj->GetOwnPropertyNames(pandaRuntime->GetEcmaVm()); - len = names->Length(pandaRuntime->GetEcmaVm()); - if (!propName) { - propName = std::make_shared(pandaRuntime, names); - } else { - std::static_pointer_cast(propName)->SetValue(pandaRuntime, names); - } - return true; -} - -bool ArkJSValue::GetEnumerablePropertyNames(shared_ptr runtime, shared_ptr &propName, int32_t &len) -{ - shared_ptr pandaRuntime = std::static_pointer_cast(runtime); - LocalScope scope(pandaRuntime->GetEcmaVm()); - if (value_.CheckException()) { - return false; - } - Local obj = value_->ToObject(pandaRuntime->GetEcmaVm()); - if (obj.CheckException()) { - return false; - } - Local names = obj->GetOwnEnumerablePropertyNames(pandaRuntime->GetEcmaVm()); - len = names->Length(pandaRuntime->GetEcmaVm()); - if (!propName) { - propName = std::make_shared(pandaRuntime, names); - } else { - std::static_pointer_cast(propName)->SetValue(pandaRuntime, names); - } - return true; -} - -shared_ptr ArkJSValue::GetProperty(shared_ptr runtime, int32_t idx) -{ - shared_ptr pandaRuntime = std::static_pointer_cast(runtime); - LocalScope scope(pandaRuntime->GetEcmaVm()); - if (value_.CheckException()) { - return std::make_shared(pandaRuntime, JSValueRef::Exception(pandaRuntime->GetEcmaVm())); - } - Local obj = value_->ToObject(pandaRuntime->GetEcmaVm()); - if (obj.CheckException()) { - return std::make_shared(pandaRuntime, JSValueRef::Exception(pandaRuntime->GetEcmaVm())); - } - Local property = obj->Get(pandaRuntime->GetEcmaVm(), idx); - if (property.CheckException()) { - return std::make_shared(pandaRuntime, JSValueRef::Exception(pandaRuntime->GetEcmaVm())); - } - return std::make_shared(pandaRuntime, property); -} - -shared_ptr ArkJSValue::GetProperty(shared_ptr runtime, const std::string &name) -{ - shared_ptr pandaRuntime = std::static_pointer_cast(runtime); - LocalScope scope(pandaRuntime->GetEcmaVm()); - shared_ptr key = pandaRuntime->NewString(name); - return GetProperty(runtime, key); -} - -shared_ptr ArkJSValue::GetProperty(shared_ptr runtime, const shared_ptr &name) -{ - shared_ptr pandaRuntime = std::static_pointer_cast(runtime); - LocalScope scope(pandaRuntime->GetEcmaVm()); - if (value_.CheckException()) { - return std::make_shared(pandaRuntime, JSValueRef::Exception(pandaRuntime->GetEcmaVm())); - } - Local obj = value_->ToObject(pandaRuntime->GetEcmaVm()); - if (obj.CheckException()) { - return std::make_shared(pandaRuntime, JSValueRef::Exception(pandaRuntime->GetEcmaVm())); - } - Local key = std::static_pointer_cast(name)->GetValue(pandaRuntime); - Local property = obj->Get(pandaRuntime->GetEcmaVm(), key); - if (property.CheckException()) { - return std::make_shared(pandaRuntime, JSValueRef::Exception(pandaRuntime->GetEcmaVm())); - } - return std::make_shared(pandaRuntime, property); -} - -bool ArkJSValue::SetProperty(shared_ptr runtime, const std::string &name, const shared_ptr &value) -{ - shared_ptr pandaRuntime = std::static_pointer_cast(runtime); - LocalScope scope(pandaRuntime->GetEcmaVm()); - shared_ptr key = pandaRuntime->NewString(name); - return SetProperty(runtime, key, value); -} - -bool ArkJSValue::SetProperty(shared_ptr runtime, const shared_ptr &name, - const shared_ptr &value) -{ - shared_ptr pandaRuntime = std::static_pointer_cast(runtime); - LocalScope scope(pandaRuntime->GetEcmaVm()); - if (value_.CheckException()) { - return false; - } - Local obj = value_->ToObject(pandaRuntime->GetEcmaVm()); - if (obj.CheckException()) { - return false; - } - Local key = std::static_pointer_cast(name)->GetValue(pandaRuntime); - Local value_ref = std::static_pointer_cast(value)->GetValue(pandaRuntime); - return obj->Set(pandaRuntime->GetEcmaVm(), key, value_ref); -} - -bool ArkJSValue::SetAccessorProperty(shared_ptr runtime, const std::string &name, - const shared_ptr &getter, const shared_ptr &setter) -{ - shared_ptr pandaRuntime = std::static_pointer_cast(runtime); - LocalScope scope(pandaRuntime->GetEcmaVm()); - shared_ptr key = pandaRuntime->NewString(name); - return SetAccessorProperty(runtime, key, getter, setter); -} - -bool ArkJSValue::SetAccessorProperty(shared_ptr runtime, const shared_ptr &name, - const shared_ptr &getter, const shared_ptr &setter) -{ - shared_ptr pandaRuntime = std::static_pointer_cast(runtime); - LocalScope scope(pandaRuntime->GetEcmaVm()); - if (value_.CheckException()) { - return false; - } - Local obj = value_->ToObject(pandaRuntime->GetEcmaVm()); - if (obj.CheckException()) { - return false; - } - Local key = std::static_pointer_cast(name)->GetValue(pandaRuntime); - Local getterValue = std::static_pointer_cast(getter)->GetValue(pandaRuntime); - Local setterValue = std::static_pointer_cast(setter)->GetValue(pandaRuntime); - return obj->SetAccessorProperty(pandaRuntime->GetEcmaVm(), key, getterValue, setterValue); -} - -int32_t ArkJSValue::GetArrayLength(shared_ptr runtime) -{ - shared_ptr pandaRuntime = std::static_pointer_cast(runtime); - LocalScope scope(pandaRuntime->GetEcmaVm()); - if (value_.CheckException()) { - return -1; - } - Local array(GetValue(pandaRuntime)); - return array->Length(pandaRuntime->GetEcmaVm()); -} - -shared_ptr ArkJSValue::GetElement(shared_ptr runtime, int32_t idx) -{ - shared_ptr pandaRuntime = std::static_pointer_cast(runtime); - LocalScope scope(pandaRuntime->GetEcmaVm()); - if (value_.CheckException()) { - return std::make_shared(pandaRuntime, JSValueRef::Exception(pandaRuntime->GetEcmaVm())); - } - Local obj(GetValue(pandaRuntime)); - if (obj.CheckException()) { - return std::make_shared(pandaRuntime, JSValueRef::Exception(pandaRuntime->GetEcmaVm())); - } - Local property = obj->Get(pandaRuntime->GetEcmaVm(), idx); - if (property.CheckException()) { - return std::make_shared(pandaRuntime, JSValueRef::Exception(pandaRuntime->GetEcmaVm())); - } - return std::make_shared(pandaRuntime, property); -} - -std::string ArkJSValue::GetJsonString(const shared_ptr& runtime) -{ - shared_ptr pandaRuntime = std::static_pointer_cast(runtime); - LocalScope scope(pandaRuntime->GetEcmaVm()); - auto stringify = panda::JSON::Stringify(pandaRuntime->GetEcmaVm(), GetValue(pandaRuntime)); - if (stringify.CheckException()) { - return ""; - } - auto valueStr = panda::Local(stringify); - if (valueStr.CheckException()) { - return ""; - } - return valueStr->ToString(); -} - -} // namespace OHOS::Ace::Framework \ No newline at end of file diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_value.h b/frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_value.h deleted file mode 100644 index 1a45c741..00000000 --- a/frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_value.h +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JSI_ARK_ARK_JS_VALUE_H -#define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JSI_ARK_ARK_JS_VALUE_H - -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_runtime.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_value.h" - -// NOLINTNEXTLINE(readability-identifier-naming) -namespace OHOS::Ace::Framework { -using panda::ArrayRef; -using panda::EscapeLocalScope; -using panda::FunctionRef; -using panda::Global; -using panda::JSON; -using panda::JSValueRef; -using panda::Local; -using panda::LocalScope; -using panda::NumberRef; -using panda::ObjectRef; -using panda::StringRef; -class PandaFunctionData; - -// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions, hicpp-special-member-functions) -class ArkJSValue final : public JsValue { -public: - ArkJSValue(const shared_ptr &runtime, Local value) : value_(runtime->GetEcmaVm(), value) - { - } - ~ArkJSValue() override - { - value_.FreeGlobalHandleAddr(); - } - - int32_t ToInt32(shared_ptr runtime) override; - double ToDouble(shared_ptr runtime) override; - std::string ToString(shared_ptr runtime) override; - bool ToBoolean(shared_ptr runtime) override; - - bool IsUndefined(shared_ptr runtime) override; - bool IsNull(shared_ptr runtime) override; - bool IsBoolean(shared_ptr runtime) override; - bool IsInt32(shared_ptr runtime) override; - bool WithinInt32(shared_ptr runtime) override; - bool IsString(shared_ptr runtime) override; - bool IsNumber(shared_ptr runtime) override; - bool IsObject(shared_ptr runtime) override; - bool IsArray(shared_ptr runtime) override; - bool IsFunction(shared_ptr runtime) override; - bool IsException(shared_ptr runtime) override; - - shared_ptr Call(shared_ptr runtime, shared_ptr thisObj, - std::vector> argv, int32_t argc) override; - - bool GetPropertyNames(shared_ptr runtime, shared_ptr &propName, int32_t &len) override; - bool GetEnumerablePropertyNames(shared_ptr runtime, - shared_ptr &propName, int32_t &len) override; - shared_ptr GetProperty(shared_ptr runtime, int32_t idx) override; - shared_ptr GetProperty(shared_ptr runtime, const std::string &name) override; - shared_ptr GetProperty(shared_ptr runtime, const shared_ptr &name) override; - - bool SetProperty(shared_ptr runtime, const std::string &name, const shared_ptr &value) override; - bool SetProperty(shared_ptr runtime, const shared_ptr &name, - const shared_ptr &value) override; - bool SetAccessorProperty(shared_ptr runtime, const std::string &name, const shared_ptr &getter, - const shared_ptr &setter) override; - bool SetAccessorProperty(shared_ptr runtime, const shared_ptr &name, - const shared_ptr &getter, const shared_ptr &setter) override; - - int32_t GetArrayLength(shared_ptr runtime) override; - shared_ptr GetElement(shared_ptr runtime, int32_t idx) override; - std::string GetJsonString(const shared_ptr& runtime) override; - - Local GetValue(const shared_ptr &runtime) - { - return value_.ToLocal(runtime->GetEcmaVm()); - } - - void SetValue(const shared_ptr &runtime, const Local &value) - { - value_ = Global(runtime->GetEcmaVm(), value); - } - -private: - Global value_ {}; -}; -} // namespace OHOS::Ace::Framework -#endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JSI_ARK_ARK_JS_VALUE_H diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_runtime.h b/frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_runtime.h deleted file mode 100644 index a8200b0b..00000000 --- a/frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_runtime.h +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FOUNDATION_ACE_FRAMEWORKS_BRIDGE_ENGINE_JSI_JS_RUNTIME_H -#define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_ENGINE_JSI_JS_RUNTIME_H - -#include -#include -#include -#include - -// NOLINTNEXTLINE(readability-identifier-naming) -namespace OHOS::Ace::Framework { -class JsValue; -class JsRuntime; - -using std::shared_ptr; -using RegisterFunctionType = std::function(shared_ptr, shared_ptr, - const std::vector> &, int32_t)>; -using LOG_PRINT = int (*)(int id, int level, const char *tag, const char *fmt, const char *message); -using UncaughtExceptionCallback = std::function, std::shared_ptr)>; - -// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions, hicpp-special-member-functions) -class JsRuntime { -public: - virtual ~JsRuntime() = default; - - // Prepare js environment, returns true if success. - virtual bool Initialize(const std::string &libraryPath, bool isDebugMode) = 0; - virtual void Reset() = 0; - virtual void SetLogPrint(LOG_PRINT out) = 0; - - // Evaluate a piece of js code, returns true if success. - // If exception occurs during execution, it is handled inside this interface. - virtual shared_ptr EvaluateJsCode(const std::string &src) = 0; - virtual bool EvaluateJsCode(const uint8_t *buffer, int32_t size) = 0; - - virtual bool ExecuteJsBin([[maybe_unused]] const std::string &fileName) - { - return true; - } - - // Get the global object. - virtual shared_ptr GetGlobal() = 0; - virtual void RunGC() = 0; - - virtual shared_ptr NewNumber(double d) = 0; - virtual shared_ptr NewInt32(int32_t value) = 0; - virtual shared_ptr NewBoolean(bool value) = 0; - virtual shared_ptr NewNull() = 0; - virtual shared_ptr NewUndefined() = 0; - virtual shared_ptr NewString(const std::string &str) = 0; - virtual shared_ptr ParseJson(const std::string &str) = 0; - virtual shared_ptr NewObject() = 0; - virtual shared_ptr NewArray() = 0; - virtual shared_ptr NewFunction(RegisterFunctionType func) = 0; - virtual shared_ptr NewNativePointer(void *ptr) = 0; - virtual void RegisterUncaughtExceptionHandler(UncaughtExceptionCallback callback) = 0; - virtual void HandleUncaughtException() = 0; - - // Set c++ data to js environment. - void SetEmbedderData(void *data) - { - embedderData_ = data; - } - // Get c++ data from js environment. - void *GetEmbedderData() const - { - return embedderData_; - } - -private: - void *embedderData_ = nullptr; -}; -} // namespace OHOS::Ace::Framework -#endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_ENGINE_JSI_JS_RUNTIME_H diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_value.h b/frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_value.h deleted file mode 100644 index 8daa3f82..00000000 --- a/frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_value.h +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FOUNDATION_ACE_FRAMEWORKS_BRIDGE_ENGINE_JSI_JS_VALUE_H -#define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_ENGINE_JSI_JS_VALUE_H - -#include -#include -#include - -// NOLINTNEXTLINE(readability-identifier-naming) -namespace OHOS::Ace::Framework { -using std::shared_ptr; -class JsRuntime; -/* A class that represent all types of js value. Since we don't have separate class for each type, please check it's - * type before calling a type specific method such as GetProperty. - */ -// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions, hicpp-special-member-functions) -class JsValue { -public: - JsValue() = default; - virtual ~JsValue() = default; - - virtual int32_t ToInt32(shared_ptr runtime) = 0; - virtual double ToDouble(shared_ptr runtime) = 0; - virtual std::string ToString(shared_ptr runtime) = 0; - virtual bool ToBoolean(shared_ptr runtime) = 0; - - virtual bool IsObject(shared_ptr runtime) = 0; - virtual bool IsArray(shared_ptr runtime) = 0; - virtual bool IsUndefined(shared_ptr runtime) = 0; - virtual bool IsNull(shared_ptr runtime) = 0; - virtual bool IsNumber(shared_ptr runtime) = 0; - virtual bool IsInt32(shared_ptr runtime) = 0; - virtual bool WithinInt32(shared_ptr runtime) = 0; - virtual bool IsBoolean(shared_ptr runtime) = 0; - virtual bool IsString(shared_ptr runtime) = 0; - virtual bool IsFunction(shared_ptr runtime) = 0; - virtual bool IsException(shared_ptr runtime) = 0; - - virtual bool GetPropertyNames(shared_ptr runtime, shared_ptr &propertyNames, int32_t &len) = 0; - virtual bool GetEnumerablePropertyNames(shared_ptr runtime, - shared_ptr &propertyNames, int32_t &len) = 0; - - // Get object property by name or index, returns an nullptr if failed to get the specified property. - virtual shared_ptr GetProperty(shared_ptr runtime, int32_t idx) = 0; - virtual shared_ptr GetProperty(shared_ptr runtime, const std::string &name) = 0; - virtual shared_ptr GetProperty(shared_ptr runtime, const shared_ptr &name) = 0; - - // Set a property with the given name and value, returns true if success. - virtual bool SetProperty(shared_ptr runtime, const std::string &name, - const shared_ptr &value) = 0; - virtual bool SetProperty(shared_ptr runtime, const shared_ptr &name, - const shared_ptr &value) = 0; - virtual bool SetAccessorProperty(shared_ptr runtime, const std::string &name, - const shared_ptr &getter, const shared_ptr &setter) = 0; - virtual bool SetAccessorProperty(shared_ptr runtime, const shared_ptr &name, - const shared_ptr &getter, const shared_ptr &setter) = 0; - - // Get an array's length. Return -1 if fails. - virtual int32_t GetArrayLength(shared_ptr runtime) = 0; - // Get an array element by index, Return nullptr if fails. - virtual shared_ptr GetElement(shared_ptr runtime, int32_t idx) = 0; - - virtual shared_ptr Call(shared_ptr runtime, shared_ptr thisObj, - std::vector> argv, int32_t argc) = 0; - - virtual std::string GetJsonString(const shared_ptr& runtime) = 0; -}; -} // namespace OHOS::Ace::Framework -#endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_ENGINE_JSI_JS_VALUE_H diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/js_converter.cpp b/frameworks/bridge/declarative_frontend/engine/jsi/js_converter.cpp index 2e3578ea..1f883ab5 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/js_converter.cpp +++ b/frameworks/bridge/declarative_frontend/engine/jsi/js_converter.cpp @@ -18,7 +18,7 @@ #include "base/log/log.h" #include "native_engine/native_value.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_runtime.h" +#include "frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.h" #include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.h" namespace OHOS::Ace::Framework { diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.cpp b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.cpp index d31010a0..10d5099f 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.cpp +++ b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.cpp @@ -28,8 +28,8 @@ #include "frameworks/bridge/declarative_frontend/engine/js_converter.h" #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h" #include "frameworks/bridge/declarative_frontend/engine/js_types.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_runtime.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_value.h" +#include "frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.h" +#include "frameworks/bridge/js_frontend/engine/jsi/ark_js_value.h" #include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_group_js_bridge.h" #include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_types.h" #include "frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_context_module.h" @@ -43,216 +43,28 @@ #include "frameworks/bridge/js_frontend/engine/common/runtime_constants.h" #include "frameworks/bridge/js_frontend/engine/jsi/jsi_base_utils.h" +#ifndef WINDOWS_PLATFORM extern const char _binary_stateMgmt_abc_start[]; extern const char _binary_stateMgmt_abc_end[]; extern const char _binary_jsEnumStyle_abc_start[]; extern const char _binary_jsEnumStyle_abc_end[]; +#endif namespace OHOS::Ace::Framework { namespace { +#if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM) +const char COMPONENT_PREVIEW_LOAD_DOCUMENT_NEW[] = "loadDocument(new"; +const char LEFT_PARENTTHESIS[] = "("; +constexpr int32_t LOAD_DOCUMENT_STR_LENGTH = 16; +#endif + #ifdef APP_USE_ARM const std::string ARK_DEBUGGER_LIB_PATH = "/system/lib/libark_debugger.z.so"; #else const std::string ARK_DEBUGGER_LIB_PATH = "/system/lib64/libark_debugger.z.so"; #endif -std::string ParseLogContent(const std::vector& params) -{ - std::string ret; - if (params.empty()) { - return ret; - } - std::string formatStr = params[0]; - int32_t size = static_cast(params.size()); - int32_t len = static_cast(formatStr.size()); - int32_t pos = 0; - int32_t count = 1; - for (; pos < len; ++pos) { - if (count >= size) { - break; - } - if (formatStr[pos] == '%') { - if (pos + 1 >= len) { - break; - } - switch (formatStr[pos + 1]) { - case 's': - case 'j': - case 'd': - case 'O': - case 'o': - case 'i': - case 'f': - case 'c': - ret += params[count++]; - ++pos; - break; - case '%': - ret += formatStr[pos]; - ++pos; - break; - default: - ret += formatStr[pos]; - break; - } - } else { - ret += formatStr[pos]; - } - } - if (pos < len) { - ret += formatStr.substr(pos, len - pos); - } - return ret; -} - -std::string GetLogContent( - const shared_ptr& runtime, const std::vector>& argv, int32_t argc) -{ - if (argc == 1) { - return argv[0]->ToString(runtime); - } - std::vector params; - for (int32_t i = 0; i < argc; ++i) { - params.emplace_back(argv[i]->ToString(runtime)); - } - return ParseLogContent(params); -} - -shared_ptr AppLogPrint( - const shared_ptr& runtime, JsLogLevel level, const std::vector>& argv, int32_t argc) -{ - // Should have at least 1 parameter. - if (argc == 0) { - LOGE("the arg is error"); - return runtime->NewUndefined(); - } - std::string content = GetLogContent(runtime, argv, argc); - 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 runtime->NewUndefined(); -} - -// native implementation for js function: console.debug() -shared_ptr AppDebugLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return AppLogPrint(runtime, JsLogLevel::DEBUG, argv, argc); -} - -// native implementation for js function: console.info() -shared_ptr AppInfoLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return AppLogPrint(runtime, JsLogLevel::INFO, argv, argc); -} - -// native implementation for js function: console.warn() -shared_ptr AppWarnLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return AppLogPrint(runtime, JsLogLevel::WARNING, argv, argc); -} - -// native implementation for js function: console.error() -shared_ptr AppErrorLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return AppLogPrint(runtime, JsLogLevel::ERROR, argv, argc); -} - -shared_ptr JsLogPrint( - const shared_ptr& runtime, JsLogLevel level, const std::vector>& argv, int32_t argc) -{ - // Should have 1 parameters. - if (argc == 0) { - LOGE("the arg is error"); - return runtime->NewUndefined(); - } - - std::string content = GetLogContent(runtime, argv, argc); - switch (level) { - case JsLogLevel::DEBUG: - LOGD("ace Log: %{public}s", content.c_str()); - break; - case JsLogLevel::INFO: - LOGI("ace Log: %{public}s", content.c_str()); - break; - case JsLogLevel::WARNING: - LOGW("ace Log: %{public}s", content.c_str()); - break; - case JsLogLevel::ERROR: - LOGE("ace Log: %{public}s", content.c_str()); - break; - } - - shared_ptr ret = runtime->NewUndefined(); - return ret; -} - -int PrintLog(int id, int level, const char* tag, const char* fmt, const char* message) -{ - switch (JsLogLevel(level - 3)) { - case JsLogLevel::INFO: - LOGI("%{public}s::%{public}s", tag, message); - break; - case JsLogLevel::WARNING: - LOGW("%{public}s::%{public}s", tag, message); - break; - case JsLogLevel::ERROR: - LOGE("%{public}s::%{public}s", tag, message); - break; - case JsLogLevel::DEBUG: - LOGD("%{public}s::%{public}s", tag, message); - break; - default: - LOGF("%{public}s::%{public}s", tag, message); - break; - } - return 0; -} - -// native implementation for js function: aceConsole.debug() -shared_ptr JsDebugLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return JsLogPrint(runtime, JsLogLevel::DEBUG, std::move(argv), argc); -} - -// native implementation for js function: aceConsole.info() -shared_ptr JsInfoLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return JsLogPrint(runtime, JsLogLevel::INFO, std::move(argv), argc); -} - -// native implementation for js function: aceConsole.warn() -shared_ptr JsWarnLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return JsLogPrint(runtime, JsLogLevel::WARNING, std::move(argv), argc); -} - -// native implementation for js function: aceConsole.error() -shared_ptr JsErrorLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return JsLogPrint(runtime, JsLogLevel::ERROR, std::move(argv), argc); -} - // native implementation for js function: perfutil.print() shared_ptr JsPerfPrint(const shared_ptr& runtime, const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) @@ -310,7 +122,6 @@ shared_ptr RequireNativeModule(const shared_ptr& runtime, co return runtime->NewNull(); } - } // namespace // ----------------------- @@ -426,6 +237,7 @@ bool JsiDeclarativeEngineInstance::FireJsEvent(const std::string& eventStr) void JsiDeclarativeEngineInstance::InitAceModule() { +#ifndef WINDOWS_PLATFORM bool stateMgmtResult = runtime_->EvaluateJsCode( (uint8_t*)_binary_stateMgmt_abc_start, _binary_stateMgmt_abc_end - _binary_stateMgmt_abc_start); if (!stateMgmtResult) { @@ -436,6 +248,7 @@ void JsiDeclarativeEngineInstance::InitAceModule() if (!jsEnumStyleResult) { LOGE("EvaluateJsCode jsEnumStyle failed"); } +#endif } extern "C" ACE_EXPORT void OHOS_ACE_PreloadAceModule(void* runtime) @@ -475,11 +288,11 @@ void JsiDeclarativeEngineInstance::PreloadAceModule(void* runtime) // preload aceConsole shared_ptr global = arkRuntime->GetGlobal(); shared_ptr aceConsoleObj = arkRuntime->NewObject(); - aceConsoleObj->SetProperty(arkRuntime, "log", arkRuntime->NewFunction(JsDebugLogPrint)); - aceConsoleObj->SetProperty(arkRuntime, "debug", arkRuntime->NewFunction(JsDebugLogPrint)); - aceConsoleObj->SetProperty(arkRuntime, "info", arkRuntime->NewFunction(JsInfoLogPrint)); - aceConsoleObj->SetProperty(arkRuntime, "warn", arkRuntime->NewFunction(JsWarnLogPrint)); - aceConsoleObj->SetProperty(arkRuntime, "error", arkRuntime->NewFunction(JsErrorLogPrint)); + aceConsoleObj->SetProperty(arkRuntime, "log", arkRuntime->NewFunction(JsiBaseUtils::JsDebugLogPrint)); + aceConsoleObj->SetProperty(arkRuntime, "debug", arkRuntime->NewFunction(JsiBaseUtils::JsDebugLogPrint)); + aceConsoleObj->SetProperty(arkRuntime, "info", arkRuntime->NewFunction(JsiBaseUtils::JsInfoLogPrint)); + aceConsoleObj->SetProperty(arkRuntime, "warn", arkRuntime->NewFunction(JsiBaseUtils::JsWarnLogPrint)); + aceConsoleObj->SetProperty(arkRuntime, "error", arkRuntime->NewFunction(JsiBaseUtils::JsErrorLogPrint)); global->SetProperty(arkRuntime, "aceConsole", aceConsoleObj); // preload perfutil @@ -495,6 +308,7 @@ void JsiDeclarativeEngineInstance::PreloadAceModule(void* runtime) global->SetProperty(arkRuntime, "exports", exportsUtilObj); global->SetProperty(arkRuntime, "requireNativeModule", arkRuntime->NewFunction(RequireNativeModule)); +#ifndef WINDOWS_PLATFORM // preload js enums bool jsEnumStyleResult = arkRuntime->EvaluateJsCode( (uint8_t*)_binary_jsEnumStyle_abc_start, _binary_jsEnumStyle_abc_end - _binary_jsEnumStyle_abc_start); @@ -514,6 +328,7 @@ void JsiDeclarativeEngineInstance::PreloadAceModule(void* runtime) isModulePreloaded_ = evalResult; globalRuntime_ = nullptr; LOGI("PreloadAceModule loaded:%{public}d", isModulePreloaded_); +#endif } void JsiDeclarativeEngineInstance::InitConsoleModule() @@ -525,11 +340,11 @@ void JsiDeclarativeEngineInstance::InitConsoleModule() // app log method if (!usingSharedRuntime_) { shared_ptr consoleObj = runtime_->NewObject(); - consoleObj->SetProperty(runtime_, "log", runtime_->NewFunction(AppDebugLogPrint)); - consoleObj->SetProperty(runtime_, "debug", runtime_->NewFunction(AppDebugLogPrint)); - consoleObj->SetProperty(runtime_, "info", runtime_->NewFunction(AppInfoLogPrint)); - consoleObj->SetProperty(runtime_, "warn", runtime_->NewFunction(AppWarnLogPrint)); - consoleObj->SetProperty(runtime_, "error", runtime_->NewFunction(AppErrorLogPrint)); + consoleObj->SetProperty(runtime_, "log", runtime_->NewFunction(JsiBaseUtils::AppDebugLogPrint)); + consoleObj->SetProperty(runtime_, "debug", runtime_->NewFunction(JsiBaseUtils::AppDebugLogPrint)); + consoleObj->SetProperty(runtime_, "info", runtime_->NewFunction(JsiBaseUtils::AppInfoLogPrint)); + consoleObj->SetProperty(runtime_, "warn", runtime_->NewFunction(JsiBaseUtils::AppWarnLogPrint)); + consoleObj->SetProperty(runtime_, "error", runtime_->NewFunction(JsiBaseUtils::AppErrorLogPrint)); global->SetProperty(runtime_, "console", consoleObj); } @@ -540,79 +355,14 @@ void JsiDeclarativeEngineInstance::InitConsoleModule() // js framework log method shared_ptr aceConsoleObj = runtime_->NewObject(); - aceConsoleObj->SetProperty(runtime_, "log", runtime_->NewFunction(JsDebugLogPrint)); - aceConsoleObj->SetProperty(runtime_, "debug", runtime_->NewFunction(JsDebugLogPrint)); - aceConsoleObj->SetProperty(runtime_, "info", runtime_->NewFunction(JsInfoLogPrint)); - aceConsoleObj->SetProperty(runtime_, "warn", runtime_->NewFunction(JsWarnLogPrint)); - aceConsoleObj->SetProperty(runtime_, "error", runtime_->NewFunction(JsErrorLogPrint)); + aceConsoleObj->SetProperty(runtime_, "log", runtime_->NewFunction(JsiBaseUtils::JsDebugLogPrint)); + aceConsoleObj->SetProperty(runtime_, "debug", runtime_->NewFunction(JsiBaseUtils::JsDebugLogPrint)); + aceConsoleObj->SetProperty(runtime_, "info", runtime_->NewFunction(JsiBaseUtils::JsInfoLogPrint)); + aceConsoleObj->SetProperty(runtime_, "warn", runtime_->NewFunction(JsiBaseUtils::JsWarnLogPrint)); + aceConsoleObj->SetProperty(runtime_, "error", runtime_->NewFunction(JsiBaseUtils::JsErrorLogPrint)); 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(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"); @@ -903,7 +653,9 @@ void JsiDeclarativeEngine::Destroy() engineInstance_->GetDelegate()->RemoveTaskObserver(); if (!runtime_ && nativeEngine_ != nullptr) { +#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) nativeEngine_->CancelCheckUVLoop(); +#endif engineInstance_->DestroyAllRootViewHandle(); delete nativeEngine_; nativeEngine_ = nullptr; @@ -961,7 +713,9 @@ bool JsiDeclarativeEngine::Initialize(const RefPtr& delegate) engineInstance_->SetNativeEngine(nativeEngine_); if (!sharedRuntime) { SetPostTask(nativeEngine_); +#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) nativeEngine_->CheckUVLoop(); +#endif if (delegate && delegate->GetAssetManager()) { std::string packagePath = delegate->GetAssetManager()->GetLibPath(); @@ -1020,11 +774,13 @@ void JsiDeclarativeEngine::RegisterInitWorkerFunc() } instance->InitConsoleModule(arkNativeEngine); +#ifndef WINDOWS_PLATFORM std::vector 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"); } +#endif }; nativeEngine_->SetInitWorkerFunc(initWorkerFunc); } @@ -1127,6 +883,41 @@ void JsiDeclarativeEngine::LoadJs(const std::string& url, const RefPtr loadDocomentPos + LOAD_DOCUMENT_STR_LENGTH) { + finalPostion = position; + break; + } + position++; + } + std::string dstReplaceStr = COMPONENT_PREVIEW_LOAD_DOCUMENT_NEW; + dstReplaceStr += " " + componentName; + preContent_.replace(lastLoadDocomentPos, finalPostion - lastLoadDocomentPos, dstReplaceStr); + + if (engineInstance_ == nullptr) { + LOGE("engineInstance is nullptr"); + return; + } + + engineInstance_->GetDelegate()->Replace(url, ""); +} +#endif + void JsiDeclarativeEngine::UpdateRunningPage(const RefPtr& page) { LOGD("JsiDeclarativeEngine UpdateRunningPage"); diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.h b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.h index 28a780ca..d5197422 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.h +++ b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.h @@ -30,7 +30,7 @@ #include "core/common/ace_application_info.h" #include "core/common/ace_page.h" #include "core/components/xcomponent/native_interface_xcomponent_impl.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_runtime.h" +#include "frameworks/bridge/js_frontend/engine/jsi/js_runtime.h" #include "frameworks/bridge/js_frontend/engine/common/js_engine.h" #include "frameworks/bridge/js_frontend/js_ace_page.h" @@ -265,6 +265,10 @@ public: return renderContext_; } +#if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM) + void ReplaceJSContent(const std::string& url, const std::string componentName) override; +#endif + private: bool CallAppFunc(const std::string& appFuncName); @@ -290,6 +294,10 @@ private: void* runtime_ = nullptr; shared_ptr renderContext_; +#if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM) + std::string preContent_ = ""; +#endif + ACE_DISALLOW_COPY_AND_MOVE(JsiDeclarativeEngine); }; diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_group_js_bridge.cpp b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_group_js_bridge.cpp index e207516c..f87cbe3c 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_group_js_bridge.cpp +++ b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_group_js_bridge.cpp @@ -586,4 +586,12 @@ void JsiDeclarativeGroupJsBridge::Destroy() runtime_.reset(); } +#if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM) +void JsiDeclarativeGroupJsBridge::TriggerModuleJsCallbackPreview( + int32_t callbackId, int32_t code, ResponseData responseData) +{ + LOGE("Not implemented yet"); +} +#endif + } // namespace OHOS::Ace::Framework diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_group_js_bridge.h b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_group_js_bridge.h index 253e297f..f05d3669 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_group_js_bridge.h +++ b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_group_js_bridge.h @@ -19,6 +19,10 @@ #include #include +#if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM) +#include "adapter/preview/osal/request_data.h" +#include "adapter/preview/osal/response_data.h" +#endif #include "base/memory/ace_type.h" #include "base/utils/singleton.h" #include "frameworks/bridge/codec/standard_function_codec.h" @@ -71,6 +75,10 @@ public: void Destroy() override; +#if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM) + void TriggerModuleJsCallbackPreview(int32_t callbackId, int32_t code, ResponseData responseData) override; +#endif + private: int32_t GetPendingCallbackIdAndIncrement() { diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_utils.cpp b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_utils.cpp index d446386a..0d2c4feb 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_utils.cpp +++ b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_utils.cpp @@ -18,7 +18,7 @@ #include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.h" namespace OHOS::Ace::Framework { -int32_t GetLineOffset() +int32_t GetLineOffset(const AceType *data) { return 0; } diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_types.cpp b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_types.cpp index a0bf303f..6c758b1c 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_types.cpp +++ b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_types.cpp @@ -15,7 +15,7 @@ #include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_types.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_runtime.h" +#include "frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.h" #include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.h" namespace OHOS::Ace::Framework { diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_types.inl b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_types.inl index 47278911..3a4c3c64 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_types.inl +++ b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_types.inl @@ -14,7 +14,7 @@ */ #include "frameworks/base/utils/string_utils.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_runtime.h" +#include "frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.h" #include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_ref.h" #include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_types.h" diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_value_conversions.h b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_value_conversions.h index c9e530f6..c3404c71 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_value_conversions.h +++ b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_value_conversions.h @@ -19,7 +19,7 @@ #include "ecmascript/napi/include/jsnapi.h" #include "base/log/log.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/ark_js_runtime.h" +#include "frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.h" #include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.h" namespace OHOS::Ace::Framework { diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_app_module.h b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_app_module.h index f4d9eada..f1580c97 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_app_module.h +++ b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_app_module.h @@ -18,8 +18,8 @@ #include -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_runtime.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_value.h" +#include "frameworks/bridge/js_frontend/engine/jsi/js_runtime.h" +#include "frameworks/bridge/js_frontend/engine/jsi/js_value.h" namespace OHOS::Ace::Framework { diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_context_module.h b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_context_module.h index 1d15a35a..4605fc7d 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_context_module.h +++ b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_context_module.h @@ -20,8 +20,8 @@ #include #include "base/utils/noncopyable.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_runtime.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_value.h" +#include "frameworks/bridge/js_frontend/engine/jsi/js_runtime.h" +#include "frameworks/bridge/js_frontend/engine/jsi/js_value.h" namespace OHOS::Ace::Framework { diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_curves_module.h b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_curves_module.h index c4753dc3..b3b3d691 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_curves_module.h +++ b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_curves_module.h @@ -18,8 +18,8 @@ #include -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_runtime.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_value.h" +#include "frameworks/bridge/js_frontend/engine/jsi/js_runtime.h" +#include "frameworks/bridge/js_frontend/engine/jsi/js_value.h" namespace OHOS::Ace::Framework { diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_matrix4_module.h b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_matrix4_module.h index 2641e009..8f7ba4c7 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_matrix4_module.h +++ b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_matrix4_module.h @@ -18,8 +18,8 @@ #include -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_runtime.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_value.h" +#include "frameworks/bridge/js_frontend/engine/jsi/js_runtime.h" +#include "frameworks/bridge/js_frontend/engine/jsi/js_value.h" namespace OHOS::Ace::Framework { diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_module_manager.h b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_module_manager.h index 57b006e1..5e27ff88 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_module_manager.h +++ b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_module_manager.h @@ -20,8 +20,8 @@ #include #include "base/utils/noncopyable.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_runtime.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_value.h" +#include "frameworks/bridge/js_frontend/engine/jsi/js_runtime.h" +#include "frameworks/bridge/js_frontend/engine/jsi/js_value.h" #include "frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_timer_module.h" #include "frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_syscap_module.h" diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_router_module.cpp b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_router_module.cpp index 924bd3e1..d91fc2de 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_router_module.cpp +++ b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_router_module.cpp @@ -24,9 +24,10 @@ namespace OHOS::Ace::Framework { -std::string ParseRouteUrl(const shared_ptr& runtime, const shared_ptr& arg, const std::string& key) +std::string DeclarativeParseRouteUrl(const shared_ptr& runtime, const shared_ptr& arg, + const std::string& key) { - LOGD("ParseRouteUrl"); + LOGD("DeclarativeParseRouteUrl"); std::string argStr = arg->GetJsonString(runtime); if (argStr.empty()) { return argStr; @@ -37,12 +38,12 @@ std::string ParseRouteUrl(const shared_ptr& runtime, const shared_ptr if (argsPtr != nullptr && argsPtr->GetValue(key) != nullptr && argsPtr->GetValue(key)->IsString()) { pageRoute = argsPtr->GetValue(key)->GetString(); } - LOGD("JsParseRouteUrl pageRoute = %{private}s", pageRoute.c_str()); + LOGD("JsDeclarativeParseRouteUrl pageRoute = %{private}s", pageRoute.c_str()); return pageRoute; } -std::string ParseRouteParams(const shared_ptr& runtime, const shared_ptr& arg, +std::string DeclarativeParseRouteParams(const shared_ptr& runtime, const shared_ptr& arg, const std::string& key) { std::string argStr = arg->GetJsonString(runtime); @@ -63,8 +64,8 @@ shared_ptr PagePush(const shared_ptr& runtime, const shared_ return runtime->NewNull(); } - std::string uri = ParseRouteUrl(runtime, argv[0], ROUTE_KEY_URI); - std::string params = ParseRouteParams(runtime, argv[0], ROUTE_KEY_PARAMS); + std::string uri = DeclarativeParseRouteUrl(runtime, argv[0], ROUTE_KEY_URI); + std::string params = DeclarativeParseRouteParams(runtime, argv[0], ROUTE_KEY_PARAMS); auto delegate = EngineHelper::GetCurrentDelegate(); if (delegate == nullptr) { LOGE("get jsi delegate failed"); @@ -84,8 +85,8 @@ shared_ptr PageReplace(const shared_ptr& runtime, const shar return runtime->NewNull(); } - std::string uri = ParseRouteUrl(runtime, argv[0], ROUTE_KEY_URI); - std::string params = ParseRouteParams(runtime, argv[0], ROUTE_KEY_PARAMS); + std::string uri = DeclarativeParseRouteUrl(runtime, argv[0], ROUTE_KEY_URI); + std::string params = DeclarativeParseRouteParams(runtime, argv[0], ROUTE_KEY_PARAMS); auto delegate = EngineHelper::GetCurrentDelegate(); if (delegate == nullptr) { LOGE("get jsi delegate failed"); @@ -108,8 +109,8 @@ shared_ptr PageBack(const shared_ptr& runtime, const shared_ std::string uri; std::string params; if (argc == 1) { - uri = ParseRouteUrl(runtime, argv[0], ROUTE_KEY_URI); - params = ParseRouteParams(runtime, argv[0], ROUTE_KEY_PARAMS); + uri = DeclarativeParseRouteUrl(runtime, argv[0], ROUTE_KEY_URI); + params = DeclarativeParseRouteParams(runtime, argv[0], ROUTE_KEY_PARAMS); } auto delegate = EngineHelper::GetCurrentDelegate(); if (delegate == nullptr) { diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_router_module.h b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_router_module.h index df0d9eca..7c426a55 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_router_module.h +++ b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_router_module.h @@ -18,8 +18,8 @@ #include -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_runtime.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_value.h" +#include "frameworks/bridge/js_frontend/engine/jsi/js_runtime.h" +#include "frameworks/bridge/js_frontend/engine/jsi/js_value.h" namespace OHOS::Ace::Framework { diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_syscap_module.h b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_syscap_module.h index 4005b85f..a489064b 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_syscap_module.h +++ b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_syscap_module.h @@ -20,8 +20,8 @@ #include #include "base/utils/noncopyable.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_runtime.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_value.h" +#include "frameworks/bridge/js_frontend/engine/jsi/js_runtime.h" +#include "frameworks/bridge/js_frontend/engine/jsi/js_value.h" namespace OHOS::Ace::Framework { class JsiSyscapModule { diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_timer_module.h b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_timer_module.h index 3d9f2ab8..136495de 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_timer_module.h +++ b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_timer_module.h @@ -20,8 +20,8 @@ #include #include "base/utils/noncopyable.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_runtime.h" -#include "frameworks/bridge/declarative_frontend/engine/jsi/ark/include/js_value.h" +#include "frameworks/bridge/js_frontend/engine/jsi/js_runtime.h" +#include "frameworks/bridge/js_frontend/engine/jsi/js_value.h" namespace OHOS::Ace::Framework { diff --git a/frameworks/bridge/declarative_frontend/engine/quickjs/qjs_declarative_engine.h b/frameworks/bridge/declarative_frontend/engine/quickjs/qjs_declarative_engine.h index ac7b4433..1f1f9e5c 100644 --- a/frameworks/bridge/declarative_frontend/engine/quickjs/qjs_declarative_engine.h +++ b/frameworks/bridge/declarative_frontend/engine/quickjs/qjs_declarative_engine.h @@ -108,7 +108,7 @@ public: } #if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM) - void ReplaceJSContent(const std::string& url, const std::string componentName); + void ReplaceJSContent(const std::string& url, const std::string componentName) override; #endif RefPtr nativeXComponentImpl_; diff --git a/frameworks/bridge/js_frontend/engine/common/js_engine.h b/frameworks/bridge/js_frontend/engine/common/js_engine.h index 9751fcc7..70022149 100644 --- a/frameworks/bridge/js_frontend/engine/common/js_engine.h +++ b/frameworks/bridge/js_frontend/engine/common/js_engine.h @@ -242,6 +242,14 @@ public: static PixelMapNapiEntry GetPixelMapNapiEntry(); #endif +#if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM) + virtual void ReplaceJSContent(const std::string& url, const std::string componentName) + { + LOGE("V8 does not support replaceJSContent"); + return; + } +#endif + protected: NativeEngine* nativeEngine_ = nullptr; std::function mediaUpdateCallback_; diff --git a/frameworks/bridge/js_frontend/engine/jsi/BUILD.gn b/frameworks/bridge/js_frontend/engine/jsi/BUILD.gn index e7483483..ed477f75 100644 --- a/frameworks/bridge/js_frontend/engine/jsi/BUILD.gn +++ b/frameworks/bridge/js_frontend/engine/jsi/BUILD.gn @@ -46,11 +46,28 @@ template("js_engine_ark") { "jsi_list_bridge.cpp", "jsi_offscreen_canvas_bridge.cpp", "jsi_stepper_bridge.cpp", - "jsi_utils.cpp", "jsi_xcomponent_bridge.cpp", ] - deps = [ "//third_party/jsframework:ark_build" ] + if (defined(config.use_build_in_js_engine) && + config.use_build_in_js_engine) { + defines += [ "BUILT_IN_JS_ENGINE" ] + } + + deps = [] + if (defined(config.build_for_preview) && config.build_for_preview) { + sources += [ "jsi_utils_windows.cpp" ] + deps += + [ "//foundation/ace/napi/native_engine/impl/ark:ace_napi_impl_ark" ] + } else { + sources += [ "jsi_utils.cpp" ] + deps += [ "//third_party/jsframework:ark_build" ] + external_deps = [ + "multimedia_image_standard:image", + "multimedia_image_standard:image_native", + "napi:ace_napi", + ] + } if (target_cpu == "arm64") { defines += [ "APP_USE_ARM64" ] } else if (target_cpu == "arm") { @@ -60,11 +77,6 @@ template("js_engine_ark") { "//ark/js_runtime:libark_jsruntime", "//foundation/ace/napi:ace_napi_ark", ] - external_deps = [ - "multimedia_image_standard:image", - "multimedia_image_standard:image_native", - "napi:ace_napi", - ] } } diff --git a/frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.cpp b/frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.cpp index f179bf8e..06493e58 100644 --- a/frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.cpp +++ b/frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.cpp @@ -40,8 +40,10 @@ bool ArkJSRuntime::Initialize(const std::string &libraryPath, bool isDebugMode) LOGI("Ark: create jsvm"); RuntimeOption option; option.SetGcType(RuntimeOption::GC_TYPE::GEN_GC); +#ifndef WINDOWS_PLATFORM option.SetArkProperties(SystemProperties::GetArkProperties()); option.SetAsmInterOption(SystemProperties::GetAsmInterOption()); +#endif const int64_t poolSize = 0x10000000; // 256M option.SetGcPoolSize(poolSize); option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); @@ -54,11 +56,23 @@ bool ArkJSRuntime::Initialize(const std::string &libraryPath, bool isDebugMode) return vm_ != nullptr; } +bool ArkJSRuntime::InitializeFromExistVM(EcmaVM* vm) +{ + vm_ = vm; + usingExistVM_ = true; + LOGI("InitializeFromExistVM %{public}p", vm); + return vm_ != nullptr; +} + void ArkJSRuntime::Reset() { if (vm_ != nullptr) { - JSNApi::StopDebugger(libPath_.c_str()); - JSNApi::DestroyJSVM(vm_); + if (!usingExistVM_) { +#ifndef WINDOWS_PLATFORM + JSNApi::StopDebugger(libPath_.c_str()); +#endif + JSNApi::DestroyJSVM(vm_); + } vm_ = nullptr; } for (auto data : dataList_) { @@ -91,7 +105,9 @@ bool ArkJSRuntime::ExecuteJsBin(const std::string &fileName) JSExecutionScope executionScope(vm_); static bool debugFlag = true; if (debugFlag && !libPath_.empty()) { +#ifndef WINDOWS_PLATFORM JSNApi::StartDebugger(libPath_.c_str(), vm_, isDebugMode_); +#endif debugFlag = false; } LocalScope scope(vm_); diff --git a/frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.h b/frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.h index 562586f2..98a79313 100644 --- a/frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.h +++ b/frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.h @@ -50,7 +50,11 @@ class PandaFunctionData; // NOLINTNEXTLINE(fuchsia-multiple-inheritance) class ArkJSRuntime final : public JsRuntime, public std::enable_shared_from_this { public: +#if !defined(WINDOWS_PLATFORM) + bool StartDebugger(const char *libraryPath, EcmaVM *vm) const; +#endif bool Initialize(const std::string &libraryPath, bool isDebugMode) override; + bool InitializeFromExistVM(EcmaVM* vm); void Reset() override; void SetLogPrint(LOG_PRINT out) override; shared_ptr EvaluateJsCode(const std::string &src) override; @@ -85,7 +89,8 @@ private: LOG_PRINT print_ { nullptr }; UncaughtExceptionCallback uncaughtErrorHandler_ { nullptr }; std::string libPath_ {}; - bool isDebugMode_ {true}; + bool usingExistVM_ = false; + bool isDebugMode_ = true; }; class PandaFunctionData { diff --git a/frameworks/bridge/js_frontend/engine/jsi/jsi_base_utils.cpp b/frameworks/bridge/js_frontend/engine/jsi/jsi_base_utils.cpp index 3c3ae1ff..50b5fe5b 100644 --- a/frameworks/bridge/js_frontend/engine/jsi/jsi_base_utils.cpp +++ b/frameworks/bridge/js_frontend/engine/jsi/jsi_base_utils.cpp @@ -62,7 +62,7 @@ std::string JsiBaseUtils::GenerateSummaryBody(std::shared_ptr error, st summaryBody.append("Stacktrace:\n"); if (pageMap || appMap) { - std::string tempStack = JsiDumpSourceFile(stackStr, pageMap, appMap); + std::string tempStack = JsiDumpSourceFile(stackStr, pageMap, appMap, data); summaryBody.append(tempStack); } else { summaryBody.append("Cannot get SourceMap info, dump raw stack:\n"); @@ -101,7 +101,7 @@ std::string JsiBaseUtils::TransSourceStack(RefPtr runningPage, const } std::string JsiBaseUtils::JsiDumpSourceFile(const std::string& stackStr, const RefPtr& pageMap, - const RefPtr& appMap) + const RefPtr& appMap, const AceType *data) { std::string ans = ""; std::string tempStack = stackStr; @@ -124,7 +124,7 @@ std::string JsiBaseUtils::JsiDumpSourceFile(const std::string& stackStr, const R break; } - const std::string sourceInfo = GetSourceInfo(line, column, pageMap, appMap, isAppPage); + const std::string sourceInfo = GetSourceInfo(line, column, pageMap, appMap, isAppPage, data); if (sourceInfo.empty()) { break; } @@ -175,9 +175,9 @@ void JsiBaseUtils::GetPosInfo(const std::string& temp, std::string& line, std::s } std::string JsiBaseUtils::GetSourceInfo(const std::string& line, const std::string& column, - const RefPtr& pageMap, const RefPtr& appMap, bool isAppPage) + const RefPtr& pageMap, const RefPtr& appMap, bool isAppPage, const AceType *data) { - int32_t offSet = GetLineOffset(); + int32_t offSet = GetLineOffset(data); std::string sourceInfo; MappingInfo mapInfo; if (isAppPage) { @@ -219,4 +219,261 @@ void JsiBaseUtils::ReportJsErrorEvent(std::shared_ptr error, std::share LOGE("summaryBody: \n%{public}s", summaryBody.c_str()); EventReport::JsErrReport(AceApplicationInfo::GetInstance().GetPackageName(), "", summaryBody); } + +std::string ParseLogContent(const std::vector& params) +{ + std::string ret; + if (params.empty()) { + return ret; + } + std::string formatStr = params[0]; + int32_t size = static_cast(params.size()); + int32_t len = static_cast(formatStr.size()); + int32_t pos = 0; + int32_t count = 1; + for (; pos < len; ++pos) { + if (count >= size) { + break; + } + if (formatStr[pos] == '%') { + if (pos + 1 >= len) { + break; + } + switch (formatStr[pos + 1]) { + case 's': + case 'j': + case 'd': + case 'O': + case 'o': + case 'i': + case 'f': + case 'c': + ret += params[count++]; + ++pos; + break; + case '%': + ret += formatStr[pos]; + ++pos; + break; + default: + ret += formatStr[pos]; + break; + } + } else { + ret += formatStr[pos]; + } + } + if (pos < len) { + ret += formatStr.substr(pos, len - pos); + } + return ret; +} + +std::string GetLogContent( + const shared_ptr& runtime, const std::vector>& argv, int32_t argc) +{ + if (argc == 1) { + return argv[0]->ToString(runtime); + } + std::vector params; + for (int32_t i = 0; i < argc; ++i) { + params.emplace_back(argv[i]->ToString(runtime)); + } + return ParseLogContent(params); +} + +shared_ptr AppLogPrint( + const shared_ptr& runtime, JsLogLevel level, const std::vector>& argv, int32_t argc) +{ + // Should have at least 1 parameters. + if (argc == 0) { + LOGE("the arg is error"); + return runtime->NewUndefined(); + } + std::string content = GetLogContent(runtime, argv, argc); + 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 runtime->NewUndefined(); +} + +// native implementation for js function: console.debug() +shared_ptr JsiBaseUtils::AppDebugLogPrint(const shared_ptr& runtime, + const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) +{ + return AppLogPrint(runtime, JsLogLevel::DEBUG, argv, argc); +} + +// native implementation for js function: console.info() +shared_ptr JsiBaseUtils::AppInfoLogPrint(const shared_ptr& runtime, + const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) +{ + return AppLogPrint(runtime, JsLogLevel::INFO, argv, argc); +} + +// native implementation for js function: console.warn() +shared_ptr JsiBaseUtils::AppWarnLogPrint(const shared_ptr& runtime, + const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) +{ + return AppLogPrint(runtime, JsLogLevel::WARNING, argv, argc); +} + +// native implementation for js function: console.error() +shared_ptr JsiBaseUtils::AppErrorLogPrint(const shared_ptr& runtime, + const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) +{ + return AppLogPrint(runtime, JsLogLevel::ERROR, argv, argc); +} + +shared_ptr JsLogPrint( + const shared_ptr& runtime, JsLogLevel level, const std::vector>& argv, int32_t argc) +{ + // Should have 1 parameters. + if (argc == 0) { + LOGE("the arg is error"); + return runtime->NewUndefined(); + } + + std::string content = GetLogContent(runtime, argv, argc); + switch (level) { + case JsLogLevel::DEBUG: + LOGD("ace Log: %{public}s", content.c_str()); + break; + case JsLogLevel::INFO: + LOGI("ace Log: %{public}s", content.c_str()); + break; + case JsLogLevel::WARNING: + LOGW("ace Log: %{public}s", content.c_str()); + break; + case JsLogLevel::ERROR: + LOGE("ace Log: %{public}s", content.c_str()); + break; + } + + shared_ptr ret = runtime->NewUndefined(); + return ret; +} + +int PrintLog(int id, int level, const char* tag, const char* fmt, const char* message) +{ + switch (JsLogLevel(level - 3)) { + case JsLogLevel::INFO: + LOGI("%{public}s::%{public}s", tag, message); + break; + case JsLogLevel::WARNING: + LOGW("%{public}s::%{public}s", tag, message); + break; + case JsLogLevel::ERROR: + LOGE("%{public}s::%{public}s", tag, message); + break; + case JsLogLevel::DEBUG: + LOGD("%{public}s::%{public}s", tag, message); + break; + default: + LOGF("%{public}s::%{public}s", tag, message); + break; + } + return 0; +} + +shared_ptr JsiBaseUtils::JsDebugLogPrint(const shared_ptr& runtime, + const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) +{ + return JsLogPrint(runtime, JsLogLevel::DEBUG, std::move(argv), argc); +} + +shared_ptr JsiBaseUtils::JsInfoLogPrint(const shared_ptr& runtime, + const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) +{ + return JsLogPrint(runtime, JsLogLevel::INFO, std::move(argv), argc); +} + +shared_ptr JsiBaseUtils::JsWarnLogPrint(const shared_ptr& runtime, + const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) +{ + return JsLogPrint(runtime, JsLogLevel::WARNING, std::move(argv), argc); +} + +shared_ptr JsiBaseUtils::JsErrorLogPrint(const shared_ptr& runtime, + const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) +{ + return JsLogPrint(runtime, JsLogLevel::ERROR, std::move(argv), argc); +} + +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(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); +} } // namespace OHOS::Ace::Framework diff --git a/frameworks/bridge/js_frontend/engine/jsi/jsi_base_utils.h b/frameworks/bridge/js_frontend/engine/jsi/jsi_base_utils.h index 4051024d..51018587 100644 --- a/frameworks/bridge/js_frontend/engine/jsi/jsi_base_utils.h +++ b/frameworks/bridge/js_frontend/engine/jsi/jsi_base_utils.h @@ -16,27 +16,60 @@ #ifndef FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_JSI_BASE_UTILS_H #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_JSI_BASE_UTILS_H +#include "frameworks/base/log/ace_trace.h" +#include "frameworks/base/log/event_report.h" +#include "frameworks/bridge/js_frontend/engine/common/runtime_constants.h" #include "frameworks/bridge/js_frontend/engine/jsi/jsi_engine.h" #include "frameworks/bridge/js_frontend/engine/jsi/js_runtime.h" #include "frameworks/bridge/js_frontend/engine/jsi/js_value.h" namespace OHOS::Ace::Framework { -int32_t GetLineOffset(); +int32_t GetLineOffset(const AceType *data); RefPtr GetRunningPage(const AceType *data); +NativeValue* AppDebugLogPrint(NativeEngine* nativeEngine, NativeCallbackInfo* info); +NativeValue* AppInfoLogPrint(NativeEngine* nativeEngine, NativeCallbackInfo* info); +NativeValue* AppWarnLogPrint(NativeEngine* nativeEngine, NativeCallbackInfo* info); +NativeValue* AppErrorLogPrint(NativeEngine* nativeEngine, NativeCallbackInfo* info); +int PrintLog(int id, int level, const char* tag, const char* fmt, const char* message); class JsiBaseUtils { public: static void ReportJsErrorEvent(std::shared_ptr error, std::shared_ptr runtime); static std::string TransSourceStack(RefPtr runningPage, const std::string& rawStack); + // native implementation for js function: console.debug() + static shared_ptr AppDebugLogPrint(const shared_ptr& runtime, + const shared_ptr& thisObj, const std::vector>& argv, int32_t argc); + // native implementation for js function: console.info() + static shared_ptr AppInfoLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, + const std::vector>& argv, int32_t argc); + // native implementation for js function: console.warn() + static shared_ptr AppWarnLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, + const std::vector>& argv, int32_t argc); + // native implementation for js function: console.error() + static shared_ptr AppErrorLogPrint(const shared_ptr& runtime, + const shared_ptr& thisObj, const std::vector>& argv, int32_t argc); + + // native implementation for js function: aceConsole.debug() + static shared_ptr JsDebugLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, + const std::vector>& argv, int32_t argc); + // native implementation for js function: aceConsole.info() + static shared_ptr JsInfoLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, + const std::vector>& argv, int32_t argc); + // native implementation for js function: aceConsole.warn() + static shared_ptr JsWarnLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, + const std::vector>& argv, int32_t argc); + // native implementation for js function: aceConsole.error() + static shared_ptr JsErrorLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, + const std::vector>& argv, int32_t argc); private: static std::string GenerateSummaryBody(std::shared_ptr error, std::shared_ptr runtime); static std::string JsiDumpSourceFile(const std::string& stackStr, const RefPtr& pageMap, - const RefPtr& appMap); + const RefPtr& appMap, const AceType *data = nullptr); static void ExtractEachInfo(const std::string& tempStack, std::vector& res); static void GetPosInfo(const std::string& temp, std::string& line, std::string& column); static std::string GetSourceInfo(const std::string& line, const std::string& column, - const RefPtr& pageMap, const RefPtr& appMap, bool isAppPage); + const RefPtr& pageMap, const RefPtr& appMap, bool isAppPage, const AceType *data); static std::string GetRelativePath(const std::string& sources); }; } // namespace OHOS::Ace::Framework diff --git a/frameworks/bridge/js_frontend/engine/jsi/jsi_canvas_bridge.cpp b/frameworks/bridge/js_frontend/engine/jsi/jsi_canvas_bridge.cpp index fb72bb9e..693d4c4a 100755 --- a/frameworks/bridge/js_frontend/engine/jsi/jsi_canvas_bridge.cpp +++ b/frameworks/bridge/js_frontend/engine/jsi/jsi_canvas_bridge.cpp @@ -329,7 +329,9 @@ void JsiCanvasBridge::HandleJsContext(const shared_ptr& runtime, Node { "createImageData", JsCreateImageData }, { "putImageData", JsPutImageData }, { "getImageData", JsGetImageData }, +#ifdef PIXEL_MAP_SUPPORTED { "getPixelMap", JsGetPixelMap }, +#endif { "getJsonData", JsGetJsonData }, { "transferFromImageBitmap", JsTransferFromImageBitmap }, { "drawBitmapMesh", JsDrawBitmapMesh }, diff --git a/frameworks/bridge/js_frontend/engine/jsi/jsi_engine.cpp b/frameworks/bridge/js_frontend/engine/jsi/jsi_engine.cpp index 92519e4a..8ece3302 100644 --- a/frameworks/bridge/js_frontend/engine/jsi/jsi_engine.cpp +++ b/frameworks/bridge/js_frontend/engine/jsi/jsi_engine.cpp @@ -15,7 +15,9 @@ #include "frameworks/bridge/js_frontend/engine/jsi/jsi_engine.h" +#ifndef WINDOWS_PLATFORM #include +#endif #include #include @@ -2156,202 +2158,6 @@ shared_ptr JsCallComponent(const shared_ptr& runtime, const return resultValue; } -std::string ParseLogContent(const std::vector& params) -{ - std::string ret; - if (params.empty()) { - return ret; - } - std::string formatStr = params[0]; - size_t size = params.size(); - size_t len = formatStr.size(); - size_t pos = 0; - int32_t count = 1; - for (; pos < len; ++pos) { - if (static_cast(count) >= size) { - break; - } - if (formatStr[pos] == '%') { - if (pos + 1 >= len) { - break; - } - switch (formatStr[pos + 1]) { - case 's': - case 'j': - case 'd': - case 'O': - case 'o': - case 'i': - case 'f': - case 'c': - ret += params[count++]; - ++pos; - break; - case '%': - ret += formatStr[pos]; - ++pos; - break; - default: - ret += formatStr[pos]; - break; - } - } else { - ret += formatStr[pos]; - } - } - if (pos < len) { - ret += formatStr.substr(pos, len - pos); - } - return ret; -} - -std::string GetLogContent( - const shared_ptr& runtime, const std::vector>& argv, int32_t argc) -{ - if (argc == 1) { - return argv[0]->ToString(runtime); - } - std::vector params; - for (int32_t i = 0; i < argc; ++i) { - params.emplace_back(argv[i]->ToString(runtime)); - } - return ParseLogContent(params); -} - -shared_ptr AppLogPrint( - const shared_ptr& runtime, JsLogLevel level, const std::vector>& argv, int32_t argc) -{ - // Should have at least 1 parameters. - if (argc == 0) { - LOGE("the arg is error"); - return runtime->NewUndefined(); - } - std::string content = GetLogContent(runtime, argv, argc); - 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 runtime->NewUndefined(); -} - -// native implementation for js function: console.debug() -shared_ptr AppDebugLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return AppLogPrint(runtime, JsLogLevel::DEBUG, argv, argc); -} - -// native implementation for js function: console.info() -shared_ptr AppInfoLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return AppLogPrint(runtime, JsLogLevel::INFO, argv, argc); -} - -// native implementation for js function: console.warn() -shared_ptr AppWarnLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return AppLogPrint(runtime, JsLogLevel::WARNING, argv, argc); -} - -// native implementation for js function: console.error() -shared_ptr AppErrorLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return AppLogPrint(runtime, JsLogLevel::ERROR, argv, argc); -} - -shared_ptr JsLogPrint( - const shared_ptr& runtime, JsLogLevel level, const std::vector>& argv, int32_t argc) -{ - // Should have 1 parameters. - if (argc == 0) { - LOGE("the arg is error"); - return runtime->NewUndefined(); - } - - std::string content = GetLogContent(runtime, argv, argc); - switch (level) { - case JsLogLevel::DEBUG: - LOGD("ace Log: %{public}s", content.c_str()); - break; - case JsLogLevel::INFO: - LOGI("ace Log: %{public}s", content.c_str()); - break; - case JsLogLevel::WARNING: - LOGW("ace Log: %{public}s", content.c_str()); - break; - case JsLogLevel::ERROR: - LOGE("ace Log: %{public}s", content.c_str()); - break; - } - - shared_ptr ret = runtime->NewUndefined(); - return ret; -} - -int PrintLog(int id, int level, const char* tag, const char* fmt, const char* message) -{ - switch (JsLogLevel(level - 3)) { - case JsLogLevel::INFO: - LOGI("%{public}s::%{public}s", tag, message); - break; - case JsLogLevel::WARNING: - LOGW("%{public}s::%{public}s", tag, message); - break; - case JsLogLevel::ERROR: - LOGE("%{public}s::%{public}s", tag, message); - break; - case JsLogLevel::DEBUG: - LOGD("%{public}s::%{public}s", tag, message); - break; - default: - LOGF("%{public}s::%{public}s", tag, message); - break; - } - return 0; -} - -// native implementation for js function: aceConsole.debug() -shared_ptr JsDebugLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return JsLogPrint(runtime, JsLogLevel::DEBUG, std::move(argv), argc); -} - -// native implementation for js function: aceConsole.info() -shared_ptr JsInfoLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return JsLogPrint(runtime, JsLogLevel::INFO, std::move(argv), argc); -} - -// native implementation for js function: aceConsole.warn() -shared_ptr JsWarnLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return JsLogPrint(runtime, JsLogLevel::WARNING, std::move(argv), argc); -} - -// native implementation for js function: aceConsole.error() -shared_ptr JsErrorLogPrint(const shared_ptr& runtime, const shared_ptr& thisObj, - const std::vector>& argv, int32_t argc) -{ - return JsLogPrint(runtime, JsLogLevel::ERROR, std::move(argv), argc); -} - int GetNodeId(const shared_ptr& runtime, const shared_ptr& arg) { int32_t id = 0; @@ -2764,89 +2570,24 @@ void JsiEngineInstance::RegisterConsoleModule() // app log method shared_ptr consoleObj = runtime_->NewObject(); - consoleObj->SetProperty(runtime_, "log", runtime_->NewFunction(AppDebugLogPrint)); - consoleObj->SetProperty(runtime_, "debug", runtime_->NewFunction(AppDebugLogPrint)); - consoleObj->SetProperty(runtime_, "info", runtime_->NewFunction(AppInfoLogPrint)); - consoleObj->SetProperty(runtime_, "warn", runtime_->NewFunction(AppWarnLogPrint)); - consoleObj->SetProperty(runtime_, "error", runtime_->NewFunction(AppErrorLogPrint)); + consoleObj->SetProperty(runtime_, "log", runtime_->NewFunction(JsiBaseUtils::AppDebugLogPrint)); + consoleObj->SetProperty(runtime_, "debug", runtime_->NewFunction(JsiBaseUtils::AppDebugLogPrint)); + consoleObj->SetProperty(runtime_, "info", runtime_->NewFunction(JsiBaseUtils::AppInfoLogPrint)); + consoleObj->SetProperty(runtime_, "warn", runtime_->NewFunction(JsiBaseUtils::AppWarnLogPrint)); + consoleObj->SetProperty(runtime_, "error", runtime_->NewFunction(JsiBaseUtils::AppErrorLogPrint)); global->SetProperty(runtime_, "console", consoleObj); // js framework log method shared_ptr aceConsoleObj = runtime_->NewObject(); - aceConsoleObj->SetProperty(runtime_, "log", runtime_->NewFunction(JsDebugLogPrint)); - aceConsoleObj->SetProperty(runtime_, "debug", runtime_->NewFunction(JsDebugLogPrint)); - aceConsoleObj->SetProperty(runtime_, "info", runtime_->NewFunction(JsInfoLogPrint)); - aceConsoleObj->SetProperty(runtime_, "warn", runtime_->NewFunction(JsWarnLogPrint)); - aceConsoleObj->SetProperty(runtime_, "error", runtime_->NewFunction(JsErrorLogPrint)); + aceConsoleObj->SetProperty(runtime_, "log", runtime_->NewFunction(JsiBaseUtils::JsDebugLogPrint)); + aceConsoleObj->SetProperty(runtime_, "debug", runtime_->NewFunction(JsiBaseUtils::JsDebugLogPrint)); + aceConsoleObj->SetProperty(runtime_, "info", runtime_->NewFunction(JsiBaseUtils::JsInfoLogPrint)); + aceConsoleObj->SetProperty(runtime_, "warn", runtime_->NewFunction(JsiBaseUtils::JsWarnLogPrint)); + aceConsoleObj->SetProperty(runtime_, "error", runtime_->NewFunction(JsiBaseUtils::JsErrorLogPrint)); global->SetProperty(runtime_, "aceConsole", aceConsoleObj); global->SetProperty(runtime_, "callNativeHandler", runtime_->NewFunction(JsCallNativeHandler)); } -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(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"); @@ -3076,7 +2817,9 @@ bool JsiEngine::Initialize(const RefPtr& delegate) nativeEngine_ = nativeEngine; engineInstance_->SetNativeEngine(nativeEngine_); SetPostTask(nativeEngine_); +#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) nativeEngine_->CheckUVLoop(); +#endif ACE_DCHECK(delegate); if (delegate && delegate->GetAssetManager()) { @@ -3176,7 +2919,9 @@ JsiEngine::~JsiEngine() { LOG_DESTROY(); if (nativeEngine_ != nullptr) { +#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) nativeEngine_->CancelCheckUVLoop(); +#endif delete nativeEngine_; nativeEngine_ = nullptr; } diff --git a/frameworks/bridge/js_frontend/engine/jsi/jsi_group_js_bridge.cpp b/frameworks/bridge/js_frontend/engine/jsi/jsi_group_js_bridge.cpp index 1830c9df..55be2f67 100644 --- a/frameworks/bridge/js_frontend/engine/jsi/jsi_group_js_bridge.cpp +++ b/frameworks/bridge/js_frontend/engine/jsi/jsi_group_js_bridge.cpp @@ -584,4 +584,12 @@ void JsiGroupJsBridge::Destroy() runtime_.reset(); } +#if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM) +void JsiGroupJsBridge::TriggerModuleJsCallbackPreview( + int32_t callbackId, int32_t code, OHOS::Ace::ResponseData responseData) +{ + LOGE("WAIT FOR IMPLEMENTED"); +} +#endif + } // namespace OHOS::Ace::Framework diff --git a/frameworks/bridge/js_frontend/engine/jsi/jsi_group_js_bridge.h b/frameworks/bridge/js_frontend/engine/jsi/jsi_group_js_bridge.h index 05b116e2..f62d0439 100644 --- a/frameworks/bridge/js_frontend/engine/jsi/jsi_group_js_bridge.h +++ b/frameworks/bridge/js_frontend/engine/jsi/jsi_group_js_bridge.h @@ -19,6 +19,10 @@ #include #include +#if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM) +#include "adapter/preview/osal/request_data.h" +#include "adapter/preview/osal/response_data.h" +#endif #include "base/memory/ace_type.h" #include "base/utils/singleton.h" #include "frameworks/bridge/codec/standard_function_codec.h" @@ -65,6 +69,11 @@ public: void TriggerEventJsCallback(int32_t callbackId, int32_t code, std::vector&& eventData) override; +#if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM) + void TriggerModuleJsCallbackPreview( + int32_t callbackId, int32_t code, OHOS::Ace::ResponseData responseData) override; +#endif + void LoadPluginJsCode(std::string&& jsCode) override; void LoadPluginJsByteCode(std::vector&& jsCode, std::vector&& jsCodeLen) override; diff --git a/frameworks/bridge/js_frontend/engine/jsi/jsi_utils.cpp b/frameworks/bridge/js_frontend/engine/jsi/jsi_utils.cpp index 1e80bdbb..e318794d 100644 --- a/frameworks/bridge/js_frontend/engine/jsi/jsi_utils.cpp +++ b/frameworks/bridge/js_frontend/engine/jsi/jsi_utils.cpp @@ -18,7 +18,7 @@ #include "frameworks/bridge/js_frontend/engine/jsi/jsi_engine.h" namespace OHOS::Ace::Framework { -int32_t GetLineOffset() +int32_t GetLineOffset(const AceType *data) { const int32_t offset = 14; return offset; diff --git a/frameworks/bridge/js_frontend/engine/jsi/jsi_utils_windows.cpp b/frameworks/bridge/js_frontend/engine/jsi/jsi_utils_windows.cpp new file mode 100644 index 00000000..b1423865 --- /dev/null +++ b/frameworks/bridge/js_frontend/engine/jsi/jsi_utils_windows.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "frameworks/bridge/js_frontend/engine/jsi/jsi_base_utils.h" + +#include "frameworks/bridge/js_frontend/engine/jsi/jsi_engine.h" +#include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.h" + +namespace OHOS::Ace::Framework { +int32_t GetLineOffset(const AceType *data) +{ + if (AceType::InstanceOf(data)) { + const int32_t jsiOffset = 14; + return jsiOffset; + } + if (AceType::InstanceOf(data)) { + const int32_t jsiDeclarativeOffset = 0; + return jsiDeclarativeOffset; + } + return 0; +} + +RefPtr GetRunningPage(const AceType *data) +{ + if (data == nullptr) { + return nullptr; + } + if (AceType::InstanceOf(data)) { + auto instance = static_cast(data); + return instance->GetRunningPage(); + } + if (AceType::InstanceOf(data)) { + auto instance = static_cast(data); + return instance->GetRunningPage(); + } + return nullptr; +} +} // namespace OHOS::Ace::Framework