mirror of
https://github.com/denoland/deno.git
synced 2025-02-12 16:59:32 -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:
parent
043021cbd3
commit
8fb1af1412
2 changed files with 30 additions and 43 deletions
|
@ -12,7 +12,6 @@ use crate::ZeroCopyBuf;
|
||||||
use rusty_v8 as v8;
|
use rusty_v8 as v8;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde_v8::to_v8;
|
use serde_v8::to_v8;
|
||||||
use std::cell::Cell;
|
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
use std::io::{stdout, Write};
|
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(
|
fn print(
|
||||||
scope: &mut v8::HandleScope,
|
scope: &mut v8::HandleScope,
|
||||||
args: v8::FunctionCallbackArguments,
|
args: v8::FunctionCallbackArguments,
|
||||||
|
@ -599,14 +575,8 @@ fn decode(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let backing_store = view.buffer(scope).unwrap().get_backing_store();
|
let zero_copy = ZeroCopyBuf::new(scope, view);
|
||||||
let buf = unsafe {
|
let buf = &zero_copy;
|
||||||
get_backing_store_slice(
|
|
||||||
&backing_store,
|
|
||||||
view.byte_offset(),
|
|
||||||
view.byte_length(),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
// Strip BOM
|
// Strip BOM
|
||||||
let buf =
|
let buf =
|
||||||
|
@ -694,14 +664,8 @@ fn deserialize(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let backing_store = view.buffer(scope).unwrap().get_backing_store();
|
let zero_copy = ZeroCopyBuf::new(scope, view);
|
||||||
let buf = unsafe {
|
let buf = &zero_copy;
|
||||||
get_backing_store_slice(
|
|
||||||
&backing_store,
|
|
||||||
view.byte_offset(),
|
|
||||||
view.byte_length(),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
let serialize_deserialize = Box::new(SerializeDeserialize {});
|
let serialize_deserialize = Box::new(SerializeDeserialize {});
|
||||||
let mut value_deserializer =
|
let mut value_deserializer =
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
use crate::bindings;
|
|
||||||
use rusty_v8 as v8;
|
use rusty_v8 as v8;
|
||||||
|
use std::cell::Cell;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::ops::DerefMut;
|
use std::ops::DerefMut;
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ impl Deref for ZeroCopyBuf {
|
||||||
type Target = [u8];
|
type Target = [u8];
|
||||||
fn deref(&self) -> &[u8] {
|
fn deref(&self) -> &[u8] {
|
||||||
unsafe {
|
unsafe {
|
||||||
bindings::get_backing_store_slice(
|
get_backing_store_slice(
|
||||||
&self.backing_store,
|
&self.backing_store,
|
||||||
self.byte_offset,
|
self.byte_offset,
|
||||||
self.byte_length,
|
self.byte_length,
|
||||||
|
@ -57,7 +57,7 @@ impl Deref for ZeroCopyBuf {
|
||||||
impl DerefMut for ZeroCopyBuf {
|
impl DerefMut for ZeroCopyBuf {
|
||||||
fn deref_mut(&mut self) -> &mut [u8] {
|
fn deref_mut(&mut self) -> &mut [u8] {
|
||||||
unsafe {
|
unsafe {
|
||||||
bindings::get_backing_store_slice_mut(
|
get_backing_store_slice_mut(
|
||||||
&self.backing_store,
|
&self.backing_store,
|
||||||
self.byte_offset,
|
self.byte_offset,
|
||||||
self.byte_length,
|
self.byte_length,
|
||||||
|
@ -77,3 +77,26 @@ impl AsMut<[u8]> for ZeroCopyBuf {
|
||||||
&mut *self
|
&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
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue