Skip to content

Commit

Permalink
feat: add DataView constructor (#1491)
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Jun 3, 2024
1 parent 756f47d commit 2a8b117
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/array_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>;
Expand Down Expand Up @@ -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()
}
}
7 changes: 7 additions & 0 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
16 changes: 16 additions & 0 deletions tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 2a8b117

Please sign in to comment.