Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ohos_rs_example/build-profile.json5
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
{
"name": "default",
"signingConfig": "default",
"compatibleSdkVersion": "4.0.0(10)",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是否应该迁移到 API12?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

支持12的。

"runtimeOS": "HarmonyOS"
}
],
"buildModeSet": [
Expand Down
67 changes: 67 additions & 0 deletions ohos_rs_example/entry/oh-package-lock.json5

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions ohos_rs_example/entry/oh-package.json5
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@
"author": "",
"license": "",
"dependencies": {
'libarraybuffer.so': 'file:./src/main/cpp/types/libarraybuffer',
'libarray.so': 'file:./src/main/cpp/types/libarray',
'libbasic.so': 'file:./src/main/cpp/types/libbasic',
'libeither.so': 'file:./src/main/cpp/types/libeither',
'libexternal.so': 'file:./src/main/cpp/types/libexternal',
'libhello.so': 'file:./src/main/cpp/types/libhello',
'libtsfn.so': 'file:./src/main/cpp/types/libtsfn',
'libtokio_rt.so': 'file:./src/main/cpp/types/libtokio_rt',
}
}
9 changes: 9 additions & 0 deletions ohos_rs_example/entry/src/main/cpp/types/libarray/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const createArray: () => [];

export const createArrayWithLength: (l: number) => [];

export const setElement: (data: number[], i: number, ele: number) => [];

export const deleteElement: (data: number[], ele: number) => [];

export const hasElement: (data: number[], ele: number) => boolean;
10 changes: 10 additions & 0 deletions ohos_rs_example/entry/src/main/cpp/types/libarray/oh-package.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "libarray.so",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里没有 type 字段?看起来加了类型没什么实际的意义。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因 使用新版 IDE报错无法编译运行项目才手动添加的类型。

"author": "",
"license": "",
"dependencies": {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

export const getArraybufferLength: (a: ArrayBuffer) => number;
export const mutateUint8Array: (a: Uint8Array) => undefined;
export const mutateUint16Array: (a: Uint16Array) => undefined;
export const mutateInt16Array: (a: Int16Array) => undefined;
export const mutateFloat32Array: (a: Float32Array) => undefined;
export const mutateFloat64Array: (a: Float64Array) => undefined;
export const mutateI64Array: (a: BigInt64Array) => undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

类型用自动生成的吧,不要手写。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohos能自动生成类型? 我没找到相关文档,要是能自动生成这个PR就可以关了。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohrs 对于使用 napi 修饰的宏会自动生成类型包。也不完全关闭,工程升级到 12 还是有必要的。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohrs 对于使用 napi 修饰的宏会自动生成类型包。也不完全关闭,工程升级到 12 还是有必要的。

ohrs 对于使用 napi 修饰的宏会自动生成类型包。 请教下, 这个我没看到?如何操作才会生成类型文件?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://ohos.rs/docs/basic/quick-start.html
通过 ohrs 构建之后在 dist 文件夹有对应的类型文件,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
我哪儿操作不对? build 后没类型文件,升级到了最新版本后 还是没。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shouyong 仅 napi 宏修饰的会产生,比如 hello basic 示例下的代码。不好意思 事情太多了

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "libarraybuffer.so",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "",
"author": "",
"license": "",
"dependencies": {
}
}
14 changes: 14 additions & 0 deletions ohos_rs_example/entry/src/main/cpp/types/libbasic/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const getBool: () => boolean;

export const getNumber: () => number;

export const getUndefined: () => undefined;

export const getNull: () => null;

export const getArray: () => [];

export const getObject: () => object;

export const bigintAdd: (a: bigint, b: bigint) => bigint;

10 changes: 10 additions & 0 deletions ohos_rs_example/entry/src/main/cpp/types/libbasic/oh-package.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "libbasic.so",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "",
"author": "",
"license": "",
"dependencies": {
}
}
5 changes: 5 additions & 0 deletions ohos_rs_example/entry/src/main/cpp/types/libeither/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const eitherNumberString: (a: number | string) => number;

export const dynamicArgumentLength: (a?: number) => number;


Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "libeither.so",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "",
"author": "",
"license": "",
"dependencies": {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const createExternal: (a: number ) => object;
export const getExternalCount: (a: object ) => number;
export const createExternalWithHint: (a: number ) => object;



Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "libexternal.so",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "",
"author": "",
"license": "",
"dependencies": {
}
}
4 changes: 4 additions & 0 deletions ohos_rs_example/entry/src/main/cpp/types/libhello/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const hello: () => string;



10 changes: 10 additions & 0 deletions ohos_rs_example/entry/src/main/cpp/types/libhello/oh-package.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "libhello.so",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "",
"author": "",
"license": "",
"dependencies": {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const executeTokioReadFile: (p: string) => Promise<[]>;
export const executeTokioReadFileFailed: (p: string) => Promise<[]>;



Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "libtokio_rt.so",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "",
"author": "",
"license": "",
"dependencies": {
}
}
4 changes: 4 additions & 0 deletions ohos_rs_example/entry/src/main/cpp/types/libtsfn/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const testTsFn: (a: (...args: number[]) => void) => string;



