0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-23 15:39:55 -05:00
denoland-rusty-v8/src/array_buffer_view.rs
Bert Belder d1ac68f0c8
Always use raw pointers to send V8 handles between C++ and Rust (#349)
And other pointer usage touch-ups on the C++ side:
- const parameters are passed by & reference.
- mutable parameters are passed by * pointer.
2020-04-14 00:34:32 +02:00

51 lines
1.4 KiB
Rust

use std::convert::TryInto;
use std::ffi::c_void;
use crate::support::int;
use crate::ArrayBuffer;
use crate::ArrayBufferView;
use crate::Local;
extern "C" {
fn v8__ArrayBufferView__Buffer(
this: *const ArrayBufferView,
) -> *const ArrayBuffer;
fn v8__ArrayBufferView__ByteLength(this: *const ArrayBufferView) -> usize;
fn v8__ArrayBufferView__ByteOffset(this: *const ArrayBufferView) -> usize;
fn v8__ArrayBufferView__CopyContents(
this: *const ArrayBufferView,
dest: *mut c_void,
byte_length: int,
) -> usize;
}
impl ArrayBufferView {
/// Returns underlying ArrayBuffer.
pub fn buffer<'sc>(&self) -> Option<Local<'sc, ArrayBuffer>> {
unsafe { Local::from_raw(v8__ArrayBufferView__Buffer(self)) }
}
/// Size of a view in bytes.
pub fn byte_length(&self) -> usize {
unsafe { v8__ArrayBufferView__ByteLength(self) }
}
/// Byte offset in |Buffer|.
pub fn byte_offset(&self) -> usize {
unsafe { v8__ArrayBufferView__ByteOffset(self) }
}
/// Copy the contents of the ArrayBufferView's buffer to an embedder defined
/// memory without additional overhead that calling ArrayBufferView::Buffer
/// might incur.
/// Returns the number of bytes actually written.
pub fn copy_contents(&self, dest: &mut [u8]) -> usize {
unsafe {
v8__ArrayBufferView__CopyContents(
self,
dest.as_mut_ptr() as *mut c_void,
dest.len().try_into().unwrap(),
)
}
}
}