fix code check

Signed-off-by: wangshi <wangshi@kaihong.com>
This commit is contained in:
wangshi 2024-10-17 15:06:38 +08:00
parent 421f1a1f64
commit 93a78cb113
11 changed files with 146 additions and 241 deletions

View File

@ -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>();
}
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<std::string>();
} else if (obj.IsBool()) {
return obj.As<std::string>();
} else if (obj.IsNumber()) {
return obj.As<std::string>();
} else if (obj.IsString()) {
aki::Value val = aki::Value::NewObject();
val.Set("name", obj.As<std::string>());
aki::Value strObj = aki::Value::FromGlobal("JSON")["stringify"](val);
return strObj.As<std::string>();
} 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);
}

View File

@ -16,6 +16,13 @@
export const SayHello: (a: string) => string;
export const AsyncSayHello: (a: string) => Promise<string>;
export const Passing: (flag: TypeFlags) => TypeFlags;
export const ReturnPromiseResolveLater: () => Promise<object>;
export const AsyncTaskReturnArrayBufferWithCommit: (uint8buff: Uint8Array) => Promise<ArrayBuffer>;
export const ConvertToString: (a: object) => string;
export class JSBind {
static initTaskRunner: (param: string) => void;
}
export enum TypeFlags {
NONE,

View File

@ -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'
}
]
}

View File

@ -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%')

View File

@ -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%')

View File

@ -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+' ##)');
})

View File

@ -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';

View File

@ -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';

View File

@ -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'))
}
}

View File

@ -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'))
}
}

View File

@ -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"