10 changes: 10 additions & 0 deletions ohos_rs_example/entry/src/main/cpp/types/libtsfn/oh-package.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "libtsfn.so",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "",
"author": "",
"license": "",
"dependencies": {
}
}
13 changes: 7 additions & 6 deletions ohos_rs_example/entry/src/main/ets/pages/Array.ets
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@ struct Array {
Row() {
Column({ space: 20 }) {
Button("createArray").onClick(() => {
const a = nativeArray.createArray();
const a: [] = nativeArray.createArray();
log.info("createArray", String(a.length === 0 && typeof a === "object"));
})
Button("createArrayWithLength").onClick(() => {
const a = nativeArray.createArrayWithLength(10);
log.info("createArrayWithLength array", String(a.length === 10 && typeof a === "object"));
const a: [] = nativeArray.createArrayWithLength(10);
const l: number = a.length;
log.info("createArrayWithLength array", String(l === 10 && typeof a === "object"));
})
Button("setElement").onClick(() => {
const a = nativeArray.setElement([1, 3, 4], 2, 2);
const a: [] = nativeArray.setElement([1, 3, 4], 2, 2);
log.info("setElement array", String(a.join("")));
})
Button("hasElement").onClick(() => {
const a = nativeArray.hasElement([1, 2, 3, 4], 5);
const a: boolean = nativeArray.hasElement([1, 2, 3, 4], 5);
log.info("hasElement array", String(a));
})
Button("deleteElement").onClick(() => {
const a = nativeArray.deleteElement([1, 2, 3, 4], 2);
const a: [] = nativeArray.deleteElement([1, 2, 3, 4], 2);
log.info("deleteElement array", String(a.join("")));
})
}
Expand Down
2 changes: 1 addition & 1 deletion ohos_rs_example/entry/src/main/ets/pages/ArrayBuffer.ets
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct ArrayBuffer {
Column({ space: 20 }) {
Button("getBufferLength").onClick(() => {
const fixture = buffer.from('wow, hello');
const length = nativeArrayBuffer.getArraybufferLength(fixture.buffer);
const length : number= nativeArrayBuffer.getArraybufferLength(fixture.buffer);
log.info("getBufferLength",String(length), String(fixture.buffer.byteLength));
})
Button("mutateUint8Array").onClick(() => {
Expand Down
16 changes: 8 additions & 8 deletions ohos_rs_example/entry/src/main/ets/pages/Basic.ets
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@ struct Basic {
Row() {
Column({ space: 20 }) {
Button('Get Boolean').onClick(() => {
const b = basic.getBool();
const b: boolean = basic.getBool();
log.info(b.toString());
})
Button("Get Number").onClick(() => {
const n = basic.getNumber();
const n : number = basic.getNumber();
log.info(n.toString());
})
Button("Get Undefined").onClick(() => {
const u = basic.getUndefined();
const u :undefined= basic.getUndefined();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

类型完善的情况下,是否可以移除这些类型声明

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已经移除。

log.info("undefined: ", String(typeof u === 'undefined'));
})
Button('Get Null').onClick(() => {
const n = basic.getNull();
const n :null= basic.getNull();
log.info("null: ", String(typeof n === 'object' && n === null));
})
Button('Get Array').onClick(() => {
const a = basic.getArray();
const a :[]= basic.getArray();
log.info(String(a));
})
Button('Bigint').onClick(() => {
const r = basic.bigintAdd(BigInt(100), BigInt(200));
const r: bigint = basic.bigintAdd(BigInt(100), BigInt(200));
log.info(String(r));
})
Button("Get Object").onClick(() => {
const o = basic.getObject();
log.info(String(o.test));
const o: object = basic.getObject();
log.info(JSON.stringify(o));
})
}
.width('100%')
Expand Down
7 changes: 4 additions & 3 deletions ohos_rs_example/entry/src/main/ets/pages/TokioRT.ets
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct TokioRT {
let file = fs.openSync(filesDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
// 写入一段内容至文件
fs.writeSync(file.fd, "Try to write str.");
const result = await tokioRT.executeTokioReadFile(filesDir + '/test.txt');
const result: [] = await tokioRT.executeTokioReadFile(filesDir + '/test.txt');
log.info(`Read file result: ${result.toString()}`)
}

Expand All @@ -27,7 +27,7 @@ struct TokioRT {
await tokioRT.executeTokioReadFileFailed(filesDir + '/test.txt');
throw new Error("Unreachable")
} catch (e) {
log.info(e.message);
log.error(e.message);
}
}

Expand All @@ -36,8 +36,9 @@ struct TokioRT {
let file = fs.openSync(filesDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
// 写入一段内容至文件
fs.writeSync(file.fd, "Try to write str.");

const buffers = await Promise.all(
Array.from({ length: 50 }).map((_) => tokioRT.executeTokioReadFile(filesDir + '/test.txt'))
Array.from<number>({ length: 50 }).map(() => tokioRT.executeTokioReadFile(filesDir + '/test.txt'))
);
for (const fileContent of buffers) {
log.info(`${fileContent.toString()}`)
Expand Down
8 changes: 4 additions & 4 deletions ohos_rs_example/entry/src/main/ets/utils/log.ets
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ class Logger {
this.domain = 0xFF00
}

debug(...args: any[]) {
debug(...args: string[]) {
hilog.debug(this.domain, this.prefix, this.format, args)
}

info(...args: any[]) {
info(...args: string[]) {
hilog.info(this.domain, this.prefix, this.format, args)
}

warn(...args: any[]) {
warn(...args: string[]) {
hilog.warn(this.domain, this.prefix, this.format, args)
}

error(...args: any[]) {
error(...args: string[]) {
hilog.error(this.domain, this.prefix, this.format, args)
}
}
Expand Down
Loading