From 5dec53c0f0d5d7af361ecaccfded028ba27586ec Mon Sep 17 00:00:00 2001 From: guojin31 Date: Tue, 19 Sep 2023 20:01:27 +0800 Subject: [PATCH] =?UTF-8?q?NAPI=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guojin31 --- .../inputmethod_extension_ability_module.cpp | 16 +- .../inputmethod_extension_context_module.cpp | 15 +- .../js/napi/inputmethodability/js_panel.cpp | 6 +- .../include/js_inputmethod_extension.h | 5 +- .../js_inputmethod_extension_context.h | 12 +- .../src/js_inputmethod_extension.cpp | 127 +++--- .../src/js_inputmethod_extension_context.cpp | 385 +++++++++--------- 7 files changed, 282 insertions(+), 284 deletions(-) diff --git a/frameworks/js/napi/inputmethod_extension_ability/inputmethod_extension_ability_module.cpp b/frameworks/js/napi/inputmethod_extension_ability/inputmethod_extension_ability_module.cpp index d355db9f2..69dc7b25d 100644 --- a/frameworks/js/napi/inputmethod_extension_ability/inputmethod_extension_ability_module.cpp +++ b/frameworks/js/napi/inputmethod_extension_ability/inputmethod_extension_ability_module.cpp @@ -20,16 +20,16 @@ extern const char _binary_inputmethod_extension_ability_js_end[]; extern const char _binary_inputmethod_extension_ability_abc_start[]; extern const char _binary_inputmethod_extension_ability_abc_end[]; +static napi_module g_ExtensionModule = { + .nm_version = 0, + .nm_modname = "InputMethodExtensionAbility", + .nm_filename = "libinputmethodextensionability_napi.so/inputmethod_extension_ability.js", +}; + + extern "C" __attribute__((constructor)) void NAPI_InputMethodExtensionAbility_AutoRegister() { - auto moduleManager = NativeModuleManager::GetInstance(); - NativeModule newModuleInfo = { - .name = "InputMethodExtensionAbility", - .fileName = "libinputmethodextensionability_napi.so/inputmethod_extension_ability.js", - }; - if (moduleManager != nullptr) { - moduleManager->Register(&newModuleInfo); - } + napi_module_register(&g_ExtensionModule); } extern "C" __attribute__((visibility("default"))) void NAPI_InputMethodExtensionAbility_GetJSCode( diff --git a/frameworks/js/napi/inputmethod_extension_context/inputmethod_extension_context_module.cpp b/frameworks/js/napi/inputmethod_extension_context/inputmethod_extension_context_module.cpp index 80c937f46..bfc41f2f0 100644 --- a/frameworks/js/napi/inputmethod_extension_context/inputmethod_extension_context_module.cpp +++ b/frameworks/js/napi/inputmethod_extension_context/inputmethod_extension_context_module.cpp @@ -20,16 +20,15 @@ extern const char _binary_inputmethod_extension_context_js_end[]; extern const char _binary_inputmethod_extension_context_abc_start[]; extern const char _binary_inputmethod_extension_context_abc_end[]; +static napi_module g_ExtensionContextModule = { + .nm_version = 0, + .nm_modname = "InputMethodExtensionContext", + .nm_filename = "libinputmethodextensioncontext_napi.so/inputmethod_extension_context.js", +}; + extern "C" __attribute__((constructor)) void NAPI_InputMethodExtensionContext_AutoRegister() { - auto moduleManager = NativeModuleManager::GetInstance(); - NativeModule newModuleInfo = { - .name = "InputMethodExtensionContext", - .fileName = "libinputmethodextensioncontext_napi.so/inputmethod_extension_context.js", - }; - if (moduleManager != nullptr) { - moduleManager->Register(&newModuleInfo); - } + napi_module_register(&g_ExtensionContextModule); } extern "C" __attribute__((visibility("default"))) void NAPI_InputMethodExtensionContext_GetJSCode( diff --git a/frameworks/js/napi/inputmethodability/js_panel.cpp b/frameworks/js/napi/inputmethodability/js_panel.cpp index 3deac8be1..0ff2d86ed 100644 --- a/frameworks/js/napi/inputmethodability/js_panel.cpp +++ b/frameworks/js/napi/inputmethodability/js_panel.cpp @@ -109,12 +109,12 @@ napi_value JsPanel::SetUiContent(napi_env env, napi_callback_info info) status = napi_typeof(env, argv[1], &valueType); CHECK_RETURN(status == napi_ok, "get valueType failed!", status); if (valueType == napi_object) { - NativeValue *storage = nullptr; - storage = reinterpret_cast(argv[1]); + napi_ref storage = nullptr; + napi_create_reference(env, argv[1], 1, &storage); auto contentStorage = (storage == nullptr) ? nullptr : std::shared_ptr( - reinterpret_cast(env)->CreateReference(storage, 1)); + reinterpret_cast(storage)); ctxt->contentStorage = contentStorage; } } diff --git a/frameworks/kits/extension/include/js_inputmethod_extension.h b/frameworks/kits/extension/include/js_inputmethod_extension.h index be57dcc95..68f8eb59d 100644 --- a/frameworks/kits/extension/include/js_inputmethod_extension.h +++ b/frameworks/kits/extension/include/js_inputmethod_extension.h @@ -18,7 +18,6 @@ #include "inputmethod_extension.h" #include "js_runtime.h" -#include "native_engine/native_value.h" namespace OHOS { namespace AbilityRuntime { @@ -105,9 +104,9 @@ public: virtual void OnStop() override; private: - NativeValue *CallObjectMethod(const char *name, NativeValue *const *argv = nullptr, size_t argc = 0); + napi_value CallObjectMethod(const char *name, const napi_value *argv = nullptr, size_t argc = 0); - void BindContext(NativeEngine &engine, NativeObject *obj); + void BindContext(napi_env env, napi_value obj); void GetSrcPath(std::string &srcPath); diff --git a/frameworks/kits/extension/include/js_inputmethod_extension_context.h b/frameworks/kits/extension/include/js_inputmethod_extension_context.h index eea4d3f5f..1fae8aa98 100644 --- a/frameworks/kits/extension/include/js_inputmethod_extension_context.h +++ b/frameworks/kits/extension/include/js_inputmethod_extension_context.h @@ -26,12 +26,12 @@ namespace OHOS { namespace AbilityRuntime { -NativeValue *CreateJsInputMethodExtensionContext( - NativeEngine &engine, std::shared_ptr context); +napi_value CreateJsInputMethodExtensionContext( + napi_env env, std::shared_ptr context); class JSInputMethodExtensionConnection : public AbilityConnectCallback { public: - explicit JSInputMethodExtensionConnection(NativeEngine &engine); + explicit JSInputMethodExtensionConnection(napi_env env); ~JSInputMethodExtensionConnection(); void OnAbilityConnectDone( const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) override; @@ -39,11 +39,11 @@ public: void HandleOnAbilityConnectDone( const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode); void HandleOnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode); - void SetJsConnectionObject(NativeValue *jsConnectionObject); + void SetJsConnectionObject(napi_value jsConnectionObject); void CallJsFailed(int32_t errorCode); private: - NativeEngine &engine_; + napi_env env_; std::unique_ptr jsConnectionObject_ = nullptr; }; @@ -64,4 +64,4 @@ static int64_t serialNumber_ = 0; static std::shared_ptr handler_ = nullptr; } // namespace AbilityRuntime } // namespace OHOS -#endif // ABILITY_RUNTIME_JS_INPUTMETHOD_EXTENSION_CONTEXT_H +#endif // ABILITY_RUNTIME_JS_INPUTMETHOD_EXTENSION_CONTEXT_H \ No newline at end of file diff --git a/frameworks/kits/extension/src/js_inputmethod_extension.cpp b/frameworks/kits/extension/src/js_inputmethod_extension.cpp index 7e7dfd1b0..5597a49fe 100644 --- a/frameworks/kits/extension/src/js_inputmethod_extension.cpp +++ b/frameworks/kits/extension/src/js_inputmethod_extension.cpp @@ -38,7 +38,7 @@ JsInputMethodExtension *JsInputMethodExtension::jsInputMethodExtension = nullptr using namespace OHOS::AppExecFwk; using namespace OHOS::MiscServices; -NativeValue *AttachInputMethodExtensionContext(NativeEngine *engine, void *value, void *) +napi_value AttachInputMethodExtensionContext(napi_env env, void *value, void *) { IMSA_HILOGI("AttachInputMethodExtensionContext"); if (value == nullptr) { @@ -50,20 +50,18 @@ NativeValue *AttachInputMethodExtensionContext(NativeEngine *engine, void *value IMSA_HILOGW("invalid context."); return nullptr; } - NativeValue *object = CreateJsInputMethodExtensionContext(*engine, ptr); - auto contextObj = JsRuntime::LoadSystemModuleByEngine(engine, "InputMethodExtensionContext", &object, 1)->Get(); - NativeObject *nObject = ConvertNativeValueTo(contextObj); - nObject->ConvertToNativeBindingObject( - engine, DetachCallbackFunc, AttachInputMethodExtensionContext, value, nullptr); + napi_value object = CreateJsInputMethodExtensionContext(env, ptr); + auto contextObj = JsRuntime::LoadSystemModuleByEngine( + env, "InputMethodExtensionContext", &object, 1)->GetNapiValue(); + napi_coerce_to_native_binding_object( + env, contextObj, DetachCallbackFunc, AttachInputMethodExtensionContext, value, nullptr); auto workContext = new (std::nothrow) std::weak_ptr(ptr); - nObject->SetNativePointer( - workContext, - [](NativeEngine *, void *data, void *) { + napi_wrap(env, contextObj, workContext, + [](napi_env, void *data, void *) { IMSA_HILOGI("Finalizer for weak_ptr input method extension context is called"); delete static_cast *>(data); - }, - nullptr); - return contextObj; + }, nullptr, nullptr); + return object; } JsInputMethodExtension *JsInputMethodExtension::Create(const std::unique_ptr &runtime) @@ -100,25 +98,24 @@ void JsInputMethodExtension::Init(const std::shared_ptr &rec IMSA_HILOGI( "JsInputMethodExtension::Init module:%{public}s,srcPath:%{public}s.", moduleName.c_str(), srcPath.c_str()); HandleScope handleScope(jsRuntime_); - auto &engine = jsRuntime_.GetNativeEngine(); - + napi_env env = jsRuntime_.GetNapiEnv(); jsObj_ = jsRuntime_.LoadModule( moduleName, srcPath, abilityInfo_->hapPath, abilityInfo_->compileMode == CompileMode::ES_MODULE); if (jsObj_ == nullptr) { IMSA_HILOGE("Failed to get jsObj_"); return; } - IMSA_HILOGI("JsInputMethodExtension::Init ConvertNativeValueTo."); - NativeObject *obj = ConvertNativeValueTo(jsObj_->Get()); + IMSA_HILOGI("JsInputMethodExtension::Init GetNapiValue."); + napi_value obj = jsObj_->GetNapiValue(); if (obj == nullptr) { IMSA_HILOGE("Failed to get JsInputMethodExtension object"); return; } - BindContext(engine, obj); + BindContext(env, obj); IMSA_HILOGI("JsInputMethodExtension::Init end."); } -void JsInputMethodExtension::BindContext(NativeEngine &engine, NativeObject *obj) +void JsInputMethodExtension::BindContext(napi_env env, napi_value obj) { IMSA_HILOGI("JsInputMethodExtension::BindContext"); auto context = GetContext(); @@ -127,28 +124,25 @@ void JsInputMethodExtension::BindContext(NativeEngine &engine, NativeObject *obj return; } IMSA_HILOGI("JsInputMethodExtension::Init CreateJsInputMethodExtensionContext."); - NativeValue *contextObj = CreateJsInputMethodExtensionContext(engine, context); + napi_value contextObj = CreateJsInputMethodExtensionContext(env, context); auto shellContextRef = jsRuntime_.LoadSystemModule("InputMethodExtensionContext", &contextObj, ARGC_ONE); - contextObj = shellContextRef->Get(); - auto nativeObj = ConvertNativeValueTo(contextObj); - if (nativeObj == nullptr) { + contextObj = shellContextRef->GetNapiValue(); + if (contextObj == nullptr) { IMSA_HILOGE("Failed to get input method extension native object"); return; } auto workContext = new (std::nothrow) std::weak_ptr(context); - nativeObj->ConvertToNativeBindingObject( - &engine, DetachCallbackFunc, AttachInputMethodExtensionContext, workContext, nullptr); + napi_coerce_to_native_binding_object( + env, contextObj, DetachCallbackFunc, AttachInputMethodExtensionContext, workContext, nullptr); IMSA_HILOGI("JsInputMethodExtension::Init Bind."); context->Bind(jsRuntime_, shellContextRef.release()); IMSA_HILOGI("JsInputMethodExtension::SetProperty."); - obj->SetProperty("context", contextObj); - nativeObj->SetNativePointer( - workContext, - [](NativeEngine *, void *data, void *) { + napi_set_named_property(env, obj, "context", contextObj); + napi_wrap(env, contextObj, workContext, + [](napi_env, void *data, void *) { IMSA_HILOGI("Finalizer for weak_ptr input method extension context is called"); delete static_cast *>(data); - }, - nullptr); + }, nullptr, nullptr); } void JsInputMethodExtension::OnStart(const AAFwk::Want &want) @@ -159,10 +153,9 @@ void JsInputMethodExtension::OnStart(const AAFwk::Want &want) FinishAsync("Extension::OnStart", static_cast(TraceTaskId::ONSTART_MIDDLE_EXTENSION)); IMSA_HILOGI("JsInputMethodExtension OnStart begin.."); HandleScope handleScope(jsRuntime_); - NativeEngine *nativeEngine = &jsRuntime_.GetNativeEngine(); - napi_value napiWant = OHOS::AppExecFwk::WrapWant(reinterpret_cast(nativeEngine), want); - NativeValue *nativeWant = reinterpret_cast(napiWant); - NativeValue *argv[] = { nativeWant }; + napi_env env = jsRuntime_.GetNapiEnv(); + napi_value napiWant = OHOS::AppExecFwk::WrapWant(env, want); + napi_value argv[] = { napiWant }; StartAsync("onCreate", static_cast(TraceTaskId::ONCREATE_EXTENSION)); CallObjectMethod("onCreate", argv, ARGC_ONE); FinishAsync("onCreate", static_cast(TraceTaskId::ONCREATE_EXTENSION)); @@ -192,34 +185,32 @@ sptr JsInputMethodExtension::OnConnect(const AAFwk::Want &want) FinishAsync("Extension::OnConnect", static_cast(TraceTaskId::ONCONNECT_MIDDLE_EXTENSION)); IMSA_HILOGI("%{public}s begin.", __func__); HandleScope handleScope(jsRuntime_); - NativeEngine *nativeEngine = &jsRuntime_.GetNativeEngine(); - napi_value napiWant = OHOS::AppExecFwk::WrapWant(reinterpret_cast(nativeEngine), want); - NativeValue *nativeWant = reinterpret_cast(napiWant); - NativeValue *argv[] = { nativeWant }; + napi_env env = (napi_env)jsRuntime_.GetNativeEnginePointer(); + napi_value napiWant = OHOS::AppExecFwk::WrapWant(env, want); + napi_value argv[] = { napiWant }; if (jsObj_ == nullptr) { IMSA_HILOGE("Not found InputMethodExtension.js"); return nullptr; } - NativeValue *value = jsObj_->Get(); - NativeObject *obj = ConvertNativeValueTo(value); + napi_value obj = jsObj_->GetNapiValue(); if (obj == nullptr) { IMSA_HILOGE("Failed to get InputMethodExtension object"); return nullptr; } - - NativeValue *method = obj->GetProperty("onConnect"); + napi_value method = nullptr; + napi_get_named_property(env, obj, "onConnect", &method); if (method == nullptr) { IMSA_HILOGE("Failed to get onConnect from InputMethodExtension object"); return nullptr; } IMSA_HILOGI("JsInputMethodExtension::CallFunction onConnect, success"); - NativeValue *remoteNative = nativeEngine->CallFunction(value, method, argv, ARGC_ONE); - if (remoteNative == nullptr) { + napi_value remoteNapi = nullptr; + napi_call_function(env, obj, method, ARGC_ONE, argv, &remoteNapi); + if (remoteNapi == nullptr) { IMSA_HILOGE("remoteNative nullptr."); } - auto remoteObj = NAPI_ohos_rpc_getNativeRemoteObject( - reinterpret_cast(nativeEngine), reinterpret_cast(remoteNative)); + auto remoteObj = NAPI_ohos_rpc_getNativeRemoteObject(env, remoteNapi); if (remoteObj == nullptr) { IMSA_HILOGE("remoteObj nullptr."); } @@ -233,28 +224,28 @@ void JsInputMethodExtension::OnDisconnect(const AAFwk::Want &want) Extension::OnDisconnect(want); IMSA_HILOGI("%{public}s begin.", __func__); HandleScope handleScope(jsRuntime_); - NativeEngine *nativeEngine = &jsRuntime_.GetNativeEngine(); - napi_value napiWant = OHOS::AppExecFwk::WrapWant(reinterpret_cast(nativeEngine), want); - NativeValue *nativeWant = reinterpret_cast(napiWant); - NativeValue *argv[] = { nativeWant }; + napi_env env = jsRuntime_.GetNapiEnv(); + napi_value napiWant = OHOS::AppExecFwk::WrapWant(env, want); + napi_value argv[] = { napiWant }; if (jsObj_ == nullptr) { IMSA_HILOGE("Not found InputMethodExtension.js"); return; } - NativeValue *value = jsObj_->Get(); - NativeObject *obj = ConvertNativeValueTo(value); + napi_value obj = jsObj_->GetNapiValue(); if (obj == nullptr) { IMSA_HILOGE("Failed to get InputMethodExtension object"); return; } - NativeValue *method = obj->GetProperty("onDisconnect"); + napi_value method = nullptr; + napi_get_named_property(env, obj, "onDisconnect", &method); if (method == nullptr) { IMSA_HILOGE("Failed to get onDisconnect from InputMethodExtension object"); return; } - nativeEngine->CallFunction(value, method, argv, ARGC_ONE); + napi_value remoteNapi = nullptr; + napi_call_function(env, obj, method, ARGC_ONE, argv, &remoteNapi); IMSA_HILOGI("%{public}s end.", __func__); } @@ -265,18 +256,16 @@ void JsInputMethodExtension::OnCommand(const AAFwk::Want &want, bool restart, in IMSA_HILOGI( "%{public}s begin restart=%{public}s,startId=%{public}d.", __func__, restart ? "true" : "false", startId); HandleScope handleScope(jsRuntime_); - NativeEngine *nativeEngine = &jsRuntime_.GetNativeEngine(); - napi_value napiWant = OHOS::AppExecFwk::WrapWant(reinterpret_cast(nativeEngine), want); - NativeValue *nativeWant = reinterpret_cast(napiWant); + napi_env env = jsRuntime_.GetNapiEnv(); + napi_value napiWant = OHOS::AppExecFwk::WrapWant(env, want); napi_value napiStartId = nullptr; - napi_create_int32(reinterpret_cast(nativeEngine), startId, &napiStartId); - NativeValue *nativeStartId = reinterpret_cast(napiStartId); - NativeValue *argv[] = { nativeWant, nativeStartId }; + napi_create_int32(env, startId, &napiStartId); + napi_value argv[] = { napiWant, napiStartId }; CallObjectMethod("onRequest", argv, ARGC_TWO); IMSA_HILOGI("%{public}s end.", __func__); } -NativeValue *JsInputMethodExtension::CallObjectMethod(const char *name, NativeValue *const *argv, size_t argc) +napi_value JsInputMethodExtension::CallObjectMethod(const char *name, const napi_value *argv, size_t argc) { IMSA_HILOGI("JsInputMethodExtension::CallObjectMethod(%{public}s), begin", name); @@ -286,22 +275,26 @@ NativeValue *JsInputMethodExtension::CallObjectMethod(const char *name, NativeVa } HandleScope handleScope(jsRuntime_); - auto &nativeEngine = jsRuntime_.GetNativeEngine(); - - NativeValue *value = jsObj_->Get(); - NativeObject *obj = ConvertNativeValueTo(value); + napi_env env = jsRuntime_.GetNapiEnv(); + napi_value obj = jsObj_->GetNapiValue(); if (obj == nullptr) { IMSA_HILOGE("Failed to get InputMethodExtension object"); return nullptr; } - NativeValue *method = obj->GetProperty(name); + napi_value method = nullptr; + napi_get_named_property(env, obj, name, &method); if (method == nullptr) { IMSA_HILOGE("Failed to get '%{public}s' from InputMethodExtension object", name); return nullptr; } IMSA_HILOGI("JsInputMethodExtension::CallFunction(%{public}s), success", name); - return nativeEngine.CallFunction(value, method, argv, argc); + napi_value remoteNapi = nullptr; + napi_status status = napi_call_function(env, obj, method, argc, argv, &remoteNapi); + if (status != napi_ok) { + return nullptr; + } + return remoteNapi; } void JsInputMethodExtension::GetSrcPath(std::string &srcPath) diff --git a/frameworks/kits/extension/src/js_inputmethod_extension_context.cpp b/frameworks/kits/extension/src/js_inputmethod_extension_context.cpp index 2ae570eb2..e0b330fa0 100644 --- a/frameworks/kits/extension/src/js_inputmethod_extension_context.cpp +++ b/frameworks/kits/extension/src/js_inputmethod_extension_context.cpp @@ -50,84 +50,79 @@ public: { } ~JsInputMethodExtensionContext() = default; + JsInputMethodExtensionContext() = default; - static void Finalizer(NativeEngine *engine, void *data, void *hint) + static void Finalizer(napi_env env, void *data, void *hint) { IMSA_HILOGI("JsInputMethodExtensionContext::Finalizer is called"); std::unique_ptr(static_cast(data)); } - static NativeValue *StartAbility(NativeEngine *engine, NativeCallbackInfo *info) + static napi_value StartAbility(napi_env env, napi_callback_info info) { - JsInputMethodExtensionContext *me = CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnStartAbility(*engine, *info) : nullptr; + GET_CB_INFO_AND_CALL(env, info, JsInputMethodExtensionContext, OnStartAbility); } - static NativeValue *StartAbilityWithAccount(NativeEngine *engine, NativeCallbackInfo *info) + static napi_value StartAbilityWithAccount(napi_env env, napi_callback_info info) { - JsInputMethodExtensionContext *me = CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnStartAbilityWithAccount(*engine, *info) : nullptr; + GET_CB_INFO_AND_CALL(env, info, JsInputMethodExtensionContext, OnStartAbilityWithAccount); } - static NativeValue *ConnectAbilityWithAccount(NativeEngine *engine, NativeCallbackInfo *info) + static napi_value ConnectAbilityWithAccount(napi_env env, napi_callback_info info) { - JsInputMethodExtensionContext *me = CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnConnectAbilityWithAccount(*engine, *info) : nullptr; + GET_CB_INFO_AND_CALL(env, info, JsInputMethodExtensionContext, OnConnectAbilityWithAccount); } - static NativeValue *TerminateAbility(NativeEngine *engine, NativeCallbackInfo *info) + static napi_value TerminateAbility(napi_env env, napi_callback_info info) { - JsInputMethodExtensionContext *me = CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnTerminateAbility(*engine, *info) : nullptr; + GET_CB_INFO_AND_CALL(env, info, JsInputMethodExtensionContext, OnTerminateAbility); } - static NativeValue *ConnectAbility(NativeEngine *engine, NativeCallbackInfo *info) + static napi_value ConnectAbility(napi_env env, napi_callback_info info) { - JsInputMethodExtensionContext *me = CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnConnectAbility(*engine, *info) : nullptr; + GET_CB_INFO_AND_CALL(env, info, JsInputMethodExtensionContext, OnConnectAbility); } - static NativeValue *DisconnectAbility(NativeEngine *engine, NativeCallbackInfo *info) + static napi_value DisconnectAbility(napi_env env, napi_callback_info info) { - JsInputMethodExtensionContext *me = CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnDisconnectAbility(*engine, *info) : nullptr; + GET_CB_INFO_AND_CALL(env, info, JsInputMethodExtensionContext, OnDisconnectAbility); } private: std::weak_ptr context_; - NativeValue *OnStartAbility(NativeEngine &engine, NativeCallbackInfo &info) + napi_value OnStartAbility(napi_env env, size_t argc, napi_value *argv) { IMSA_HILOGI("InputMethodExtensionContext OnStartAbility"); // only support one or two or three params - if (info.argc != ARGC_ONE && info.argc != ARGC_TWO && info.argc != ARGC_THREE) { + if (argc != ARGC_ONE && argc != ARGC_TWO && argc != ARGC_THREE) { IMSA_HILOGE("Not enough params"); - return engine.CreateUndefined(); + return CreateJsUndefined(env); } - decltype(info.argc) unwrapArgc = 0; + decltype(argc) unwrapArgc = 0; AAFwk::Want want; - OHOS::AppExecFwk::UnwrapWant( - reinterpret_cast(&engine), reinterpret_cast(info.argv[INDEX_ZERO]), want); + OHOS::AppExecFwk::UnwrapWant(env, argv[INDEX_ZERO], want); IMSA_HILOGI("%{public}s bundlename:%{public}s abilityname:%{public}s", __func__, want.GetBundle().c_str(), want.GetElement().GetAbilityName().c_str()); unwrapArgc++; AAFwk::StartOptions startOptions; - if (info.argc > ARGC_ONE && info.argv[INDEX_ONE]->TypeOf() == NATIVE_OBJECT) { + napi_valuetype valueType = napi_undefined; + napi_typeof(env, argv[INDEX_ONE], &valueType); + if (argc > ARGC_ONE && valueType == napi_object) { IMSA_HILOGI("OnStartAbility start options is used."); - AppExecFwk::UnwrapStartOptions( - reinterpret_cast(&engine), reinterpret_cast(info.argv[INDEX_ONE]), startOptions); + AppExecFwk::UnwrapStartOptions(env, argv[INDEX_ONE], startOptions); unwrapArgc++; } - AsyncTask::CompleteCallback complete = [weak = context_, want, startOptions, unwrapArgc]( - NativeEngine &engine, AsyncTask &task, int32_t status) { + NapiAsyncTask::CompleteCallback complete = [weak = context_, want, startOptions, unwrapArgc]( + napi_env env, NapiAsyncTask &task, int32_t status) { IMSA_HILOGI("startAbility begin"); auto context = weak.lock(); if (context == nullptr) { IMSA_HILOGW("context is released"); - task.Reject(engine, CreateJsError(engine, ERROR_CODE_ONE, "Context is released")); + task.Reject(env, CreateJsError(env, ERROR_CODE_ONE, "Context is released")); return; } @@ -135,121 +130,122 @@ private: (unwrapArgc == 1) ? errcode = context->StartAbility(want) : errcode = context->StartAbility(want, startOptions); if (errcode == 0) { - task.Resolve(engine, engine.CreateUndefined()); + task.Resolve(env, CreateJsUndefined(env)); } else { - task.Reject(engine, CreateJsError(engine, errcode, "Start Ability failed.")); + task.Reject(env, CreateJsError(env, errcode, "Start Ability failed.")); } }; - NativeValue *lastParam = (info.argc == unwrapArgc) ? nullptr : info.argv[unwrapArgc]; - NativeValue *result = nullptr; - AsyncTask::Schedule("InputMethodExtensionContext::OnStartAbility", engine, - CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); + napi_value lastParam = argc > unwrapArgc ? argv[unwrapArgc] : nullptr; + napi_value result = nullptr; + NapiAsyncTask::Schedule("InputMethodExtensionContext::OnStartAbility", env, + CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result)); return result; } - NativeValue *OnStartAbilityWithAccount(NativeEngine &engine, NativeCallbackInfo &info) + napi_value OnStartAbilityWithAccount(napi_env env, size_t argc, napi_value *argv) { IMSA_HILOGI("OnStartAbilityWithAccount is called"); // only support two or three or four params - if (info.argc != ARGC_TWO && info.argc != ARGC_THREE && info.argc != ARGC_FOUR) { + if (argc != ARGC_TWO && argc != ARGC_THREE && argc != ARGC_FOUR) { IMSA_HILOGE("Not enough params"); - return engine.CreateUndefined(); + return CreateJsUndefined(env); } - decltype(info.argc) argc = 0; + decltype(argc) unwrapArgc = 0; AAFwk::Want want; - OHOS::AppExecFwk::UnwrapWant( - reinterpret_cast(&engine), reinterpret_cast(info.argv[INDEX_ZERO]), want); + OHOS::AppExecFwk::UnwrapWant(env, argv[INDEX_ZERO], want); IMSA_HILOGI("bundleName:%{public}s abilityName:%{public}s", want.GetBundle().c_str(), want.GetElement().GetAbilityName().c_str()); - argc++; + unwrapArgc++; + int32_t accountId = 0; - if (!OHOS::AppExecFwk::UnwrapInt32FromJS2( - reinterpret_cast(&engine), reinterpret_cast(info.argv[INDEX_ONE]), accountId)) { + if (!OHOS::AppExecFwk::UnwrapInt32FromJS2(env, argv[INDEX_ONE], accountId)) { IMSA_HILOGI("%{public}s called, the second parameter is invalid.", __func__); - return engine.CreateUndefined(); + return CreateJsUndefined(env); } IMSA_HILOGI("%{public}d accountId:", accountId); - argc++; + unwrapArgc++; + AAFwk::StartOptions startOptions; - if (info.argc > ARGC_TWO && info.argv[INDEX_TWO]->TypeOf() == NATIVE_OBJECT) { + napi_valuetype valueType = napi_undefined; + napi_typeof(env, argv[INDEX_ONE], &valueType); + if (argc > ARGC_TWO && valueType == napi_object) { IMSA_HILOGI("OnStartAbilityWithAccount start options is used."); - AppExecFwk::UnwrapStartOptions( - reinterpret_cast(&engine), reinterpret_cast(info.argv[INDEX_TWO]), startOptions); - argc++; + AppExecFwk::UnwrapStartOptions(env, argv[INDEX_TWO], startOptions); + unwrapArgc++; } - AsyncTask::CompleteCallback complete = [weak = context_, want, accountId, startOptions, argc]( - NativeEngine &engine, AsyncTask &task, int32_t status) { + NapiAsyncTask::CompleteCallback complete = [weak = context_, want, accountId, startOptions, unwrapArgc]( + napi_env env, NapiAsyncTask &task, int32_t status) { IMSA_HILOGI("startAbility begin"); auto context = weak.lock(); if (context == nullptr) { - task.Reject(engine, CreateJsError(engine, ERROR_CODE_ONE, "Context is released")); + task.Reject(env, CreateJsError(env, ERROR_CODE_ONE, "Context is released")); return; } - ErrCode errcode = (argc == ARGC_TWO) ? context->StartAbilityWithAccount(want, accountId) + ErrCode errcode = (unwrapArgc == ARGC_TWO) ? context->StartAbilityWithAccount(want, accountId) : context->StartAbilityWithAccount(want, accountId, startOptions); if (errcode == 0) { - task.Resolve(engine, engine.CreateUndefined()); + task.Resolve(env, CreateJsUndefined(env)); } - task.Reject(engine, CreateJsError(engine, errcode, "Start Ability failed.")); + task.Reject(env, CreateJsError(env, errcode, "Start Ability failed.")); }; - NativeValue *lastParam = (info.argc == argc) ? nullptr : info.argv[argc]; - NativeValue *result = nullptr; - AsyncTask::Schedule("InputMethodExtensionContext::OnStartAbilityWithAccount", engine, - CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); + + napi_value lastParam = argc == unwrapArgc ? nullptr : argv[unwrapArgc]; + napi_value result = nullptr; + NapiAsyncTask::Schedule("InputMethodExtensionContext::OnStartAbilityWithAccount", env, + CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result)); return result; } - NativeValue *OnTerminateAbility(NativeEngine &engine, NativeCallbackInfo &info) + napi_value OnTerminateAbility(napi_env env, size_t argc, napi_value *argv) { IMSA_HILOGI("OnTerminateAbility is called"); // only support one or zero params - if (info.argc != ARGC_ZERO && info.argc != ARGC_ONE) { + if (argc != ARGC_ZERO && argc != ARGC_ONE) { IMSA_HILOGE("Not enough params"); - return engine.CreateUndefined(); + return CreateJsUndefined(env); } - AsyncTask::CompleteCallback complete = [weak = context_]( - NativeEngine &engine, AsyncTask &task, int32_t status) { + NapiAsyncTask::CompleteCallback complete = [weak = context_]( + napi_env env, NapiAsyncTask &task, int32_t status) { IMSA_HILOGI("TerminateAbility begin"); auto context = weak.lock(); if (context == nullptr) { IMSA_HILOGW("context is released"); - task.Reject(engine, CreateJsError(engine, ERROR_CODE_ONE, "Context is released")); + task.Reject(env, CreateJsError(env, ERROR_CODE_ONE, "Context is released")); return; } auto errcode = context->TerminateAbility(); if (errcode == 0) { - task.Resolve(engine, engine.CreateUndefined()); + task.Resolve(env, CreateJsUndefined(env)); } else { - task.Reject(engine, CreateJsError(engine, errcode, "Terminate Ability failed.")); + task.Reject(env, CreateJsError(env, errcode, "Terminate Ability failed.")); } }; - NativeValue *lastParam = (info.argc == ARGC_ZERO) ? nullptr : info.argv[INDEX_ZERO]; - NativeValue *result = nullptr; - AsyncTask::Schedule("InputMethodExtensionContext::OnTerminateAbility", engine, - CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); + napi_value lastParam = argc == ARGC_ZERO ? nullptr : argv[INDEX_ZERO]; + napi_value result = nullptr; + NapiAsyncTask::Schedule("InputMethodExtensionContext::OnTerminateAbility", env, + CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result)); return result; } - NativeValue *OnConnectAbility(NativeEngine &engine, NativeCallbackInfo &info) + napi_value OnConnectAbility(napi_env env, size_t argc, napi_value *argv) { IMSA_HILOGI("OnConnectAbility"); // only support two params - if (info.argc != ARGC_TWO) { + if (argc != ARGC_TWO) { IMSA_HILOGE("Not enough params"); - return engine.CreateUndefined(); + return CreateJsUndefined(env); } AAFwk::Want want; - OHOS::AppExecFwk::UnwrapWant( - reinterpret_cast(&engine), reinterpret_cast(info.argv[INDEX_ZERO]), want); + OHOS::AppExecFwk::UnwrapWant(env, argv[INDEX_ZERO], want); IMSA_HILOGI("%{public}s bundlename:%{public}s abilityname:%{public}s", __func__, want.GetBundle().c_str(), want.GetElement().GetAbilityName().c_str()); - sptr connection = new JSInputMethodExtensionConnection(engine); - connection->SetJsConnectionObject(info.argv[1]); + sptr connection = new JSInputMethodExtensionConnection(env); + connection->SetJsConnectionObject(argv[1]); int64_t connectId = serialNumber_; ConnectionKey key; key.id = serialNumber_; @@ -260,51 +256,52 @@ private: } else { serialNumber_ = 0; } - AsyncTask::CompleteCallback complete = [weak = context_, want, connection, connectId]( - NativeEngine &engine, AsyncTask &task, int32_t status) { + NapiAsyncTask::CompleteCallback complete = [weak = context_, want, connection, connectId]( + napi_env env, NapiAsyncTask &task, int32_t status) { IMSA_HILOGI("OnConnectAbility begin"); auto context = weak.lock(); if (context == nullptr) { IMSA_HILOGW("context is released"); - task.Reject(engine, CreateJsError(engine, ERROR_CODE_ONE, "Context is released")); + task.Reject(env, CreateJsError(env, ERROR_CODE_ONE, "Context is released")); return; } IMSA_HILOGI("context->ConnectAbility connection:%{public}d", (int32_t)connectId); if (!context->ConnectAbility(want, connection)) { connection->CallJsFailed(ERROR_CODE_ONE); } - task.Resolve(engine, engine.CreateUndefined()); + task.Resolve(env, CreateJsUndefined(env)); }; - NativeValue *result = nullptr; - AsyncTask::Schedule("InputMethodExtensionContext::OnConnectAbility", engine, - CreateAsyncTaskWithLastParam(engine, nullptr, nullptr, std::move(complete), &result)); - return engine.CreateNumber(connectId); + napi_value result = nullptr; + NapiAsyncTask::Schedule("InputMethodExtensionContext::OnConnectAbility", env, + CreateAsyncTaskWithLastParam(env, nullptr, nullptr, std::move(complete), &result)); + + napi_value connectResult = nullptr; + napi_create_int64(env, connectId, &connectResult); + return connectResult; } - NativeValue *OnConnectAbilityWithAccount(NativeEngine &engine, NativeCallbackInfo &info) + napi_value OnConnectAbilityWithAccount(napi_env env, size_t argc, napi_value *argv) { IMSA_HILOGI("OnConnectAbilityWithAccount is called"); // only support three params - if (info.argc != ARGC_THREE) { + if (argc != ARGC_THREE) { IMSA_HILOGE("Not enough params"); - return engine.CreateUndefined(); + return CreateJsUndefined(env); } AAFwk::Want want; - OHOS::AppExecFwk::UnwrapWant( - reinterpret_cast(&engine), reinterpret_cast(info.argv[INDEX_ZERO]), want); + OHOS::AppExecFwk::UnwrapWant(env, argv[INDEX_ZERO], want); IMSA_HILOGI("%{public}s bundlename:%{public}s abilityname:%{public}s", __func__, want.GetBundle().c_str(), want.GetElement().GetAbilityName().c_str()); int32_t accountId = 0; - if (!OHOS::AppExecFwk::UnwrapInt32FromJS2(reinterpret_cast(&engine), - reinterpret_cast(info.argv[INDEX_ONE]), accountId)) { + if (!OHOS::AppExecFwk::UnwrapInt32FromJS2(env, argv[INDEX_ONE], accountId)) { IMSA_HILOGI("%{public}s called, the second parameter is invalid.", __func__); - return engine.CreateUndefined(); + return CreateJsUndefined(env); } - sptr connection = new JSInputMethodExtensionConnection(engine); - connection->SetJsConnectionObject(info.argv[1]); + sptr connection = new JSInputMethodExtensionConnection(env); + connection->SetJsConnectionObject(argv[1]); int64_t connectId = serialNumber_; ConnectionKey key; key.id = serialNumber_; @@ -315,41 +312,42 @@ private: } else { serialNumber_ = 0; } - AsyncTask::CompleteCallback complete = [weak = context_, want, accountId, connection, connectId]( - NativeEngine &engine, AsyncTask &task, int32_t status) { + NapiAsyncTask::CompleteCallback complete = [weak = context_, want, accountId, connection, connectId]( + napi_env env, NapiAsyncTask &task, int32_t status) { IMSA_HILOGI("OnConnectAbilityWithAccount begin"); auto context = weak.lock(); if (context == nullptr) { IMSA_HILOGW("context is released"); - task.Reject(engine, CreateJsError(engine, ERROR_CODE_ONE, "Context is released")); + task.Reject(env, CreateJsError(env, ERROR_CODE_ONE, "Context is released")); return; } IMSA_HILOGI("context->ConnectAbilityWithAccount connection:%{public}d", (int32_t)connectId); if (!context->ConnectAbilityWithAccount(want, accountId, connection)) { connection->CallJsFailed(ERROR_CODE_ONE); } - task.Resolve(engine, engine.CreateUndefined()); + task.Resolve(env, CreateJsUndefined(env)); }; - NativeValue *result = nullptr; - AsyncTask::Schedule("InputMethodExtensionContext::OnConnectAbilityWithAccount", engine, - CreateAsyncTaskWithLastParam(engine, nullptr, nullptr, std::move(complete), &result)); - return engine.CreateNumber(connectId); + napi_value result = nullptr; + NapiAsyncTask::Schedule("InputMethodExtensionContext::OnConnectAbilityWithAccount", env, + CreateAsyncTaskWithLastParam(env, nullptr, nullptr, std::move(complete), &result)); + napi_value connectResult = nullptr; + napi_create_int64(env, connectId, &connectResult); + return connectResult; } - NativeValue *OnDisconnectAbility(NativeEngine &engine, NativeCallbackInfo &info) + napi_value OnDisconnectAbility(napi_env env, size_t argc, napi_value *argv) { IMSA_HILOGI("OnDisconnectAbility is called"); // only support one or two params - if (info.argc != ARGC_ONE && info.argc != ARGC_TWO) { + if (argc != ARGC_ONE && argc != ARGC_TWO) { IMSA_HILOGE("Not enough params"); - return engine.CreateUndefined(); + return CreateJsUndefined(env); } AAFwk::Want want; int64_t connectId = -1; sptr connection = nullptr; - napi_get_value_int64( - reinterpret_cast(&engine), reinterpret_cast(info.argv[INDEX_ZERO]), &connectId); + napi_get_value_int64(env, argv[INDEX_ZERO], &connectId); IMSA_HILOGI("OnDisconnectAbility connection:%{public}d", static_cast(connectId)); auto item = std::find_if(connects_.begin(), connects_.end(), [&connectId](const std::map>::value_type &obj) { @@ -361,120 +359,122 @@ private: connection = item->second; } // begin disconnect - AsyncTask::CompleteCallback complete = [weak = context_, want, connection]( - NativeEngine &engine, AsyncTask &task, int32_t status) { + NapiAsyncTask::CompleteCallback complete = [weak = context_, want, connection]( + napi_env env, NapiAsyncTask &task, int32_t status) { IMSA_HILOGI("OnDisconnectAbility begin"); auto context = weak.lock(); if (context == nullptr) { IMSA_HILOGW("context is released"); - task.Reject(engine, CreateJsError(engine, ERROR_CODE_ONE, "Context is released")); + task.Reject(env, CreateJsError(env, ERROR_CODE_ONE, "Context is released")); return; } if (connection == nullptr) { IMSA_HILOGW("connection nullptr"); - task.Reject(engine, CreateJsError(engine, ERROR_CODE_TWO, "not found connection")); + task.Reject(env, CreateJsError(env, ERROR_CODE_TWO, "not found connection")); return; } IMSA_HILOGI("context->DisconnectAbility"); auto errcode = context->DisconnectAbility(want, connection); - errcode == 0 ? task.Resolve(engine, engine.CreateUndefined()) - : task.Reject(engine, CreateJsError(engine, errcode, "Disconnect Ability failed.")); + errcode == 0 ? task.Resolve(env, CreateJsUndefined(env)) + : task.Reject(env, CreateJsError(env, errcode, "Disconnect Ability failed.")); }; - NativeValue *lastParam = (info.argc == ARGC_ONE) ? nullptr : info.argv[INDEX_ONE]; - NativeValue *result = nullptr; - AsyncTask::Schedule("InputMethodExtensionContext::OnDisconnectAbility", engine, - CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); + napi_value lastParam = argc == ARGC_ONE ? nullptr : argv[INDEX_ONE]; + napi_value result = nullptr; + NapiAsyncTask::Schedule("InputMethodExtensionContext::OnDisconnectAbility", env, + CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result)); return result; } }; } // namespace -NativeValue *CreateJsMetadata(NativeEngine &engine, const AppExecFwk::Metadata &Info) +napi_value CreateJsMetadata(napi_env env, const AppExecFwk::Metadata &info) { IMSA_HILOGI("CreateJsMetadata"); - NativeValue *objValue = engine.CreateObject(); - NativeObject *object = ConvertNativeValueTo(objValue); - object->SetProperty("name", CreateJsValue(engine, Info.name)); - object->SetProperty("value", CreateJsValue(engine, Info.value)); - object->SetProperty("resource", CreateJsValue(engine, Info.resource)); + napi_value objValue = nullptr; + napi_create_object(env, &objValue); + + napi_set_named_property(env, objValue, "name", CreateJsValue(env, info.name)); + napi_set_named_property(env, objValue, "value", CreateJsValue(env, info.value)); + napi_set_named_property(env, objValue, "resource", CreateJsValue(env, info.resource)); return objValue; } -NativeValue *CreateJsMetadataArray(NativeEngine &engine, const std::vector &info) +napi_value CreateJsMetadataArray(napi_env env, const std::vector &info) { IMSA_HILOGI("CreateJsMetadataArray"); - NativeValue *arrayValue = engine.CreateArray(info.size()); - NativeArray *array = ConvertNativeValueTo(arrayValue); + napi_value arrayValue = nullptr; + napi_create_array_with_length(env, info.size(), &arrayValue); uint32_t index = 0; for (const auto &item : info) { - array->SetElement(index++, CreateJsMetadata(engine, item)); + napi_set_element(env, arrayValue, index++, CreateJsMetadata(env, item)); } return arrayValue; } -NativeValue *CreateJsExtensionAbilityInfo(NativeEngine &engine, const AppExecFwk::ExtensionAbilityInfo &info) +napi_value CreateJsExtensionAbilityInfo(napi_env env, const AppExecFwk::ExtensionAbilityInfo &info) { IMSA_HILOGI("CreateJsExtensionAbilityInfo"); - NativeValue *objValue = engine.CreateObject(); - NativeObject *object = ConvertNativeValueTo(objValue); - object->SetProperty("bundleName", CreateJsValue(engine, info.bundleName)); - object->SetProperty("moduleName", CreateJsValue(engine, info.moduleName)); - object->SetProperty("name", CreateJsValue(engine, info.name)); - object->SetProperty("labelId", CreateJsValue(engine, info.labelId)); - object->SetProperty("descriptionId", CreateJsValue(engine, info.descriptionId)); - object->SetProperty("iconId", CreateJsValue(engine, info.iconId)); - object->SetProperty("isVisible", CreateJsValue(engine, info.visible)); - object->SetProperty("extensionAbilityType", CreateJsValue(engine, info.type)); - NativeValue *permissionArrayValue = engine.CreateArray(info.permissions.size()); - NativeArray *permissionArray = ConvertNativeValueTo(permissionArrayValue); + napi_value objValue = nullptr; + napi_create_object(env, &objValue); + + napi_set_named_property(env, objValue, "bundleName", CreateJsValue(env, info.bundleName)); + napi_set_named_property(env, objValue, "moduleName", CreateJsValue(env, info.moduleName)); + napi_set_named_property(env, objValue, "name", CreateJsValue(env, info.name)); + napi_set_named_property(env, objValue, "labelId", CreateJsValue(env, info.labelId)); + napi_set_named_property(env, objValue, "descriptionId", CreateJsValue(env, info.descriptionId)); + napi_set_named_property(env, objValue, "iconId", CreateJsValue(env, info.iconId)); + napi_set_named_property(env, objValue, "isVisible", CreateJsValue(env, info.visible)); + napi_set_named_property(env, objValue, "extensionAbilityType", CreateJsValue(env, info.type)); + + napi_value permissionArray = nullptr; + napi_create_array_with_length(env, info.permissions.size(), &permissionArray); + if (permissionArray != nullptr) { int index = 0; for (auto permission : info.permissions) { - permissionArray->SetElement(index++, CreateJsValue(engine, permission)); + napi_set_element(env, permissionArray, index++, CreateJsValue(env, permission)); } } - object->SetProperty("permissions", permissionArrayValue); - object->SetProperty("applicationInfo", CreateJsApplicationInfo(engine, info.applicationInfo)); - object->SetProperty("metadata", CreateJsMetadataArray(engine, info.metadata)); - object->SetProperty("enabled", CreateJsValue(engine, info.enabled)); - object->SetProperty("readPermission", CreateJsValue(engine, info.readPermission)); - object->SetProperty("writePermission", CreateJsValue(engine, info.writePermission)); + napi_set_named_property(env, objValue, "permissions", permissionArray); + napi_set_named_property(env, objValue, "applicationInfo", CreateJsApplicationInfo(env, info.applicationInfo)); + napi_set_named_property(env, objValue, "metadata", CreateJsMetadataArray(env, info.metadata)); + napi_set_named_property(env, objValue, "enabled", CreateJsValue(env, info.enabled)); + napi_set_named_property(env, objValue, "readPermission", CreateJsValue(env, info.readPermission)); + napi_set_named_property(env, objValue, "writePermission", CreateJsValue(env, info.writePermission)); return objValue; } -NativeValue *CreateJsInputMethodExtensionContext( - NativeEngine &engine, std::shared_ptr context) +napi_value CreateJsInputMethodExtensionContext( + napi_env env, std::shared_ptr context) { IMSA_HILOGI("CreateJsInputMethodExtensionContext begin"); if (context != nullptr) { auto abilityInfo = context->GetAbilityInfo(); } - NativeValue *objValue = CreateJsExtensionContext(engine, context); - NativeObject *object = ConvertNativeValueTo(objValue); + napi_value objValue = CreateJsExtensionContext(env, context); std::unique_ptr jsContext = std::make_unique(context); - object->SetNativePointer(jsContext.release(), JsInputMethodExtensionContext::Finalizer, nullptr); - + napi_wrap(env, objValue, jsContext.release(), JsInputMethodExtensionContext::Finalizer, nullptr, nullptr); // make handler handler_ = std::make_shared(AppExecFwk::EventRunner::GetMainEventRunner()); const char *moduleName = "JsInputMethodExtensionContext"; - BindNativeFunction(engine, *object, "startAbility", moduleName, JsInputMethodExtensionContext::StartAbility); - BindNativeFunction(engine, *object, "terminateSelf", moduleName, JsInputMethodExtensionContext::TerminateAbility); - BindNativeFunction(engine, *object, "destroy", moduleName, JsInputMethodExtensionContext::TerminateAbility); - BindNativeFunction(engine, *object, "connectAbility", moduleName, JsInputMethodExtensionContext::ConnectAbility); + BindNativeFunction(env, objValue, "startAbility", moduleName, JsInputMethodExtensionContext::StartAbility); + BindNativeFunction(env, objValue, "terminateSelf", moduleName, JsInputMethodExtensionContext::TerminateAbility); + BindNativeFunction(env, objValue, "destroy", moduleName, JsInputMethodExtensionContext::TerminateAbility); + BindNativeFunction(env, objValue, "connectAbility", moduleName, JsInputMethodExtensionContext::ConnectAbility); BindNativeFunction( - engine, *object, "disconnectAbility", moduleName, JsInputMethodExtensionContext::DisconnectAbility); - BindNativeFunction(engine, *object, "startAbilityWithAccount", moduleName, + env, objValue, "disconnectAbility", moduleName, JsInputMethodExtensionContext::DisconnectAbility); + BindNativeFunction(env, objValue, "startAbilityWithAccount", moduleName, JsInputMethodExtensionContext::StartAbilityWithAccount); - BindNativeFunction(engine, *object, "connectAbilityWithAccount", moduleName, + BindNativeFunction(env, objValue, "connectAbilityWithAccount", moduleName, JsInputMethodExtensionContext::ConnectAbilityWithAccount); return objValue; } -JSInputMethodExtensionConnection::JSInputMethodExtensionConnection(NativeEngine &engine) : engine_(engine) +JSInputMethodExtensionConnection::JSInputMethodExtensionConnection(napi_env env) : env_(env) { } @@ -505,32 +505,33 @@ void JSInputMethodExtensionConnection::HandleOnAbilityConnectDone( { IMSA_HILOGI("HandleOnAbilityConnectDone begin, resultCode:%{public}d", resultCode); // wrap ElementName - napi_value napiElementName = OHOS::AppExecFwk::WrapElementName(reinterpret_cast(&engine_), element); - NativeValue *nativeElementName = reinterpret_cast(napiElementName); + napi_value napiElementName = OHOS::AppExecFwk::WrapElementName(env_, element); // wrap RemoteObject IMSA_HILOGI("OnAbilityConnectDone begin NAPI_ohos_rpc_CreateJsRemoteObject"); napi_value napiRemoteObject = - NAPI_ohos_rpc_CreateJsRemoteObject(reinterpret_cast(&engine_), remoteObject); - NativeValue *nativeRemoteObject = reinterpret_cast(napiRemoteObject); - NativeValue *argv[] = { nativeElementName, nativeRemoteObject }; + NAPI_ohos_rpc_CreateJsRemoteObject(env_, remoteObject); + napi_value argv[] = { napiElementName, napiRemoteObject }; + if (jsConnectionObject_ == nullptr) { IMSA_HILOGE("jsConnectionObject_ nullptr"); return; } - NativeValue *value = jsConnectionObject_->Get(); - NativeObject *obj = ConvertNativeValueTo(value); + + napi_value obj = jsConnectionObject_->GetNapiValue(); if (obj == nullptr) { IMSA_HILOGE("Failed to get object"); return; } - NativeValue *methodOnConnect = obj->GetProperty("onConnect"); + napi_value methodOnConnect = nullptr; + napi_get_named_property(env_, obj, "onConnect", &methodOnConnect); if (methodOnConnect == nullptr) { IMSA_HILOGE("Failed to get onConnect from object"); return; } IMSA_HILOGI("JSInputMethodExtensionConnection::CallFunction onConnect, success"); - engine_.CallFunction(value, methodOnConnect, argv, ARGC_TWO); + napi_value callResult = nullptr; + napi_call_function(env_, obj, methodOnConnect, ARGC_TWO, argv, &callResult); IMSA_HILOGI("OnAbilityConnectDone end"); } @@ -557,21 +558,21 @@ void JSInputMethodExtensionConnection::HandleOnAbilityDisconnectDone( const AppExecFwk::ElementName &element, int resultCode) { IMSA_HILOGI("HandleOnAbilityDisconnectDone begin, resultCode:%{public}d", resultCode); - napi_value napiElementName = OHOS::AppExecFwk::WrapElementName(reinterpret_cast(&engine_), element); - NativeValue *nativeElementName = reinterpret_cast(napiElementName); - NativeValue *argv[] = { nativeElementName }; + napi_value napiElementName = OHOS::AppExecFwk::WrapElementName(env_, element); + napi_value argv[] = { napiElementName }; if (jsConnectionObject_ == nullptr) { IMSA_HILOGE("jsConnectionObject_ nullptr"); return; } - NativeValue *value = jsConnectionObject_->Get(); - NativeObject *obj = ConvertNativeValueTo(value); + + napi_value obj = jsConnectionObject_->GetNapiValue(); if (obj == nullptr) { IMSA_HILOGE("Failed to get object"); return; } - NativeValue *method = obj->GetProperty("onDisconnect"); + napi_value method = nullptr; + napi_get_named_property(env_, obj, "onDisconnect", &method); if (method == nullptr) { IMSA_HILOGE("Failed to get onDisconnect from object"); return; @@ -593,12 +594,15 @@ void JSInputMethodExtensionConnection::HandleOnAbilityDisconnectDone( IMSA_HILOGI("OnAbilityDisconnectDone erase connects_.size:%{public}zu", connects_.size()); } IMSA_HILOGI("OnAbilityDisconnectDone CallFunction success"); - engine_.CallFunction(value, method, argv, ARGC_ONE); + napi_value callResult = nullptr; + napi_call_function(env_, obj, method, ARGC_TWO, argv, &callResult); } -void JSInputMethodExtensionConnection::SetJsConnectionObject(NativeValue *jsConnectionObject) +void JSInputMethodExtensionConnection::SetJsConnectionObject(napi_value jsConnectionObject) { - jsConnectionObject_ = std::unique_ptr(engine_.CreateReference(jsConnectionObject, 1)); + napi_ref value = nullptr; + napi_create_reference(env_, jsConnectionObject, 1, &value); + jsConnectionObject_ = std::unique_ptr(reinterpret_cast(value)); } void JSInputMethodExtensionConnection::CallJsFailed(int32_t errorCode) @@ -608,21 +612,24 @@ void JSInputMethodExtensionConnection::CallJsFailed(int32_t errorCode) IMSA_HILOGE("jsConnectionObject_ nullptr"); return; } - NativeValue *value = jsConnectionObject_->Get(); - NativeObject *obj = ConvertNativeValueTo(value); + napi_value obj = jsConnectionObject_->GetNapiValue(); if (obj == nullptr) { IMSA_HILOGE("Failed to get object"); return; } - NativeValue *method = obj->GetProperty("onFailed"); + napi_value method = nullptr; + napi_get_named_property(env_, obj, "onFailed", &method); if (method == nullptr) { IMSA_HILOGE("Failed to get onFailed from object"); return; } - NativeValue *argv[] = { engine_.CreateNumber(errorCode) }; + napi_value result = nullptr; + napi_create_int32(env_, errorCode, &result); + napi_value argv[] = { result }; IMSA_HILOGI("CallJsFailed CallFunction success"); - engine_.CallFunction(value, method, argv, ARGC_ONE); + napi_value callResult = nullptr; + napi_call_function(env_, obj, method, ARGC_ONE, argv, &callResult); IMSA_HILOGI("CallJsFailed end"); } } // namespace AbilityRuntime -- Gitee