From f29ddc3a19ffbe4b2aced8e80e529ee3aa0ca32b Mon Sep 17 00:00:00 2001 From: LongYinan Date: Mon, 20 Apr 2020 00:43:48 +0800 Subject: [PATCH] feat(napi): impl create_uint32 --- README.md | 2 +- napi/src/lib.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 676a5257..32e11029 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,7 @@ npm test | [napi_create_typedarray](https://nodejs.org/api/n-api.html#n_api_napi_create_typedarray) | 1 | v8.0.0 | ⛔️ | | [napi_create_dataview](https://nodejs.org/api/n-api.html#n_api_napi_create_dataview) | 1 | v8.3.0 | ⛔️ | | [napi_create_int32](https://nodejs.org/api/n-api.html#n_api_napi_create_int32) | 1 | v8.4.0 | ⛔️ | -| [napi_create_uint32](https://nodejs.org/api/n-api.html#n_api_napi_create_uint32) | 1 | v8.4.0 | ⛔️ | +| [napi_create_uint32](https://nodejs.org/api/n-api.html#n_api_napi_create_uint32) | 1 | v8.4.0 | ✅ | | [napi_create_int64](https://nodejs.org/api/n-api.html#n_api_napi_create_int64) | 1 | v8.4.0 | ✅ | | [napi_create_double](https://nodejs.org/api/n-api.html#n_api_napi_create_double) | 1 | v8.4.0 | ⛔️ | | [napi_create_bigint_int64](https://nodejs.org/api/n-api.html#n_api_napi_create_bigint_int64) | 6 | v10.7.0 | ⛔️ | diff --git a/napi/src/lib.rs b/napi/src/lib.rs index c6f5f08f..e0f7045f 100644 --- a/napi/src/lib.rs +++ b/napi/src/lib.rs @@ -45,6 +45,7 @@ pub struct Boolean { #[derive(Clone, Copy, Debug)] pub enum Number { Int(i64), + U32(u32), Double(f64), } @@ -266,6 +267,14 @@ impl Env { Ok(Value::from_raw_value(self, raw_value, Number::Int(int))) } + pub fn create_uint32<'a>(&'a self, number: u32) -> Result> { + let mut raw_value = ptr::null_mut(); + let status = + unsafe { sys::napi_create_uint32(self.0, number, (&mut raw_value) as *mut sys::napi_value) }; + check_status(status)?; + Ok(Value::from_raw_value(self, raw_value, Number::U32(number))) + } + pub fn create_double<'a>(&'a self, double: f64) -> Result> { let mut raw_value = ptr::null_mut(); let status = @@ -1043,6 +1052,12 @@ impl<'env> Value<'env, Object> { } } +impl<'env> AsRef<[u8]> for Value<'env, Buffer> { + fn as_ref(&self) -> &[u8] { + self.deref() + } +} + impl<'env> Deref for Value<'env, Buffer> { type Target = [u8];