1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00

Upgrade to rusty_v8 v0.1.1 (#3741)

This commit is contained in:
Bert Belder 2020-01-21 20:24:31 +01:00
parent fa7f34eb8c
commit 8c3cd634a8
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
6 changed files with 149 additions and 152 deletions

6
Cargo.lock generated
View file

@ -303,7 +303,7 @@ dependencies = [
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rusty_v8 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "rusty_v8 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
"url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1155,7 +1155,7 @@ dependencies = [
[[package]] [[package]]
name = "rusty_v8" name = "rusty_v8"
version = "0.1.0" version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1906,7 +1906,7 @@ dependencies = [
"checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" "checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783"
"checksum rustls 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b25a18b1bf7387f0145e7f8324e700805aade3842dd3db2e74e4cdeb4677c09e" "checksum rustls 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b25a18b1bf7387f0145e7f8324e700805aade3842dd3db2e74e4cdeb4677c09e"
"checksum rustls-native-certs 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "51ffebdbb48c14f84eba0b715197d673aff1dd22cc1007ca647e28483bbcc307" "checksum rustls-native-certs 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "51ffebdbb48c14f84eba0b715197d673aff1dd22cc1007ca647e28483bbcc307"
"checksum rusty_v8 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e0acbe68bf184bc575f1f33294e74d37d5cd6d28e5857cb7b5501c17575decda" "checksum rusty_v8 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0d0a2e0cf49117db64379701eb50d12ab9ec3ca8292047f140c13c5174599441"
"checksum rustyline 5.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a23cb19702a8d6afb6edb3c842386e680d4883760e0df74e6848e23c2a87a635" "checksum rustyline 5.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a23cb19702a8d6afb6edb3c842386e680d4883760e0df74e6848e23c2a87a635"
"checksum ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8" "checksum ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8"
"checksum schannel 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "87f550b06b6cba9c8b8be3ee73f391990116bf527450d2556e9b9ce263b9a021" "checksum schannel 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "87f550b06b6cba9c8b8be3ee73f391990116bf527450d2556e9b9ce263b9a021"

View file

@ -21,7 +21,7 @@ libc = "0.2.66"
log = "0.4.8" log = "0.4.8"
serde_json = "1.0.44" serde_json = "1.0.44"
url = "2.1.0" url = "2.1.0"
rusty_v8 = "=0.1.0" rusty_v8 = "0.1.1"
[[example]] [[example]]
name = "deno_core_http_bench" name = "deno_core_http_bench"

View file

@ -6,10 +6,8 @@ use crate::isolate::PinnedBuf;
use crate::isolate::SHARED_RESPONSE_BUF_SIZE; use crate::isolate::SHARED_RESPONSE_BUF_SIZE;
use rusty_v8 as v8; use rusty_v8 as v8;
use v8::InIsolate;
use v8::MapFnTo; use v8::MapFnTo;
use libc::c_void;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::option::Option; use std::option::Option;
use std::ptr; use std::ptr;
@ -95,16 +93,19 @@ pub fn module_origin<'a>(
) )
} }
pub fn initialize_context<'a>( pub fn initialize_context<'s>(
scope: &mut impl v8::ToLocal<'a>, scope: &mut impl v8::ToLocal<'s>,
mut context: v8::Local<v8::Context>, ) -> v8::Local<'s, v8::Context> {
) { let mut hs = v8::EscapableHandleScope::new(scope);
context.enter(); let scope = hs.enter();
let context = v8::Context::new(scope);
let global = context.global(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( global.set(
context, context,
v8::String::new(scope, "Deno").unwrap().into(), v8::String::new(scope, "Deno").unwrap().into(),
@ -112,7 +113,6 @@ pub fn initialize_context<'a>(
); );
let mut core_val = v8::Object::new(scope); let mut core_val = v8::Object::new(scope);
deno_val.set( deno_val.set(
context, context,
v8::String::new(scope, "core").unwrap().into(), v8::String::new(scope, "core").unwrap().into(),
@ -178,7 +178,7 @@ pub fn initialize_context<'a>(
queue_microtask_val.into(), queue_microtask_val.into(),
); );
context.exit(); scope.escape(context)
} }
pub unsafe fn slice_to_uint8array<'sc>( pub unsafe fn slice_to_uint8array<'sc>(
@ -221,7 +221,7 @@ pub extern "C" fn host_import_module_dynamically_callback(
referrer: v8::Local<v8::ScriptOrModule>, referrer: v8::Local<v8::ScriptOrModule>,
specifier: v8::Local<v8::String>, specifier: v8::Local<v8::String>,
) -> *mut v8::Promise { ) -> *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 mut hs = v8::EscapableHandleScope::new(cbs.enter());
let scope = hs.enter(); let scope = hs.enter();
let isolate = scope.isolate(); let isolate = scope.isolate();
@ -307,7 +307,7 @@ pub extern "C" fn message_callback(
// TerminateExecution was called // TerminateExecution was called
if scope.isolate().is_execution_terminating() { 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()); deno_isolate.handle_exception(scope, context, u.into());
return; return;
} }
@ -324,8 +324,9 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) {
let deno_isolate: &mut Isolate = let deno_isolate: &mut Isolate =
unsafe { &mut *(scope.isolate().get_data(0) as *mut Isolate) }; unsafe { &mut *(scope.isolate().get_data(0) as *mut Isolate) };
let mut context = deno_isolate.global_context.get(scope).unwrap(); let context = deno_isolate.global_context.get(scope).unwrap();
context.enter(); let mut cs = v8::ContextScope::new(scope, context);
let scope = cs.enter();
let promise = message.get_promise(); let promise = message.get_promise();
let promise_id = promise.get_identity_hash(); let promise_id = promise.get_identity_hash();
@ -351,8 +352,6 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) {
// Should not warn. See #1272 // Should not warn. See #1272
} }
}; };
context.exit();
} }
fn print( fn print(
@ -465,7 +464,7 @@ fn eval_context(
Ok(s) => s, Ok(s) => s,
Err(_) => { Err(_) => {
let msg = v8::String::new(scope, "Invalid argument").unwrap(); 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); scope.isolate().throw_exception(exception);
return; return;
} }
@ -494,7 +493,7 @@ fn eval_context(
output.set( output.set(
context, context,
v8::Integer::new(scope, 0).into(), v8::Integer::new(scope, 0).into(),
v8::new_null(scope).into(), v8::null(scope).into(),
); );
let errinfo_obj = v8::Object::new(scope); let errinfo_obj = v8::Object::new(scope);
@ -535,7 +534,7 @@ fn eval_context(
output.set( output.set(
context, context,
v8::Integer::new(scope, 0).into(), v8::Integer::new(scope, 0).into(),
v8::new_null(scope).into(), v8::null(scope).into(),
); );
let errinfo_obj = v8::Object::new(scope); let errinfo_obj = v8::Object::new(scope);
@ -577,7 +576,7 @@ fn eval_context(
output.set( output.set(
context, context,
v8::Integer::new(scope, 1).into(), v8::Integer::new(scope, 1).into(),
v8::new_null(scope).into(), v8::null(scope).into(),
); );
rv.set(output.into()); rv.set(output.into());
} }
@ -591,11 +590,7 @@ fn error_to_json(
unsafe { &mut *(scope.isolate().get_data(0) as *mut Isolate) }; unsafe { &mut *(scope.isolate().get_data(0) as *mut Isolate) };
let context = deno_isolate.global_context.get(scope).unwrap(); let context = deno_isolate.global_context.get(scope).unwrap();
// TODO(piscisaureus): This is transmute necessary because of a bug in let message = v8::Exception::create_message(scope, args.get(0));
// 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 json_obj = encode_message_as_object(scope, context, message); let json_obj = encode_message_as_object(scope, context, message);
let json_string = v8::json::stringify(context, json_obj.into()).unwrap(); let json_string = v8::json::stringify(context, json_obj.into()).unwrap();
@ -611,7 +606,7 @@ fn queue_microtask(
Ok(f) => scope.isolate().enqueue_microtask(f), Ok(f) => scope.isolate().enqueue_microtask(f),
Err(_) => { Err(_) => {
let msg = v8::String::new(scope, "Invalid argument").unwrap(); 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); scope.isolate().throw_exception(exception);
} }
}; };
@ -628,15 +623,10 @@ fn shared_getter(
// Lazily initialize the persistent external ArrayBuffer. // Lazily initialize the persistent external ArrayBuffer.
if deno_isolate.shared_ab.is_empty() { if deno_isolate.shared_ab.is_empty() {
let data_ptr = deno_isolate.shared.bytes.as_ptr(); let ab = v8::SharedArrayBuffer::new_with_backing_store(
let data_len = deno_isolate.shared.bytes.len(); scope,
let ab = unsafe { deno_isolate.shared.get_backing_store(),
v8::SharedArrayBuffer::new_DEPRECATED( );
scope,
data_ptr as *mut c_void,
data_len,
)
};
deno_isolate.shared_ab.set(scope, ab); deno_isolate.shared_ab.set(scope, ab);
} }
@ -649,7 +639,7 @@ pub fn module_resolve_callback<'s>(
specifier: v8::Local<'s, v8::String>, specifier: v8::Local<'s, v8::String>,
referrer: v8::Local<'s, v8::Module>, referrer: v8::Local<'s, v8::Module>,
) -> Option<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 mut scope = v8::EscapableHandleScope::new(scope.enter());
let scope = scope.enter(); let scope = scope.enter();

View file

@ -87,7 +87,7 @@ impl Drop for EsIsolate {
// Clear persistent handles we own. // Clear persistent handles we own.
{ {
let mut locker = v8::Locker::new(&isolate); 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(); let scope = hs.enter();
for module in self.modules.info.values_mut() { for module in self.modules.info.values_mut() {
module.handle.reset(scope); module.handle.reset(scope);
@ -142,11 +142,12 @@ impl EsIsolate {
let isolate = self.core_isolate.v8_isolate.as_ref().unwrap(); let isolate = self.core_isolate.v8_isolate.as_ref().unwrap();
let mut locker = v8::Locker::new(&isolate); 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(); let scope = hs.enter();
assert!(!self.core_isolate.global_context.is_empty()); assert!(!self.core_isolate.global_context.is_empty());
let mut context = self.core_isolate.global_context.get(scope).unwrap(); let context = self.core_isolate.global_context.get(scope).unwrap();
context.enter(); let mut cs = v8::ContextScope::new(scope, context);
let scope = cs.enter();
let name_str = v8::String::new(scope, name).unwrap(); let name_str = v8::String::new(scope, name).unwrap();
let source_str = v8::String::new(scope, source).unwrap(); let source_str = v8::String::new(scope, source).unwrap();
@ -166,7 +167,6 @@ impl EsIsolate {
context, context,
tc.exception().unwrap(), tc.exception().unwrap(),
); );
context.exit();
return 0; return 0;
} }
let module = maybe_module.unwrap(); let module = maybe_module.unwrap();
@ -183,7 +183,6 @@ impl EsIsolate {
self self
.modules .modules
.register(id, name, main, handle, import_specifiers); .register(id, name, main, handle, import_specifiers);
context.exit();
id id
} }
@ -203,18 +202,17 @@ impl EsIsolate {
fn mod_instantiate2(&mut self, id: ModuleId) { fn mod_instantiate2(&mut self, id: ModuleId) {
let isolate = self.core_isolate.v8_isolate.as_ref().unwrap(); let isolate = self.core_isolate.v8_isolate.as_ref().unwrap();
let mut locker = v8::Locker::new(isolate); 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(); let scope = hs.enter();
assert!(!self.core_isolate.global_context.is_empty()); assert!(!self.core_isolate.global_context.is_empty());
let mut context = self.core_isolate.global_context.get(scope).unwrap(); let context = self.core_isolate.global_context.get(scope).unwrap();
context.enter(); let mut cs = v8::ContextScope::new(scope, context);
let mut try_catch = v8::TryCatch::new(scope); let mut try_catch = v8::TryCatch::new(cs.enter());
let tc = try_catch.enter(); let tc = try_catch.enter();
let maybe_info = self.modules.get_info(id); let maybe_info = self.modules.get_info(id);
if maybe_info.is_none() { if maybe_info.is_none() {
context.exit();
return; return;
} }
@ -222,7 +220,6 @@ impl EsIsolate {
let mut module = module_handle.get(scope).unwrap(); let mut module = module_handle.get(scope).unwrap();
if module.get_status() == v8::ModuleStatus::Errored { if module.get_status() == v8::ModuleStatus::Errored {
context.exit();
return; return;
} }
@ -237,8 +234,6 @@ impl EsIsolate {
tc.exception().unwrap(), tc.exception().unwrap(),
); );
} }
context.exit();
} }
/// Instanciates a ES module /// Instanciates a ES module
@ -254,11 +249,12 @@ impl EsIsolate {
fn mod_evaluate2(&mut self, id: ModuleId) { fn mod_evaluate2(&mut self, id: ModuleId) {
let isolate = self.core_isolate.v8_isolate.as_ref().unwrap(); let isolate = self.core_isolate.v8_isolate.as_ref().unwrap();
let mut locker = v8::Locker::new(isolate); 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(); let scope = hs.enter();
assert!(!self.core_isolate.global_context.is_empty()); assert!(!self.core_isolate.global_context.is_empty());
let mut context = self.core_isolate.global_context.get(scope).unwrap(); let context = self.core_isolate.global_context.get(scope).unwrap();
context.enter(); let mut cs = v8::ContextScope::new(scope, context);
let scope = cs.enter();
let info = self.modules.get_info(id).expect("ModuleInfo not found"); let info = self.modules.get_info(id).expect("ModuleInfo not found");
let mut module = info.handle.get(scope).expect("Empty module handle"); let mut module = info.handle.get(scope).expect("Empty module handle");
@ -292,8 +288,6 @@ impl EsIsolate {
} }
other => panic!("Unexpected module status {:?}", other), other => panic!("Unexpected module status {:?}", other),
}; };
context.exit();
} }
/// Evaluates an already instantiated ES module. /// Evaluates an already instantiated ES module.
@ -353,11 +347,13 @@ impl EsIsolate {
); );
let isolate = self.core_isolate.v8_isolate.as_ref().unwrap(); let isolate = self.core_isolate.v8_isolate.as_ref().unwrap();
let mut locker = v8::Locker::new(isolate); 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(); let scope = hs.enter();
assert!(!self.core_isolate.global_context.is_empty()); assert!(!self.core_isolate.global_context.is_empty());
let mut context = self.core_isolate.global_context.get(scope).unwrap(); let context = self.core_isolate.global_context.get(scope).unwrap();
context.enter(); let mut cs = v8::ContextScope::new(scope, context);
let scope = cs.enter();
let mut resolver_handle = self let mut resolver_handle = self
.dyn_import_map .dyn_import_map
.remove(&id) .remove(&id)
@ -367,7 +363,7 @@ impl EsIsolate {
// Resolution error. // Resolution error.
if let Some(error_str) = error { if let Some(error_str) = error {
let msg = v8::String::new(scope, &error_str).unwrap(); 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(); resolver.reject(context, e).unwrap();
} else { } else {
let e = self.core_isolate.last_exception_handle.get(scope).unwrap(); let e = self.core_isolate.last_exception_handle.get(scope).unwrap();
@ -375,8 +371,8 @@ impl EsIsolate {
self.core_isolate.last_exception.take(); self.core_isolate.last_exception.take();
resolver.reject(context, e).unwrap(); resolver.reject(context, e).unwrap();
} }
isolate.run_microtasks();
context.exit(); scope.isolate().run_microtasks();
self.core_isolate.check_last_exception() self.core_isolate.check_last_exception()
} }
@ -389,11 +385,13 @@ impl EsIsolate {
assert!(mod_id != 0); assert!(mod_id != 0);
let isolate = self.core_isolate.v8_isolate.as_ref().unwrap(); let isolate = self.core_isolate.v8_isolate.as_ref().unwrap();
let mut locker = v8::Locker::new(isolate); 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(); let scope = hs.enter();
assert!(!self.core_isolate.global_context.is_empty()); assert!(!self.core_isolate.global_context.is_empty());
let mut context = self.core_isolate.global_context.get(scope).unwrap(); let context = self.core_isolate.global_context.get(scope).unwrap();
context.enter(); let mut cs = v8::ContextScope::new(scope, context);
let scope = cs.enter();
let mut resolver_handle = self let mut resolver_handle = self
.dyn_import_map .dyn_import_map
.remove(&id) .remove(&id)
@ -409,8 +407,7 @@ impl EsIsolate {
assert_eq!(module.get_status(), v8::ModuleStatus::Evaluated); assert_eq!(module.get_status(), v8::ModuleStatus::Evaluated);
let module_namespace = module.get_module_namespace(); let module_namespace = module.get_module_namespace();
resolver.resolve(context, module_namespace).unwrap(); resolver.resolve(context, module_namespace).unwrap();
isolate.run_microtasks(); scope.isolate().run_microtasks();
context.exit();
self.core_isolate.check_last_exception() self.core_isolate.check_last_exception()
} }

