use std::ops::Deref;
pub struct HostStr<'a>(&'a str);
impl<'a> HostStr<'a> {
#[inline]
pub unsafe fn from_raw(ptr: *const u8, len: usize) -> Self {
if len == 0 || ptr.is_null() {
return Self("");
}
let slice = unsafe { std::slice::from_raw_parts(ptr, len) };
let s = std::str::from_utf8(slice).unwrap_or("");
Self(s)
}
}
impl<'a> Deref for HostStr<'a> {
type Target = str;
#[inline]
fn deref(&self) -> &str {
self.0
}
}
impl<'a> AsRef<str> for HostStr<'a> {
#[inline]
fn as_ref(&self) -> &str {
self.0
}
}
impl<'a> std::fmt::Display for HostStr<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl std::fmt::Debug for HostStr<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
#[repr(C)]
pub struct JsStr {
pub ptr: *const u8,
pub len: isize,
}
impl JsStr {
#[inline]
pub fn new(s: &str) -> Self {
if s.is_empty() {
return Self::empty();
}
unsafe extern "C" {
fn js_allocator_dupe(src: *const u8, len: usize) -> *mut u8;
}
let ptr = unsafe { js_allocator_dupe(s.as_ptr(), s.len()) };
assert!(
!ptr.is_null(),
"js_allocator_dupe returned null: Zig arena OOM"
);
Self {
ptr,
len: s.len() as isize,
}
}
#[inline]
pub fn empty() -> Self {
Self {
ptr: std::ptr::null(),
len: 0,
}
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len == 0
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct JsStrField {
pub ptr: *const u8,
pub len: usize,
}
impl JsStrField {
#[inline]
pub fn new(s: &str) -> Self {
if s.is_empty() {
return Self::empty();
}
unsafe extern "C" {
fn js_allocator_dupe(src: *const u8, len: usize) -> *mut u8;
}
let ptr = unsafe { js_allocator_dupe(s.as_ptr(), s.len()) };
assert!(
!ptr.is_null(),
"js_allocator_dupe returned null: Zig arena OOM"
);
Self { ptr, len: s.len() }
}
#[inline]
pub fn empty() -> Self {
Self {
ptr: std::ptr::null(),
len: 0,
}
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len == 0
}
}
#[cfg(any(test, feature = "stub-allocator"))]
#[unsafe(no_mangle)]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn js_allocator_dupe(src: *const u8, len: usize) -> *mut u8 {
let layout = std::alloc::Layout::from_size_align(len, 1).unwrap();
let ptr = unsafe { std::alloc::alloc(layout) };
if ptr.is_null() {
return std::ptr::null_mut();
}
if len > 0 {
unsafe {
std::ptr::copy_nonoverlapping(src, ptr, len);
}
}
ptr
}
#[cfg(any(test, feature = "stub-allocator"))]
#[unsafe(no_mangle)]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn js_allocator_alloc(size: usize) -> *mut u8 {
if size == 0 {
return std::ptr::null_mut();
}
let layout = std::alloc::Layout::from_size_align(size, 1).unwrap();
let ptr = unsafe { std::alloc::alloc(layout) };
if ptr.is_null() {
std::ptr::null_mut()
} else {
ptr
}
}
#[doc(hidden)]
pub unsafe fn js_str_to_result(ptr: *const u8, len: isize) -> Result<String, String> {
if len < 0 {
let err_len = (-len) as usize;
let err_msg = if err_len > 0 && !ptr.is_null() {
let slice = unsafe { std::slice::from_raw_parts(ptr, err_len) };
String::from_utf8_lossy(slice).into_owned()
} else {
"unknown error".to_string()
};
return Err(err_msg);
}
if ptr.is_null() || len == 0 {
return Ok(String::new());
}
let slice = unsafe { std::slice::from_raw_parts(ptr, len as usize) };
Ok(String::from_utf8_lossy(slice).into_owned())
}