From fd191a458645c309c7b67e54c384460aa0120bae Mon Sep 17 00:00:00 2001 From: huzz Date: Thu, 11 Aug 2022 22:47:13 +0800 Subject: [PATCH] feat(napi): support rust array to js array --- crates/backend/src/typegen.rs | 4 +++ .../src/bindgen_runtime/js_values/array.rs | 30 ++++++++++++++++++ examples/napi/__test__/typegen.spec.ts.md | 2 ++ examples/napi/__test__/typegen.spec.ts.snap | Bin 3471 -> 3488 bytes examples/napi/__test__/values.spec.ts | 4 +++ examples/napi/index.d.ts | 2 ++ examples/napi/src/array.rs | 10 ++++++ 7 files changed, 52 insertions(+) diff --git a/crates/backend/src/typegen.rs b/crates/backend/src/typegen.rs index 344351d7..d0a2357c 100644 --- a/crates/backend/src/typegen.rs +++ b/crates/backend/src/typegen.rs @@ -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), } } diff --git a/crates/napi/src/bindgen_runtime/js_values/array.rs b/crates/napi/src/bindgen_runtime/js_values/array.rs index 31fdaf63..96100b31 100644 --- a/crates/napi/src/bindgen_runtime/js_values/array.rs +++ b/crates/napi/src/bindgen_runtime/js_values/array.rs @@ -177,6 +177,36 @@ impl TypeName for Vec { } } +impl ToNapiValue for [T; N] +where + T: ToNapiValue + Copy, +{ + unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result { + 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 ToNapiValue for &[T] +where + T: ToNapiValue + Copy, +{ + unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result { + 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 ToNapiValue for Vec where T: ToNapiValue, diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index ef2b65cc..bfa3b602 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -26,6 +26,8 @@ Generated by [AVA](https://avajs.dev). export function getNums(): Array␊ export function sumNums(nums: Array): number␊ export function toJsObj(): object␊ + export function getNumArr(): number[]␊ + export function getNestedNumArr(): number[][][]␊ export function readFileAsync(path: string): Promise␊ export function asyncMultiTwo(arg: number): Promise␊ export function bigintAdd(a: bigint, b: bigint): bigint␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 3068b74ca194f4a5f01c846aad48cf54a35de93e..8a9b5be2004743d3d5dba552ee91d92b7cc4cef8 100644 GIT binary patch literal 3488 zcmV;R4PWv>RzV2oz;_fomiW`x#z2`H~d+$S^`%>Kf!v2KLkfKCM)b9E&`LIQ5IGh>IH}&&)6pQd& z{`3n|T*d6_Z*nTKI3g34u#_rBLXoB{R75T2j6`fq#ynQv{`uV>@BI1qHydBxCrZXVQN4&uI*J*&|K(knWP0*}Gl{b_Q{sW$Ga{n}IqbjL ze{g)F0oc`Cq>6+wm6GgUDV8QQJ{o-lx}FZ7lk2;Lq>M%)i5KLHFM4{SHkBp(Q^tr~ z%tj*CuTNj~^5I#J3}Nc#?hTM~pA4s568J}f_e3hBz!IRxJG*=Rx37oa4~|ZTJ(6Uz z5lbz^I7>psMFR3u??f8OpwlD$G^LBDQl&hZJl9)%&6Ffk%vio$I-9sC4zrnq!N5Lm z0LpBp0pY!@!FE~+RN|!s5h8Y>CE2n+pbH8tr)OR$CgISUu=uwDkEgN+D%6*YBn;+M zP2m`{Xkg`=RLrz4L^T~dqg7v`mpFCayX{JF1-adKU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8>**GW^32GI^Vai;-jrBZbzQ8D*6KL9Z)|``6%r-S`Nk1j(K(G z3_L)LmaciEb;X8}f%l7%m0G-(i`4QI`WrRFOOE|~|PhGW& zvMRdet5I+M4&0f1ZQ&9q#(Y13xEuD{vJ9tEn(OS`?C4V%XcN2TE3U|zW%}*Pd&%2-{|D;ZF~Wf{007XRs2TtO diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index 5b21a020..459e4c68 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -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) => { diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index bfbd2069..f653f1bb 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -16,6 +16,8 @@ export function getWords(): Array export function getNums(): Array export function sumNums(nums: Array): number export function toJsObj(): object +export function getNumArr(): number[] +export function getNestedNumArr(): number[][][] export function readFileAsync(path: string): Promise export function asyncMultiTwo(arg: number): Promise export function bigintAdd(a: bigint, b: bigint): bigint diff --git a/examples/napi/src/array.rs b/examples/napi/src/array.rs index bec01a49..1ba32d80 100644 --- a/examples/napi/src/array.rs +++ b/examples/napi/src/array.rs @@ -23,3 +23,13 @@ fn to_js_obj(env: Env) -> napi::Result { 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]]] +}