neon/sys/
string.rs

1use std::{mem::MaybeUninit, ptr};
2
3use super::{
4    bindings as napi,
5    raw::{Env, Local},
6};
7
8pub unsafe fn new(out: &mut Local, env: Env, data: *const u8, len: i32) -> bool {
9    let status = napi::create_string_utf8(env, data as *const _, len as usize, out);
10
11    status.is_ok()
12}
13
14pub unsafe fn utf8_len(env: Env, value: Local) -> usize {
15    let mut len = MaybeUninit::uninit();
16    napi::get_value_string_utf8(env, value, ptr::null_mut(), 0, len.as_mut_ptr()).unwrap();
17
18    len.assume_init()
19}
20
21pub unsafe fn data(env: Env, out: *mut u8, len: usize, value: Local) -> usize {
22    let mut read = MaybeUninit::uninit();
23
24    napi::get_value_string_utf8(env, value, out as *mut _, len, read.as_mut_ptr()).unwrap();
25
26    read.assume_init()
27}
28
29pub unsafe fn utf16_len(env: Env, value: Local) -> usize {
30    let mut len = MaybeUninit::uninit();
31
32    napi::get_value_string_utf16(env, value, ptr::null_mut(), 0, len.as_mut_ptr()).unwrap();
33
34    len.assume_init()
35}
36
37pub unsafe fn data_utf16(env: Env, out: *mut u16, len: usize, value: Local) -> usize {
38    let mut read = MaybeUninit::uninit();
39    napi::get_value_string_utf16(env, value, out, len, read.as_mut_ptr()).unwrap();
40
41    read.assume_init()
42}
43
44pub unsafe fn run_script(out: &mut Local, env: Env, value: Local) -> bool {
45    let status = napi::run_script(env, value, out as *mut _);
46
47    status == Ok(())
48}