1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-08 15:19:40 -05:00

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.
This commit is contained in:
Aaron O'Mullan 2021-04-18 14:39:26 +02:00 committed by GitHub
parent 043021cbd3
commit 8fb1af1412
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 43 deletions

View file

@ -12,7 +12,6 @@ use crate::ZeroCopyBuf;
use rusty_v8 as v8;
use serde::Serialize;
use serde_v8::to_v8;
use std::cell::Cell;
use std::convert::TryFrom;
use std::convert::TryInto;
use std::io::{stdout, Write};
@ -287,29 +286,6 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) {
};
}
pub(crate) 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)]
pub(crate) 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
}
fn print(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
@ -599,14 +575,8 @@ fn decode(
}
};
let backing_store = view.buffer(scope).unwrap().get_backing_store();
let buf = unsafe {
get_backing_store_slice(
&backing_store,
view.byte_offset(),
view.byte_length(),
)
};
let zero_copy = ZeroCopyBuf::new(scope, view);
let buf = &zero_copy;
// Strip BOM
let buf =
@ -694,14 +664,8 @@ fn deserialize(
}
};
let backing_store = view.buffer(scope).unwrap().get_backing_store();
let buf = unsafe {
get_backing_store_slice(
&backing_store,
view.byte_offset(),
view.byte_length(),
)
};
let zero_copy = ZeroCopyBuf::new(scope, view);
let buf = &zero_copy;
let serialize_deserialize = Box::new(SerializeDeserialize {});
let mut value_deserializer =

View file

@ -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
}