feat(napi): support rust array to js array
This commit is contained in:
parent
8784ed9459
commit
fd191a4586
7 changed files with 52 additions and 0 deletions
|
@ -351,6 +351,10 @@ pub fn ty_to_ts_type(ty: &Type, is_return_ty: bool, is_struct_field: bool) -> (S
|
|||
ts_ty.unwrap_or_else(|| ("any".to_owned(), false))
|
||||
}
|
||||
Type::Group(g) => ty_to_ts_type(&g.elem, is_return_ty, is_struct_field),
|
||||
Type::Array(a) => {
|
||||
let (element_type, is_optional) = ty_to_ts_type(&a.elem, is_return_ty, is_struct_field);
|
||||
(format!("{}[]", element_type), is_optional)
|
||||
}
|
||||
_ => ("any".to_owned(), false),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -177,6 +177,36 @@ impl<T> TypeName for Vec<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T, const N: usize> ToNapiValue for [T; N]
|
||||
where
|
||||
T: ToNapiValue + Copy,
|
||||
{
|
||||
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
|
||||
let mut arr = Array::new(env, val.len() as u32)?;
|
||||
|
||||
for (i, v) in val.into_iter().enumerate() {
|
||||
arr.set(i as u32, v)?;
|
||||
}
|
||||
|
||||
unsafe { Array::to_napi_value(env, arr) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ToNapiValue for &[T]
|
||||
where
|
||||
T: ToNapiValue + Copy,
|
||||
{
|
||||
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
|
||||
let mut arr = Array::new(env, val.len() as u32)?;
|
||||
|
||||
for (i, v) in val.iter().enumerate() {
|
||||
arr.set(i as u32, *v)?;
|
||||
}
|
||||
|
||||
unsafe { Array::to_napi_value(env, arr) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ToNapiValue for Vec<T>
|
||||
where
|
||||
T: ToNapiValue,
|
||||
|
|
|
@ -26,6 +26,8 @@ Generated by [AVA](https://avajs.dev).
|
|||
export function getNums(): Array<number>␊
|
||||
export function sumNums(nums: Array<number>): number␊
|
||||
export function toJsObj(): object␊
|
||||
export function getNumArr(): number[]␊
|
||||
export function getNestedNumArr(): number[][][]␊
|
||||
export function readFileAsync(path: string): Promise<Buffer>␊
|
||||
export function asyncMultiTwo(arg: number): Promise<number>␊
|
||||
export function bigintAdd(a: bigint, b: bigint): bigint␊
|
||||
|
|
Binary file not shown.
|
@ -105,6 +105,8 @@ import {
|
|||
AnotherClassForEither,
|
||||
receiveDifferentClass,
|
||||
useTokioWithoutAsync,
|
||||
getNumArr,
|
||||
getNestedNumArr,
|
||||
} from '../'
|
||||
|
||||
test('export const', (t) => {
|
||||
|
@ -144,6 +146,8 @@ test('array', (t) => {
|
|||
t.deepEqual(getWords(), ['foo', 'bar'])
|
||||
|
||||
t.is(sumNums([1, 2, 3, 4, 5]), 15)
|
||||
t.deepEqual(getNumArr(), [1, 2])
|
||||
t.deepEqual(getNestedNumArr(), [[[1]], [[1]]])
|
||||
})
|
||||
|
||||
test('map', (t) => {
|
||||
|
|
2
examples/napi/index.d.ts
vendored
2
examples/napi/index.d.ts
vendored
|
@ -16,6 +16,8 @@ export function getWords(): Array<string>
|
|||
export function getNums(): Array<number>
|
||||
export function sumNums(nums: Array<number>): number
|
||||
export function toJsObj(): object
|
||||
export function getNumArr(): number[]
|
||||
export function getNestedNumArr(): number[][][]
|
||||
export function readFileAsync(path: string): Promise<Buffer>
|
||||
export function asyncMultiTwo(arg: number): Promise<number>
|
||||
export function bigintAdd(a: bigint, b: bigint): bigint
|
||||
|
|
|
@ -23,3 +23,13 @@ fn to_js_obj(env: Env) -> napi::Result<JsObject> {
|
|||
arr.insert(42)?;
|
||||
arr.coerce_to_object()
|
||||
}
|
||||
|
||||
#[napi]
|
||||
fn get_num_arr() -> [u32; 2] {
|
||||
[1, 2]
|
||||
}
|
||||
|
||||
#[napi]
|
||||
fn get_nested_num_arr() -> [[[u32; 1]; 1]; 2] {
|
||||
[[[1]], [[1]]]
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue