2020-12-24 12:53:23 +09:00
|
|
|
use std::mem::ManuallyDrop;
|
2020-06-21 20:10:06 +09:00
|
|
|
use std::str;
|
|
|
|
|
2020-12-24 12:53:23 +09:00
|
|
|
use napi::{
|
|
|
|
noop_finalize, CallContext, ContextlessResult, Env, Error, JsBuffer, JsNumber, JsObject,
|
2020-12-25 01:21:37 +09:00
|
|
|
JsString, JsUndefined, Result, Status,
|
2020-12-24 12:53:23 +09:00
|
|
|
};
|
2020-06-21 20:10:06 +09:00
|
|
|
|
|
|
|
#[js_function(1)]
|
|
|
|
pub fn get_buffer_length(ctx: CallContext) -> Result<JsNumber> {
|
2020-09-28 01:27:37 +09:00
|
|
|
let buffer = ctx.get::<JsBuffer>(0)?.into_value()?;
|
2022-02-06 11:49:16 +09:00
|
|
|
ctx.env.create_uint32(buffer.len() as u32)
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
#[js_function(1)]
|
|
|
|
pub fn buffer_to_string(ctx: CallContext) -> Result<JsString> {
|
2020-09-28 01:27:37 +09:00
|
|
|
let buffer = ctx.get::<JsBuffer>(0)?.into_value()?;
|
2020-06-21 20:10:06 +09:00
|
|
|
ctx.env.create_string(
|
|
|
|
str::from_utf8(&buffer).map_err(|e| Error::new(Status::StringExpected, format!("{}", e)))?,
|
|
|
|
)
|
|
|
|
}
|
2020-09-02 18:05:53 +09:00
|
|
|
|
2020-10-11 22:54:23 +09:00
|
|
|
#[js_function(1)]
|
|
|
|
pub fn copy_buffer(ctx: CallContext) -> Result<JsBuffer> {
|
|
|
|
let buffer = ctx.get::<JsBuffer>(0)?.into_value()?;
|
|
|
|
ctx.env.create_buffer_copy(buffer).map(|b| b.into_raw())
|
|
|
|
}
|
|
|
|
|
2020-12-24 12:53:23 +09:00
|
|
|
#[contextless_function]
|
|
|
|
pub fn create_borrowed_buffer_with_noop_finalize(env: Env) -> ContextlessResult<JsBuffer> {
|
|
|
|
let data = vec![1, 2, 3];
|
|
|
|
let data_ptr = data.as_ptr();
|
|
|
|
let length = data.len();
|
|
|
|
let manually_drop = ManuallyDrop::new(data);
|
|
|
|
|
2020-12-24 16:45:59 +09:00
|
|
|
unsafe { env.create_buffer_with_borrowed_data(data_ptr, length, manually_drop, noop_finalize) }
|
|
|
|
.map(|b| Some(b.into_raw()))
|
2020-12-24 12:53:23 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
#[contextless_function]
|
|
|
|
pub fn create_borrowed_buffer_with_finalize(env: Env) -> ContextlessResult<JsBuffer> {
|
|
|
|
let data = vec![1, 2, 3];
|
|
|
|
let data_ptr = data.as_ptr();
|
|
|
|
let length = data.len();
|
|
|
|
let manually_drop = ManuallyDrop::new(data);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
env.create_buffer_with_borrowed_data(
|
|
|
|
data_ptr,
|
|
|
|
length,
|
|
|
|
manually_drop,
|
2020-12-24 16:45:59 +09:00
|
|
|
|mut hint: ManuallyDrop<Vec<u8>>, _| {
|
2020-12-24 12:53:23 +09:00
|
|
|
ManuallyDrop::drop(&mut hint);
|
2020-12-24 16:45:59 +09:00
|
|
|
},
|
2020-12-24 12:53:23 +09:00
|
|
|
)
|
|
|
|
}
|
|
|
|
.map(|b| Some(b.into_raw()))
|
|
|
|
}
|
|
|
|
|
2022-04-23 17:43:36 +09:00
|
|
|
#[contextless_function]
|
|
|
|
pub fn create_empty_borrowed_buffer_with_finalize(env: Env) -> ContextlessResult<JsBuffer> {
|
|
|
|
let data = vec![];
|
|
|
|
let data_ptr = data.as_ptr();
|
|
|
|
let length = data.len();
|
|
|
|
let manually_drop = ManuallyDrop::new(data);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
env.create_buffer_with_borrowed_data(
|
|
|
|
data_ptr,
|
|
|
|
length,
|
|
|
|
manually_drop,
|
|
|
|
|mut hint: ManuallyDrop<Vec<u8>>, _| {
|
|
|
|
ManuallyDrop::drop(&mut hint);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
.map(|b| Some(b.into_raw()))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[contextless_function]
|
|
|
|
pub fn create_empty_buffer(env: Env) -> ContextlessResult<JsBuffer> {
|
|
|
|
let data = vec![];
|
|
|
|
|
|
|
|
env
|
|
|
|
.create_buffer_with_data(data)
|
|
|
|
.map(|b| Some(b.into_raw()))
|
|
|
|
}
|
|
|
|
|
2020-12-25 01:21:37 +09:00
|
|
|
#[js_function(1)]
|
|
|
|
fn mutate_buffer(ctx: CallContext) -> Result<JsUndefined> {
|
|
|
|
let buffer = &mut ctx.get::<JsBuffer>(0)?.into_value()?;
|
|
|
|
buffer[1] = 42;
|
|
|
|
ctx.env.get_undefined()
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
pub fn register_js(exports: &mut JsObject) -> Result<()> {
|
|
|
|
exports.create_named_method("getBufferLength", get_buffer_length)?;
|
|
|
|
exports.create_named_method("bufferToString", buffer_to_string)?;
|
|
|
|
exports.create_named_method("copyBuffer", copy_buffer)?;
|
2020-12-24 12:53:23 +09:00
|
|
|
exports.create_named_method(
|
|
|
|
"createBorrowedBufferWithNoopFinalize",
|
|
|
|
create_borrowed_buffer_with_noop_finalize,
|
|
|
|
)?;
|
|
|
|
exports.create_named_method(
|
|
|
|
"createBorrowedBufferWithFinalize",
|
|
|
|
create_borrowed_buffer_with_finalize,
|
|
|
|
)?;
|
2022-04-23 17:43:36 +09:00
|
|
|
exports.create_named_method(
|
|
|
|
"createEmptyBorrowedBufferWithFinalize",
|
|
|
|
create_empty_borrowed_buffer_with_finalize,
|
|
|
|
)?;
|
|
|
|
exports.create_named_method("createEmptyBuffer", create_empty_buffer)?;
|
2020-12-25 01:21:37 +09:00
|
|
|
exports.create_named_method("mutateBuffer", mutate_buffer)?;
|
2020-09-02 18:05:53 +09:00
|
|
|
Ok(())
|
|
|
|
}
|