Merge pull request #1263 from xhuz/main

feat(napi): support rust array to js array
This commit is contained in:
LongYinan 2022-08-12 21:04:43 +08:00 committed by GitHub
commit 6bd682279d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 0 deletions

View file

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

View file

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

View file

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

View file

@ -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) => {

View file

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

View file

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