From 122289802f3becbad0ab26c6bb059d51c87353e6 Mon Sep 17 00:00:00 2001 From: song-song-song Date: Mon, 3 Mar 2025 15:23:57 +0800 Subject: [PATCH] =?UTF-8?q?demo=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: song-song-song --- .../plugins/ets/runtime/ani/docs/napi2ani.md | 102 ++++++++++++++---- 1 file changed, 80 insertions(+), 22 deletions(-) diff --git a/static_core/plugins/ets/runtime/ani/docs/napi2ani.md b/static_core/plugins/ets/runtime/ani/docs/napi2ani.md index aa7a47d21..a630ac17b 100644 --- a/static_core/plugins/ets/runtime/ani/docs/napi2ani.md +++ b/static_core/plugins/ets/runtime/ani/docs/napi2ani.md @@ -599,16 +599,23 @@ if (cls == nullptr) { ### napi_throw_error迁移示例 #### 代码示例对比 - +通过创建error或获取未处理的error,抛出error。 +https://gitee.com/openharmony/arkcompiler_runtime_core/issues/IBOBNG #### **N-API 示例** ```cpp napi_throw_error(env, nullptr, "Something went wrong"); ``` -#### **ANI 示例** +#### **ANI 示例1** ```cpp ani_error error; //假定已创建error env_->ThrowError(error); ``` +#### **ANI 示例2** +```cpp +ani_error error; +env_->GetUnhandledError(&error); +env_->ThrowError(error); +``` ### napi_fatal_error迁移示例 #### 代码示例对比 @@ -3838,7 +3845,7 @@ napi_value staticMethod; // 直接调用类名下的静态方法 napi_get_named_property(env, js_Class_name, "TsMethod", &staticMethod); napi_call_function(env, js_Class_name, staticMethod, 0, nullptr, nullptr); ``` -#### **ANI 示例** +#### **ANI 示例1** ```cpp // ets class Operations { @@ -3857,25 +3864,7 @@ env_->Class_CallStaticMethod_Boolean(cls, method, &result, ANI_TRUE, ANI_FALSE); ASSERT_EQ(result, ANI_TRUE); ``` -### napi_call_function迁移示例 - -#### 代码示例对比 - -#### **N-API 示例** -```cpp -// js -export class TsClass { // 这里定义一个类 - public static TsMethod(): void { // 这里定义一个静态方法 - log.info('do static TsMethod'); - } -} -// cpp -napi_value js_Class_name; // 假定类名已获取 -napi_value staticMethod; // 直接调用类名下的静态方法 -napi_get_named_property(env, js_Class_name, "TsMethod", &staticMethod); -napi_call_function(env, js_Class_name, staticMethod, 0, nullptr, nullptr); -``` -#### **ANI 示例** +#### **ANI 示例2** ```cpp // ets class Operations { @@ -3891,6 +3880,75 @@ ani_boolean result; env_->Class_CallStaticMethodByName_Boolean(cls, "or", &result, ANI_TRUE, ANI_FALSE); ASSERT_EQ(result, ANI_TRUE); ``` + +#### **ANI 示例3** +重载Static Method,在根据函数名调用函数时利用不同的签名区分不同的函数。 +https://gitee.com/openharmony/arkcompiler_runtime_core/issues/IBORFQ +```cpp +// ets +class B { + constructor(root : int) { + this.root = root; + } + root : int +} + +class Foo { + static Add(a0: int, a1: int): int + { + return a0 + a1; + } + + static Add(a0: int, a1: B): int + { + return a0; + } +} + +// cpp +static const char *className = "Lsetfield/Foo;"; +ani_class cls; +if (ANI_OK != env->FindClass(className, &cls)) { + std::cerr << "Not found '" << className << "'" << std::endl; + return ; +} + +ani_class cls1; +if (env->FindClass("Lsetfield/B;", &cls1) != ANI_OK) { + std::cerr << "Not found '" << "B" << "'" << std::endl; + return ; +} + +ani_method ctor1; +if (env->Class_FindMethod(cls1, "", "I:V", &ctor1) != ANI_OK) { + std::cerr << "Not found '" << "B ctor" << "'" << std::endl; + return ; +} +ani_object b; +ani_int root = 1; +if (env->c_api->Object_New(env, cls1, ctor1, &b, root)!= ANI_OK) { + std::cerr << "Not create '" << "B ctor" << "'" << std::endl; + return ; +} + +ani_static_method method; +if (env->Class_FindStaticMethod(cls, "Add", "ILsetfield/B;:I", &method)!= ANI_OK) { + std::cerr << "Not found '" << "Add" << "'" << std::endl; + return ; +} + +ani_int sum; +if (env->c_api->Class_CallStaticMethodByName_Int(env, cls, "Add", "II:I", &sum, 5U, 6U) != ANI_OK) { + std::cerr << "Not call '" << "Add_II:I" << "'" << std::endl; + return ; +} + +ani_int sum1; +if (env->c_api->Class_CallStaticMethodByName_Int(env, cls, "Add", "ILsetfield/B;:I", &sum1, 5U, &b) != ANI_OK) { + std::cerr << "Not call '" << "Add_ILsetfield/B;:I" << "'" << std::endl; + return ; +} +``` ## 11. String Operations -- Gitee