From 36e808d2a622a9f540ac828f665ecd4f352f7835 Mon Sep 17 00:00:00 2001 From: Jose Acevedo Date: Mon, 17 Jan 2022 20:50:55 -0800 Subject: [PATCH 1/2] feat: add 'coerce_to_object' method to Array --- crates/napi/src/bindgen_runtime/js_values/array.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/napi/src/bindgen_runtime/js_values/array.rs b/crates/napi/src/bindgen_runtime/js_values/array.rs index f687439e..2b6c2137 100644 --- a/crates/napi/src/bindgen_runtime/js_values/array.rs +++ b/crates/napi/src/bindgen_runtime/js_values/array.rs @@ -1,6 +1,6 @@ use std::ptr; -use crate::{bindgen_prelude::*, check_status, sys, ValueType}; +use crate::{bindgen_prelude::*, check_status, sys, JsObject, Value, ValueType}; pub struct Array { env: sys::napi_env, @@ -69,6 +69,16 @@ impl Array { pub fn len(&self) -> u32 { self.len } + + pub fn coerce_to_object(self) -> Result { + let mut new_raw_value = ptr::null_mut(); + check_status!(unsafe { sys::napi_coerce_to_object(self.env, self.inner, &mut new_raw_value) })?; + Ok(JsObject(Value { + env: self.env, + value: new_raw_value, + value_type: ValueType::Object, + })) + } } impl TypeName for Array { From 9a4f8cf32c115d95577fd7b82ef4c4cbd6e9be68 Mon Sep 17 00:00:00 2001 From: Jose Acevedo Date: Mon, 17 Jan 2022 21:29:47 -0800 Subject: [PATCH 2/2] test: added test for coerce_to_object on Array --- examples/napi/__test__/typegen.spec.ts.md | 1 + examples/napi/__test__/typegen.spec.ts.snap | Bin 2354 -> 2362 bytes examples/napi/index.d.ts | 1 + examples/napi/src/array.rs | 10 ++++++++++ 4 files changed, 12 insertions(+) diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index eb49d821..61d797e9 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -25,6 +25,7 @@ Generated by [AVA](https://avajs.dev). /** Gets some numbers */␊ export function getNums(): Array␊ export function sumNums(nums: Array): number␊ + export function toJsObj(): object␊ 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 68d623b1b8ad6d4f91b34e697949b6f9afcc8f78..6da8ac63cf3d82676b385d6fbf7c040d75150413 100644 GIT binary patch delta 2219 zcmV;c2vqm761oyHK~_N^Q*L2!b7*gLAa*kf0|0(qCWoEzUDOr7O|Z*7e#L$MjMS2o z*dZs<6ICkd-_VgVDGo9dCrF&`EV984CDAs=k!f0glHWc1!!!K(&F`Q6@wdTOFJBO? zA}(|*&_8%A9J%17o5B%59AhZ6=}RC+f6b6j%mUatgfFcmV|8mlak{34SB4(`P}k< zWzI)QA`|itaAycbE?C$kaIp0zm&rYXZ;?h(Nz6rVjO!6gE)D)!(leLhaS^B5p;}yC zjXNa_6ck5>RE}KWEKsSDV5f#DeE3pEjEWL_xHOgwXut@#vI+a!GPlPXe!DCA!57e7 zrgXsgE$d)jDlBJ-w`RmnlmY0<#F}S+CB4&Be=$k)LAZYkkw+Npky{$2;CM8_R4z0Z zsf6sdvkm!2Q)CXoNH$AJ&fvb|A~aS-ss}U~n>^}VWzy?r^6ixc2`KADyz?bMzy@1 zRB4I1@Ca8Z_?ck2B#?(pFyiMZVYt78i>^!)_}<0uOMVZZd-!?8ha=SD8(j3bq4QUR z%rL08Hmm#!*!Jb|&bG%|^TI62!=psrnXvZi0@Nm~yzY65|D8F(wNMZ1lIlDW8|@z2 zl?m%xW1rv0_uzWm#8_6Pa9*bmjeN#fazaQ=Gwj)ke zo)~O#TUsAZX&j>^0qBB3uM}n`Ymz0^I!CevyBcqa)o6ScN{v=^1T61);6~p@(x=7<*z(E&8JdkM(AB0eJ)AY^ ziRw8IKV_Xlf%^+qKt)*h0;u4@E1b>J_~=T&|n}gtPWv+M#966*QM4ZEF-lb>Wn)k zzd$b-JGXVc*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rNubvP22P>PTFfd60Yy(i&n}?r*#YpS9&GJB9sC1@ zUTW(p@Csbk%)iG1)25w;^O?9liP}VKG12PTNWBPl2d%42b=!yC4%jV`Q3TCyP32gB zH8di%r;utA#fPTJFwhrf@dWxfiecYKLq!Lyn{B%j+Q!BPTr!vyT=eSNj9u_m+oY6e z^=F-6;QtErx>VEXM275FCilf6LIBu~3Z24!m6KnL!JmQo>A-2iYB0d5A^qsfoo!S8 z^WNm@_A&wH||5(4=t_69G#kf{?&occy}X79p?Gtz#4f$KADP%g-Rx$h>UGj zgEalg?tX%_^*QDERsfy_jS-7rs?w79kaAN$;z1M1NSylaz+WM;3<;ma0E(4^IOGxT z&sX>$;W@jF>os?HG7n~L!6!5-COtFuL7Jy_+hq9SscZ~~Edx^223vTrC^&q7ESj_7 z%`lsDHKwsZh!QAEfG{N!1{or>lwexbJTPI7BphmZy+%@i0v9v1y@peZRK8ik6(9N> z)^<6;cdPXXhrU2gCNLq{ePoUWmQCahC;$D=zyE3WQM|j6q!T{J*44>M58*PW3`h&3 zv{9$w+SGFUmch)9bGvy43`R760khysAP&dH614lALa}7ZV(AeK-`Nu__zY741^3J! zX`^Y&g8guH{qXqmht~7wg$lO4KSj?nuL~#>h*xp)9oWH+w;ktxF+_P1&ML8WE_^Jg zXe(ULn7@|=e5CpR5ioAhk*R$JTUP9uxrzQPhCvp7+Y`6 zpuxoeHF((rG@omwGC5rmMqt-v+0+}eX)9+8>qcvVYo&w$I1>*Rl{^vXfh^b!>mW?j zMv@7g`mD!T_@?E&ZB?{iu2CEFDjY)o_wXg(=^HX0$ialFO(Z zHoUXDtH12#9AL?JdR*Lp(0-ceh}E^W9h3XX6kmY^f)j1v{{->7~uFfpN-}(`JHMjg)jZDiTzkGnmv4|xEP@9@zCjyGEz%hW0z;n zvVvnBL-UWFGpa{bVU;;uo*uec3a_xb<5Rs-R&JF@*aTh$sUf+4E();tGFqWafq~>y zj3dzf{b /** Gets some numbers */ export function getNums(): Array export function sumNums(nums: Array): number +export function toJsObj(): object 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 01d5f546..bec01a49 100644 --- a/examples/napi/src/array.rs +++ b/examples/napi/src/array.rs @@ -1,3 +1,5 @@ +use napi::{Env, JsObject}; + #[napi] pub fn get_words() -> Vec<&'static str> { vec!["foo", "bar"] @@ -13,3 +15,11 @@ fn get_nums() -> Vec { fn sum_nums(nums: Vec) -> u32 { nums.iter().sum() } + +#[napi] +fn to_js_obj(env: Env) -> napi::Result { + let mut arr = env.create_array(0)?; + arr.insert("a string")?; + arr.insert(42)?; + arr.coerce_to_object() +}