0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-24 15:19:31 -05:00

feat: add DataView constructor (#1491)

This commit is contained in:
snek 2024-06-03 13:47:57 -07:00 committed by GitHub
parent 756f47dcb9
commit 2a8b117c93
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 47 additions and 0 deletions

View file

@ -16,6 +16,7 @@ use crate::support::SharedRef;
use crate::support::UniquePtr;
use crate::support::UniqueRef;
use crate::ArrayBuffer;
use crate::DataView;
use crate::HandleScope;
use crate::Isolate;
use crate::Local;
@ -68,6 +69,12 @@ extern "C" {
) -> bool;
fn v8__BackingStore__DELETE(this: *mut BackingStore);
fn v8__DataView__New(
arraybuffer: *const ArrayBuffer,
byte_offset: usize,
length: usize,
) -> *const DataView;
fn std__shared_ptr__v8__BackingStore__COPY(
ptr: *const SharedPtrBase<BackingStore>,
) -> SharedPtrBase<BackingStore>;
@ -616,3 +623,20 @@ impl ArrayBuffer {
))
}
}
impl DataView {
/// Returns a new DataView.
#[inline(always)]
pub fn new<'s>(
scope: &mut HandleScope<'s>,
arraybuffer: Local<'s, ArrayBuffer>,
byte_offset: usize,
length: usize,
) -> Local<'s, DataView> {
unsafe {
scope
.cast_local(|_| v8__DataView__New(&*arraybuffer, byte_offset, length))
}
.unwrap()
}
}

View file

@ -1802,6 +1802,13 @@ size_t v8__ArrayBuffer__ByteLength(const v8::ArrayBuffer& self) {
return self.ByteLength();
}
const v8::DataView* v8__DataView__New(
const v8::ArrayBuffer& ab,
size_t offset,
size_t length) {
return local_to_ptr(v8::DataView::New(ptr_to_local(&ab), offset, length));
}
struct InternalFieldData {
uint32_t data;
};

View file

@ -734,6 +734,22 @@ fn handles_from_isolate() {
let _ = v8::Boolean::new(isolate, true);
}
#[test]
fn data_view() {
let _setup_guard = setup::parallel_test();
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, 42);
let dv = v8::DataView::new(scope, ab, 0, 42);
assert!(dv.is_data_view());
}
}
#[test]
fn array_buffer() {
let _setup_guard = setup::parallel_test();