Skip to content

Commit

Permalink
Upgrade to rusty_v8 v0.4.0 (denoland#4856)
Browse files Browse the repository at this point in the history
  • Loading branch information
piscisaureus committed Apr 23, 2020
1 parent c43aaa3 commit 10a1748
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 117 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,10 @@ jobs:
target/*/.*
target/*/build
target/*/deps
target/*/gn_out
key:
${{ matrix.config.os }}-${{ matrix.config.kind }}-${{ hashFiles('Cargo.lock') }}
restore-keys: |
${{ matrix.config.os }}-${{ matrix.config.kind }}-${{ hashFiles('Cargo.lock') }}
${{ matrix.config.os }}-${{ matrix.config.kind }}-
${{ matrix.config.os }}-
- name: lint.py
if: matrix.config.kind == 'lint'
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions cli/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub static WINDOW_LIB: &str = include_str!("js/lib.deno.window.d.ts");
#[test]
fn cli_snapshot() {
let mut isolate = deno_core::Isolate::new(
deno_core::StartupData::Snapshot(CLI_SNAPSHOT),
deno_core::StartupData::Snapshot(deno_core::Snapshot::Static(CLI_SNAPSHOT)),
false,
);
deno_core::js_check(isolate.execute(
Expand All @@ -41,7 +41,9 @@ fn cli_snapshot() {
#[test]
fn compiler_snapshot() {
let mut isolate = deno_core::Isolate::new(
deno_core::StartupData::Snapshot(COMPILER_SNAPSHOT),
deno_core::StartupData::Snapshot(deno_core::Snapshot::Static(
COMPILER_SNAPSHOT,
)),
false,
);
deno_core::js_check(isolate.execute(
Expand Down
5 changes: 3 additions & 2 deletions cli/startup_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use deno_core::Script;

use crate::js::CLI_SNAPSHOT;
use crate::js::COMPILER_SNAPSHOT;
use deno_core::Snapshot;
use deno_core::StartupData;

#[cfg(feature = "no-snapshot-init")]
Expand All @@ -29,7 +30,7 @@ pub fn deno_isolate_init() -> StartupData<'static> {
#[cfg(feature = "check-only")]
let data = b"";

StartupData::Snapshot(data)
StartupData::Snapshot(Snapshot::Static(data))
}

#[cfg(feature = "no-snapshot-init")]
Expand All @@ -55,5 +56,5 @@ pub fn compiler_isolate_init() -> StartupData<'static> {
#[cfg(feature = "check-only")]
let data = b"";

StartupData::Snapshot(data)
StartupData::Snapshot(Snapshot::Static(data))
}
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ futures = { version = "0.3.4", features = ["thread-pool", "compat"] }
lazy_static = "1.4.0"
libc = "0.2.68"
log = "0.4.8"
rusty_v8 = "0.3.10"
rusty_v8 = "0.4.0"
serde_json = "1.0.51"
url = "2.1.1"

Expand Down
96 changes: 61 additions & 35 deletions core/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::js_errors::JSError;
use rusty_v8 as v8;
use v8::MapFnTo;

use std::cell::Cell;
use std::convert::TryFrom;
use std::option::Option;
use url::Url;
Expand Down Expand Up @@ -236,9 +237,8 @@ pub fn boxed_slice_to_uint8array<'sc>(
assert!(!buf.is_empty());
let buf_len = buf.len();
let backing_store = v8::ArrayBuffer::new_backing_store_from_boxed_slice(buf);
let mut backing_store_shared = backing_store.make_shared();
let ab =
v8::ArrayBuffer::with_backing_store(scope, &mut backing_store_shared);
let backing_store_shared = backing_store.make_shared();
let ab = v8::ArrayBuffer::with_backing_store(scope, &backing_store_shared);
v8::Uint8Array::new(ab, 0, buf_len).expect("Failed to create UintArray8")
}

Expand Down Expand Up @@ -271,7 +271,7 @@ pub extern "C" fn host_import_module_dynamically_callback(
let host_defined_options = referrer.get_host_defined_options();
assert_eq!(host_defined_options.length(), 0);

let mut resolver = v8::PromiseResolver::new(scope, context).unwrap();
let resolver = v8::PromiseResolver::new(scope, context).unwrap();
let promise = resolver.get_promise(scope);

let mut resolver_handle = v8::Global::new();
Expand Down Expand Up @@ -355,6 +355,29 @@ 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: v8::FunctionCallbackScope,
args: v8::FunctionCallbackArguments,
Expand Down Expand Up @@ -416,26 +439,27 @@ fn send(
unsafe { &mut *(scope.isolate().get_data(0) as *mut Isolate) };
assert!(!deno_isolate.global_context.is_empty());

let r = v8::Local::<v8::Uint32>::try_from(args.get(0));

if let Err(err) = r {
let s = format!("bad op id {}", err);
let msg = v8::String::new(scope, &s).unwrap();
scope.isolate().throw_exception(msg.into());
return;
}

let op_id = r.unwrap().value() as u32;
let op_id = match v8::Local::<v8::Uint32>::try_from(args.get(0)) {
Ok(op_id) => op_id.value() as u32,
Err(err) => {
let msg = format!("invalid op id: {}", err);
let msg = v8::String::new(scope, &msg).unwrap();
scope.isolate().throw_exception(msg.into());
return;
}
};

let control_backing_store: v8::SharedRef<v8::BackingStore>;
let control = match v8::Local::<v8::ArrayBufferView>::try_from(args.get(1)) {
Ok(view) => {
let byte_offset = view.byte_offset();
let byte_length = view.byte_length();
let backing_store = view.buffer().unwrap().get_backing_store();
let buf = unsafe { &**backing_store.get() };
&buf[byte_offset..byte_offset + byte_length]
}
Err(..) => &[],
Ok(view) => unsafe {
control_backing_store = view.buffer().unwrap().get_backing_store();
get_backing_store_slice(
&control_backing_store,
view.byte_offset(),
view.byte_length(),
)
},
Err(_) => &[],
};

let zero_copy: Option<ZeroCopyBuf> =
Expand Down Expand Up @@ -654,9 +678,8 @@ fn encode(
let buf_len = text_bytes.len();
let backing_store =
v8::ArrayBuffer::new_backing_store_from_boxed_slice(text_bytes);
let mut backing_store_shared = backing_store.make_shared();
let ab =
v8::ArrayBuffer::with_backing_store(scope, &mut backing_store_shared);
let backing_store_shared = backing_store.make_shared();
let ab = v8::ArrayBuffer::with_backing_store(scope, &backing_store_shared);
v8::Uint8Array::new(ab, 0, buf_len).expect("Failed to create UintArray8")
};

Expand All @@ -668,22 +691,25 @@ fn decode(
args: v8::FunctionCallbackArguments,
mut rv: v8::ReturnValue,
) {
let buf = match v8::Local::<v8::ArrayBufferView>::try_from(args.get(0)) {
Ok(view) => {
let byte_offset = view.byte_offset();
let byte_length = view.byte_length();
let backing_store = view.buffer().unwrap().get_backing_store();
let buf = unsafe { &**backing_store.get() };
&buf[byte_offset..byte_offset + byte_length]
}
Err(..) => {
let view = match v8::Local::<v8::ArrayBufferView>::try_from(args.get(0)) {
Ok(view) => view,
Err(_) => {
let msg = v8::String::new(scope, "Invalid argument").unwrap();
let exception = v8::Exception::type_error(scope, msg);
scope.isolate().throw_exception(exception);
return;
}
};

let backing_store = view.buffer().unwrap().get_backing_store();
let buf = unsafe {
get_backing_store_slice(
&backing_store,
view.byte_offset(),
view.byte_length(),
)
};

let text_str =
v8::String::new_from_utf8(scope, &buf, v8::NewStringType::Normal).unwrap();
rv.set(text_str.into())
Expand Down Expand Up @@ -791,7 +817,7 @@ fn get_promise_details(
assert!(!deno_isolate.global_context.is_empty());
let context = deno_isolate.global_context.get(scope).unwrap();

let mut promise = match v8::Local::<v8::Promise>::try_from(args.get(0)) {
let promise = match v8::Local::<v8::Promise>::try_from(args.get(0)) {
Ok(val) => val,
Err(_) => {
let msg = v8::String::new(scope, "Invalid argument").unwrap();
Expand Down
6 changes: 3 additions & 3 deletions core/es_isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ impl EsIsolate {
let scope = cs.enter();

let info = self.modules.get_info(id).expect("ModuleInfo not found");
let mut module = info.handle.get(scope).expect("Empty module handle");
let module = info.handle.get(scope).expect("Empty module handle");
let mut status = module.get_status();

if status == v8::ModuleStatus::Instantiated {
Expand Down Expand Up @@ -312,7 +312,7 @@ impl EsIsolate {
.dyn_import_map
.remove(&id)
.expect("Invalid dyn import id");
let mut resolver = resolver_handle.get(scope).unwrap();
let resolver = resolver_handle.get(scope).unwrap();
resolver_handle.reset(scope);

let exception = err
Expand Down Expand Up @@ -348,7 +348,7 @@ impl EsIsolate {
.dyn_import_map
.remove(&id)
.expect("Invalid dyn import id");
let mut resolver = resolver_handle.get(scope).unwrap();
let resolver = resolver_handle.get(scope).unwrap();
resolver_handle.reset(scope);
let info = self
.modules
Expand Down
Loading

0 comments on commit 10a1748

Please sign in to comment.