1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(napi): correct arguments for napi_get_typedarray_info (#17306)

This commit is contained in:
Bartek Iwańczuk 2023-01-14 14:00:48 +01:00 committed by GitHub
parent d0f88fc1ca
commit b4ce48c80a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 190 additions and 18 deletions

View file

@ -1375,7 +1375,9 @@ fn napi_get_arraybuffer_info(
if !data.is_null() {
*data = get_array_buffer_ptr(buf);
}
*length = buf.byte_length();
if !length.is_null() {
*length = buf.byte_length();
}
Ok(())
}
@ -1409,7 +1411,9 @@ fn napi_get_buffer_info(
if !data.is_null() {
*data = get_array_buffer_ptr(abuf);
}
*length = abuf.byte_length();
if !length.is_null() {
*length = abuf.byte_length();
}
Ok(())
}
@ -1649,22 +1653,59 @@ fn napi_get_reference_value(
#[napi_sym::napi_sym]
fn napi_get_typedarray_info(
env: *mut Env,
value: napi_value,
data: *mut *mut u8,
typedarray: napi_value,
type_: *mut napi_typedarray_type,
length: *mut usize,
data: *mut *mut c_void,
arraybuffer: *mut napi_value,
byte_offset: *mut usize,
) -> Result {
let env: &mut Env = env.as_mut().ok_or(Error::InvalidArg)?;
let value = transmute::<napi_value, v8::Local<v8::Value>>(value);
let buf = v8::Local::<v8::TypedArray>::try_from(value).unwrap();
let buffer_name = v8::String::new(&mut env.scope(), "buffer").unwrap();
let abuf = v8::Local::<v8::ArrayBuffer>::try_from(
buf.get(&mut env.scope(), buffer_name.into()).unwrap(),
)
.unwrap();
if !data.is_null() {
*data = get_array_buffer_ptr(abuf);
let value = transmute::<napi_value, v8::Local<v8::Value>>(typedarray);
let array = v8::Local::<v8::TypedArray>::try_from(value)
.ok()
.ok_or(Error::InvalidArg)?;
if !type_.is_null() {
if value.is_int8_array() {
*type_ = napi_int8_array;
} else if value.is_uint8_array() {
*type_ = napi_uint8_array;
} else if value.is_uint8_clamped_array() {
*type_ = napi_uint8_clamped_array;
} else if value.is_int16_array() {
*type_ = napi_int16_array;
} else if value.is_uint16_array() {
*type_ = napi_uint16_array;
} else if value.is_int32_array() {
*type_ = napi_int32_array;
} else if value.is_uint32_array() {
*type_ = napi_uint32_array;
} else if value.is_float32_array() {
*type_ = napi_float32_array;
} else if value.is_float64_array() {
*type_ = napi_float64_array;
}
}
*length = abuf.byte_length();
if !length.is_null() {
*length = array.length();
}
if !data.is_null() || !arraybuffer.is_null() {
let buf = array.buffer(&mut env.scope()).unwrap();
if !data.is_null() {
*data = get_array_buffer_ptr(buf) as *mut c_void;
}
if !arraybuffer.is_null() {
*arraybuffer = buf.into();
}
}
if !byte_offset.is_null() {
*byte_offset = array.byte_offset();
}
Ok(())
}

View file

@ -1,12 +1,116 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use crate::assert_napi_ok;
use crate::napi_get_callback_info;
use crate::napi_new_property;
use core::ffi::c_void;
use napi_sys::TypedarrayType::uint8_array;
use napi_sys::Status::napi_ok;
use napi_sys::TypedarrayType;
use napi_sys::ValueType::napi_number;
use napi_sys::ValueType::napi_object;
use napi_sys::*;
use std::ptr;
extern "C" fn test_multiply(
env: napi_env,
info: napi_callback_info,
) -> napi_value {
let (args, argc, _) = napi_get_callback_info!(env, info, 2);
assert_eq!(argc, 2);
let mut ty = -1;
assert_napi_ok!(napi_typeof(env, args[0], &mut ty));
assert_eq!(ty, napi_object);
let input_array = args[0];
let mut is_typed_array = false;
assert!(
unsafe { napi_is_typedarray(env, input_array, &mut is_typed_array) }
== napi_ok
);
let mut ty = -1;
assert_napi_ok!(napi_typeof(env, args[1], &mut ty));
assert_eq!(ty, napi_number);
let mut multiplier: f64 = 0.0;
assert_napi_ok!(napi_get_value_double(env, args[1], &mut multiplier));
let mut ty = -1;
let mut input_buffer = ptr::null_mut();
let mut byte_offset = 0;
let mut length = 0;
assert_napi_ok!(napi_get_typedarray_info(
env,
input_array,
&mut ty,
&mut length,
ptr::null_mut(),
&mut input_buffer,
&mut byte_offset,
));
let mut data = ptr::null_mut();
let mut byte_length = 0;
assert_napi_ok!(napi_get_arraybuffer_info(
env,
input_buffer,
&mut data,
&mut byte_length
));
let mut output_buffer = ptr::null_mut();
let mut output_ptr = ptr::null_mut();
assert_napi_ok!(napi_create_arraybuffer(
env,
byte_length,
&mut output_ptr,
&mut output_buffer,
));
let mut output_array: napi_value = ptr::null_mut();
assert_napi_ok!(napi_create_typedarray(
env,
ty,
length,
output_buffer,
byte_offset,
&mut output_array,
));
if ty == TypedarrayType::uint8_array {
let input_bytes = unsafe { (data as *mut u8).offset(byte_offset as isize) };
let output_bytes = output_ptr as *mut u8;
for i in 0..length {
unsafe {
*output_bytes.offset(i as isize) =
(*input_bytes.offset(i as isize) as f64 * multiplier) as u8;
}
}
} else if ty == TypedarrayType::float64_array {
let input_doubles =
unsafe { (data as *mut f64).offset(byte_offset as isize) };
let output_doubles = output_ptr as *mut f64;
for i in 0..length {
unsafe {
*output_doubles.offset(i as isize) =
*input_doubles.offset(i as isize) * multiplier;
}
}
} else {
assert_napi_ok!(napi_throw_error(
env,
ptr::null(),
"Typed array was of a type not expected by test.".as_ptr() as *const i8,
));
return ptr::null_mut();
}
output_array
}
extern "C" fn test_external(
env: napi_env,
_info: napi_callback_info,
@ -25,7 +129,7 @@ extern "C" fn test_external(
let mut typedarray: napi_value = ptr::null_mut();
assert_napi_ok!(napi_create_typedarray(
env,
uint8_array,
TypedarrayType::uint8_array,
external.len(),
arraybuffer,
0,
@ -37,7 +141,10 @@ extern "C" fn test_external(
}
pub fn init(env: napi_env, exports: napi_value) {
let properties = &[napi_new_property!(env, "test_external", test_external)];
let properties = &[
napi_new_property!(env, "test_external", test_external),
napi_new_property!(env, "test_multiply", test_multiply),
];
assert_napi_ok!(napi_define_properties(
env,

View file

@ -1,9 +1,33 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { assertEquals, loadTestLibrary } from "./common.js";
import { assert, assertEquals, loadTestLibrary } from "./common.js";
const typedarray = loadTestLibrary();
Deno.test("napi typedarray uint8", function () {
const byteArray = new Uint8Array([0, 1, 2]);
assertEquals(byteArray.length, 3);
const byteResult = typedarray.test_multiply(byteArray, 3);
assert(byteResult instanceof Uint8Array);
assertEquals(byteResult.length, 3);
assertEquals(byteResult[0], 0);
assertEquals(byteResult[1], 3);
assertEquals(byteResult[2], 6);
});
Deno.test("napi typedarray float64", function () {
const doubleArray = new Float64Array([0.0, 1.1, 2.2]);
assertEquals(doubleArray.length, 3);
const doubleResult = typedarray.test_multiply(doubleArray, -3);
assert(doubleResult instanceof Float64Array);
assertEquals(doubleResult.length, 3);
assertEquals(doubleResult[0], -0);
assertEquals(Math.round(10 * doubleResult[1]) / 10, -3.3);
assertEquals(Math.round(10 * doubleResult[2]) / 10, -6.6);
});
Deno.test("napi typedarray external", function () {
assertEquals(
new Uint8Array(typedarray.test_external()),