Merge pull request #247 from napi-rs/create-array
feat(napi): implement create_array
This commit is contained in:
commit
b71ea17176
4 changed files with 109 additions and 0 deletions
|
@ -215,6 +215,12 @@ impl Env {
|
|||
Ok(JsObject::from_raw_unchecked(self.0, raw_value))
|
||||
}
|
||||
|
||||
pub fn create_array(&self) -> Result<JsObject> {
|
||||
let mut raw_value = ptr::null_mut();
|
||||
check_status(unsafe { sys::napi_create_array(self.0, &mut raw_value) })?;
|
||||
Ok(JsObject::from_raw_unchecked(self.0, raw_value))
|
||||
}
|
||||
|
||||
pub fn create_array_with_length(&self, length: usize) -> Result<JsObject> {
|
||||
let mut raw_value = ptr::null_mut();
|
||||
check_status(unsafe {
|
||||
|
|
47
test_module/__test__/array.spec.ts
Normal file
47
test_module/__test__/array.spec.ts
Normal file
|
@ -0,0 +1,47 @@
|
|||
import test from 'ava'
|
||||
|
||||
const bindings = require('../index.node')
|
||||
|
||||
test('should be able to create array', (t) => {
|
||||
const arr: number[] = bindings.testCreateArray()
|
||||
t.true(arr instanceof Array)
|
||||
t.true(Array.isArray(arr))
|
||||
arr.push(1, 2, 3)
|
||||
t.deepEqual(arr, [1, 2, 3])
|
||||
})
|
||||
|
||||
test('should be able to create array with length', (t) => {
|
||||
const len = 100
|
||||
const arr: number[] = bindings.testCreateArrayWithLength(len)
|
||||
t.true(arr instanceof Array)
|
||||
t.true(Array.isArray(arr))
|
||||
t.is(arr.length, len)
|
||||
})
|
||||
|
||||
test('should be able to set element', (t) => {
|
||||
const obj = {}
|
||||
const index = 29
|
||||
const arr: unknown[] = []
|
||||
bindings.testSetElement(arr, index, obj)
|
||||
t.is(arr[index], obj)
|
||||
})
|
||||
|
||||
test('should be able to use has_element', (t) => {
|
||||
const arr: any[] = [1, '3', undefined]
|
||||
const index = 29
|
||||
arr[index] = {}
|
||||
t.true(bindings.testHasElement(arr, 0))
|
||||
t.true(bindings.testHasElement(arr, 1))
|
||||
t.true(bindings.testHasElement(arr, 2))
|
||||
t.false(bindings.testHasElement(arr, 3))
|
||||
t.false(bindings.testHasElement(arr, 10))
|
||||
t.true(bindings.testHasElement(arr, index))
|
||||
})
|
||||
|
||||
test('should be able to delete element', (t) => {
|
||||
const arr: number[] = [0, 1, 2, 3]
|
||||
for (const [index] of arr.entries()) {
|
||||
t.true(bindings.testDeleteElement(arr, index))
|
||||
t.true(arr[index] === undefined)
|
||||
}
|
||||
})
|
54
test_module/src/array.rs
Normal file
54
test_module/src/array.rs
Normal file
|
@ -0,0 +1,54 @@
|
|||
use std::convert::TryInto;
|
||||
|
||||
use napi::{
|
||||
CallContext, ContextlessResult, Env, JsBoolean, JsNumber, JsObject, JsUndefined, JsUnknown,
|
||||
Module, Result,
|
||||
};
|
||||
|
||||
#[contextless_function]
|
||||
fn test_create_array(env: Env) -> ContextlessResult<JsObject> {
|
||||
env.create_array().map(Some)
|
||||
}
|
||||
|
||||
#[js_function(1)]
|
||||
fn test_create_array_with_length(ctx: CallContext) -> Result<JsObject> {
|
||||
ctx
|
||||
.env
|
||||
.create_array_with_length(ctx.get::<JsNumber>(0)?.try_into()?)
|
||||
}
|
||||
|
||||
#[js_function(3)]
|
||||
fn test_set_element(ctx: CallContext) -> Result<JsUndefined> {
|
||||
let mut arr = ctx.get::<JsObject>(0)?;
|
||||
let index = ctx.get::<JsNumber>(1)?;
|
||||
let ele = ctx.get::<JsUnknown>(2)?;
|
||||
arr.set_element(index.try_into()?, ele)?;
|
||||
|
||||
ctx.env.get_undefined()
|
||||
}
|
||||
|
||||
#[js_function(2)]
|
||||
fn test_has_element(ctx: CallContext) -> Result<JsBoolean> {
|
||||
let arr = ctx.get::<JsObject>(0)?;
|
||||
let index = ctx.get::<JsNumber>(1)?;
|
||||
|
||||
ctx.env.get_boolean(arr.has_element(index.try_into()?)?)
|
||||
}
|
||||
|
||||
#[js_function(2)]
|
||||
fn test_delete_element(ctx: CallContext) -> Result<JsBoolean> {
|
||||
let mut arr = ctx.get::<JsObject>(0)?;
|
||||
let index = ctx.get::<JsNumber>(1)?;
|
||||
|
||||
ctx.env.get_boolean(arr.delete_element(index.try_into()?)?)
|
||||
}
|
||||
|
||||
pub fn register_js(module: &mut Module) -> Result<()> {
|
||||
module.create_named_method("testCreateArray", test_create_array)?;
|
||||
module.create_named_method("testCreateArrayWithLength", test_create_array_with_length)?;
|
||||
module.create_named_method("testSetElement", test_set_element)?;
|
||||
module.create_named_method("testHasElement", test_has_element)?;
|
||||
module.create_named_method("testDeleteElement", test_delete_element)?;
|
||||
|
||||
Ok(())
|
||||
}
|
|
@ -20,6 +20,7 @@ mod napi6;
|
|||
#[cfg(napi4)]
|
||||
mod tokio_rt;
|
||||
|
||||
mod array;
|
||||
mod arraybuffer;
|
||||
mod buffer;
|
||||
mod class;
|
||||
|
@ -42,6 +43,7 @@ register_module!(test_module, init);
|
|||
|
||||
fn init(module: &mut Module) -> Result<()> {
|
||||
module.create_named_method("getNapiVersion", get_napi_version)?;
|
||||
array::register_js(module)?;
|
||||
error::register_js(module)?;
|
||||
string::register_js(module)?;
|
||||
serde::register_js(module)?;
|
||||
|
|
Loading…
Reference in a new issue