From 93a78cb113de8d63e23f58736c71dc85f698bf39 Mon Sep 17 00:00:00 2001 From: wangshi Date: Thu, 17 Oct 2024 15:06:38 +0800 Subject: [PATCH] fix code check Signed-off-by: wangshi --- .../entry/src/main/cpp/napi_init.cpp | 75 +++++++++++++ .../src/main/cpp/types/libentry/Index.d.ts | 7 ++ .../src/main/ets/pages/async/AsyncMgr.ets | 6 +- .../main/ets/pages/async/akiarraybuffer.ets | 27 +++-- .../src/main/ets/pages/async/akiposttask.ets | 20 ++-- .../src/main/ets/pages/async/akivalue.ets | 30 ++++- .../main/ets/pages/nodeapi/akibindclass.ets | 7 +- .../main/ets/pages/nodeapi/akibindenum.ets | 7 +- .../main/ets/pages/nodeapi/akibindglobal.ets | 103 ------------------ .../main/ets/pages/nodeapi/akibindmethod.ets | 103 ------------------ .../resources/base/profile/main_pages.json | 2 - 11 files changed, 146 insertions(+), 241 deletions(-) delete mode 100644 examples/akitutorials/entry/src/main/ets/pages/nodeapi/akibindglobal.ets delete mode 100644 examples/akitutorials/entry/src/main/ets/pages/nodeapi/akibindmethod.ets diff --git a/examples/akitutorials/entry/src/main/cpp/napi_init.cpp b/examples/akitutorials/entry/src/main/cpp/napi_init.cpp index 6cdae5ba..74aabb80 100644 --- a/examples/akitutorials/entry/src/main/cpp/napi_init.cpp +++ b/examples/akitutorials/entry/src/main/cpp/napi_init.cpp @@ -100,6 +100,78 @@ TypeFlags Passing(TypeFlags flag) return flag; } +// PostTask + +static aki::Promise ReturnPromiseResolveLater() +{ + aki::Promise promise; + + std::thread t([promise] () { + aki::TaskRunner::PostTask("main", [promise] () { + promise.Resolve(1); + }); + }); + t.detach(); + return promise; +} + +// arraybuffer +aki::ArrayBuffer AsyncTaskReturnArrayBufferWithCommit(aki::ArrayBuffer input) +{ + uint8_t temp[4] = {10, 20, 30, 40}; + aki::ArrayBuffer arrayBufferOne(temp, TypeFlags.NUM); + aki::ArrayBuffer arrayBufferTwo(input.GetData(), input.GetLength()); + arrayBufferOne.Commit(); + return arrayBufferOne; +} + +// aki-value +std::string GetFromGlobal(aki::Value obj) +{ + aki::Value jsonObj = aki::Value::FromGlobal("JSON"); + aki::Value strObj = jsonObj["stringify"](obj); + return strObj.As(); +} + +std::string ConvertToString(aki::Value obj) +{ + if (obj.IsNull()) { + return "null value"; + } else if (obj.IsArray()) { + obj.CallMethod("push", "from C++"); + aki::Value val1 = obj[0]; + aki::Value strObj = aki::Value::FromGlobal("JSON")["stringify"](obj); + return strObj.As(); + } else if (obj.IsBool()) { + return obj.As(); + } else if (obj.IsNumber()) { + return obj.As(); + } else if (obj.IsString()) { + aki::Value val = aki::Value::NewObject(); + val.Set("name", obj.As()); + aki::Value strObj = aki::Value::FromGlobal("JSON")["stringify"](val); + return strObj.As(); + } else { + napi_value nvalue = obj.GetHandle(); + napi_value nage, nname; + napi_get_named_property(aki::JSBind::GetScopedEnv(), nvalue, "age", &nage); + napi_get_named_property(aki::JSBind::GetScopedEnv(), nvalue, "name", &nname); + + int age; + std::string name; + napi_get_value_int32(aki::JSBind::GetScopedEnv(), nage, &age); + + size_t stringSize = 0; + napi_get_value_string_utf8(aki::JSBind::GetScopedEnv(), nname, nullptr, 0, &stringSize); // 获取字符串长度 + name.resize(stringSize + 1); + + // 根据长度传换成字符串 + napi_get_value_string_utf8(aki::JSBind::GetScopedEnv(), nname, &name[0], stringSize + 1, &stringSize); + AKI_LOG(INFO) << name << " : " << age; + } +} + + // Step 1 注册 AKI 插件 JSBIND_ADDON(hello) // 注册 AKI 插件名: 即为编译*.so名称,规则与NAPI一致 @@ -108,4 +180,7 @@ JSBIND_GLOBAL() { JSBIND_FUNCTION(SayHello); JSBIND_PFUNCTION(AsyncSayHello); JSBIND_FUNCTION(Passing); + JSBIND_FUNCTION(ReturnPromiseResolveLater); + JSBIND_PFUNCTION(AsyncTaskReturnArrayBufferWithCommit); + JSBIND_FUNCTION(ConvertToString); } \ No newline at end of file diff --git a/examples/akitutorials/entry/src/main/cpp/types/libentry/Index.d.ts b/examples/akitutorials/entry/src/main/cpp/types/libentry/Index.d.ts index 1736cdfe..20d84598 100644 --- a/examples/akitutorials/entry/src/main/cpp/types/libentry/Index.d.ts +++ b/examples/akitutorials/entry/src/main/cpp/types/libentry/Index.d.ts @@ -16,6 +16,13 @@ export const SayHello: (a: string) => string; export const AsyncSayHello: (a: string) => Promise; export const Passing: (flag: TypeFlags) => TypeFlags; +export const ReturnPromiseResolveLater: () => Promise; +export const AsyncTaskReturnArrayBufferWithCommit: (uint8buff: Uint8Array) => Promise; +export const ConvertToString: (a: object) => string; + +export class JSBind { + static initTaskRunner: (param: string) => void; +} export enum TypeFlags { NONE, diff --git a/examples/akitutorials/entry/src/main/ets/pages/async/AsyncMgr.ets b/examples/akitutorials/entry/src/main/ets/pages/async/AsyncMgr.ets index b54699ea..d4467438 100644 --- a/examples/akitutorials/entry/src/main/ets/pages/async/AsyncMgr.ets +++ b/examples/akitutorials/entry/src/main/ets/pages/async/AsyncMgr.ets @@ -22,15 +22,15 @@ const SIMPLE_ASYNCRONOUS_OPERATIONS: ThirdLevelCategory = childNodes: [ { title: $r('app.string.aki_post_task'), - url: 'pages/image/basicSample/image2Gray' + url: 'pages/async/akiposttask' }, { title: $r('app.string.aki_value'), - url: 'pages/image/basicSample/image2Gray' + url: 'pages/async/akivalue' }, { title: $r('app.string.aki_arraybuffer'), - url: 'pages/image/basicSample/image2Gray' + url: 'pages/async/akiarraybuffer' } ] } diff --git a/examples/akitutorials/entry/src/main/ets/pages/async/akiarraybuffer.ets b/examples/akitutorials/entry/src/main/ets/pages/async/akiarraybuffer.ets index ccf0c7d2..0f0bfc63 100644 --- a/examples/akitutorials/entry/src/main/ets/pages/async/akiarraybuffer.ets +++ b/examples/akitutorials/entry/src/main/ets/pages/async/akiarraybuffer.ets @@ -29,9 +29,10 @@ struct akiarraybuffer { private pixelMapFormat: image.PixelMapFormat = 3; @State isSetInstance: Boolean = false; @State imagePixelMap: PixelMap | undefined = undefined; - @State textcont: string = 'napi_value 这是一个用于表示 JavaScript 值的不透明指针。' - @State testcont: string = ' // 获取 N-API value \n' - + ' const myData = addon.testNapiValue(); \n' + @State textcont: string = '当在非 JS 线程使用 aki::ArrayBuffer,' + + '需要关注数据字节流生命周期,并考虑是否需要结合Commit()函数使用。'; + @State testcont: string = ' // 异步返回 ArrayBuffer 数据 \n' + + ' const myData = test.AsyncTaskReturnArrayBufferWithCommit(); \n' + ' // 使用获取的自定义数据 \n' + ' console.log(myData); // 输出自定义数据 \n'; @@ -64,8 +65,8 @@ struct akiarraybuffer { .width('90%') .margin(10) .fontSize(16) - .fontColor('#ff400336') - .backgroundColor('#ff985307') + .fontColor($r('app.color.COLOR_80000000')) + .backgroundColor($r('app.color.COLOR_99FFFFFF')) .enabled(false) } .width('100%') @@ -83,10 +84,18 @@ struct akiarraybuffer { .margin({ left: 24 }) .id('napi_async_complete_callback') .onClick(() => { - let res = ''; - // let res = testNapi.testNapiValue(); - hilog.info(0x0000, 'testTag', `testNapiValue = ${res} `); - this.testcont = this.testcont.replace('log(myData)', 'log(## '+res+' ##)'); + let buff: ArrayBuffer = new ArrayBuffer(4); + let uint8Buff: Uint8Array = new Uint8Array(buff); + uint8Buff[0] = 0; + uint8Buff[1] = 1; + uint8Buff[2] = 2; + uint8Buff[3] = 3; + testNapi.AsyncTaskReturnArrayBufferWithCommit(uint8Buff).then(res => { + let uint8Buff1 = new Uint8Array(res); + let msg = uint8Buff1.toString(); + hilog.info(0x0000, 'testTag', `testNapiValue = ${msg} `); + this.testcont = this.testcont.replace('log(myData)', 'log(## '+msg+' ##)'); + }); }) } .width('100%') diff --git a/examples/akitutorials/entry/src/main/ets/pages/async/akiposttask.ets b/examples/akitutorials/entry/src/main/ets/pages/async/akiposttask.ets index 831f4e76..37e07a10 100644 --- a/examples/akitutorials/entry/src/main/ets/pages/async/akiposttask.ets +++ b/examples/akitutorials/entry/src/main/ets/pages/async/akiposttask.ets @@ -29,9 +29,9 @@ struct akiposttask { private pixelMapFormat: image.PixelMapFormat = 3; @State isSetInstance: Boolean = false; @State imagePixelMap: PixelMap | undefined = undefined; - @State textcont: string = 'napi_value 这是一个用于表示 JavaScript 值的不透明指针。' - @State testcont: string = ' // 获取 N-API value \n' - + ' const myData = addon.testNapiValue(); \n' + @State textcont: string = 'TaskRunner提供JS线程的任务调度器,开发人员可以从C++往JS线程PostTask。'; + @State testcont: string = ' // js侧创建线程,然后在C++侧进行异步返回 \n' + + ' testNapi.ReturnPromiseResolveLater; \n' + ' // 使用获取的自定义数据 \n' + ' console.log(myData); // 输出自定义数据 \n'; @@ -64,8 +64,8 @@ struct akiposttask { .width('90%') .margin(10) .fontSize(16) - .fontColor('#ff400336') - .backgroundColor('#ff985307') + .fontColor($r('app.color.COLOR_80000000')) + .backgroundColor($r('app.color.COLOR_99FFFFFF')) .enabled(false) } .width('100%') @@ -83,10 +83,12 @@ struct akiposttask { .margin({ left: 24 }) .id('napi_async_complete_callback') .onClick(() => { - let res = ''; - // let res = testNapi.testNapiValue(); - hilog.info(0x0000, 'testTag', `testNapiValue = ${res} `); - this.testcont = this.testcont.replace('log(myData)', 'log(## '+res+' ##)'); + testNapi.JSBind.initTaskRunner("main"); + testNapi.ReturnPromiseResolveLater().then((value) => { + hilog.info(0x0000, 'testTag', `[AKI] ReturnPromiseResolveLater then: ${value}`); + this.testcont = this.testcont.replace('log(myData)', `log(## ${value} ##)`); + }) + }) } .width('100%') diff --git a/examples/akitutorials/entry/src/main/ets/pages/async/akivalue.ets b/examples/akitutorials/entry/src/main/ets/pages/async/akivalue.ets index 883648e6..ff3590f7 100644 --- a/examples/akitutorials/entry/src/main/ets/pages/async/akivalue.ets +++ b/examples/akitutorials/entry/src/main/ets/pages/async/akivalue.ets @@ -22,6 +22,22 @@ import hilog from '@ohos.hilog'; const TAG: string = 'napi_value'; +export class Person +{ + name:string= "zhangsan"; + age: number = 1; + constructor(name: string, age: number) { + this.name = name; + this.age = age; + }; + addAge() { + return this.age; + }; + changeName() { + return + }; +} + @Entry @Component struct akivalue { @@ -29,9 +45,10 @@ struct akivalue { private pixelMapFormat: image.PixelMapFormat = 3; @State isSetInstance: Boolean = false; @State imagePixelMap: PixelMap | undefined = undefined; - @State textcont: string = 'napi_value 这是一个用于表示 JavaScript 值的不透明指针。' - @State testcont: string = ' // 获取 N-API value \n' - + ' const myData = addon.testNapiValue(); \n' + @State textcont: string = 'JavaScript 是弱类型语言,可用泛型any表示任意类型。' + + 'C/C++使用aki::Value映射 JavaScript 的any类型。'; + @State testcont: string = ' // 本示例用来验证any类型转换 \n' + + ' const myData = testNapi.ConvertToString(); \n' + ' // 使用获取的自定义数据 \n' + ' console.log(myData); // 输出自定义数据 \n'; @@ -64,8 +81,8 @@ struct akivalue { .width('90%') .margin(10) .fontSize(16) - .fontColor('#ff400336') - .backgroundColor('#ff985307') + .fontColor($r('app.color.COLOR_80000000')) + .backgroundColor($r('app.color.COLOR_99FFFFFF')) .enabled(false) } .width('100%') @@ -84,7 +101,8 @@ struct akivalue { .id('napi_async_complete_callback') .onClick(() => { let res = ''; - // let res = testNapi.testNapiValue(); + let strArray: string[] = ['aki', 'jsbind']; + res = testNapi.ConvertToString(strArray); hilog.info(0x0000, 'testTag', `testNapiValue = ${res} `); this.testcont = this.testcont.replace('log(myData)', 'log(## '+res+' ##)'); }) diff --git a/examples/akitutorials/entry/src/main/ets/pages/nodeapi/akibindclass.ets b/examples/akitutorials/entry/src/main/ets/pages/nodeapi/akibindclass.ets index 00be9197..b21dde3e 100644 --- a/examples/akitutorials/entry/src/main/ets/pages/nodeapi/akibindclass.ets +++ b/examples/akitutorials/entry/src/main/ets/pages/nodeapi/akibindclass.ets @@ -29,9 +29,10 @@ struct akibindclass { private pixelMapFormat: image.PixelMapFormat = 3; @State isSetInstance: Boolean = false; @State imagePixelMap: PixelMap | undefined = undefined; - @State textcont: string = 'napi_value 这是一个用于表示 JavaScript 值的不透明指针。' - @State testcont: string = ' // 获取 N-API value \n' - + ' const myData = addon.testNapiValue(); \n' + @State textcont: string = 'AKI 提供 JSBIND_CLASS 对 C++ 类/结构体进行绑定,' + + '在JSBIND_CLASS作用域下可绑定:类构造函数、类成员函数、类成员属性的类特性。'; + @State testcont: string = ' // js侧使用c++侧绑定的类 \n' + + ' new testNapi.TestObject(10.16); \n' + ' // 使用获取的自定义数据 \n' + ' console.log(myData); // 输出自定义数据 \n'; diff --git a/examples/akitutorials/entry/src/main/ets/pages/nodeapi/akibindenum.ets b/examples/akitutorials/entry/src/main/ets/pages/nodeapi/akibindenum.ets index aaf5fb50..0c6cd1ec 100644 --- a/examples/akitutorials/entry/src/main/ets/pages/nodeapi/akibindenum.ets +++ b/examples/akitutorials/entry/src/main/ets/pages/nodeapi/akibindenum.ets @@ -29,9 +29,10 @@ struct akibindenum { private pixelMapFormat: image.PixelMapFormat = 3; @State isSetInstance: Boolean = false; @State imagePixelMap: PixelMap | undefined = undefined; - @State textcont: string = 'napi_value 这是一个用于表示 JavaScript 值的不透明指针。' - @State testcont: string = ' // 获取 N-API value \n' - + ' const myData = addon.testNapiValue(); \n' + @State textcont: string = 'JSBind语法糖JSBIND_ENUM、JSBIND_ENUM_VALUE支持绑定 C/C++ 枚举类型,' + + '映射为 JavaScript 的Number类型。'; + @State testcont: string = ' // 获取 c++ 侧绑定的枚举型 \n' + + ' testNapi.Passing(testNapi.TypeFlags.NUM); \n' + ' // 使用获取的自定义数据 \n' + ' console.log(myData); // 输出自定义数据 \n'; diff --git a/examples/akitutorials/entry/src/main/ets/pages/nodeapi/akibindglobal.ets b/examples/akitutorials/entry/src/main/ets/pages/nodeapi/akibindglobal.ets deleted file mode 100644 index 17ad5279..00000000 --- a/examples/akitutorials/entry/src/main/ets/pages/nodeapi/akibindglobal.ets +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 router from '@ohos.router'; -import image from '@ohos.multimedia.image'; -import Logger from '../../util/Logger'; -import testNapi from 'libentry.so'; -import { TitleBar } from '../../common/TitleBar' -import hilog from '@ohos.hilog'; - -const TAG: string = 'napi_value'; - -@Entry -@Component -struct akibindfunction { - private btnFontColor: Resource = $r('app.color.white'); - private pixelMapFormat: image.PixelMapFormat = 3; - @State isSetInstance: Boolean = false; - @State imagePixelMap: PixelMap | undefined = undefined; - @State textcont: string = '使用 JSBIND_GLOBAL 在js全局范围内绑定 function' - @State testcont: string = ' // 调用 aki 方法 \n' - + ' const myData = testNapi.SayHello(); \n' - + ' // 使用获取的方法返回数据 \n' - + ' console.log(myData); // 输出返回数据 \n'; - - controller: TextAreaController = new TextAreaController() - - build() { - Column() { - // 标题 - TitleBar({ title: $r('app.string.aki_bind_function') }) - - Column() { - Column() { - TextArea({ - text: this.textcont, - placeholder: '', - }) - .placeholderFont({ size: 16, weight: 400 }) - .width('90%') - .margin(10) - .fontSize(16) - .fontColor('#182431') - .backgroundColor('#FFFFFF') - .enabled(false) - - TextArea({ - text: this.testcont, - placeholder: '', - }) - .placeholderFont({ size: 16, weight: 400 }) - .width('90%') - .margin(10) - .fontSize(16) - .fontColor($r('app.color.COLOR_80000000')) - .backgroundColor($r('app.color.COLOR_99FFFFFF')) - .enabled(false) - } - .width('100%') - .alignItems(HorizontalAlign.Center) - .justifyContent(FlexAlign.Start) - - Row() { - - Button($r('app.string.aki_bind_function'), { type: ButtonType.Capsule }) - .backgroundColor(Color.Blue) - .width('80%') - .height(48) - .fontSize(16) - .fontWeight(500) - .fontColor(this.btnFontColor) - .margin({ left: 24 }) - .id('napi_async_execute_callback') - .onClick(() => { - let res = testNapi.SayHello("hell to cpp"); - hilog.info(0x0000, 'testTag', `testNapiValue = ${res} `); - this.testcont = this.testcont.replace('log(myData)', 'log(## '+res+' ##)'); - }) - } - .width('100%') - .height(48) - .alignItems(VerticalAlign.Center) - .justifyContent(FlexAlign.SpaceBetween) - } - .width('100%') - } - .height('100%') - .width('100%') - .backgroundColor($r('app.color.background_shallow_grey')) - } -} diff --git a/examples/akitutorials/entry/src/main/ets/pages/nodeapi/akibindmethod.ets b/examples/akitutorials/entry/src/main/ets/pages/nodeapi/akibindmethod.ets deleted file mode 100644 index d5fb74ef..00000000 --- a/examples/akitutorials/entry/src/main/ets/pages/nodeapi/akibindmethod.ets +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 router from '@ohos.router'; -import image from '@ohos.multimedia.image'; -import Logger from '../../util/Logger'; -import testNapi from 'libentry.so'; -import { TitleBar } from '../../common/TitleBar' -import hilog from '@ohos.hilog'; - -const TAG: string = 'napi_value'; - -@Entry -@Component -struct akibindmethod { - private btnFontColor: Resource = $r('app.color.white'); - private pixelMapFormat: image.PixelMapFormat = 3; - @State isSetInstance: Boolean = false; - @State imagePixelMap: PixelMap | undefined = undefined; - @State textcont: string = 'napi_value 这是一个用于表示 JavaScript 值的不透明指针。' - @State testcont: string = ' // 获取 N-API value \n' - + ' const myData = addon.testNapiValue(); \n' - + ' // 使用获取的自定义数据 \n' - + ' console.log(myData); // 输出自定义数据 \n'; - - controller: TextAreaController = new TextAreaController() - - build() { - Column() { - // 标题 - TitleBar({ title: $r('app.string.aki_bind_method') }) - - Column() { - Column() { - TextArea({ - text: this.textcont, - placeholder: '', - }) - .placeholderFont({ size: 16, weight: 400 }) - .width('90%') - .margin(10) - .fontSize(16) - .fontColor('#182431') - .backgroundColor('#FFFFFF') - .enabled(false) - - TextArea({ - text: this.testcont, - placeholder: '', - }) - .placeholderFont({ size: 16, weight: 400 }) - .width('90%') - .margin(10) - .fontSize(16) - .fontColor('#ff400336') - .backgroundColor('#ff985307') - .enabled(false) - } - .width('100%') - .alignItems(HorizontalAlign.Center) - .justifyContent(FlexAlign.Start) - - Row() { - - Button($r('app.string.aki_bind_method'), { type: ButtonType.Capsule }) - .backgroundColor(Color.Blue) - .width('80%') - .height(48) - .fontSize(16) - .fontWeight(500) - .fontColor(this.btnFontColor) - .margin({ left: 24 }) - .id('napi_async_complete_callback') - .onClick(() => { - let res = ''; - hilog.info(0x0000, 'testTag', `testNapiValue = ${res} `); - this.testcont = this.testcont.replace('log(myData)', 'log(## '+res+' ##)'); - }) - } - .width('100%') - .height(48) - .alignItems(VerticalAlign.Center) - .justifyContent(FlexAlign.SpaceBetween) - } - .width('100%') - } - .height('100%') - .width('100%') - .backgroundColor($r('app.color.background_shallow_grey')) - } -} diff --git a/examples/akitutorials/entry/src/main/resources/base/profile/main_pages.json b/examples/akitutorials/entry/src/main/resources/base/profile/main_pages.json index 5d185b28..d0efca29 100644 --- a/examples/akitutorials/entry/src/main/resources/base/profile/main_pages.json +++ b/examples/akitutorials/entry/src/main/resources/base/profile/main_pages.json @@ -4,8 +4,6 @@ "pages/nodeapi/akibindclass", "pages/nodeapi/akibindenum", "pages/nodeapi/akibindfunction", - "pages/nodeapi/akibindglobal", - "pages/nodeapi/akibindmethod", "pages/async/akiarraybuffer", "pages/async/akiposttask", "pages/async/akivalue" -- Gitee