Merge pull request #39 from Brooooooklyn/tweak

feat(napi): tweaks
This commit is contained in:
LongYinan 2020-05-01 01:45:19 +08:00 committed by GitHub
commit efb590447e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 15 deletions

View file

@ -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<std::ffi::NulError> 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<Value<'a, String>> {
pub fn create_string<'a, 'b>(&'a self, s: &'b str) -> Result<Value<'a, JsString>> {
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<Value<'a, String>> {
pub fn create_string_utf16(&self, chars: &[u16]) -> Result<Value<JsString>> {
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<Value<'a, Object>> {
@ -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<T>(&self, value: &Value<T>) -> Result<Ref<T>> {
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<Self> {
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<Value<'env, String>> {
pub fn coerce_to_string(self) -> Result<Value<'env, JsString>> {
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<usize> {
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<Value<'env, String>> for Vec<u16> {
impl<'env> TryFrom<Value<'env, JsString>> for Vec<u16> {
type Error = Error;
fn try_from(value: Value<'env, String>) -> Result<Vec<u16>> {
fn try_from(value: Value<'env, JsString>) -> Result<Vec<u16>> {
let mut result = Vec::with_capacity(value.len()? + 1); // Leave room for trailing null byte
unsafe {

View file

@ -17,7 +17,6 @@ pub enum Status {
EscapeCalledTwice,
HandleScopeMismatch,
CallbackScopeMismatch,
StringContainsNull,
QueueFull,
Closing,
BigintExpected,
@ -52,3 +51,29 @@ impl From<napi_status> for Status {
}
}
}
impl Into<self::napi_status> 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,
}
}
}