View file

@ -200,7 +200,7 @@ impl Drop for Isolate {
// Clear persistent handles we own. // Clear persistent handles we own.
{ {
let mut locker = v8::Locker::new(&isolate); 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(); let scope = hs.enter();
// </Boilerplate> // </Boilerplate>
self.global_context.reset(scope); self.global_context.reset(scope);
@ -232,7 +232,7 @@ static DENO_INIT: Once = Once::new();
#[allow(clippy::missing_safety_doc)] #[allow(clippy::missing_safety_doc)]
pub unsafe fn v8_init() { pub unsafe fn v8_init() {
let platform = v8::platform::new_default_platform(); let platform = v8::new_default_platform();
v8::V8::initialize_platform(platform); v8::V8::initialize_platform(platform);
v8::V8::initialize(); v8::V8::initialize();
// TODO(ry) This makes WASM compile synchronously. Eventually we should // TODO(ry) This makes WASM compile synchronously. Eventually we should
@ -282,16 +282,14 @@ impl Isolate {
let isolate = Isolate::setup_isolate(isolate); let isolate = Isolate::setup_isolate(isolate);
let mut locker = v8::Locker::new(&isolate); let mut locker = v8::Locker::new(&isolate);
{ let scope = locker.enter();
let mut hs = v8::HandleScope::new(&mut locker);
let scope = hs.enter(); let mut hs = v8::HandleScope::new(scope);
let context = v8::Context::new(scope); let scope = hs.enter();
// context.enter();
global_context.set(scope, context); let context = bindings::initialize_context(scope);
creator.set_default_context(context); global_context.set(scope, context);
bindings::initialize_context(scope, context); creator.set_default_context(context);
// context.exit();
}
(isolate, Some(creator)) (isolate, Some(creator))
} else { } else {
@ -302,23 +300,25 @@ impl Isolate {
params.set_snapshot_blob(snapshot); params.set_snapshot_blob(snapshot);
} }
let load_snapshot_is_null = load_snapshot.is_none();
let isolate = v8::Isolate::new(params); let isolate = v8::Isolate::new(params);
let isolate = Isolate::setup_isolate(isolate); let isolate = Isolate::setup_isolate(isolate);
{ let mut locker = v8::Locker::new(&isolate);
let mut locker = v8::Locker::new(&isolate); let scope = locker.enter();
let mut hs = v8::HandleScope::new(&mut locker);
let scope = hs.enter();
let context = v8::Context::new(scope);
if load_snapshot_is_null { let mut hs = v8::HandleScope::new(scope);
let scope = hs.enter();
let context = match load_snapshot {
Some(_) => v8::Context::new(scope),
None => {
// If no snapshot is provided, we initialize the context with empty // If no snapshot is provided, we initialize the context with empty
// main source code and source maps. // main source code and source maps.
bindings::initialize_context(scope, context); bindings::initialize_context(scope)
} }
global_context.set(scope, context); };
} global_context.set(scope, context);
(isolate, None) (isolate, None)
}; };
@ -374,7 +374,7 @@ impl Isolate {
pub fn clear_exception(&mut self) { pub fn clear_exception(&mut self) {
let isolate = self.v8_isolate.as_ref().unwrap(); let isolate = self.v8_isolate.as_ref().unwrap();
let mut locker = v8::Locker::new(isolate); 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(); let scope = hs.enter();
self.last_exception_handle.reset(scope); self.last_exception_handle.reset(scope);
self.last_exception.take(); self.last_exception.take();
@ -395,7 +395,7 @@ impl Isolate {
let exception = if exception.is_null_or_undefined() { let exception = if exception.is_null_or_undefined() {
let exception_str = let exception_str =
v8::String::new(scope, "execution terminated").unwrap(); v8::String::new(scope, "execution terminated").unwrap();
v8::error(scope, exception_str) v8::Exception::error(scope, exception_str)
} else { } else {
exception exception
}; };
@ -419,7 +419,7 @@ impl Isolate {
context: v8::Local<'a, v8::Context>, context: v8::Local<'a, v8::Context>,
exception: v8::Local<'a, v8::Value>, exception: v8::Local<'a, v8::Value>,
) -> String { ) -> String {
let message = v8::create_message(scope, exception); let message = v8::Exception::create_message(scope, exception);
self.encode_message_as_json(scope, context, message) self.encode_message_as_json(scope, context, message)
} }
@ -434,15 +434,6 @@ impl Isolate {
json_string.to_rust_string_lossy(s) json_string.to_rust_string_lossy(s)
} }
#[allow(dead_code)]
pub fn run_microtasks(&mut self) {
let isolate = self.v8_isolate.as_mut().unwrap();
let _locker = v8::Locker::new(isolate);
isolate.enter();
isolate.run_microtasks();
isolate.exit();
}
// TODO(bartlomieju): `error_handler` should be removed // TODO(bartlomieju): `error_handler` should be removed
#[allow(dead_code)] #[allow(dead_code)]
pub fn set_error_handler(&mut self, handler: Box<IsolateErrorHandleFn>) { pub fn set_error_handler(&mut self, handler: Box<IsolateErrorHandleFn>) {
@ -545,24 +536,25 @@ impl Isolate {
let isolate = self.v8_isolate.as_ref().unwrap(); let isolate = self.v8_isolate.as_ref().unwrap();
let mut locker = v8::Locker::new(isolate); let mut locker = v8::Locker::new(isolate);
assert!(!self.global_context.is_empty()); assert!(!self.global_context.is_empty());
let mut hs = v8::HandleScope::new(&mut locker); let mut hs = v8::HandleScope::new(locker.enter());
let s = hs.enter(); let scope = hs.enter();
let mut context = self.global_context.get(s).unwrap(); let context = self.global_context.get(scope).unwrap();
context.enter(); let mut cs = v8::ContextScope::new(scope, context);
let source = v8::String::new(s, js_source).unwrap(); let scope = cs.enter();
let name = v8::String::new(s, js_filename).unwrap();
let mut try_catch = v8::TryCatch::new(s); let source = v8::String::new(scope, js_source).unwrap();
let name = v8::String::new(scope, js_filename).unwrap();
let mut try_catch = v8::TryCatch::new(scope);
let tc = try_catch.enter(); let tc = try_catch.enter();
let origin = bindings::script_origin(s, name); let origin = bindings::script_origin(scope, name);
let mut script = let mut script =
v8::Script::compile(s, context, source, Some(&origin)).unwrap(); v8::Script::compile(scope, context, source, Some(&origin)).unwrap();
let result = script.run(s, context); let result = script.run(scope, context);
if result.is_none() { if result.is_none() {
assert!(tc.has_caught()); assert!(tc.has_caught());
let exception = tc.exception().unwrap(); let exception = tc.exception().unwrap();
self.handle_exception(s, context, exception); self.handle_exception(scope, context, exception);
} }
context.exit();
self.check_last_exception() self.check_last_exception()
} }
@ -596,10 +588,11 @@ impl Isolate {
let mut locker = v8::Locker::new(isolate); let mut locker = v8::Locker::new(isolate);
assert!(!self.global_context.is_empty()); assert!(!self.global_context.is_empty());
let mut hs = v8::HandleScope::new(&mut locker); let mut hs = v8::HandleScope::new(locker.enter());
let scope = hs.enter(); let scope = hs.enter();
let mut context = self.global_context.get(scope).unwrap(); let context = self.global_context.get(scope).unwrap();
context.enter(); let mut cs = v8::ContextScope::new(scope, context);
let scope = cs.enter();
let pending_promises: Vec<(i32, v8::Global<v8::Value>)> = let pending_promises: Vec<(i32, v8::Global<v8::Value>)> =
self.pending_promise_map.drain().collect(); self.pending_promise_map.drain().collect();
@ -608,17 +601,15 @@ impl Isolate {
self.handle_exception(scope, context, error); self.handle_exception(scope, context, error);
handle.reset(scope); handle.reset(scope);
} }
context.exit();
} }
fn throw_exception(&mut self, text: &str) { fn throw_exception(&mut self, text: &str) {
let isolate = self.v8_isolate.as_ref().unwrap(); let isolate = self.v8_isolate.as_ref().unwrap();
let mut locker = v8::Locker::new(isolate); 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(); let scope = hs.enter();
let msg = v8::String::new(scope, text).unwrap(); let msg = v8::String::new(scope, text).unwrap();
isolate.throw_exception(msg.into()); scope.isolate().throw_exception(msg.into());
} }
fn async_op_response2(&mut self, op_id: OpId, buf: Box<[u8]>) { fn async_op_response2(&mut self, op_id: OpId, buf: Box<[u8]>) {
@ -626,10 +617,11 @@ impl Isolate {
// println!("deno_execute -> Isolate ptr {:?}", isolate); // println!("deno_execute -> Isolate ptr {:?}", isolate);
let mut locker = v8::Locker::new(isolate); let mut locker = v8::Locker::new(isolate);
assert!(!self.global_context.is_empty()); assert!(!self.global_context.is_empty());
let mut hs = v8::HandleScope::new(&mut locker); let mut hs = v8::HandleScope::new(locker.enter());
let scope = hs.enter(); let scope = hs.enter();
let mut context = self.global_context.get(scope).unwrap(); let context = self.global_context.get(scope).unwrap();
context.enter(); let mut cs = v8::ContextScope::new(scope, context);
let scope = cs.enter();
let mut try_catch = v8::TryCatch::new(scope); let mut try_catch = v8::TryCatch::new(scope);
let tc = try_catch.enter(); let tc = try_catch.enter();
@ -642,28 +634,24 @@ impl Isolate {
return; return;
} }
let mut argc = 0; let global: v8::Local<v8::Value> = context.global(scope).into();
let mut args: Vec<v8::Local<v8::Value>> = vec![];
if !buf.is_empty() { let maybe_value = if !buf.is_empty() {
argc = 2; let op_id: v8::Local<v8::Value> =
let op_id = v8::Integer::new(scope, op_id as i32); v8::Integer::new(scope, op_id as i32).into();
args.push(op_id.into()); let buf: v8::Local<v8::Value> =
let buf = unsafe { bindings::slice_to_uint8array(self, scope, &buf) }; unsafe { bindings::slice_to_uint8array(self, scope, &buf) }.into();
args.push(buf.into());
}
let global = context.global(scope);
let maybe_value =
js_recv_cb js_recv_cb
.unwrap() .unwrap()
.call(scope, context, global.into(), argc, args); .call(scope, context, global, &[op_id, buf])
} else {
js_recv_cb.unwrap().call(scope, context, global, &[])
};
if tc.has_caught() { if tc.has_caught() {
assert!(maybe_value.is_none()); assert!(maybe_value.is_none());
self.handle_exception(scope, context, tc.exception().unwrap()); self.handle_exception(scope, context, tc.exception().unwrap());
} }
context.exit();
} }
fn async_op_response( fn async_op_response(
@ -689,7 +677,7 @@ impl Isolate {
let isolate = self.v8_isolate.as_ref().unwrap(); let isolate = self.v8_isolate.as_ref().unwrap();
let mut locker = v8::Locker::new(isolate); 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(); let scope = hs.enter();
self.global_context.reset(scope); self.global_context.reset(scope);

View file

@ -17,6 +17,7 @@ SharedQueue Binary Layout
*/ */
use crate::ops::OpId; use crate::ops::OpId;
use rusty_v8 as v8;
const MAX_RECORDS: usize = 100; const MAX_RECORDS: usize = 100;
/// Total number of records added. /// Total number of records added.
@ -34,18 +35,39 @@ const HEAD_INIT: usize = 4 * INDEX_RECORDS;
pub const RECOMMENDED_SIZE: usize = 128 * MAX_RECORDS; pub const RECOMMENDED_SIZE: usize = 128 * MAX_RECORDS;
pub struct SharedQueue { pub struct SharedQueue {
pub bytes: Vec<u8>, buf: v8::SharedRef<v8::BackingStore>,
} }
impl SharedQueue { impl SharedQueue {
pub fn new(len: usize) -> Self { pub fn new(len: usize) -> Self {
let mut bytes = Vec::new(); let mut buf = Vec::new();
bytes.resize(HEAD_INIT + len, 0); buf.resize(HEAD_INIT + len, 0);
let mut q = Self { bytes }; let buf = buf.into_boxed_slice();
let buf =
unsafe { v8::SharedArrayBuffer::new_backing_store_from_boxed_slice(buf) };
let mut q = Self { buf };
q.reset(); q.reset();
q q
} }
pub fn get_backing_store(&mut self) -> &mut v8::SharedRef<v8::BackingStore> {
&mut self.buf
}
pub fn bytes(&self) -> &[u8] {
unsafe {
// This is quite bad. The rusty_v8 issue that makes it necessitates it
// just barely missed the rusty_v8 v0.1.1 release cutoff.
#[allow(clippy::cast_ref_to_mut)]
let self_mut = &mut *(self as *const _ as *mut Self);
self_mut.bytes_mut()
}
}
pub fn bytes_mut(&mut self) -> &mut [u8] {
self.buf.data_bytes()
}
fn reset(&mut self) { fn reset(&mut self) {
debug!("rust:shared_queue:reset"); debug!("rust:shared_queue:reset");
let s: &mut [u32] = self.as_u32_slice_mut(); let s: &mut [u32] = self.as_u32_slice_mut();
@ -55,21 +77,21 @@ impl SharedQueue {
} }
fn as_u32_slice(&self) -> &[u32] { fn as_u32_slice(&self) -> &[u32] {
let p = self.bytes.as_ptr(); let p = self.bytes().as_ptr();
// Assert pointer is 32 bit aligned before casting. // Assert pointer is 32 bit aligned before casting.
assert_eq!((p as usize) % std::mem::align_of::<u32>(), 0); assert_eq!((p as usize) % std::mem::align_of::<u32>(), 0);
#[allow(clippy::cast_ptr_alignment)] #[allow(clippy::cast_ptr_alignment)]
let p32 = p as *const u32; let p32 = p as *const u32;
unsafe { std::slice::from_raw_parts(p32, self.bytes.len() / 4) } unsafe { std::slice::from_raw_parts(p32, self.bytes().len() / 4) }
} }
fn as_u32_slice_mut(&mut self) -> &mut [u32] { fn as_u32_slice_mut(&mut self) -> &mut [u32] {
let p = self.bytes.as_mut_ptr(); let p = self.bytes_mut().as_mut_ptr();
// Assert pointer is 32 bit aligned before casting. // Assert pointer is 32 bit aligned before casting.
assert_eq!((p as usize) % std::mem::align_of::<u32>(), 0); assert_eq!((p as usize) % std::mem::align_of::<u32>(), 0);
#[allow(clippy::cast_ptr_alignment)] #[allow(clippy::cast_ptr_alignment)]
let p32 = p as *mut u32; let p32 = p as *mut u32;
unsafe { std::slice::from_raw_parts_mut(p32, self.bytes.len() / 4) } unsafe { std::slice::from_raw_parts_mut(p32, self.bytes().len() / 4) }
} }
pub fn size(&self) -> usize { pub fn size(&self) -> usize {
@ -149,7 +171,7 @@ impl SharedQueue {
self.num_shifted_off(), self.num_shifted_off(),
self.head() self.head()
); );
Some((op_id, &self.bytes[off..end])) Some((op_id, &self.bytes()[off..end]))
} }
/// Because JS-side may cast `record` to Int32Array it is required /// Because JS-side may cast `record` to Int32Array it is required
@ -166,13 +188,13 @@ impl SharedQueue {
); );
assert_eq!(record.len() % 4, 0); assert_eq!(record.len() % 4, 0);
let index = self.num_records(); let index = self.num_records();
if end > self.bytes.len() || index >= MAX_RECORDS { if end > self.bytes().len() || index >= MAX_RECORDS {
debug!("WARNING the sharedQueue overflowed"); debug!("WARNING the sharedQueue overflowed");
return false; return false;
} }
self.set_meta(index, end, op_id); self.set_meta(index, end, op_id);
assert_eq!(end - off, record.len()); assert_eq!(end - off, record.len());
self.bytes[off..end].copy_from_slice(record); self.bytes_mut()[off..end].copy_from_slice(record);
let u32_slice = self.as_u32_slice_mut(); let u32_slice = self.as_u32_slice_mut();
u32_slice[INDEX_NUM_RECORDS] += 1; u32_slice[INDEX_NUM_RECORDS] += 1;
u32_slice[INDEX_HEAD] = end as u32; u32_slice[INDEX_HEAD] = end as u32;