feat(napi): expose create_string_from_c_char for C ffi scenario

This commit is contained in:
LongYinan 2021-04-14 14:05:07 +08:00
parent 3be8136a7a
commit d1bceccbe6
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
2 changed files with 21 additions and 15 deletions

View file

@ -1,3 +1,4 @@
#![allow(clippy::expect_fun_call)]
use std::collections::hash_map::DefaultHasher;
use std::env;
use std::fs::{metadata, write};

View file

@ -173,29 +173,34 @@ impl Env {
#[inline]
pub fn create_string(&self, s: &str) -> Result<JsString> {
self.create_string_from_chars(s.as_ptr() as *const c_char, s.len())
unsafe { self.create_string_from_c_char(s.as_ptr() as *const c_char, s.len()) }
}
#[inline]
pub fn create_string_from_std(&self, s: String) -> Result<JsString> {
self.create_string_from_chars(s.as_ptr() as *const c_char, s.len())
unsafe { self.create_string_from_c_char(s.as_ptr() as *const c_char, s.len()) }
}
#[inline]
pub fn create_string_from_vec_u8(&self, bytes: Vec<u8>) -> Result<JsString> {
self.create_string_from_chars(bytes.as_ptr() as *const c_char, bytes.len())
}
#[inline]
pub fn create_string_from_vec_i8(&self, bytes: Vec<i8>) -> Result<JsString> {
self.create_string_from_chars(bytes.as_ptr() as *const c_char, bytes.len())
}
#[inline]
fn create_string_from_chars(&self, data_ptr: *const c_char, len: usize) -> Result<JsString> {
/// This API is used for C ffi scenario.
/// Convert raw *const c_char into JsString
///
/// # Safety
///
/// Create JsString from known valid utf-8 string
pub unsafe fn create_string_from_c_char(
&self,
data_ptr: *const c_char,
len: usize,
) -> Result<JsString> {
let mut raw_value = ptr::null_mut();
check_status!(unsafe { sys::napi_create_string_utf8(self.0, data_ptr, len, &mut raw_value) })?;
Ok(unsafe { JsString::from_raw_unchecked(self.0, raw_value) })
check_status!(sys::napi_create_string_utf8(
self.0,
data_ptr,
len,
&mut raw_value
))?;
Ok(JsString::from_raw_unchecked(self.0, raw_value))
}
#[inline]