diff options
Diffstat (limited to 'core/core_isolate.rs')
-rw-r--r-- | core/core_isolate.rs | 78 |
1 files changed, 34 insertions, 44 deletions
diff --git a/core/core_isolate.rs b/core/core_isolate.rs index c9e718d2d..74bb2f545 100644 --- a/core/core_isolate.rs +++ b/core/core_isolate.rs @@ -95,10 +95,10 @@ pub struct CoreIsolate { /// embedder slots. pub struct CoreIsolateState { pub resource_table: Rc<RefCell<ResourceTable>>, - pub global_context: v8::Global<v8::Context>, - pub(crate) shared_ab: v8::Global<v8::SharedArrayBuffer>, - pub(crate) js_recv_cb: v8::Global<v8::Function>, - pub(crate) js_macrotask_cb: v8::Global<v8::Function>, + pub global_context: Option<v8::Global<v8::Context>>, + pub(crate) shared_ab: Option<v8::Global<v8::SharedArrayBuffer>>, + pub(crate) js_recv_cb: Option<v8::Global<v8::Function>>, + pub(crate) js_macrotask_cb: Option<v8::Global<v8::Function>>, pub(crate) pending_promise_exceptions: HashMap<i32, v8::Global<v8::Value>>, pub(crate) js_error_create_fn: Box<JSErrorCreateFn>, pub(crate) shared: SharedQueue, @@ -177,7 +177,7 @@ impl CoreIsolate { StartupData::None => (None, None), }; - let mut global_context = v8::Global::<v8::Context>::new(); + let global_context; let (mut isolate, maybe_snapshot_creator) = if will_snapshot { // TODO(ry) Support loading snapshots before snapshotting. assert!(startup_snapshot.is_none()); @@ -188,7 +188,7 @@ impl CoreIsolate { { let scope = &mut v8::HandleScope::new(&mut isolate); let context = bindings::initialize_context(scope); - global_context.set(scope, context); + global_context = v8::Global::new(scope, context); creator.set_default_context(context); } (isolate, Some(creator)) @@ -217,18 +217,18 @@ impl CoreIsolate { // main source code and source maps. bindings::initialize_context(scope) }; - global_context.set(scope, context); + global_context = v8::Global::new(scope, context); } (isolate, None) }; isolate.set_slot(Rc::new(RefCell::new(CoreIsolateState { - global_context, + global_context: Some(global_context), resource_table: Rc::new(RefCell::new(ResourceTable::default())), pending_promise_exceptions: HashMap::new(), - shared_ab: v8::Global::<v8::SharedArrayBuffer>::new(), - js_recv_cb: v8::Global::<v8::Function>::new(), - js_macrotask_cb: v8::Global::<v8::Function>::new(), + shared_ab: None, + js_recv_cb: None, + js_macrotask_cb: None, js_error_create_fn: Box::new(JSError::create), shared: SharedQueue::new(RECOMMENDED_SIZE), pending_ops: FuturesUnordered::new(), @@ -285,9 +285,10 @@ impl CoreIsolate { let state_rc = Self::state(self); let state = state_rc.borrow(); - let scope = &mut v8::HandleScope::new(self.v8_isolate.as_mut().unwrap()); - let context = state.global_context.get(scope).unwrap(); - let scope = &mut v8::ContextScope::new(scope, context); + let scope = &mut v8::HandleScope::with_context( + self.v8_isolate.as_mut().unwrap(), + state.global_context.as_ref().unwrap(), + ); drop(state); @@ -326,13 +327,8 @@ impl CoreIsolate { let state = Self::state(self); // Note: create_blob() method must not be called from within a HandleScope. - // The HandleScope created here is exited at the end of the block. // TODO(piscisaureus): The rusty_v8 type system should enforce this. - { - let v8_isolate = self.v8_isolate.as_mut().unwrap(); - let scope = &mut v8::HandleScope::new(v8_isolate); - state.borrow_mut().global_context.reset(scope); - } + state.borrow_mut().global_context.take(); let snapshot_creator = self.snapshot_creator.as_mut().unwrap(); let snapshot = snapshot_creator @@ -372,12 +368,10 @@ impl Future for CoreIsolate { state.waker.register(cx.waker()); } - let scope = &mut v8::HandleScope::new(&mut **core_isolate); - let context = { - let state = state_rc.borrow(); - state.global_context.get(scope).unwrap() - }; - let scope = &mut v8::ContextScope::new(scope, context); + let scope = &mut v8::HandleScope::with_context( + &mut **core_isolate, + state_rc.borrow().global_context.as_ref().unwrap(), + ); check_promise_exceptions(scope)?; @@ -528,15 +522,12 @@ fn async_op_response<'s>( ) -> Result<(), ErrBox> { let context = scope.get_current_context(); let global: v8::Local<v8::Value> = context.global(scope).into(); - - let state_rc = CoreIsolate::state(scope); - let state = state_rc.borrow_mut(); - - let js_recv_cb = state + let js_recv_cb = CoreIsolate::state(scope) + .borrow() .js_recv_cb - .get(scope) + .as_ref() + .map(|cb| v8::Local::new(scope, cb)) .expect("Deno.core.recv has not been called."); - drop(state); let tc_scope = &mut v8::TryCatch::new(scope); @@ -561,22 +552,21 @@ 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); - let state = state_rc.borrow_mut(); - state.js_macrotask_cb.get(scope) + let js_macrotask_cb = match CoreIsolate::state(scope) + .borrow_mut() + .js_macrotask_cb + .as_ref() + { + Some(cb) => v8::Local::new(scope, cb), + None => return Ok(()), }; - if js_macrotask_cb.is_none() { - return Ok(()); - } - let js_macrotask_cb = js_macrotask_cb.unwrap(); // Repeatedly invoke macrotask callback until it returns true (done), // such that ready microtasks would be automatically run before // next macrotask is processed. - loop { - let tc_scope = &mut v8::TryCatch::new(scope); + let tc_scope = &mut v8::TryCatch::new(scope); + loop { let is_done = js_macrotask_cb.call(tc_scope, global, &[]); if let Some(exception) = tc_scope.exception() { @@ -641,7 +631,7 @@ fn check_promise_exceptions<'s>( if let Some(&key) = state.pending_promise_exceptions.keys().next() { let handle = state.pending_promise_exceptions.remove(&key).unwrap(); drop(state); - let exception = handle.get(scope).expect("empty error handle"); + let exception = v8::Local::new(scope, handle); exception_to_err_result(scope, exception) } else { Ok(()) |