From 8fb1af14129850ce5a124572b6b8131056727594 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Sun, 18 Apr 2021 14:39:26 +0200 Subject: [PATCH] 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. --- core/bindings.rs | 44 ++++--------------------------------------- core/zero_copy_buf.rs | 29 +++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 43 deletions(-) diff --git a/core/bindings.rs b/core/bindings.rs index 0a67be1022..48450a6194 100644 --- a/core/bindings.rs +++ b/core/bindings.rs @@ -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, - byte_offset: usize, - byte_length: usize, -) -> &[u8] { - let cells: *const [Cell] = - &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, - byte_offset: usize, - byte_length: usize, -) -> &mut [u8] { - let cells: *const [Cell] = - &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 = diff --git a/core/zero_copy_buf.rs b/core/zero_copy_buf.rs index 75a3caf224..1f07292ba6 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, + byte_offset: usize, + byte_length: usize, +) -> &[u8] { + let cells: *const [Cell] = + &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, + byte_offset: usize, + byte_length: usize, +) -> &mut [u8] { + let cells: *const [Cell] = + &backing_store[byte_offset..byte_offset + byte_length]; + let bytes = cells as *const _ as *mut [u8]; + &mut *bytes +}