From 5921ae4f956507082469d8dc0c52ddf00a102b07 Mon Sep 17 00:00:00 2001 From: zhuhan <2281542033@qq.com> Date: Tue, 13 Aug 2024 21:57:47 +0800 Subject: [PATCH] so part Signed-off-by: zhuhan <2281542033@qq.com> Change-Id: I01304b72d8c96631f1290900bf60e1fa0b5407d0 --- frameworks/js/napi/app/ability_stage/BUILD.gn | 2 +- .../ability_stage_napi_module.cpp | 64 ++++++++ .../js/napi/configuration_constant/BUILD.gn | 2 +- .../configuration_constant_napi_module.cpp | 142 ++++++++++++++++++ .../napi/service_extension_ability/BUILD.gn | 2 +- .../service_extension_ability_module.cpp | 34 ----- .../service_extension_ability_napi_module.cpp | 57 +++++++ 7 files changed, 266 insertions(+), 37 deletions(-) create mode 100644 frameworks/js/napi/app/ability_stage/ability_stage_napi_module.cpp create mode 100644 frameworks/js/napi/configuration_constant/configuration_constant_napi_module.cpp create mode 100644 frameworks/js/napi/service_extension_ability/service_extension_ability_napi_module.cpp diff --git a/frameworks/js/napi/app/ability_stage/BUILD.gn b/frameworks/js/napi/app/ability_stage/BUILD.gn index 22f5fff80f..8379ecdda4 100644 --- a/frameworks/js/napi/app/ability_stage/BUILD.gn +++ b/frameworks/js/napi/app/ability_stage/BUILD.gn @@ -52,7 +52,7 @@ ohos_shared_library("abilitystage") { } ohos_shared_library("abilitystage_napi") { - sources = [ "ability_stage_module.cpp" ] + sources = [ "ability_stage_napi_module.cpp" ] deps = [ ":ability_stage_abc", diff --git a/frameworks/js/napi/app/ability_stage/ability_stage_napi_module.cpp b/frameworks/js/napi/app/ability_stage/ability_stage_napi_module.cpp new file mode 100644 index 0000000000..a4251e28c5 --- /dev/null +++ b/frameworks/js/napi/app/ability_stage/ability_stage_napi_module.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2024 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 "native_engine/native_engine.h" + +extern const char _binary_ability_stage_js_start[]; +extern const char _binary_ability_stage_js_end[]; +extern const char _binary_ability_stage_abc_start[]; +extern const char _binary_ability_stage_abc_end[]; + +static napi_module _module = { +#ifndef ENABLE_ERRCODE + .nm_version = 0, + .nm_filename = "application/libabilitystage_napi.so/ability_stage.js", + .nm_modname = "application.AbilityStage", +#endif +}; +extern "C" __attribute__((constructor)) +#ifndef ENABLE_ERRCODE +void NAPI_application_AbilityStage_AutoRegister() +#endif +{ + napi_module_register(&_module); +} + +extern "C" __attribute__((visibility("default"))) +#ifndef ENABLE_ERRCODE +void NAPI_application_AbilityStage_GetJSCode(const char **buf, int *bufLen) +#endif +{ + if (buf != nullptr) { + *buf = _binary_ability_stage_js_start; + } + + if (bufLen != nullptr) { + *bufLen = _binary_ability_stage_js_end - _binary_ability_stage_js_start; + } +} + +// ability_stage JS register +extern "C" __attribute__((visibility("default"))) +#ifndef ENABLE_ERRCODE +void NAPI_application_AbilityStage_GetABCCode(const char **buf, int *buflen) +#endif +{ + if (buf != nullptr) { + *buf = _binary_ability_stage_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_ability_stage_abc_end - _binary_ability_stage_abc_start; + } +} diff --git a/frameworks/js/napi/configuration_constant/BUILD.gn b/frameworks/js/napi/configuration_constant/BUILD.gn index e2dc085756..c780f98604 100644 --- a/frameworks/js/napi/configuration_constant/BUILD.gn +++ b/frameworks/js/napi/configuration_constant/BUILD.gn @@ -16,7 +16,7 @@ import("//build/ohos.gni") ohos_shared_library("configurationconstant_napi") { include_dirs = [] - sources = [ "configuration_constant_module.cpp" ] + sources = [ "configuration_constant_napi_module.cpp" ] deps = [] diff --git a/frameworks/js/napi/configuration_constant/configuration_constant_napi_module.cpp b/frameworks/js/napi/configuration_constant/configuration_constant_napi_module.cpp new file mode 100644 index 0000000000..89bbe902e7 --- /dev/null +++ b/frameworks/js/napi/configuration_constant/configuration_constant_napi_module.cpp @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2024 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 "res_common.h" +#include "napi/native_api.h" +#include "napi/native_common.h" + +namespace OHOS { +namespace AAFwk { +static napi_status SetEnumItem(napi_env env, napi_value object, const char* name, int32_t value) +{ + napi_status status; + napi_value itemName; + napi_value itemValue; + + NAPI_CALL_BASE(env, status = napi_create_string_utf8(env, name, NAPI_AUTO_LENGTH, &itemName), status); + NAPI_CALL_BASE(env, status = napi_create_int32(env, value, &itemValue), status); + + NAPI_CALL_BASE(env, status = napi_set_property(env, object, itemName, itemValue), status); + NAPI_CALL_BASE(env, status = napi_set_property(env, object, itemValue, itemName), status); + + return napi_ok; +} + +static napi_value InitColorModeObject(napi_env env) +{ + napi_value object; + NAPI_CALL(env, napi_create_object(env, &object)); + + NAPI_CALL(env, SetEnumItem(env, object, "COLOR_MODE_NOT_SET", Global::Resource::COLOR_MODE_NOT_SET)); + NAPI_CALL(env, SetEnumItem(env, object, "COLOR_MODE_DARK", Global::Resource::DARK)); + NAPI_CALL(env, SetEnumItem(env, object, "COLOR_MODE_LIGHT", Global::Resource::LIGHT)); + + return object; +} + +static napi_value InitTimeFormatObject(napi_env env) +{ + napi_value object; + NAPI_CALL(env, napi_create_object(env, &object)); + + NAPI_CALL(env, SetEnumItem(env, object, "HOUR_NOT_SET", Global::Resource::HOUR_NOT_SET)); + NAPI_CALL(env, SetEnumItem(env, object, "HOUR_12", Global::Resource::HOUR_12)); + NAPI_CALL(env, SetEnumItem(env, object, "HOUR_24", Global::Resource::HOUR_24)); + + return object; +} + +static napi_value InitDirectionObject(napi_env env) +{ + napi_value object; + NAPI_CALL(env, napi_create_object(env, &object)); + + NAPI_CALL(env, SetEnumItem(env, object, "DIRECTION_NOT_SET", Global::Resource::DIRECTION_NOT_SET)); + NAPI_CALL(env, SetEnumItem(env, object, "DIRECTION_VERTICAL", Global::Resource::DIRECTION_VERTICAL)); + NAPI_CALL(env, SetEnumItem(env, object, "DIRECTION_HORIZONTAL", Global::Resource::DIRECTION_HORIZONTAL)); + + return object; +} + +static napi_value InitScreenDensityObject(napi_env env) +{ + napi_value object; + NAPI_CALL(env, napi_create_object(env, &object)); + + NAPI_CALL(env, SetEnumItem(env, object, "SCREEN_DENSITY_NOT_SET", Global::Resource::SCREEN_DENSITY_NOT_SET)); + NAPI_CALL(env, SetEnumItem(env, object, "SCREEN_DENSITY_SDPI", Global::Resource::SCREEN_DENSITY_SDPI)); + NAPI_CALL(env, SetEnumItem(env, object, "SCREEN_DENSITY_MDPI", Global::Resource::SCREEN_DENSITY_MDPI)); + NAPI_CALL(env, SetEnumItem(env, object, "SCREEN_DENSITY_LDPI", Global::Resource::SCREEN_DENSITY_LDPI)); + NAPI_CALL(env, SetEnumItem(env, object, "SCREEN_DENSITY_XLDPI", Global::Resource::SCREEN_DENSITY_XLDPI)); + NAPI_CALL(env, SetEnumItem(env, object, "SCREEN_DENSITY_XXLDPI", Global::Resource::SCREEN_DENSITY_XXLDPI)); + NAPI_CALL(env, SetEnumItem(env, object, "SCREEN_DENSITY_XXXLDPI", Global::Resource::SCREEN_DENSITY_XXXLDPI)); + + return object; +} + +/* + * The module initialization. + */ +static napi_value ConfigurationConstantInit(napi_env env, napi_value exports) +{ + napi_value colorMode = InitColorModeObject(env); + NAPI_ASSERT(env, colorMode != nullptr, "failed to create color mode object"); + + napi_value time24 = InitTimeFormatObject(env); + NAPI_ASSERT(env, time24 != nullptr, "failed to create time format object"); + + napi_value direction = InitDirectionObject(env); + NAPI_ASSERT(env, direction != nullptr, "failed to create direction object"); + + napi_value screenDensity = InitScreenDensityObject(env); + NAPI_ASSERT(env, screenDensity != nullptr, "failed to create screen density object"); + + napi_property_descriptor exportObjs[] = { + DECLARE_NAPI_PROPERTY("ColorMode", colorMode), + DECLARE_NAPI_PROPERTY("Time24", time24), + DECLARE_NAPI_PROPERTY("Direction", direction), + DECLARE_NAPI_PROPERTY("ScreenDensity", screenDensity), + }; + + napi_status status = napi_define_properties(env, exports, sizeof(exportObjs) / sizeof(exportObjs[0]), exportObjs); + NAPI_ASSERT(env, status == napi_ok, "failed to define properties for exports"); + + return exports; +} + +/* + * The module definition. + */ +static napi_module _module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = ConfigurationConstantInit, +#ifndef ENABLE_ERRCODE + .nm_modname = "application.ConfigurationConstant", +#endif + .nm_priv = (static_cast(0)), + .reserved = {0} +}; + +/* + * The module registration. + */ +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&_module); +} +} // namespace AAFwk +} // namespace OHOS diff --git a/frameworks/js/napi/service_extension_ability/BUILD.gn b/frameworks/js/napi/service_extension_ability/BUILD.gn index fcb1d34720..91abec9b79 100644 --- a/frameworks/js/napi/service_extension_ability/BUILD.gn +++ b/frameworks/js/napi/service_extension_ability/BUILD.gn @@ -35,7 +35,7 @@ gen_js_obj("service_extension_ability_abc") { } ohos_shared_library("serviceextensionability_napi") { - sources = [ "service_extension_ability_module.cpp" ] + sources = [ "service_extension_ability_napi_module.cpp" ] deps = [ ":service_extension_ability_abc", diff --git a/frameworks/js/napi/service_extension_ability/service_extension_ability_module.cpp b/frameworks/js/napi/service_extension_ability/service_extension_ability_module.cpp index 1da7780897..5da277e182 100644 --- a/frameworks/js/napi/service_extension_ability/service_extension_ability_module.cpp +++ b/frameworks/js/napi/service_extension_ability/service_extension_ability_module.cpp @@ -54,38 +54,4 @@ void NAPI_app_ability_ServiceExtensionAbility_GetABCCode(const char **buf, int * *buflen = _binary_service_extension_ability_abc_end - _binary_service_extension_ability_abc_start; } } -#else -static napi_module _module = { - .nm_version = 0, - .nm_filename = "application/libserviceextensionability_napi.so/service_extension_ability.js", - .nm_modname = "application.ServiceExtensionAbility", -}; -extern "C" __attribute__((constructor)) -void NAPI_application_ServiceExtensionAbility_AutoRegister() -{ - napi_module_register(&_module); -} - -extern "C" __attribute__((visibility("default"))) -void NAPI_application_ServiceExtensionAbility_GetJSCode(const char **buf, int *bufLen) -{ - if (buf != nullptr) { - *buf = _binary_service_extension_ability_js_start; - } - - if (bufLen != nullptr) { - *bufLen = _binary_service_extension_ability_js_end - _binary_service_extension_ability_js_start; - } -} - -extern "C" __attribute__((visibility("default"))) -void NAPI_application_ServiceExtensionAbility_GetABCCode(const char **buf, int *buflen) -{ - if (buf != nullptr) { - *buf = _binary_service_extension_ability_abc_start; - } - if (buflen != nullptr) { - *buflen = _binary_service_extension_ability_abc_end - _binary_service_extension_ability_abc_start; - } -} #endif diff --git a/frameworks/js/napi/service_extension_ability/service_extension_ability_napi_module.cpp b/frameworks/js/napi/service_extension_ability/service_extension_ability_napi_module.cpp new file mode 100644 index 0000000000..98d14e7d70 --- /dev/null +++ b/frameworks/js/napi/service_extension_ability/service_extension_ability_napi_module.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2024 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 "native_engine/native_engine.h" + +extern const char _binary_service_extension_ability_js_start[]; +extern const char _binary_service_extension_ability_js_end[]; +extern const char _binary_service_extension_ability_abc_start[]; +extern const char _binary_service_extension_ability_abc_end[]; //zhuhan + +#ifndef ENABLE_ERRCODE +static napi_module _module = { + .nm_version = 0, + .nm_filename = "application/libserviceextensionability_napi.so/service_extension_ability.js", + .nm_modname = "application.ServiceExtensionAbility", +}; +extern "C" __attribute__((constructor)) +void NAPI_application_ServiceExtensionAbility_AutoRegister() +{ + napi_module_register(&_module); +} + +extern "C" __attribute__((visibility("default"))) +void NAPI_application_ServiceExtensionAbility_GetJSCode(const char **buf, int *bufLen) +{ + if (buf != nullptr) { + *buf = _binary_service_extension_ability_js_start; + } + + if (bufLen != nullptr) { + *bufLen = _binary_service_extension_ability_js_end - _binary_service_extension_ability_js_start; + } +} + +extern "C" __attribute__((visibility("default"))) +void NAPI_application_ServiceExtensionAbility_GetABCCode(const char **buf, int *buflen) +{ + if (buf != nullptr) { + *buf = _binary_service_extension_ability_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_service_extension_ability_abc_end - _binary_service_extension_ability_abc_start; + } +} +#endif \ No newline at end of file