diff options
Diffstat (limited to 'core/core_isolate.rs')
-rw-r--r-- | core/core_isolate.rs | 151 |
1 files changed, 61 insertions, 90 deletions
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() { |