Skip to content

Commit

Permalink
Upgrade to rusty_v8 v0.1.1 (denoland#3741)
Browse files Browse the repository at this point in the history
  • Loading branch information
piscisaureus committed Jan 21, 2020
1 parent fa7f34e commit 8c3cd63
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 152 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ libc = "0.2.66"
log = "0.4.8"
serde_json = "1.0.44"
url = "2.1.0"
rusty_v8 = "=0.1.0"
rusty_v8 = "0.1.1"

[[example]]
name = "deno_core_http_bench"
Expand Down
62 changes: 26 additions & 36 deletions core/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ use crate::isolate::PinnedBuf;
use crate::isolate::SHARED_RESPONSE_BUF_SIZE;

use rusty_v8 as v8;
use v8::InIsolate;
use v8::MapFnTo;

use libc::c_void;
use std::convert::TryFrom;
use std::option::Option;
use std::ptr;
Expand Down Expand Up @@ -95,24 +93,26 @@ pub fn module_origin<'a>(
)
}

pub fn initialize_context<'a>(
scope: &mut impl v8::ToLocal<'a>,
mut context: v8::Local<v8::Context>,
) {
context.enter();
pub fn initialize_context<'s>(
scope: &mut impl v8::ToLocal<'s>,
) -> v8::Local<'s, v8::Context> {
let mut hs = v8::EscapableHandleScope::new(scope);
let scope = hs.enter();

let context = v8::Context::new(scope);
let global = context.global(scope);

let deno_val = v8::Object::new(scope);
let mut cs = v8::ContextScope::new(scope, context);
let scope = cs.enter();

let deno_val = v8::Object::new(scope);
global.set(
context,
v8::String::new(scope, "Deno").unwrap().into(),
deno_val.into(),
);

let mut core_val = v8::Object::new(scope);

deno_val.set(
context,
v8::String::new(scope, "core").unwrap().into(),
Expand Down Expand Up @@ -178,7 +178,7 @@ pub fn initialize_context<'a>(
queue_microtask_val.into(),
);

context.exit();
scope.escape(context)
}

pub unsafe fn slice_to_uint8array<'sc>(
Expand Down Expand Up @@ -221,7 +221,7 @@ pub extern "C" fn host_import_module_dynamically_callback(
referrer: v8::Local<v8::ScriptOrModule>,
specifier: v8::Local<v8::String>,
) -> *mut v8::Promise {
let mut cbs = v8::CallbackScope::new(context);
let mut cbs = v8::CallbackScope::new_escapable(context);
let mut hs = v8::EscapableHandleScope::new(cbs.enter());
let scope = hs.enter();
let isolate = scope.isolate();
Expand Down Expand Up @@ -307,7 +307,7 @@ pub extern "C" fn message_callback(

// TerminateExecution was called
if scope.isolate().is_execution_terminating() {
let u = v8::new_undefined(scope);
let u = v8::undefined(scope);
deno_isolate.handle_exception(scope, context, u.into());
return;
}
Expand All @@ -324,8 +324,9 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) {
let deno_isolate: &mut Isolate =
unsafe { &mut *(scope.isolate().get_data(0) as *mut Isolate) };

let mut context = deno_isolate.global_context.get(scope).unwrap();
context.enter();
let context = deno_isolate.global_context.get(scope).unwrap();
let mut cs = v8::ContextScope::new(scope, context);
let scope = cs.enter();

let promise = message.get_promise();
let promise_id = promise.get_identity_hash();
Expand All @@ -351,8 +352,6 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) {
// Should not warn. See #1272
}
};

context.exit();
}

