2021-05-25 00:53:08 +09:00
|
|
|
use std::f64::consts::PI;
|
2020-09-30 19:22:48 +09:00
|
|
|
use std::str;
|
|
|
|
|
2022-11-30 21:54:13 +09:00
|
|
|
use napi::{
|
|
|
|
bindgen_prelude::Uint8Array, CallContext, JsArrayBuffer, JsNumber, JsObject, JsTypedArray,
|
|
|
|
JsUndefined, Result,
|
|
|
|
};
|
2020-09-30 19:22:48 +09:00
|
|
|
|
|
|
|
#[js_function(1)]
|
|
|
|
pub fn get_arraybuffer_length(ctx: CallContext) -> Result<JsNumber> {
|
|
|
|
let buffer = ctx.get::<JsArrayBuffer>(0)?.into_value()?;
|
2022-02-06 11:49:16 +09:00
|
|
|
ctx.env.create_uint32(buffer.len() as u32)
|
2020-09-30 19:22:48 +09:00
|
|
|
}
|
|
|
|
|
2021-05-25 00:53:08 +09:00
|
|
|
#[js_function(1)]
|
|
|
|
pub fn mutate_uint8_array(ctx: CallContext) -> Result<JsUndefined> {
|
2022-11-30 21:54:13 +09:00
|
|
|
let mut buffer = ctx.get::<Uint8Array>(0)?;
|
2021-05-25 00:53:08 +09:00
|
|
|
let buffer_mut_ref: &mut [u8] = buffer.as_mut();
|
|
|
|
buffer_mut_ref[0] = 42;
|
|
|
|
ctx.env.get_undefined()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[js_function(1)]
|
|
|
|
pub fn mutate_uint16_array(ctx: CallContext) -> Result<JsUndefined> {
|
|
|
|
let mut buffer = ctx.get::<JsTypedArray>(0)?.into_value()?;
|
|
|
|
let buffer_mut_ref: &mut [u16] = buffer.as_mut();
|
|
|
|
buffer_mut_ref[0] = 65535;
|
|
|
|
ctx.env.get_undefined()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[js_function(1)]
|
|
|
|
pub fn mutate_int16_array(ctx: CallContext) -> Result<JsUndefined> {
|
|
|
|
let mut buffer = ctx.get::<JsTypedArray>(0)?.into_value()?;
|
|
|
|
let buffer_mut_ref: &mut [i16] = buffer.as_mut();
|
|
|
|
buffer_mut_ref[0] = 32767;
|
|
|
|
ctx.env.get_undefined()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[js_function(1)]
|
|
|
|
pub fn mutate_float32_array(ctx: CallContext) -> Result<JsUndefined> {
|
|
|
|
let mut buffer = ctx.get::<JsTypedArray>(0)?.into_value()?;
|
|
|
|
let buffer_mut_ref: &mut [f32] = buffer.as_mut();
|
2021-05-28 19:50:16 +09:00
|
|
|
buffer_mut_ref[0] = 3.33;
|
2021-05-25 00:53:08 +09:00
|
|
|
ctx.env.get_undefined()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[js_function(1)]
|
|
|
|
pub fn mutate_float64_array(ctx: CallContext) -> Result<JsUndefined> {
|
|
|
|
let mut buffer = ctx.get::<JsTypedArray>(0)?.into_value()?;
|
|
|
|
let buffer_mut_ref: &mut [f64] = buffer.as_mut();
|
|
|
|
buffer_mut_ref[0] = PI;
|
|
|
|
ctx.env.get_undefined()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[js_function(1)]
|
|
|
|
#[cfg(feature = "latest")]
|
|
|
|
pub fn mutate_i64_array(ctx: CallContext) -> Result<JsUndefined> {
|
|
|
|
let mut buffer = ctx.get::<JsTypedArray>(0)?.into_value()?;
|
|
|
|
let buffer_mut_ref: &mut [i64] = buffer.as_mut();
|
|
|
|
buffer_mut_ref[0] = 9223372036854775807;
|
|
|
|
ctx.env.get_undefined()
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
pub fn register_js(exports: &mut JsObject) -> Result<()> {
|
|
|
|
exports.create_named_method("getArraybufferLength", get_arraybuffer_length)?;
|
2021-05-25 00:53:08 +09:00
|
|
|
exports.create_named_method("mutateUint8Array", mutate_uint8_array)?;
|
|
|
|
exports.create_named_method("mutateUint16Array", mutate_uint16_array)?;
|
|
|
|
exports.create_named_method("mutateInt16Array", mutate_int16_array)?;
|
|
|
|
exports.create_named_method("mutateFloat32Array", mutate_float32_array)?;
|
|
|
|
exports.create_named_method("mutateFloat64Array", mutate_float64_array)?;
|
|
|
|
#[cfg(feature = "latest")]
|
|
|
|
exports.create_named_method("mutateI64Array", mutate_i64_array)?;
|
2020-09-30 19:22:48 +09:00
|
|
|
Ok(())
|
|
|
|
}
|