Skip to content

Commit 1c450fb

Browse files
committed
feat: support struct with field type stringArr doubleArr
1 parent 36affdd commit 1c450fb

File tree

6 files changed

+321
-195
lines changed

6 files changed

+321
-195
lines changed

cpp/sum.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,16 @@ typedef struct Person {
5252
const char *name;
5353
int age;
5454
double doubleProps;
55-
// char **stringArrProps;
55+
char **stringArrProps;
5656
// Parent parent;
5757
} Person;
5858

5959
extern "C" const Person *getStruct(const Person *person) {
6060
printf("Name: %s\n", person->name);
6161
printf("Age: %d\n", person->age);
6262
printf("doubleProps: %f \n", person->doubleProps);
63-
// printf("stringArrProps: %s\n", person->stringArrProps[0]);
63+
printf("stringArrProps: %s\n", person->stringArrProps[0]);
64+
printf("stringArrProps: %s\n", person->stringArrProps[1]);
6465
// printf("Parent Age: %d\n", person->parent.age);
6566
// printf("Parent Name: %s\n", person->parent.name);
6667
return person;

scripts/type.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ const { readFile, writeFile } = require('fs/promises')
22
const { resolve } = require('path');
33

44
(async () => {
5+
const entryContent = (await readFile(resolve(process.cwd(), './index.js'))).toString()
6+
.replace('paramsType: Array<unknown>', 'paramsType: Array<DataFieldType>')
7+
.replace('retType: unknown', 'retType: DataFieldType')
8+
await writeFile(resolve(process.cwd(), './index.js'), `
9+
${entryContent}
10+
module.exports.arrayConstructor = (options) => ({
11+
...options,
12+
ffiTypeTag: 'array'
13+
})
14+
`)
15+
516
const typeContent = (await readFile(resolve(process.cwd(), './index.d.ts'))).toString()
617
.replace('paramsType: Array<unknown>', 'paramsType: Array<DataFieldType>')
718
.replace('retType: unknown', 'retType: DataFieldType')
@@ -13,14 +24,6 @@ const { resolve } = require('path');
1324
retType: DataType.I32 | DataType.Double
1425
}): number
1526
16-
export function load(params: FfiParams & {
17-
retType: DataType.I32Array | DataType.DoubleArray
18-
}): Array<number>
19-
20-
export function load(params: FfiParams & {
21-
retType: DataType.StringArray
22-
}): Array<string>
23-
2427
export function load(params: FfiParams & {
2528
retType: DataType.Boolean
2629
}): Boolean
@@ -41,8 +44,23 @@ const { resolve } = require('path');
4144
never;
4245
};
4346
export function load<T extends Record<string, DataType>>(params: FfiParams & { retType: T }): DataTypeToType<T>
47+
export type ArrayConstructorOptions = {
48+
type: DataType
49+
length: number
50+
}
51+
export function arrayConstructor<T extends ffiTypeTag>(options: ArrayConstructorOptions): ArrayConstructorOptions & {
52+
ffiTypeTag: T
53+
}
54+
type ffiTypeTag = 'double' | 'string' | 'i32';
55+
56+
type FfiReturnType<T extends ffiTypeTag> = T extends 'double' ? number :
57+
T extends 'i32' ? number :
58+
string;
4459
45-
export type DataFieldType = DataType | Record<string, DataType>
60+
export function load<T extends ffiTypeTag>(params: FfiParams & { retType: { ffiTypeTag: T } }): Array<FfiReturnType<T>>
61+
export type DataFieldType = DataType | Record<string, DataType>| ArrayConstructorOptions & {
62+
ffiTypeTag: ffiTypeTag
63+
}
4664
${typeContent}
4765
`)
4866
})()

src/define.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use indexmap::IndexMap;
22
use napi::bindgen_prelude::*;
3-
use napi::{Env, JsObject};
3+
use napi::{Env, JsObject, JsUnknown};
44
use std::hash::Hash;
55

66
#[derive(Clone)]
@@ -84,3 +84,17 @@ pub enum RsArgsValue {
8484
Void(()),
8585
Function(JsFunction, JsFunction),
8686
}
87+
88+
#[napi(object)]
89+
pub struct FFIParams {
90+
pub library: String,
91+
pub func_name: String,
92+
pub ret_type: JsUnknown,
93+
pub params_type: Vec<JsUnknown>,
94+
pub params_value: Vec<JsUnknown>,
95+
}
96+
#[napi(object)]
97+
pub struct OpenParams {
98+
pub library: String,
99+
pub path: String,
100+
}

0 commit comments

Comments
 (0)