fn print(
Expand Down Expand Up @@ -465,7 +464,7 @@ fn eval_context(
Ok(s) => s,
Err(_) => {
let msg = v8::String::new(scope, "Invalid argument").unwrap();
let exception = v8::type_error(scope, msg);
let exception = v8::Exception::type_error(scope, msg);
scope.isolate().throw_exception(exception);
return;
}
Expand Down Expand Up @@ -494,7 +493,7 @@ fn eval_context(
output.set(
context,
v8::Integer::new(scope, 0).into(),
v8::new_null(scope).into(),
v8::null(scope).into(),
);

let errinfo_obj = v8::Object::new(scope);
Expand Down Expand Up @@ -535,7 +534,7 @@ fn eval_context(
output.set(
context,
v8::Integer::new(scope, 0).into(),
v8::new_null(scope).into(),
v8::null(scope).into(),
);

let errinfo_obj = v8::Object::new(scope);
Expand Down Expand Up @@ -577,7 +576,7 @@ fn eval_context(
output.set(
context,
v8::Integer::new(scope, 1).into(),
v8::new_null(scope).into(),
v8::null(scope).into(),
);
rv.set(output.into());
}
Expand All @@ -591,11 +590,7 @@ fn error_to_json(
unsafe { &mut *(scope.isolate().get_data(0) as *mut Isolate) };
let context = deno_isolate.global_context.get(scope).unwrap();

// TODO(piscisaureus): This is transmute necessary because of a bug in
// rusty_v8's implementation of `v8::create_message()`, which needs to be
// fixed.
let exception = unsafe { std::mem::transmute(args.get(0)) };
let message = v8::create_message(scope, exception);
let message = v8::Exception::create_message(scope, args.get(0));
let json_obj = encode_message_as_object(scope, context, message);
let json_string = v8::json::stringify(context, json_obj.into()).unwrap();

Expand All @@ -611,7 +606,7 @@ fn queue_microtask(
Ok(f) => scope.isolate().enqueue_microtask(f),
Err(_) => {
let msg = v8::String::new(scope, "Invalid argument").unwrap();
let exception = v8::type_error(scope, msg);
let exception = v8::Exception::type_error(scope, msg);
scope.isolate().throw_exception(exception);
}
};
Expand All @@ -628,15 +623,10 @@ fn shared_getter(

// Lazily initialize the persistent external ArrayBuffer.
if deno_isolate.shared_ab.is_empty() {
let data_ptr = deno_isolate.shared.bytes.as_ptr();
let data_len = deno_isolate.shared.bytes.len();
let ab = unsafe {
v8::SharedArrayBuffer::new_DEPRECATED(
scope,
data_ptr as *mut c_void,
data_len,
)
};
let ab = v8::SharedArrayBuffer::new_with_backing_store(
scope,
deno_isolate.shared.get_backing_store(),
);
deno_isolate.shared_ab.set(scope, ab);
}

Expand All @@ -649,7 +639,7 @@ pub fn module_resolve_callback<'s>(
specifier: v8::Local<'s, v8::String>,
referrer: v8::Local<'s, v8::Module>,
) -> Option<v8::Local<'s, v8::Module>> {
let mut scope = v8::CallbackScope::new(context);
let mut scope = v8::CallbackScope::new_escapable(context);
let mut scope = v8::EscapableHandleScope::new(scope.enter());
let scope = scope.enter();

Expand Down
57 changes: 27 additions & 30 deletions core/es_isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Drop for EsIsolate {
// Clear persistent handles we own.
{
let mut locker = v8::Locker::new(&isolate);
let mut hs = v8::HandleScope::new(&mut locker);
let mut hs = v8::HandleScope::new(locker.enter());
let scope = hs.enter();
for module in self.modules.info.values_mut() {
module.handle.reset(scope);
Expand Down Expand Up @@ -142,11 +142,12 @@ impl EsIsolate {
let isolate = self.core_isolate.v8_isolate.as_ref().unwrap();
let mut locker = v8::Locker::new(&isolate);

let mut hs = v8::HandleScope::new(&mut locker);
let mut hs = v8::HandleScope::new(locker.enter());
let scope = hs.enter();
assert!(!self.core_isolate.global_context.is_empty());
let mut context = self.core_isolate.global_context.get(scope).unwrap();
context.enter();
let context = self.core_isolate.global_context.get(scope).unwrap();
let mut cs = v8::ContextScope::new(scope, context);
let scope = cs.enter();

let name_str = v8::String::new(scope, name).unwrap();
let source_str = v8::String::new(scope, source).unwrap();
Expand All @@ -166,7 +167,6 @@ impl EsIsolate {
context,
tc.exception().unwrap(),
);
context.exit();
return 0;
}
let module = maybe_module.unwrap();
Expand All @@ -183,7 +183,6 @@ impl EsIsolate {
self
.modules
.register(id, name, main, handle, import_specifiers);
context.exit();
id
}

Expand All @@ -203,26 +202,24 @@ impl EsIsolate {
fn mod_instantiate2(&mut self, id: ModuleId) {
let isolate = self.core_isolate.v8_isolate.as_ref().unwrap();
let mut locker = v8::Locker::new(isolate);
let mut hs = v8::HandleScope::new(&mut locker);
let mut hs = v8::HandleScope::new(locker.enter());
let scope = hs.enter();
assert!(!self.core_isolate.global_context.is_empty());
let mut context = self.core_isolate.global_context.get(scope).unwrap();
context.enter();
let mut try_catch = v8::TryCatch::new(scope);
let context = self.core_isolate.global_context.get(scope).unwrap();
let mut cs = v8::ContextScope::new(scope, context);
let mut try_catch = v8::TryCatch::new(cs.enter());
let tc = try_catch.enter();

let maybe_info = self.modules.get_info(id);

if maybe_info.is_none() {
context.exit();
return;
}

let module_handle = &maybe_info.unwrap().handle;
let mut module = module_handle.get(scope).unwrap();

if module.get_status() == v8::ModuleStatus::Errored {
context.exit();
return;
}

Expand All @@ -237,8 +234,6 @@ impl EsIsolate {
tc.exception().unwrap(),
);
}

context.exit();
}

/// Instanciates a ES module
Expand All @@ -254,11 +249,12 @@ impl EsIsolate {
fn mod_evaluate2(&mut self, id: ModuleId) {
let isolate = self.core_isolate.v8_isolate.as_ref().unwrap();
let mut locker = v8::Locker::new(isolate);
let mut hs = v8::HandleScope::new(&mut locker);
let mut hs = v8::HandleScope::new(locker.enter());
let scope = hs.enter();
assert!(!self.core_isolate.global_context.is_empty());
let mut context = self.core_isolate.global_context.get(scope).unwrap();
context.enter();
let context = self.core_isolate.global_context.get(scope).unwrap();
let mut cs = v8::ContextScope::new(scope, context);
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");
Expand Down Expand Up @@ -292,8 +288,6 @@ impl EsIsolate {
}
other => panic!("Unexpected module status {:?}", other),
};

context.exit();
}

/// Evaluates an already instantiated ES module.
Expand Down Expand Up @@ -353,11 +347,13 @@ impl EsIsolate {
);
let isolate = self.core_isolate.v8_isolate.as_ref().unwrap();
let mut locker = v8::Locker::new(isolate);
let mut hs = v8::HandleScope::new(&mut locker);
let mut hs = v8::HandleScope::new(locker.enter());
let scope = hs.enter();
assert!(!self.core_isolate.global_context.is_empty());
let mut context = self.core_isolate.global_context.get(scope).unwrap();
context.enter();
let context = self.core_isolate.global_context.get(scope).unwrap();
let mut cs = v8::ContextScope::new(scope, context);
let scope = cs.enter();

let mut resolver_handle = self
.dyn_import_map
.remove(&id)
Expand All @@ -367,16 +363,16 @@ impl EsIsolate {
// Resolution error.
if let Some(error_str) = error {
let msg = v8::String::new(scope, &error_str).unwrap();
let e = v8::type_error(scope, msg);
let e = v8::Exception::type_error(scope, msg);
resolver.reject(context, e).unwrap();
} else {
let e = self.core_isolate.last_exception_handle.get(scope).unwrap();
self.core_isolate.last_exception_handle.reset(scope);
self.core_isolate.last_exception.take();
resolver.reject(context, e).unwrap();
}
isolate.run_microtasks();
context.exit();

scope.isolate().run_microtasks();
self.core_isolate.check_last_exception()
}

Expand All @@ -389,11 +385,13 @@ impl EsIsolate {
assert!(mod_id != 0);
let isolate = self.core_isolate.v8_isolate.as_ref().unwrap();
let mut locker = v8::Locker::new(isolate);
let mut hs = v8::HandleScope::new(&mut locker);
let mut hs = v8::HandleScope::new(locker.enter());
let scope = hs.enter();
assert!(!self.core_isolate.global_context.is_empty());
let mut context = self.core_isolate.global_context.get(scope).unwrap();
context.enter();
let context = self.core_isolate.global_context.get(scope).unwrap();
let mut cs = v8::ContextScope::new(scope, context);
let scope = cs.enter();

let mut resolver_handle = self
.dyn_import_map
.remove(&id)
Expand All @@ -409,8 +407,7 @@ impl EsIsolate {
assert_eq!(module.get_status(), v8::ModuleStatus::Evaluated);
let module_namespace = module.get_module_namespace();
resolver.resolve(context, module_namespace).unwrap();
isolate.run_microtasks();
context.exit();
scope.isolate().run_microtasks();
self.core_isolate.check_last_exception()
}

Expand Down
Loading

0 comments on commit 8c3cd63

Please sign in to comment.