mirror of
https://github.com/denoland/rusty_v8.git
synced 2025-01-11 08:34:01 -05:00
add all TypedArray constructors (#482)
This commit is contained in:
parent
dbc0509f2a
commit
4f924a686a
5 changed files with 106 additions and 35 deletions
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
|
|
39
src/typed_array.rs
Normal file
39
src/typed_array.rs
Normal file
|
@ -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<ArrayBuffer>,
|
||||
byte_offset: usize,
|
||||
length: usize,
|
||||
) -> Option<Local<'s, $name>> {
|
||||
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);
|
|
@ -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<ArrayBuffer>,
|
||||
byte_offset: usize,
|
||||
length: usize,
|
||||
) -> Option<Local<'s, Uint8Array>> {
|
||||
unsafe {
|
||||
scope.cast_local(|_| v8__Uint8Array__New(&*buf, byte_offset, length))
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue