diff --git a/bundle.json b/bundle.json index 7b68098c0b9c193e55ea30b03140638ca50c8e0d..1aee9c45c54b74c4e3a7bb4a1ec542d302fb05c9 100644 --- a/bundle.json +++ b/bundle.json @@ -47,6 +47,7 @@ "deps": { "components": [ "ace_engine", + "runtime_core", "bounds_checking_function", "napi", "hilog", @@ -70,6 +71,7 @@ "sub_component": [ "//commonlibrary/ets_utils/base_sdk:base_sdk_ets", "//commonlibrary/ets_utils/base_sdk:base_transfer_ets", + "//commonlibrary/ets_utils/js_demo_module/demo:commonlib_demo", "//commonlibrary/ets_utils/js_api_module/uri:uri_packages", "//commonlibrary/ets_utils/js_api_module/url:url_packages", "//commonlibrary/ets_utils/js_api_module/convertxml:convertxml_packages", diff --git a/js_demo_module/demo/BUILD.gn b/js_demo_module/demo/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..0e7cd2fd2c4f6b923c7cb0ea4298dcde6932084e --- /dev/null +++ b/js_demo_module/demo/BUILD.gn @@ -0,0 +1,72 @@ +# Copyright (c) 2021-2025 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. + +import("//build/config/components/ets_frontend/ets2abc_config.gni") +import("//build/ohos.gni") +import("//commonlibrary/ets_utils/ets_utils_config.gni") + +ohos_prebuilt_etc("commonlib_ets_utils_demo") { + source = "$target_out_dir/commonlib_ets_utils.abc" + module_install_dir = "framework" + subsystem_name = "commonlibrary" + part_name = "ets_utils" + deps = [ ":commonlib_ets_utils" ] +} + +generate_static_abc("commonlib_ets_utils") { + base_url = "./src" + files = [ + "./src/@ohos.demo.ets", + ] + is_boot_abc = "True" + device_dst_file = "/system/framework/commonlib_ets_utils.abc" +} + +ets_util_sources = [ + "$ets_util_path/js_demo_module/demo/demo.cpp", + "$ets_util_path/js_demo_module/demo/demoAni.cpp", +] + +ohos_shared_library("commonlib_ets_ani") { + include_dirs = [ ets_util_path ] + + configs = [] + sources = ets_util_sources + + external_deps = [ + "icu:shared_icuuc", + "openssl:libcrypto_shared", + "runtime_core:ani", + "napi:ace_napi", + ] + if (is_standard_system) { + external_deps += [ "hilog:libhilog" ] + } else { + external_deps += [ + "c_utils:utils", + "hilog:libhilog", + ] + } + external_deps += [ "bounds_checking_function:libsec_shared" ] + + part_name = "ets_utils" + subsystem_name = "commonlibrary" + output_extension = "so" +} + +group("commonlib_demo") { + deps = [ + ":commonlib_ets_ani", + ":commonlib_ets_utils_demo", + ] +} \ No newline at end of file diff --git a/js_demo_module/demo/demo.cpp b/js_demo_module/demo/demo.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1837a600a36aa79a5219cb3d5c11f4b81d55ca84 --- /dev/null +++ b/js_demo_module/demo/demo.cpp @@ -0,0 +1,86 @@ +#include + +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "native_engine.h" +#include "stdlib/native/core/stdlib_ani_helpers.h" + +class Demo { + public: + explicit Demo(std:: string input): encoding_(input) {}; + + private: + std::string encoding_ {}; +} + +static napi_value GetDemoObject(napi_env env, napi_callback_info info) +{ + auto objectInfo = new Demo("utf-8"); + napi_value result = nullptr; + napi_value res = napi_create_object(env, &result); + napi_status status = napi_wrap(env, res, objectInfo, + [](napi_env environment, void *data, void *hint) { + auto objInfo = reinterpret_cast(data); + if (objInfo != nullptr) { + delete objInfo; + objInfo = nullptr; + } + }, nullptr, nullptr); + if (status != napi_ok && objectInfo != nullptr) { + delete objectInfo; + objectInfo = nullptr; + } + return res; +} + +static napi_value DemoInit(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + { "getObject", nullptr, GetDemoObject, nullptr, nullptr, nullptr, napi_default, nullptr } + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} + +// demo JS register +extern "C" +__attribute__((visibility("default"))) void NAPI_demo_GetJSCode(const char **buf, int *buflen) +{ + if (buf != nullptr) { + *buf = _binary_demo_js_js_start; + } + if (buflen != nullptr) { + *buflen = _binary_demo_js_js_end - _binary_demo_js_js_start; + } +} + +extern "C" +__attribute__((visibility("default"))) void NAPI_demo_GetABCCode(const char** buf, int* buflen) +{ + if (buf != nullptr) { + *buf = _binary_demo_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_demo_abc_end - _binary_demo_abc_start; + } +} + +// demo module define +static napi_module_with_js demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = DemoInit, + .nm_modname = "demo", + .nm_priv = ((void*)0), + .nm_get_abc_code = NAPI_demo_GetABCCode, + .nm_get_js_code = NAPI_demo_GetJSCode, +}; + +// demo module register +extern "C" +__attribute__((constructor)) void demoRegisterModule() +{ + napi_module_with_js_register(&demoModule); + demoPluginJniRegister(); +} \ No newline at end of file diff --git a/js_demo_module/demo/demoAni.cpp b/js_demo_module/demo/demoAni.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc1074c9db38b6383d24a7f639b29928a10cc408 --- /dev/null +++ b/js_demo_module/demo/demoAni.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include "stdlib/native/core/stdlib_ani_helpers.h" + +static void BindNativeDemo(ani_env *env, ani_object object, ani_string aniEncoding) +{ + std::string stringEncoding = ark::ets::stdlib::ConvertFromAniString(env, aniEncoding); + auto nativeDemo = new Demo(stringEncoding); + env->Object_SetFieldByName_Long(object, "nativeDemo_", reinterpret_cast(nativeDemo)); +} + +static ani_long TransferDemoToStatic([[maybe_unused]] ani_env *env, ani_object object, ani_object esValue) +{ + void *nativePtr = nullptr; + if (!arkts_esvalue_unwrap(env, esValue, &nativePtr) || nativePtr == nullptr) { + return 0; + } + env->Object_SetFieldByName_Long(object, "nativeDemo_", reinterpret_cast(nativePtr)); +} + +static ani_status BindDemo(ani_env *env) +{ + const char *className = "L@ohos/demo/Demo;"; + ani_class cls; + if (ANI_OK != env->FindClass(className, &cls)) { + std::cerr << "Not found '" << className << "'" << std::endl; + return ANI_ERROR; + } + + std::array methods = { + ani_native_function {"bindNativeDemo", "Lstd/core/String;:V", reinterpret_cast(BindNativeDemo)}, + ani_native_function {"transferDemoToStatic", "Lstd/core/String;:V", reinterpret_cast(TransferDemoToStatic)}, + }; + + if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) + LOG_ERROR_SDK("Demo:: Cannot bind native methods to className : %{public}s", className); + return ANI_ERROR; + } + return ANI_OK; +} + +extern "C" ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + ani_env *env; + if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { + std::cerr << "Unsupported ANI_VERSION_1" << std::endl; + return ANI_ERROR; + } + // NOLINTNEXTLINE(hicpp-signed-bitwise,-warnings-as-errors) + auto status = static_cast(BindDemo(env)); + if (status != ANI_OK) { + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} \ No newline at end of file diff --git a/js_demo_module/demo/src/@ohos.demo.ets b/js_demo_module/demo/src/@ohos.demo.ets new file mode 100644 index 0000000000000000000000000000000000000000..df87bc8d65700cea092cf6d51c1aa56da52b33e5 --- /dev/null +++ b/js_demo_module/demo/src/@ohos.demo.ets @@ -0,0 +1,24 @@ +type Any = Object | undefined | null; + +export class Demo { + private nativeDemo_: long; + constructor(input: string) { + this.bindNativeDemo(input); + } + + static transferDemoStatic(input: Any): Object { + let newDemo: Demo = new Demo("utf-8"); + this.transferDemoToStatic(newDemo, input); + return newDemo as Object; + } + + private native bindNativeDemo(input: string): void; + private native transferDemoToStatic(newDemo: Demo, input: Any); + + + // static transferDemoDynamic(input: Object): Any { + // return transferDemoToDynamic(input); + // } + + // private native transferDemoToDynamic(input: Object): Any; +} \ No newline at end of file