From 4f924a686a27c7bc22de88d15ef01fc6b05b8420 Mon Sep 17 00:00:00 2001 From: devsnek Date: Tue, 6 Oct 2020 13:16:22 -0500 Subject: [PATCH] add all TypedArray constructors (#482) --- src/binding.cc | 30 ++++++++++++++++++++++-------- src/lib.rs | 2 +- src/typed_array.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/uint8_array.rs | 26 -------------------------- tests/test_api.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 35 deletions(-) create mode 100644 src/typed_array.rs delete mode 100644 src/uint8_array.rs diff --git a/src/binding.cc b/src/binding.cc index 6dab2f38..cb173a43 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -788,8 +788,8 @@ int v8__ObjectTemplate__InternalFieldCount(const v8::ObjectTemplate& self) { return ptr_to_local(&self)->InternalFieldCount(); } -void v8__ObjectTemplate__SetInternalFieldCount( - const v8::ObjectTemplate& self, int value) { +void v8__ObjectTemplate__SetInternalFieldCount(const v8::ObjectTemplate& self, + int value) { ptr_to_local(&self)->SetInternalFieldCount(value); } @@ -936,7 +936,7 @@ const v8::Value* v8__Object__GetInternalField(const v8::Object& self, } void v8__Object__SetInternalField(const v8::Object& self, int index, - const v8::Value& value) { + const v8::Value& value) { ptr_to_local(&self)->SetInternalField(index, ptr_to_local(&value)); } @@ -1395,11 +1395,25 @@ void v8__TryCatch__SetCaptureMessage(v8::TryCatch* self, bool value) { self->SetCaptureMessage(value); } -const v8::Uint8Array* v8__Uint8Array__New(const v8::ArrayBuffer& buf_ptr, - size_t byte_offset, size_t length) { - return local_to_ptr( - v8::Uint8Array::New(ptr_to_local(&buf_ptr), byte_offset, length)); -} +#define V(NAME) \ + const v8::NAME* v8__##NAME##__New(const v8::ArrayBuffer& buf_ptr, \ + size_t byte_offset, size_t length) { \ + return local_to_ptr( \ + v8::NAME::New(ptr_to_local(&buf_ptr), byte_offset, length)); \ + } + +V(Uint8Array) +V(Uint8ClampedArray) +V(Int8Array) +V(Uint16Array) +V(Int16Array) +V(Uint32Array) +V(Int32Array) +V(Float32Array) +V(Float64Array) +V(BigUint64Array) +V(BigInt64Array) +#undef V const v8::Script* v8__Script__Compile(const v8::Context& context, const v8::String& source, diff --git a/src/lib.rs b/src/lib.rs index a30975ca..facd0f60 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,7 +66,7 @@ mod string; mod support; mod symbol; mod template; -mod uint8_array; +mod typed_array; mod value; mod value_deserializer; mod value_serializer; diff --git a/src/typed_array.rs b/src/typed_array.rs new file mode 100644 index 00000000..dacb1d23 --- /dev/null +++ b/src/typed_array.rs @@ -0,0 +1,39 @@ +// Copyright 2019-2020 the Deno authors. All rights reserved. MIT license. +use crate::ArrayBuffer; +use crate::HandleScope; +use crate::Local; + +macro_rules! typed_array { + ($name:ident, $func:ident) => { + use crate::$name; + impl $name { + pub fn new<'s>( + scope: &mut HandleScope<'s>, + buf: Local, + byte_offset: usize, + length: usize, + ) -> Option> { + extern "C" { + fn $func( + buf_ptr: *const ArrayBuffer, + byte_offset: usize, + length: usize, + ) -> *const $name; + } + unsafe { scope.cast_local(|_| $func(&*buf, byte_offset, length)) } + } + } + }; +} + +typed_array!(Uint8Array, v8__Uint8Array__New); +typed_array!(Uint8ClampedArray, v8__Uint8ClampedArray__New); +typed_array!(Int8Array, v8__Int8Array__New); +typed_array!(Uint16Array, v8__Uint16Array__New); +typed_array!(Int16Array, v8__Int16Array__New); +typed_array!(Uint32Array, v8__Uint32Array__New); +typed_array!(Int32Array, v8__Int32Array__New); +typed_array!(Float32Array, v8__Float32Array__New); +typed_array!(Float64Array, v8__Float64Array__New); +typed_array!(BigUint64Array, v8__BigUint64Array__New); +typed_array!(BigInt64Array, v8__BigInt64Array__New); diff --git a/src/uint8_array.rs b/src/uint8_array.rs deleted file mode 100644 index d633e33b..00000000 --- a/src/uint8_array.rs +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2019-2020 the Deno authors. All rights reserved. MIT license. -use crate::ArrayBuffer; -use crate::HandleScope; -use crate::Local; -use crate::Uint8Array; - -extern "C" { - fn v8__Uint8Array__New( - buf_ptr: *const ArrayBuffer, - byte_offset: usize, - length: usize, - ) -> *const Uint8Array; -} - -impl Uint8Array { - pub fn new<'s>( - scope: &mut HandleScope<'s>, - buf: Local, - byte_offset: usize, - length: usize, - ) -> Option> { - unsafe { - scope.cast_local(|_| v8__Uint8Array__New(&*buf, byte_offset, length)) - } - } -} diff --git a/tests/test_api.rs b/tests/test_api.rs index aeff2e14..d17a797a 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -2313,6 +2313,50 @@ fn uint8_array() { } } +#[test] +fn typed_array_constructors() { + let _setup_guard = setup(); + let isolate = &mut v8::Isolate::new(Default::default()); + let scope = &mut v8::HandleScope::new(isolate); + let context = v8::Context::new(scope); + let scope = &mut v8::ContextScope::new(scope, context); + + let ab = v8::ArrayBuffer::new(scope, 8); + + let t = v8::Uint8Array::new(scope, ab, 0, 0).unwrap(); + assert!(t.is_uint8_array()); + + let t = v8::Uint8ClampedArray::new(scope, ab, 0, 0).unwrap(); + assert!(t.is_uint8_clamped_array()); + + let t = v8::Int8Array::new(scope, ab, 0, 0).unwrap(); + assert!(t.is_int8_array()); + + let t = v8::Uint16Array::new(scope, ab, 0, 0).unwrap(); + assert!(t.is_uint16_array()); + + let t = v8::Int16Array::new(scope, ab, 0, 0).unwrap(); + assert!(t.is_int16_array()); + + let t = v8::Uint32Array::new(scope, ab, 0, 0).unwrap(); + assert!(t.is_uint32_array()); + + let t = v8::Int32Array::new(scope, ab, 0, 0).unwrap(); + assert!(t.is_int32_array()); + + let t = v8::Float32Array::new(scope, ab, 0, 0).unwrap(); + assert!(t.is_float32_array()); + + let t = v8::Float64Array::new(scope, ab, 0, 0).unwrap(); + assert!(t.is_float64_array()); + + let t = v8::BigUint64Array::new(scope, ab, 0, 0).unwrap(); + assert!(t.is_big_uint64_array()); + + let t = v8::BigInt64Array::new(scope, ab, 0, 0).unwrap(); + assert!(t.is_big_int64_array()); +} + #[test] fn dynamic_import() { let _setup_guard = setup();