diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/Cargo.toml | 2 | ||||
-rw-r--r-- | core/bindings.rs | 470 | ||||
-rw-r--r-- | core/core_isolate.rs | 151 | ||||
-rw-r--r-- | core/errors.rs | 67 | ||||
-rw-r--r-- | core/es_isolate.rs | 70 | ||||
-rw-r--r-- | core/zero_copy_buf.rs | 2 |
6 files changed, 312 insertions, 450 deletions
diff --git a/core/Cargo.toml b/core/Cargo.toml index bb7b3dd96..8a691b5db 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -19,7 +19,7 @@ futures = { version = "0.3.5", features = ["thread-pool", "compat"] } lazy_static = "1.4.0" libc = "0.2.71" log = "0.4.8" -rusty_v8 = "0.5.1" +rusty_v8 = "0.6.0" serde_json = "1.0.55" url = "2.1.1" diff --git a/core/bindings.rs b/core/bindings.rs index 9b66bfc48..6407cb555 100644 --- a/core/bindings.rs +++ b/core/bindings.rs @@ -53,7 +53,7 @@ lazy_static! { } pub fn script_origin<'a>( - s: &mut impl v8::ToLocal<'a>, + s: &mut v8::HandleScope<'a>, resource_name: v8::Local<'a, v8::String>, ) -> v8::ScriptOrigin<'a> { let resource_line_offset = v8::Integer::new(s, 0); @@ -78,7 +78,7 @@ pub fn script_origin<'a>( } pub fn module_origin<'a>( - s: &mut impl v8::ToLocal<'a>, + s: &mut v8::HandleScope<'a>, resource_name: v8::Local<'a, v8::String>, ) -> v8::ScriptOrigin<'a> { let resource_line_offset = v8::Integer::new(s, 0); @@ -103,127 +103,92 @@ pub fn module_origin<'a>( } pub fn initialize_context<'s>( - scope: &mut impl v8::ToLocal<'s>, + scope: &mut v8::HandleScope<'s, ()>, ) -> v8::Local<'s, v8::Context> { - let mut hs = v8::EscapableHandleScope::new(scope); - let scope = hs.enter(); + let scope = &mut v8::EscapableHandleScope::new(scope); let context = v8::Context::new(scope); let global = context.global(scope); - let mut cs = v8::ContextScope::new(scope, context); - let scope = cs.enter(); + let scope = &mut v8::ContextScope::new(scope, context); + let deno_key = v8::String::new(scope, "Deno").unwrap(); 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(), - core_val.into(), - ); - - let mut print_tmpl = v8::FunctionTemplate::new(scope, print); - let print_val = print_tmpl.get_function(scope, context).unwrap(); - core_val.set( - context, - v8::String::new(scope, "print").unwrap().into(), - print_val.into(), - ); - - let mut recv_tmpl = v8::FunctionTemplate::new(scope, recv); - let recv_val = recv_tmpl.get_function(scope, context).unwrap(); - core_val.set( - context, - v8::String::new(scope, "recv").unwrap().into(), - recv_val.into(), - ); - - let mut send_tmpl = v8::FunctionTemplate::new(scope, send); - let send_val = send_tmpl.get_function(scope, context).unwrap(); - core_val.set( - context, - v8::String::new(scope, "send").unwrap().into(), - send_val.into(), - ); - - let mut set_macrotask_callback_tmpl = + global.set(scope, deno_key.into(), deno_val.into()); + + let core_key = v8::String::new(scope, "core").unwrap(); + let core_val = v8::Object::new(scope); + deno_val.set(scope, core_key.into(), core_val.into()); + + let print_key = v8::String::new(scope, "print").unwrap(); + let print_tmpl = v8::FunctionTemplate::new(scope, print); + let print_val = print_tmpl.get_function(scope).unwrap(); + core_val.set(scope, print_key.into(), print_val.into()); + + let recv_key = v8::String::new(scope, "recv").unwrap(); + let recv_tmpl = v8::FunctionTemplate::new(scope, recv); + let recv_val = recv_tmpl.get_function(scope).unwrap(); + core_val.set(scope, recv_key.into(), recv_val.into()); + + let send_key = v8::String::new(scope, "send").unwrap(); + let send_tmpl = v8::FunctionTemplate::new(scope, send); + let send_val = send_tmpl.get_function(scope).unwrap(); + core_val.set(scope, send_key.into(), send_val.into()); + + let set_macrotask_callback_key = + v8::String::new(scope, "setMacrotaskCallback").unwrap(); + let set_macrotask_callback_tmpl = v8::FunctionTemplate::new(scope, set_macrotask_callback); - let set_macrotask_callback_val = set_macrotask_callback_tmpl - .get_function(scope, context) - .unwrap(); + let set_macrotask_callback_val = + set_macrotask_callback_tmpl.get_function(scope).unwrap(); core_val.set( - context, - v8::String::new(scope, "setMacrotaskCallback") - .unwrap() - .into(), + scope, + set_macrotask_callback_key.into(), set_macrotask_callback_val.into(), ); - let mut eval_context_tmpl = v8::FunctionTemplate::new(scope, eval_context); - let eval_context_val = - eval_context_tmpl.get_function(scope, context).unwrap(); - core_val.set( - context, - v8::String::new(scope, "evalContext").unwrap().into(), - eval_context_val.into(), - ); - - let mut format_error_tmpl = v8::FunctionTemplate::new(scope, format_error); - let format_error_val = - format_error_tmpl.get_function(scope, context).unwrap(); - core_val.set( - context, - v8::String::new(scope, "formatError").unwrap().into(), - format_error_val.into(), - ); - - let mut encode_tmpl = v8::FunctionTemplate::new(scope, encode); - let encode_val = encode_tmpl.get_function(scope, context).unwrap(); - core_val.set( - context, - v8::String::new(scope, "encode").unwrap().into(), - encode_val.into(), - ); - - let mut decode_tmpl = v8::FunctionTemplate::new(scope, decode); - let decode_val = decode_tmpl.get_function(scope, context).unwrap(); - core_val.set( - context, - v8::String::new(scope, "decode").unwrap().into(), - decode_val.into(), - ); - - let mut get_promise_details_tmpl = + let eval_context_key = v8::String::new(scope, "evalContext").unwrap(); + let eval_context_tmpl = v8::FunctionTemplate::new(scope, eval_context); + let eval_context_val = eval_context_tmpl.get_function(scope).unwrap(); + core_val.set(scope, eval_context_key.into(), eval_context_val.into()); + + let format_error_key = v8::String::new(scope, "formatError").unwrap(); + let format_error_tmpl = v8::FunctionTemplate::new(scope, format_error); + let format_error_val = format_error_tmpl.get_function(scope).unwrap(); + core_val.set(scope, format_error_key.into(), format_error_val.into()); + + let encode_key = v8::String::new(scope, "encode").unwrap(); + let encode_tmpl = v8::FunctionTemplate::new(scope, encode); + let encode_val = encode_tmpl.get_function(scope).unwrap(); + core_val.set(scope, encode_key.into(), encode_val.into()); + + let decode_key = v8::String::new(scope, "decode").unwrap(); + let decode_tmpl = v8::FunctionTemplate::new(scope, decode); + let decode_val = decode_tmpl.get_function(scope).unwrap(); + core_val.set(scope, decode_key.into(), decode_val.into()); + + let get_promise_details_key = + v8::String::new(scope, "getPromiseDetails").unwrap(); + let get_promise_details_tmpl = v8::FunctionTemplate::new(scope, get_promise_details); - let get_promise_details_val = get_promise_details_tmpl - .get_function(scope, context) - .unwrap(); + let get_promise_details_val = + get_promise_details_tmpl.get_function(scope).unwrap(); core_val.set( - context, - v8::String::new(scope, "getPromiseDetails").unwrap().into(), + scope, + get_promise_details_key.into(), get_promise_details_val.into(), ); - core_val.set_accessor( - context, - v8::String::new(scope, "shared").unwrap().into(), - shared_getter, - ); + let shared_key = v8::String::new(scope, "shared").unwrap(); + core_val.set_accessor(scope, shared_key.into(), shared_getter); // Direct bindings on `window`. - let mut queue_microtask_tmpl = - v8::FunctionTemplate::new(scope, queue_microtask); - let queue_microtask_val = - queue_microtask_tmpl.get_function(scope, context).unwrap(); + let queue_microtask_key = v8::String::new(scope, "queueMicrotask").unwrap(); + let queue_microtask_tmpl = v8::FunctionTemplate::new(scope, queue_microtask); + let queue_microtask_val = queue_microtask_tmpl.get_function(scope).unwrap(); global.set( - context, - v8::String::new(scope, "queueMicrotask").unwrap().into(), + scope, + queue_microtask_key.into(), queue_microtask_val.into(), ); @@ -231,7 +196,7 @@ pub fn initialize_context<'s>( } pub fn boxed_slice_to_uint8array<'sc>( - scope: &mut impl v8::ToLocal<'sc>, + scope: &mut v8::HandleScope<'sc>, buf: Box<[u8]>, ) -> v8::Local<'sc, v8::Uint8Array> { assert!(!buf.is_empty()); @@ -248,9 +213,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_escapable(context); - let mut hs = v8::EscapableHandleScope::new(cbs.enter()); - let scope = hs.enter(); + let scope = &mut unsafe { v8::CallbackScope::new(context) }; // NOTE(bartlomieju): will crash for non-UTF-8 specifier let specifier_str = specifier @@ -269,19 +232,19 @@ 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 resolver = v8::PromiseResolver::new(scope, context).unwrap(); + let resolver = v8::PromiseResolver::new(scope).unwrap(); let promise = resolver.get_promise(scope); let mut resolver_handle = v8::Global::new(); resolver_handle.set(scope, resolver); { - let state_rc = EsIsolate::state(scope.isolate()); + let state_rc = EsIsolate::state(scope); let mut state = state_rc.borrow_mut(); state.dyn_import_cb(resolver_handle, &specifier_str, &referrer_name_str); } - &mut *scope.escape(promise) + &*promise as *const _ as *mut _ } pub extern "C" fn host_initialize_import_meta_object_callback( @@ -289,10 +252,8 @@ pub extern "C" fn host_initialize_import_meta_object_callback( module: v8::Local<v8::Module>, meta: v8::Local<v8::Object>, ) { - let mut cbs = v8::CallbackScope::new(context); - let mut hs = v8::HandleScope::new(cbs.enter()); - let scope = hs.enter(); - let state_rc = EsIsolate::state(scope.isolate()); + let scope = &mut unsafe { v8::CallbackScope::new(context) }; + let state_rc = EsIsolate::state(scope); let state = state_rc.borrow(); let id = module.get_identity_hash(); @@ -300,30 +261,21 @@ pub extern "C" fn host_initialize_import_meta_object_callback( let info = state.modules.get_info(id).expect("Module not found"); - meta.create_data_property( - context, - v8::String::new(scope, "url").unwrap().into(), - v8::String::new(scope, &info.name).unwrap().into(), - ); - meta.create_data_property( - context, - v8::String::new(scope, "main").unwrap().into(), - v8::Boolean::new(scope, info.main).into(), - ); + let url_key = v8::String::new(scope, "url").unwrap(); + let url_val = v8::String::new(scope, &info.name).unwrap(); + meta.create_data_property(scope, url_key.into(), url_val.into()); + + let main_key = v8::String::new(scope, "main").unwrap(); + let main_val = v8::Boolean::new(scope, info.main); + meta.create_data_property(scope, main_key.into(), main_val.into()); } pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) { - let mut cbs = v8::CallbackScope::new(&message); - let mut hs = v8::HandleScope::new(cbs.enter()); - let scope = hs.enter(); + let scope = &mut unsafe { v8::CallbackScope::new(&message) }; - let state_rc = CoreIsolate::state(scope.isolate()); + let state_rc = CoreIsolate::state(scope); let mut state = state_rc.borrow_mut(); - let context = state.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(); @@ -374,7 +326,7 @@ pub(crate) unsafe fn get_backing_store_slice_mut( } fn print( - scope: v8::FunctionCallbackScope, + scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments, _rv: v8::ReturnValue, ) { @@ -384,9 +336,6 @@ fn print( let obj = args.get(0); let is_err_arg = args.get(1); - let mut hs = v8::HandleScope::new(scope); - let scope = hs.enter(); - let mut is_err = false; if arg_len == 2 { let int_val = is_err_arg @@ -394,30 +343,29 @@ fn print( .expect("Unable to convert to integer"); is_err = int_val != 0; }; - let mut try_catch = v8::TryCatch::new(scope); - let _tc = try_catch.enter(); - let str_ = match obj.to_string(scope) { + let tc_scope = &mut v8::TryCatch::new(scope); + let str_ = match obj.to_string(tc_scope) { Some(s) => s, - None => v8::String::new(scope, "").unwrap(), + None => v8::String::new(tc_scope, "").unwrap(), }; if is_err { - eprint!("{}", str_.to_rust_string_lossy(scope)); + eprint!("{}", str_.to_rust_string_lossy(tc_scope)); } else { - print!("{}", str_.to_rust_string_lossy(scope)); + print!("{}", str_.to_rust_string_lossy(tc_scope)); } } fn recv( - scope: v8::FunctionCallbackScope, + scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments, _rv: v8::ReturnValue, ) { - let state_rc = CoreIsolate::state(scope.isolate()); + let state_rc = CoreIsolate::state(scope); let mut state = state_rc.borrow_mut(); if !state.js_recv_cb.is_empty() { let msg = v8::String::new(scope, "Deno.core.recv already called.").unwrap(); - scope.isolate().throw_exception(msg.into()); + scope.throw_exception(msg.into()); return; } @@ -426,7 +374,7 @@ fn recv( } fn send( - scope: v8::FunctionCallbackScope, + scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments, mut rv: v8::ReturnValue, ) { @@ -435,7 +383,7 @@ fn send( Err(err) => { let msg = format!("invalid op id: {}", err); let msg = v8::String::new(scope, &msg).unwrap(); - scope.isolate().throw_exception(msg.into()); + scope.throw_exception(msg.into()); return; } }; @@ -453,7 +401,7 @@ fn send( Err(_) => &[], }; - let state_rc = CoreIsolate::state(scope.isolate()); + let state_rc = CoreIsolate::state(scope); let mut state = state_rc.borrow_mut(); assert!(!state.global_context.is_empty()); @@ -493,7 +441,7 @@ fn send( let maybe_response = match buf_iter_result { Ok(bufs) => state.dispatch_op(scope, op_id, control, bufs), Err(exc) => { - scope.isolate().throw_exception(exc); + scope.throw_exception(exc); return; } }; @@ -511,18 +459,18 @@ fn send( } fn set_macrotask_callback( - scope: v8::FunctionCallbackScope, + scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments, _rv: v8::ReturnValue, ) { - let state_rc = CoreIsolate::state(scope.isolate()); + let state_rc = CoreIsolate::state(scope); let mut state = state_rc.borrow_mut(); if !state.js_macrotask_cb.is_empty() { let msg = v8::String::new(scope, "Deno.core.setMacrotaskCallback already called.") .unwrap(); - scope.isolate().throw_exception(msg.into()); + scope.throw_exception(msg.into()); return; } @@ -532,23 +480,16 @@ fn set_macrotask_callback( } fn eval_context( - scope: v8::FunctionCallbackScope, + scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments, mut rv: v8::ReturnValue, ) { - let state_rc = CoreIsolate::state(scope.isolate()); - let context = { - let state = state_rc.borrow(); - assert!(!state.global_context.is_empty()); - state.global_context.get(scope).unwrap() - }; - let source = match v8::Local::<v8::String>::try_from(args.get(0)) { Ok(s) => s, Err(_) => { let msg = v8::String::new(scope, "Invalid argument").unwrap(); let exception = v8::Exception::type_error(scope, msg); - scope.isolate().throw_exception(exception); + scope.throw_exception(exception); return; } }; @@ -566,116 +507,108 @@ fn eval_context( isCompileError: boolean, } */ - let mut try_catch = v8::TryCatch::new(scope); - let tc = try_catch.enter(); + let tc_scope = &mut v8::TryCatch::new(scope); let name = - v8::String::new(scope, url.as_ref().map_or("<unknown>", Url::as_str)) + v8::String::new(tc_scope, url.as_ref().map_or("<unknown>", Url::as_str)) .unwrap(); - let origin = script_origin(scope, name); - let maybe_script = v8::Script::compile(scope, context, source, Some(&origin)); + let origin = script_origin(tc_scope, name); + let maybe_script = v8::Script::compile(tc_scope, source, Some(&origin)); if maybe_script.is_none() { - assert!(tc.has_caught()); - let exception = tc.exception(scope).unwrap(); + assert!(tc_scope.has_caught()); + let exception = tc_scope.exception().unwrap(); - output.set( - context, - v8::Integer::new(scope, 0).into(), - v8::null(scope).into(), - ); + let js_zero = v8::Integer::new(tc_scope, 0); + let js_null = v8::null(tc_scope); + output.set(tc_scope, js_zero.into(), js_null.into()); - let errinfo_obj = v8::Object::new(scope); - errinfo_obj.set( - context, - v8::String::new(scope, "isCompileError").unwrap().into(), - v8::Boolean::new(scope, true).into(), - ); + let errinfo_obj = v8::Object::new(tc_scope); + let is_compile_error_key = + v8::String::new(tc_scope, "isCompileError").unwrap(); + let is_compile_error_val = v8::Boolean::new(tc_scope, true); errinfo_obj.set( - context, - v8::String::new(scope, "isNativeError").unwrap().into(), - v8::Boolean::new(scope, exception.is_native_error()).into(), + tc_scope, + is_compile_error_key.into(), + is_compile_error_val.into(), ); + let is_native_error_key = + v8::String::new(tc_scope, "isNativeError").unwrap(); + let is_native_error_val = + v8::Boolean::new(tc_scope, exception.is_native_error()); errinfo_obj.set( - context, - v8::String::new(scope, "thrown").unwrap().into(), - exception, + tc_scope, + is_native_error_key.into(), + is_native_error_val.into(), ); - output.set( - context, - v8::Integer::new(scope, 1).into(), - errinfo_obj.into(), - ); + let thrown_key = v8::String::new(tc_scope, "thrown").unwrap(); + errinfo_obj.set(tc_scope, thrown_key.into(), exception); + + let js_one = v8::Integer::new(tc_scope, 1); + output.set(tc_scope, js_one.into(), errinfo_obj.into()); rv.set(output.into()); return; } - let result = maybe_script.unwrap().run(scope, context); + let result = maybe_script.unwrap().run(tc_scope); if result.is_none() { - assert!(tc.has_caught()); - let exception = tc.exception(scope).unwrap(); - - output.set( - context, - v8::Integer::new(scope, 0).into(), - v8::null(scope).into(), - ); + assert!(tc_scope.has_caught()); + let exception = tc_scope.exception().unwrap(); - let errinfo_obj = v8::Object::new(scope); - errinfo_obj.set( - context, - v8::String::new(scope, "isCompileError").unwrap().into(), - v8::Boolean::new(scope, false).into(), - ); + let js_zero = v8::Integer::new(tc_scope, 0); + let js_null = v8::null(tc_scope); + output.set(tc_scope, js_zero.into(), js_null.into()); - let is_native_error = if exception.is_native_error() { - v8::Boolean::new(scope, true) - } else { - v8::Boolean::new(scope, false) - }; + let errinfo_obj = v8::Object::new(tc_scope); + let is_compile_error_key = + v8::String::new(tc_scope, "isCompileError").unwrap(); + let is_compile_error_val = v8::Boolean::new(tc_scope, false); errinfo_obj.set( - context, - v8::String::new(scope, "isNativeError").unwrap().into(), - is_native_error.into(), + tc_scope, + is_compile_error_key.into(), + is_compile_error_val.into(), ); + let is_native_error_key = + v8::String::new(tc_scope, "isNativeError").unwrap(); + let is_native_error_val = + v8::Boolean::new(tc_scope, exception.is_native_error()); errinfo_obj.set( - context, - v8::String::new(scope, "thrown").unwrap().into(), - exception, + tc_scope, + is_native_error_key.into(), + is_native_error_val.into(), ); - output.set( - context, - v8::Integer::new(scope, 1).into(), - errinfo_obj.into(), - ); + let thrown_key = v8::String::new(tc_scope, "thrown").unwrap(); + errinfo_obj.set(tc_scope, thrown_key.into(), exception); + + let js_one = v8::Integer::new(tc_scope, 1); + output.set(tc_scope, js_one.into(), errinfo_obj.into()); rv.set(output.into()); return; } - output.set(context, v8::Integer::new(scope, 0).into(), result.unwrap()); - output.set( - context, - v8::Integer::new(scope, 1).into(), - v8::null(scope).into(), - ); + let js_zero = v8::Integer::new(tc_scope, 0); + let js_one = v8::Integer::new(tc_scope, 1); + let js_null = v8::null(tc_scope); + output.set(tc_scope, js_zero.into(), result.unwrap()); + output.set(tc_scope, js_one.into(), js_null.into()); rv.set(output.into()); } fn format_error( - scope: v8::FunctionCallbackScope, + scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments, mut rv: v8::ReturnValue, ) { let e = JSError::from_v8_exception(scope, args.get(0)); - let state_rc = CoreIsolate::state(scope.isolate()); + let state_rc = CoreIsolate::state(scope); let state = state_rc.borrow(); let e = (state.js_error_create_fn)(e); let e = e.to_string(); @@ -684,7 +617,7 @@ fn format_error( } fn encode( - scope: v8::FunctionCallbackScope, + scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments, mut rv: v8::ReturnValue, ) { @@ -693,7 +626,7 @@ fn encode( Err(_) => { let msg = v8::String::new(scope, "Invalid argument").unwrap(); let exception = v8::Exception::type_error(scope, msg); - scope.isolate().throw_exception(exception); + scope.throw_exception(exception); return; } }; @@ -717,7 +650,7 @@ fn encode( } fn decode( - scope: v8::FunctionCallbackScope, + scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments, mut rv: v8::ReturnValue, ) { @@ -726,7 +659,7 @@ fn decode( Err(_) => { let msg = v8::String::new(scope, "Invalid argument").unwrap(); let exception = v8::Exception::type_error(scope, msg); - scope.isolate().throw_exception(exception); + scope.throw_exception(exception); return; } }; @@ -746,27 +679,27 @@ fn decode( } fn queue_microtask( - scope: v8::FunctionCallbackScope, + scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments, _rv: v8::ReturnValue, ) { match v8::Local::<v8::Function>::try_from(args.get(0)) { - Ok(f) => scope.isolate().enqueue_microtask(f), + Ok(f) => scope.enqueue_microtask(f), Err(_) => { let msg = v8::String::new(scope, "Invalid argument").unwrap(); let exception = v8::Exception::type_error(scope, msg); - scope.isolate().throw_exception(exception); + scope.throw_exception(exception); } }; } fn shared_getter( - scope: v8::PropertyCallbackScope, + scope: &mut v8::HandleScope, _name: v8::Local<v8::Name>, _args: v8::PropertyCallbackArguments, mut rv: v8::ReturnValue, ) { - let state_rc = CoreIsolate::state(scope.isolate()); + let state_rc = CoreIsolate::state(scope); let mut state = state_rc.borrow_mut(); // Lazily initialize the persistent external ArrayBuffer. @@ -787,11 +720,9 @@ 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_escapable(context); - let mut scope = v8::EscapableHandleScope::new(scope.enter()); - let scope = scope.enter(); + let scope = &mut unsafe { v8::CallbackScope::new(context) }; - let state_rc = EsIsolate::state(scope.isolate()); + let state_rc = EsIsolate::state(scope); let mut state = state_rc.borrow_mut(); let referrer_id = referrer.get_identity_hash(); @@ -819,13 +750,11 @@ pub fn module_resolve_callback<'s>( req_str, referrer_name ); let msg = v8::String::new(scope, &msg).unwrap(); - scope.isolate().throw_exception(msg.into()); + scope.throw_exception(msg.into()); break; } - return maybe_info - .and_then(|i| i.handle.get(scope)) - .map(|m| scope.escape(m)); + return maybe_info.and_then(|i| i.handle.get(scope)); } } @@ -833,26 +762,21 @@ pub fn module_resolve_callback<'s>( } // Returns promise details or throw TypeError, if argument passed isn't a Promise. -// Promise details is a two elements array. +// Promise details is a js_two elements array. // promise_details = [State, Result] // State = enum { Pending = 0, Fulfilled = 1, Rejected = 2} // Result = PromiseResult<T> | PromiseError fn get_promise_details( - scope: v8::FunctionCallbackScope, + scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments, mut rv: v8::ReturnValue, ) { - let state_rc = CoreIsolate::state(scope.isolate()); - let state = state_rc.borrow(); - assert!(!state.global_context.is_empty()); - let context = state.global_context.get(scope).unwrap(); - 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(); let exception = v8::Exception::type_error(scope, msg); - scope.isolate().throw_exception(exception); + scope.throw_exception(exception); return; } }; @@ -861,37 +785,25 @@ fn get_promise_details( match promise.state() { v8::PromiseState::Pending => { - promise_details.set( - context, - v8::Integer::new(scope, 0).into(), - v8::Integer::new(scope, 0).into(), - ); + let js_zero = v8::Integer::new(scope, 0); + promise_details.set(scope, js_zero.into(), js_zero.into()); rv.set(promise_details.into()); } v8::PromiseState::Fulfilled => { - promise_details.set( - context, - v8::Integer::new(scope, 0).into(), - v8::Integer::new(scope, 1).into(), - ); - promise_details.set( - context, - v8::Integer::new(scope, 1).into(), - promise.result(scope), - ); + let js_zero = v8::Integer::new(scope, 0); + let js_one = v8::Integer::new(scope, 1); + let promise_result = promise.result(scope); + promise_details.set(scope, js_zero.into(), js_one.into()); + promise_details.set(scope, js_one.into(), promise_result); rv.set(promise_details.into()); } v8::PromiseState::Rejected => { - promise_details.set( - context, - v8::Integer::new(scope, 0).into(), - v8::Integer::new(scope, 2).into(), - ); - promise_details.set( - context, - v8::Integer::new(scope, 1).into(), - promise.result(scope), - ); + let js_zero = v8::Integer::new(scope, 0); + let js_one = v8::Integer::new(scope, 1); + let js_two = v8::Integer::new(scope, 2); + let promise_result = promise.result(scope); + promise_details.set(scope, js_zero.into(), js_two.into()); + promise_details.set(scope, js_one.into(), promise_result); rv.set(promise_details.into()); } } diff --git a/core/core_isolate.rs b/core/core_isolate.rs index dce707c63..bb1807f62 100644 --- a/core/core_isolate.rs +++ b/core/core_isolate.rs @@ -109,14 +109,6 @@ pub struct CoreIsolateState { waker: AtomicWaker, } -// TODO(ry) The trait v8::InIsolate is superfluous. HandleScope::new should just -// take &mut v8::Isolate. -impl v8::InIsolate for CoreIsolate { - fn isolate(&mut self) -> &mut v8::Isolate { - self.v8_isolate.as_mut().unwrap() - } -} - impl Deref for CoreIsolate { type Target = v8::Isolate; fn deref(&self) -> &v8::Isolate { @@ -193,14 +185,12 @@ impl CoreIsolate { v8::SnapshotCreator::new(Some(&bindings::EXTERNAL_REFERENCES)); let isolate = unsafe { creator.get_owned_isolate() }; let mut isolate = CoreIsolate::setup_isolate(isolate); - - let mut hs = v8::HandleScope::new(&mut isolate); - let scope = hs.enter(); - - let context = bindings::initialize_context(scope); - global_context.set(scope, context); - creator.set_default_context(context); - + { + let scope = &mut v8::HandleScope::new(&mut isolate); + let context = bindings::initialize_context(scope); + global_context.set(scope, context); + creator.set_default_context(context); + } (isolate, Some(creator)) } else { let mut params = v8::Isolate::create_params() @@ -218,19 +208,17 @@ impl CoreIsolate { let isolate = v8::Isolate::new(params); let mut isolate = CoreIsolate::setup_isolate(isolate); - - let mut hs = v8::HandleScope::new(&mut isolate); - let scope = hs.enter(); - - let context = if snapshot_loaded { - v8::Context::new(scope) - } else { - // If no snapshot is provided, we initialize the context with empty - // main source code and source maps. - bindings::initialize_context(scope) - }; - global_context.set(scope, context); - + { + let scope = &mut v8::HandleScope::new(&mut isolate); + let context = if snapshot_loaded { + v8::Context::new(scope) + } else { + // If no snapshot is provided, we initialize the context with empty + // main source code and source maps. + bindings::initialize_context(scope) + }; + global_context.set(scope, context); + } (isolate, None) }; @@ -297,11 +285,9 @@ impl CoreIsolate { let state_rc = Self::state(self); let state = state_rc.borrow(); - let mut hs = v8::HandleScope::new(self.v8_isolate.as_mut().unwrap()); - let scope = hs.enter(); + let scope = &mut v8::HandleScope::new(self.v8_isolate.as_mut().unwrap()); let context = state.global_context.get(scope).unwrap(); - let mut cs = v8::ContextScope::new(scope, context); - let scope = cs.enter(); + let scope = &mut v8::ContextScope::new(scope, context); drop(state); @@ -309,24 +295,22 @@ impl CoreIsolate { let name = v8::String::new(scope, js_filename).unwrap(); let origin = bindings::script_origin(scope, name); - let mut try_catch = v8::TryCatch::new(scope); - let tc = try_catch.enter(); + let tc_scope = &mut v8::TryCatch::new(scope); - let mut script = - match v8::Script::compile(scope, context, source, Some(&origin)) { - Some(script) => script, - None => { - let exception = tc.exception(scope).unwrap(); - return exception_to_err_result(scope, exception); - } - }; + let script = match v8::Script::compile(tc_scope, source, Some(&origin)) { + Some(script) => script, + None => { + let exception = tc_scope.exception().unwrap(); + return exception_to_err_result(tc_scope, exception); + } + }; - match script.run(scope, context) { + match script.run(tc_scope) { Some(_) => Ok(()), None => { - assert!(tc.has_caught()); - let exception = tc.exception(scope).unwrap(); - exception_to_err_result(scope, exception) + assert!(tc_scope.has_caught()); + let exception = tc_scope.exception().unwrap(); + exception_to_err_result(tc_scope, exception) } } } @@ -346,8 +330,7 @@ impl CoreIsolate { // TODO(piscisaureus): The rusty_v8 type system should enforce this. { let v8_isolate = self.v8_isolate.as_mut().unwrap(); - let mut hs = v8::HandleScope::new(v8_isolate); - let scope = hs.enter(); + let scope = &mut v8::HandleScope::new(v8_isolate); state.borrow_mut().global_context.reset(scope); } @@ -388,14 +371,12 @@ impl Future for CoreIsolate { state.waker.register(cx.waker()); } - let mut hs = v8::HandleScope::new(core_isolate); - let scope = hs.enter(); + let scope = &mut v8::HandleScope::new(&mut **core_isolate); let context = { let state = state_rc.borrow(); state.global_context.get(scope).unwrap() }; - let mut cs = v8::ContextScope::new(scope, context); - let scope = cs.enter(); + let scope = &mut v8::ContextScope::new(scope, context); check_promise_exceptions(scope)?; @@ -502,7 +483,7 @@ impl CoreIsolateState { pub fn dispatch_op<'s>( &mut self, - scope: &mut impl v8::ToLocal<'s>, + scope: &mut v8::HandleScope<'s>, op_id: OpId, control_buf: &[u8], zero_copy_bufs: &mut [ZeroCopyBuf], @@ -513,7 +494,7 @@ impl CoreIsolateState { let message = v8::String::new(scope, &format!("Unknown op id: {}", op_id)).unwrap(); let exception = v8::Exception::type_error(scope, message); - scope.isolate().throw_exception(exception); + scope.throw_exception(exception); return None; }; @@ -542,13 +523,13 @@ impl CoreIsolateState { } fn async_op_response<'s>( - scope: &mut impl v8::ToLocal<'s>, + scope: &mut v8::HandleScope<'s>, maybe_buf: Option<(OpId, Box<[u8]>)>, ) -> Result<(), ErrBox> { - let context = scope.get_current_context().unwrap(); + let context = scope.get_current_context(); let global: v8::Local<v8::Value> = context.global(scope).into(); - let state_rc = CoreIsolate::state(scope.isolate()); + let state_rc = CoreIsolate::state(scope); let state = state_rc.borrow_mut(); let js_recv_cb = state @@ -557,35 +538,31 @@ fn async_op_response<'s>( .expect("Deno.core.recv has not been called."); drop(state); - // TODO(piscisaureus): properly integrate TryCatch in the scope chain. - let mut try_catch = v8::TryCatch::new(scope); - let tc = try_catch.enter(); + let tc_scope = &mut v8::TryCatch::new(scope); match maybe_buf { Some((op_id, buf)) => { let op_id: v8::Local<v8::Value> = - v8::Integer::new(scope, op_id as i32).into(); + v8::Integer::new(tc_scope, op_id as i32).into(); let ui8: v8::Local<v8::Value> = - bindings::boxed_slice_to_uint8array(scope, buf).into(); - js_recv_cb.call(scope, context, global, &[op_id, ui8]) + bindings::boxed_slice_to_uint8array(tc_scope, buf).into(); + js_recv_cb.call(tc_scope, global, &[op_id, ui8]) } - None => js_recv_cb.call(scope, context, global, &[]), + None => js_recv_cb.call(tc_scope, global, &[]), }; - match tc.exception(scope) { + match tc_scope.exception() { None => Ok(()), - Some(exception) => exception_to_err_result(scope, exception), + Some(exception) => exception_to_err_result(tc_scope, exception), } } -fn drain_macrotasks<'s>( - scope: &mut impl v8::ToLocal<'s>, -) -> Result<(), ErrBox> { - let context = scope.get_current_context().unwrap(); +fn drain_macrotasks<'s>(scope: &mut v8::HandleScope<'s>) -> Result<(), ErrBox> { + let context = scope.get_current_context(); let global: v8::Local<v8::Value> = context.global(scope).into(); let js_macrotask_cb = { - let state_rc = CoreIsolate::state(scope.isolate()); + let state_rc = CoreIsolate::state(scope); let state = state_rc.borrow_mut(); state.js_macrotask_cb.get(scope) }; @@ -598,13 +575,12 @@ fn drain_macrotasks<'s>( // such that ready microtasks would be automatically run before // next macrotask is processed. loop { - let mut try_catch = v8::TryCatch::new(scope); - let tc = try_catch.enter(); + let tc_scope = &mut v8::TryCatch::new(scope); - let is_done = js_macrotask_cb.call(scope, context, global, &[]); + let is_done = js_macrotask_cb.call(tc_scope, global, &[]); - if let Some(exception) = tc.exception(scope) { - return exception_to_err_result(scope, exception); + if let Some(exception) = tc_scope.exception() { + return exception_to_err_result(tc_scope, exception); } let is_done = is_done.unwrap(); @@ -617,15 +593,13 @@ fn drain_macrotasks<'s>( } pub(crate) fn exception_to_err_result<'s, T>( - scope: &mut impl v8::ToLocal<'s>, + scope: &mut v8::HandleScope<'s>, exception: v8::Local<v8::Value>, ) -> Result<T, ErrBox> { // TODO(piscisaureus): in rusty_v8, `is_execution_terminating()` should // also be implemented on `struct Isolate`. - let is_terminating_exception = scope - .isolate() - .thread_safe_handle() - .is_execution_terminating(); + let is_terminating_exception = + scope.thread_safe_handle().is_execution_terminating(); let mut exception = exception; if is_terminating_exception { @@ -633,10 +607,7 @@ pub(crate) fn exception_to_err_result<'s, T>( // exception can be created.. // TODO(piscisaureus): in rusty_v8, `cancel_terminate_execution()` should // also be implemented on `struct Isolate`. - scope - .isolate() - .thread_safe_handle() - .cancel_terminate_execution(); + scope.thread_safe_handle().cancel_terminate_execution(); // Maybe make a new exception object. if exception.is_null_or_undefined() { @@ -647,7 +618,7 @@ pub(crate) fn exception_to_err_result<'s, T>( let js_error = JSError::from_v8_exception(scope, exception); - let state_rc = CoreIsolate::state(scope.isolate()); + let state_rc = CoreIsolate::state(scope); let state = state_rc.borrow(); let js_error = (state.js_error_create_fn)(js_error); @@ -655,16 +626,16 @@ pub(crate) fn exception_to_err_result<'s, T>( // Re-enable exception termination. // TODO(piscisaureus): in rusty_v8, `terminate_execution()` should also // be implemented on `struct Isolate`. - scope.isolate().thread_safe_handle().terminate_execution(); + scope.thread_safe_handle().terminate_execution(); } Err(js_error) } fn check_promise_exceptions<'s>( - scope: &mut impl v8::ToLocal<'s>, + scope: &mut v8::HandleScope<'s>, ) -> Result<(), ErrBox> { - let state_rc = CoreIsolate::state(scope.isolate()); + let state_rc = CoreIsolate::state(scope); let mut state = state_rc.borrow_mut(); if let Some(&key) = state.pending_promise_exceptions.keys().next() { diff --git a/core/errors.rs b/core/errors.rs index bc821b266..27c08fac5 100644 --- a/core/errors.rs +++ b/core/errors.rs @@ -106,13 +106,12 @@ pub struct JSStackFrame { } fn get_property<'a>( - scope: &mut impl v8::ToLocal<'a>, - context: v8::Local<v8::Context>, + scope: &mut v8::HandleScope<'a>, object: v8::Local<v8::Object>, key: &str, ) -> Option<v8::Local<'a, v8::Value>> { let key = v8::String::new(scope, key).unwrap(); - object.get(scope, context, key.into()) + object.get(scope, key.into()) } impl JSError { @@ -121,14 +120,12 @@ impl JSError { } pub fn from_v8_exception( - scope: &mut impl v8::InIsolate, + scope: &mut v8::HandleScope, exception: v8::Local<v8::Value>, ) -> Self { // Create a new HandleScope because we're creating a lot of new local // handles below. - let mut hs = v8::HandleScope::new(scope); - let scope = hs.enter(); - let context = { scope.get_current_context().unwrap() }; + let scope = &mut v8::HandleScope::new(scope); let msg = v8::Exception::create_message(scope, exception); @@ -138,11 +135,11 @@ impl JSError { exception.clone().try_into().unwrap(); // Get the message by formatting error.name and error.message. - let name = get_property(scope, context, exception, "name") + let name = get_property(scope, exception, "name") .and_then(|m| m.to_string(scope)) .map(|s| s.to_rust_string_lossy(scope)) .unwrap_or_else(|| "undefined".to_string()); - let message_prop = get_property(scope, context, exception, "message") + let message_prop = get_property(scope, exception, "message") .and_then(|m| m.to_string(scope)) .map(|s| s.to_rust_string_lossy(scope)) .unwrap_or_else(|| "undefined".to_string()); @@ -150,17 +147,16 @@ impl JSError { // Access error.stack to ensure that prepareStackTrace() has been called. // This should populate error.__callSiteEvals and error.__formattedFrames. - let _ = get_property(scope, context, exception, "stack"); + let _ = get_property(scope, exception, "stack"); // Read an array of structured frames from error.__callSiteEvals. - let frames_v8 = - get_property(scope, context, exception, "__callSiteEvals"); + let frames_v8 = get_property(scope, exception, "__callSiteEvals"); let frames_v8: Option<v8::Local<v8::Array>> = frames_v8.and_then(|a| a.try_into().ok()); // Read an array of pre-formatted frames from error.__formattedFrames. let formatted_frames_v8 = - get_property(scope, context, exception, "__formattedFrames"); + get_property(scope, exception, "__formattedFrames"); let formatted_frames_v8: Option<v8::Local<v8::Array>> = formatted_frames_v8.and_then(|a| a.try_into().ok()); @@ -171,92 +167,89 @@ impl JSError { (frames_v8, formatted_frames_v8) { for i in 0..frames_v8.length() { - let call_site: v8::Local<v8::Object> = frames_v8 - .get_index(scope, context, i) - .unwrap() - .try_into() - .unwrap(); + let call_site: v8::Local<v8::Object> = + frames_v8.get_index(scope, i).unwrap().try_into().unwrap(); let type_name: Option<v8::Local<v8::String>> = - get_property(scope, context, call_site, "typeName") + get_property(scope, call_site, "typeName") .unwrap() .try_into() .ok(); let type_name = type_name.map(|s| s.to_rust_string_lossy(scope)); let function_name: Option<v8::Local<v8::String>> = - get_property(scope, context, call_site, "functionName") + get_property(scope, call_site, "functionName") .unwrap() .try_into() .ok(); let function_name = function_name.map(|s| s.to_rust_string_lossy(scope)); let method_name: Option<v8::Local<v8::String>> = - get_property(scope, context, call_site, "methodName") + get_property(scope, call_site, "methodName") .unwrap() .try_into() .ok(); let method_name = method_name.map(|s| s.to_rust_string_lossy(scope)); let file_name: Option<v8::Local<v8::String>> = - get_property(scope, context, call_site, "fileName") + get_property(scope, call_site, "fileName") .unwrap() .try_into() .ok(); let file_name = file_name.map(|s| s.to_rust_string_lossy(scope)); let line_number: Option<v8::Local<v8::Integer>> = - get_property(scope, context, call_site, "lineNumber") + get_property(scope, call_site, "lineNumber") .unwrap() .try_into() .ok(); let line_number = line_number.map(|n| n.value()); let column_number: Option<v8::Local<v8::Integer>> = - get_property(scope, context, call_site, "columnNumber") + get_property(scope, call_site, "columnNumber") .unwrap() .try_into() .ok(); let column_number = column_number.map(|n| n.value()); let eval_origin: Option<v8::Local<v8::String>> = - get_property(scope, context, call_site, "evalOrigin") + get_property(scope, call_site, "evalOrigin") .unwrap() .try_into() .ok(); let eval_origin = eval_origin.map(|s| s.to_rust_string_lossy(scope)); let is_top_level: Option<v8::Local<v8::Boolean>> = - get_property(scope, context, call_site, "isTopLevel") + get_property(scope, call_site, "isTopLevel") .unwrap() .try_into() .ok(); let is_top_level = is_top_level.map(|b| b.is_true()); let is_eval: v8::Local<v8::Boolean> = - get_property(scope, context, call_site, "isEval") + get_property(scope, call_site, "isEval") .unwrap() .try_into() .unwrap(); let is_eval = is_eval.is_true(); let is_native: v8::Local<v8::Boolean> = - get_property(scope, context, call_site, "isNative") + get_property(scope, call_site, "isNative") .unwrap() .try_into() .unwrap(); let is_native = is_native.is_true(); let is_constructor: v8::Local<v8::Boolean> = - get_property(scope, context, call_site, "isConstructor") + get_property(scope, call_site, "isConstructor") .unwrap() .try_into() .unwrap(); let is_constructor = is_constructor.is_true(); let is_async: v8::Local<v8::Boolean> = - get_property(scope, context, call_site, "isAsync") + get_property(scope, call_site, "isAsync") .unwrap() .try_into() .unwrap(); let is_async = is_async.is_true(); let is_promise_all: v8::Local<v8::Boolean> = - get_property(scope, context, call_site, "isPromiseAll") + get_property(scope, call_site, "isPromiseAll") .unwrap() .try_into() .unwrap(); let is_promise_all = is_promise_all.is_true(); let promise_index: Option<v8::Local<v8::Integer>> = - get_property(scope, context, call_site, "columnNumber") + get_property(scope, call_site, "columnNumber") .unwrap() .try_into() .ok(); @@ -278,7 +271,7 @@ impl JSError { promise_index, }); let formatted_frame: v8::Local<v8::String> = formatted_frames_v8 - .get_index(scope, context, i) + .get_index(scope, i) .unwrap() .try_into() .unwrap(); @@ -301,9 +294,9 @@ impl JSError { .and_then(|v| v8::Local::<v8::String>::try_from(v).ok()) .map(|v| v.to_rust_string_lossy(scope)), source_line: msg - .get_source_line(scope, context) + .get_source_line(scope) .map(|v| v.to_rust_string_lossy(scope)), - line_number: msg.get_line_number(context).and_then(|v| v.try_into().ok()), + line_number: msg.get_line_number(scope).and_then(|v| v.try_into().ok()), start_column: msg.get_start_column().try_into().ok(), end_column: msg.get_end_column().try_into().ok(), frames, @@ -365,7 +358,7 @@ impl fmt::Display for JSError { } pub(crate) fn attach_handle_to_error( - scope: &mut impl v8::InIsolate, + scope: &mut v8::Isolate, err: ErrBox, handle: v8::Local<v8::Value>, ) -> ErrBox { @@ -381,7 +374,7 @@ pub struct ErrWithV8Handle { impl ErrWithV8Handle { pub fn new( - scope: &mut impl v8::InIsolate, + scope: &mut v8::Isolate, err: ErrBox, handle: v8::Local<v8::Value>, ) -> Self { diff --git a/core/es_isolate.rs b/core/es_isolate.rs index c778de1cc..5bb1a55c0 100644 --- a/core/es_isolate.rs +++ b/core/es_isolate.rs @@ -111,14 +111,11 @@ impl EsIsolate { source: &str, ) -> Result<ModuleId, ErrBox> { let state_rc = Self::state(self); - let core_state_rc = CoreIsolate::state(self); let core_state = core_state_rc.borrow(); - let mut hs = v8::HandleScope::new(&mut self.0); - let scope = hs.enter(); + let scope = &mut v8::HandleScope::new(&mut *self.0); let context = core_state.global_context.get(scope).unwrap(); - let mut cs = v8::ContextScope::new(scope, context); - let scope = cs.enter(); + let scope = &mut v8::ContextScope::new(scope, context); let name_str = v8::String::new(scope, name).unwrap(); let source_str = v8::String::new(scope, source).unwrap(); @@ -126,15 +123,14 @@ impl EsIsolate { let origin = bindings::module_origin(scope, name_str); let source = v8::script_compiler::Source::new(source_str, &origin); - let mut try_catch = v8::TryCatch::new(scope); - let tc = try_catch.enter(); + let tc_scope = &mut v8::TryCatch::new(scope); - let maybe_module = v8::script_compiler::compile_module(scope, source); + let maybe_module = v8::script_compiler::compile_module(tc_scope, source); - if tc.has_caught() { + if tc_scope.has_caught() { assert!(maybe_module.is_none()); - let e = tc.exception(scope).unwrap(); - return exception_to_err_result(scope, e); + let e = tc_scope.exception().unwrap(); + return exception_to_err_result(tc_scope, e); } let module = maybe_module.unwrap(); @@ -143,7 +139,7 @@ impl EsIsolate { let mut import_specifiers: Vec<ModuleSpecifier> = vec![]; for i in 0..module.get_module_requests_length() { let import_specifier = - module.get_module_request(i).to_rust_string_lossy(scope); + module.get_module_request(i).to_rust_string_lossy(tc_scope); let state = state_rc.borrow(); let module_specifier = state.loader.resolve(&import_specifier, name, false)?; @@ -151,7 +147,7 @@ impl EsIsolate { } let mut handle = v8::Global::<v8::Module>::new(); - handle.set(scope, module); + handle.set(tc_scope, module); { let mut state = state_rc.borrow_mut(); @@ -173,34 +169,30 @@ impl EsIsolate { let core_state_rc = CoreIsolate::state(self); let core_state = core_state_rc.borrow(); - let mut hs = v8::HandleScope::new(&mut self.0); - let scope = hs.enter(); + let scope = &mut v8::HandleScope::new(&mut *self.0); let context = core_state.global_context.get(scope).unwrap(); - let mut cs = v8::ContextScope::new(scope, context); - let scope = cs.enter(); - - let mut try_catch = v8::TryCatch::new(scope); - let tc = try_catch.enter(); + let scope = &mut v8::ContextScope::new(scope, context); + let tc_scope = &mut v8::TryCatch::new(scope); let module_info = match state.modules.get_info(id) { Some(info) => info, None if id == 0 => return Ok(()), _ => panic!("module id {} not found in module table", id), }; - let mut module = module_info.handle.get(scope).unwrap(); + let module = module_info.handle.get(tc_scope).unwrap(); drop(state); if module.get_status() == v8::ModuleStatus::Errored { - exception_to_err_result(scope, module.get_exception())? + exception_to_err_result(tc_scope, module.get_exception())? } let result = - module.instantiate_module(context, bindings::module_resolve_callback); + module.instantiate_module(tc_scope, bindings::module_resolve_callback); match result { Some(_) => Ok(()), None => { - let exception = tc.exception(scope).unwrap(); - exception_to_err_result(scope, exception) + let exception = tc_scope.exception().unwrap(); + exception_to_err_result(tc_scope, exception) } } } @@ -217,14 +209,12 @@ impl EsIsolate { let core_state_rc = CoreIsolate::state(self); - let mut hs = v8::HandleScope::new(&mut self.0); - let scope = hs.enter(); + let scope = &mut v8::HandleScope::new(&mut *self.0); let context = { let core_state = core_state_rc.borrow(); core_state.global_context.get(scope).unwrap() }; - let mut cs = v8::ContextScope::new(scope, context); - let scope = cs.enter(); + let scope = &mut v8::ContextScope::new(scope, context); let info = state.modules.get_info(id).expect("ModuleInfo not found"); let module = info.handle.get(scope).expect("Empty module handle"); @@ -247,7 +237,7 @@ impl EsIsolate { // For more details see: // https://github.com/denoland/deno/issues/4908 // https://v8.dev/features/top-level-await#module-execution-order - let maybe_value = module.evaluate(scope, context); + let maybe_value = module.evaluate(scope); // Update status after evaluating. status = module.get_status(); @@ -293,11 +283,9 @@ impl EsIsolate { let core_state_rc = CoreIsolate::state(self); let core_state = core_state_rc.borrow(); - let mut hs = v8::HandleScope::new(&mut self.0); - let scope = hs.enter(); + let scope = &mut v8::HandleScope::new(&mut *self.0); let context = core_state.global_context.get(scope).unwrap(); - let mut cs = v8::ContextScope::new(scope, context); - let scope = cs.enter(); + let scope = &mut v8::ContextScope::new(scope, context); drop(core_state); @@ -319,8 +307,8 @@ impl EsIsolate { v8::Exception::type_error(scope, message) }); - resolver.reject(context, exception).unwrap(); - scope.isolate().run_microtasks(); + resolver.reject(scope, exception).unwrap(); + scope.run_microtasks(); Ok(()) } @@ -335,14 +323,12 @@ impl EsIsolate { debug!("dyn_import_done {} {:?}", id, mod_id); assert!(mod_id != 0); - let mut hs = v8::HandleScope::new(&mut self.0); - let scope = hs.enter(); + let scope = &mut v8::HandleScope::new(&mut *self.0); let context = { let core_state = core_state_rc.borrow(); core_state.global_context.get(scope).unwrap() }; - let mut cs = v8::ContextScope::new(scope, context); - let scope = cs.enter(); + let scope = &mut v8::ContextScope::new(scope, context); let mut resolver_handle = { let mut state = state_rc.borrow_mut(); @@ -366,8 +352,8 @@ impl EsIsolate { assert_eq!(module.get_status(), v8::ModuleStatus::Evaluated); let module_namespace = module.get_module_namespace(); - resolver.resolve(context, module_namespace).unwrap(); - scope.isolate().run_microtasks(); + resolver.resolve(scope, module_namespace).unwrap(); + scope.run_microtasks(); Ok(()) } diff --git a/core/zero_copy_buf.rs b/core/zero_copy_buf.rs index be61d5f98..a458e12ff 100644 --- a/core/zero_copy_buf.rs +++ b/core/zero_copy_buf.rs @@ -19,7 +19,7 @@ unsafe impl Send for ZeroCopyBuf {} impl ZeroCopyBuf { pub fn new<'s>( - scope: &mut impl v8::ToLocal<'s>, + scope: &mut v8::HandleScope<'s>, view: v8::Local<v8::ArrayBufferView>, ) -> Self { let backing_store = view.buffer(scope).unwrap().get_backing_store(); |