Merge pull request #1038 from jose-acevedoflores/add_coerce_to_object_to_array
Add 'coerce_to_object' method to Array
This commit is contained in:
commit
785e2ada06
5 changed files with 23 additions and 1 deletions
|
@ -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<JsObject> {
|
||||
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 {
|
||||
|
|
|
@ -25,6 +25,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
/** Gets some numbers */␊
|
||||
export function getNums(): Array<number>␊
|
||||
export function sumNums(nums: Array<number>): number␊
|
||||
export function toJsObj(): object␊
|
||||
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.
1
examples/napi/index.d.ts
vendored
1
examples/napi/index.d.ts
vendored
|
@ -15,6 +15,7 @@ export function getWords(): Array<string>
|
|||
/** Gets some numbers */
|
||||
export function getNums(): Array<number>
|
||||
export function sumNums(nums: Array<number>): number
|
||||
export function toJsObj(): object
|
||||
export function readFileAsync(path: string): Promise<Buffer>
|
||||
export function asyncMultiTwo(arg: number): Promise<number>
|
||||
export function bigintAdd(a: bigint, b: bigint): bigint
|
||||
|
|
|
@ -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<u32> {
|
|||
fn sum_nums(nums: Vec<u32>) -> u32 {
|
||||
nums.iter().sum()
|
||||
}
|
||||
|
||||
#[napi]
|
||||
fn to_js_obj(env: Env) -> napi::Result<JsObject> {
|
||||
let mut arr = env.create_array(0)?;
|
||||
arr.insert("a string")?;
|
||||
arr.insert(42)?;
|
||||
arr.coerce_to_object()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue