feat(napi): expose create_string_from_c_char for C ffi scenario
This commit is contained in:
parent
3be8136a7a
commit
d1bceccbe6
2 changed files with 21 additions and 15 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
#![allow(clippy::expect_fun_call)]
|
||||||
use std::collections::hash_map::DefaultHasher;
|
use std::collections::hash_map::DefaultHasher;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::{metadata, write};
|
use std::fs::{metadata, write};
|
||||||
|
|
|
@ -173,29 +173,34 @@ impl Env {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn create_string(&self, s: &str) -> Result<JsString> {
|
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]
|
#[inline]
|
||||||
pub fn create_string_from_std(&self, s: String) -> Result<JsString> {
|
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]
|
#[inline]
|
||||||
pub fn create_string_from_vec_u8(&self, bytes: Vec<u8>) -> Result<JsString> {
|
/// This API is used for C ffi scenario.
|
||||||
self.create_string_from_chars(bytes.as_ptr() as *const c_char, bytes.len())
|
/// Convert raw *const c_char into JsString
|
||||||
}
|
///
|
||||||
|
/// # Safety
|
||||||
#[inline]
|
///
|
||||||
pub fn create_string_from_vec_i8(&self, bytes: Vec<i8>) -> Result<JsString> {
|
/// Create JsString from known valid utf-8 string
|
||||||
self.create_string_from_chars(bytes.as_ptr() as *const c_char, bytes.len())
|
pub unsafe fn create_string_from_c_char(
|
||||||
}
|
&self,
|
||||||
|
data_ptr: *const c_char,
|
||||||
#[inline]
|
len: usize,
|
||||||
fn create_string_from_chars(&self, data_ptr: *const c_char, len: usize) -> Result<JsString> {
|
) -> Result<JsString> {
|
||||||
let mut raw_value = ptr::null_mut();
|
let mut raw_value = ptr::null_mut();
|
||||||
check_status!(unsafe { sys::napi_create_string_utf8(self.0, data_ptr, len, &mut raw_value) })?;
|
check_status!(sys::napi_create_string_utf8(
|
||||||
Ok(unsafe { JsString::from_raw_unchecked(self.0, raw_value) })
|
self.0,
|
||||||
|
data_ptr,
|
||||||
|
len,
|
||||||
|
&mut raw_value
|
||||||
|
))?;
|
||||||
|
Ok(JsString::from_raw_unchecked(self.0, raw_value))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
Loading…
Reference in a new issue