summaryrefslogtreecommitdiff
path: root/core/zero_copy_buf.rs
diff options
context:
space:
mode:
authorAaron O'Mullan <aaron.omullan@gmail.com>2021-04-18 14:39:26 +0200
committerGitHub <noreply@github.com>2021-04-18 14:39:26 +0200
commit8fb1af14129850ce5a124572b6b8131056727594 (patch)
treed5e008582f255b0f4190e4384a29a21d1104dfc9 /core/zero_copy_buf.rs
parent043021cbd3b9c26807978d956babcf9f607cfbc2 (diff)
refactor(core): remove ZeroCopyBuf's dep on the bindings mod (#10232)
Also cleanup `bindings::deserialize()/decode()` so they use the `ZeroCopyBuf` abstraction rather than reimplementing it. This cleanup will facilitate moving `ZeroCopyBuf` to `serde_v8` since it's now self contained and there are no other `get_backing_store_slice()` callers.
Diffstat (limited to 'core/zero_copy_buf.rs')
-rw-r--r--core/zero_copy_buf.rs29
1 files changed, 26 insertions, 3 deletions
diff --git a/core/zero_copy_buf.rs b/core/zero_copy_buf.rs
index 75a3caf22..1f07292ba 100644
--- a/core/zero_copy_buf.rs
+++ b/core/zero_copy_buf.rs
@@ -1,7 +1,7 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
-use crate::bindings;
use rusty_v8 as v8;
+use std::cell::Cell;
use std::ops::Deref;
use std::ops::DerefMut;
@@ -45,7 +45,7 @@ impl Deref for ZeroCopyBuf {
type Target = [u8];
fn deref(&self) -> &[u8] {
unsafe {
- bindings::get_backing_store_slice(
+ get_backing_store_slice(
&self.backing_store,
self.byte_offset,
self.byte_length,
@@ -57,7 +57,7 @@ impl Deref for ZeroCopyBuf {
impl DerefMut for ZeroCopyBuf {
fn deref_mut(&mut self) -> &mut [u8] {
unsafe {
- bindings::get_backing_store_slice_mut(
+ get_backing_store_slice_mut(
&self.backing_store,
self.byte_offset,
self.byte_length,
@@ -77,3 +77,26 @@ impl AsMut<[u8]> for ZeroCopyBuf {
&mut *self
}
}
+
+unsafe fn get_backing_store_slice(
+ backing_store: &v8::SharedRef<v8::BackingStore>,
+ byte_offset: usize,
+ byte_length: usize,
+) -> &[u8] {
+ let cells: *const [Cell<u8>] =
+ &backing_store[byte_offset..byte_offset + byte_length];
+ let bytes = cells as *const [u8];
+ &*bytes
+}
+
+#[allow(clippy::mut_from_ref)]
+unsafe fn get_backing_store_slice_mut(
+ backing_store: &v8::SharedRef<v8::BackingStore>,
+ byte_offset: usize,
+ byte_length: usize,
+) -> &mut [u8] {
+ let cells: *const [Cell<u8>] =
+ &backing_store[byte_offset..byte_offset + byte_length];
+ let bytes = cells as *const _ as *mut [u8];
+ &mut *bytes
+}