From f74c3834084ed2224990351ab218a86a5a2c7d04 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Fri, 1 May 2020 01:26:56 +0800 Subject: [PATCH] feat(napi): tweaks env.throw_error String => JsString JsString.as_str Status into napi_status --- napi/src/lib.rs | 40 ++++++++++++++++++++++++++-------------- napi/src/sys/stable.rs | 27 ++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/napi/src/lib.rs b/napi/src/lib.rs index e0c1f857..1f99ab99 100644 --- a/napi/src/lib.rs +++ b/napi/src/lib.rs @@ -9,6 +9,7 @@ use std::ops::{Deref, DerefMut}; use std::os::raw::{c_char, c_void}; use std::ptr; use std::slice; +use std::str; use std::string::String as RustString; mod call_context; @@ -53,7 +54,7 @@ pub enum Number { } #[derive(Clone, Copy, Debug)] -pub struct String; +pub struct JsString; #[derive(Clone, Copy, Debug)] pub struct Object; @@ -169,7 +170,7 @@ impl Error { impl From for Error { fn from(_error: std::ffi::NulError) -> Self { Error { - status: Status::StringContainsNull, + status: Status::StringExpected, } } } @@ -236,7 +237,7 @@ impl Env { )) } - pub fn create_string<'a, 'b>(&'a self, s: &'b str) -> Result> { + pub fn create_string<'a, 'b>(&'a self, s: &'b str) -> Result> { let mut raw_value = ptr::null_mut(); let status = unsafe { sys::napi_create_string_utf8( @@ -247,16 +248,16 @@ impl Env { ) }; check_status(status)?; - Ok(Value::from_raw_value(self, raw_value, String)) + Ok(Value::from_raw_value(self, raw_value, JsString)) } - pub fn create_string_utf16<'a, 'b>(&'a self, chars: &[u16]) -> Result> { + pub fn create_string_utf16(&self, chars: &[u16]) -> Result> { let mut raw_value = ptr::null_mut(); let status = unsafe { sys::napi_create_string_utf16(self.0, chars.as_ptr(), chars.len() as u64, &mut raw_value) }; check_status(status)?; - Ok(Value::from_raw_value(self, raw_value, String)) + Ok(Value::from_raw_value(self, raw_value, JsString)) } pub fn create_object<'a>(&'a self) -> Result> { @@ -386,6 +387,12 @@ impl Env { Ok(Value::from_raw_value(self, raw_result, Function)) } + pub fn throw_error(&self, msg: &str) -> Result<()> { + let status = unsafe { sys::napi_throw_error(self.0, ptr::null(), msg.as_ptr() as *const _) }; + check_status(status)?; + Ok(()) + } + pub fn create_reference(&self, value: &Value) -> Result> { let mut raw_ref = ptr::null_mut(); unsafe { @@ -606,9 +613,9 @@ impl ValueType for Number { } } -impl ValueType for String { +impl ValueType for JsString { fn from_raw(_env: sys::napi_env, _raw: sys::napi_value) -> Result { - Ok(String {}) + Ok(JsString {}) } fn matches_raw_type(env: sys::napi_env, raw: sys::napi_value) -> bool { @@ -724,7 +731,7 @@ impl<'env, T: ValueType> Value<'env, T> { }) } - pub fn coerce_to_string(self) -> Result> { + pub fn coerce_to_string(self) -> Result> { let mut new_raw_value = ptr::null_mut(); let status = unsafe { sys::napi_coerce_to_string(self.env.0, self.raw_value, &mut new_raw_value) }; @@ -732,7 +739,7 @@ impl<'env, T: ValueType> Value<'env, T> { Ok(Value { env: self.env, raw_value: self.raw_value, - value: String, + value: JsString, }) } @@ -773,7 +780,7 @@ fn get_raw_type(env: sys::napi_env, raw_value: sys::napi_value) -> sys::napi_val } } -impl<'env> Value<'env, String> { +impl<'env> Value<'env, JsString> { pub fn len(&self) -> Result { let mut raw_length = ptr::null_mut(); unsafe { @@ -790,7 +797,8 @@ impl<'env> Value<'env, String> { } } -impl<'env> Value<'env, String> { +impl<'env> Value<'env, JsString> { + #[inline] pub fn get_ref(&self) -> Result<&[u8]> { let mut written_char_count: u64 = 0; let len = self.len()? + 1; @@ -814,6 +822,10 @@ impl<'env> Value<'env, String> { } } + pub fn as_str(&self) -> Result<&str> { + str::from_utf8(self.get_ref()?).map_err(|_| Error::new(Status::GenericFailure)) + } + pub fn get_ref_mut(&mut self) -> Result<&mut [u8]> { let mut written_char_count: u64 = 0; let len = self.len()? + 1; @@ -838,10 +850,10 @@ impl<'env> Value<'env, String> { } } -impl<'env> TryFrom> for Vec { +impl<'env> TryFrom> for Vec { type Error = Error; - fn try_from(value: Value<'env, String>) -> Result> { + fn try_from(value: Value<'env, JsString>) -> Result> { let mut result = Vec::with_capacity(value.len()? + 1); // Leave room for trailing null byte unsafe { diff --git a/napi/src/sys/stable.rs b/napi/src/sys/stable.rs index f94440aa..8cad684c 100644 --- a/napi/src/sys/stable.rs +++ b/napi/src/sys/stable.rs @@ -17,7 +17,6 @@ pub enum Status { EscapeCalledTwice, HandleScopeMismatch, CallbackScopeMismatch, - StringContainsNull, QueueFull, Closing, BigintExpected, @@ -52,3 +51,29 @@ impl From for Status { } } } + +impl Into for Status { + fn into(self) -> napi_status { + match self { + Self::Ok => napi_status::napi_ok, + Self::InvalidArg => napi_status::napi_invalid_arg, + Self::ObjectExpected => napi_status::napi_object_expected, + Self::StringExpected => napi_status::napi_string_expected, + Self::NameExpected => napi_status::napi_name_expected, + Self::FunctionExpected => napi_status::napi_function_expected, + Self::NumberExpected => napi_status::napi_number_expected, + Self::BooleanExpected => napi_status::napi_boolean_expected, + Self::ArrayExpected => napi_status::napi_array_expected, + Self::GenericFailure => napi_status::napi_generic_failure, + Self::PendingException => napi_status::napi_pending_exception, + Self::Cancelled => napi_status::napi_cancelled, + Self::EscapeCalledTwice => napi_status::napi_escape_called_twice, + Self::HandleScopeMismatch => napi_status::napi_handle_scope_mismatch, + Self::CallbackScopeMismatch => napi_status::napi_callback_scope_mismatch, + Self::QueueFull => napi_status::napi_queue_full, + Self::Closing => napi_status::napi_closing, + Self::BigintExpected => napi_status::napi_bigint_expected, + Self::Unknown => napi_status::napi_generic_failure, + } + } +}