Skip to content

Commit

Permalink
Revert realms from deno_core (denoland#16366)
Browse files Browse the repository at this point in the history
This revert has been discussed at length out-of-band (including with
@andreubotella). The realms work in impeding ongoing event loop and
performance work. We very much want to land realms but it needs to wait
until these lower-level refactors are complete. We hope to bring realms
back in a couple weeks.
  • Loading branch information
littledivy authored Oct 21, 2022
1 parent 4b6168d commit c007657
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 454 deletions.
14 changes: 5 additions & 9 deletions core/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::modules::validate_import_assertions;
use crate::modules::ImportAssertionsKind;
use crate::modules::ModuleMap;
use crate::ops::OpCtx;
use crate::JsRealm;
use crate::JsRuntime;
use log::debug;
use std::option::Option;
Expand Down Expand Up @@ -425,15 +424,15 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) {
// SAFETY: `CallbackScope` can be safely constructed from `&PromiseRejectMessage`
let scope = &mut unsafe { v8::CallbackScope::new(&message) };

let realm_state_rc = JsRealm::state_from_scope(scope);
let state_rc = JsRuntime::state(scope);
let mut state = state_rc.borrow_mut();

if let Some(js_promise_reject_cb) =
realm_state_rc.borrow().js_promise_reject_cb.clone()
{
if let Some(js_promise_reject_cb) = state.js_promise_reject_cb.clone() {
let tc_scope = &mut v8::TryCatch::new(scope);
let undefined: v8::Local<v8::Value> = v8::undefined(tc_scope).into();
let type_ = v8::Integer::new(tc_scope, message.get_event() as i32);
let promise = message.get_promise();
drop(state); // Drop borrow, callbacks can call back into runtime.

let reason = match message.get_event() {
PromiseRejectWithNoHandler
Expand All @@ -456,7 +455,6 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) {
};

if has_unhandled_rejection_handler {
let state_rc = JsRuntime::state(tc_scope);
let mut state = state_rc.borrow_mut();
if let Some(pending_mod_evaluate) = state.pending_mod_evaluate.as_mut() {
if !pending_mod_evaluate.has_evaluated {
Expand All @@ -467,8 +465,6 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) {
}
}
} else {
let state_rc = JsRuntime::state(scope);
let mut state = state_rc.borrow_mut();
let promise = message.get_promise();
let promise_global = v8::Global::new(scope, promise);
match message.get_event() {
Expand All @@ -487,7 +483,7 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) {
// Should not warn. See #1272
}
}
};
}
}

/// This binding should be used if there's a custom console implementation
Expand Down
8 changes: 3 additions & 5 deletions core/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

use crate::runtime::GetErrorClassFn;
use crate::runtime::JsRealm;
use crate::runtime::JsRuntime;
use crate::source_map::apply_source_map;
use crate::source_map::get_source_line;
Expand Down Expand Up @@ -98,7 +97,7 @@ pub fn to_v8_error<'a>(
get_class: GetErrorClassFn,
error: &Error,
) -> v8::Local<'a, v8::Value> {
let cb = JsRealm::state_from_scope(scope)
let cb = JsRuntime::state(scope)
.borrow()
.js_build_custom_error_cb
.clone()
Expand Down Expand Up @@ -218,10 +217,10 @@ impl JsError {
let msg = v8::Exception::create_message(scope, exception);

let mut exception_message = None;
let realm_state_rc = JsRealm::state_from_scope(scope);
let state_rc = JsRuntime::state(scope);

let js_format_exception_cb =
realm_state_rc.borrow().js_format_exception_cb.clone();
state_rc.borrow().js_format_exception_cb.clone();
if let Some(format_exception_cb) = js_format_exception_cb {
let format_exception_cb = format_exception_cb.open(scope);
let this = v8::undefined(scope).into();
Expand Down Expand Up @@ -286,7 +285,6 @@ impl JsError {
let mut source_line = None;
let mut source_line_frame_index = None;
{
let state_rc = JsRuntime::state(scope);
let state = &mut *state_rc.borrow_mut();

// When the stack frame array is empty, but the source location given by
Expand Down
40 changes: 15 additions & 25 deletions core/ops_builtin_v8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::ops_builtin::WasmStreamingResource;
use crate::resolve_url_or_path;
use crate::serde_v8::from_v8;
use crate::source_map::apply_source_map as apply_source_map_;
use crate::JsRealm;
use crate::JsRuntime;
use crate::OpDecl;
use crate::ZeroCopyBuf;
Expand Down Expand Up @@ -74,14 +73,14 @@ fn to_v8_local_fn(

#[op(v8)]
fn op_ref_op(scope: &mut v8::HandleScope, promise_id: i32) {
let context_state = JsRealm::state_from_scope(scope);
context_state.borrow_mut().unrefed_ops.remove(&promise_id);
let state_rc = JsRuntime::state(scope);
state_rc.borrow_mut().unrefed_ops.remove(&promise_id);
}

#[op(v8)]
fn op_unref_op(scope: &mut v8::HandleScope, promise_id: i32) {
let context_state = JsRealm::state_from_scope(scope);
context_state.borrow_mut().unrefed_ops.insert(promise_id);
let state_rc = JsRuntime::state(scope);
state_rc.borrow_mut().unrefed_ops.insert(promise_id);
}

#[op(v8)]
Expand Down Expand Up @@ -112,8 +111,8 @@ fn op_set_promise_reject_callback<'a>(
cb: serde_v8::Value,
) -> Result<Option<serde_v8::Value<'a>>, Error> {
let cb = to_v8_fn(scope, cb)?;
let realm_state_rc = JsRealm::state_from_scope(scope);
let old = realm_state_rc.borrow_mut().js_promise_reject_cb.replace(cb);
let state_rc = JsRuntime::state(scope);
let old = state_rc.borrow_mut().js_promise_reject_cb.replace(cb);
let old = old.map(|v| v8::Local::new(scope, v));
Ok(old.map(|v| from_v8(scope, v.into()).unwrap()))
}
Expand Down Expand Up @@ -675,28 +674,22 @@ fn op_set_wasm_streaming_callback(
cb: serde_v8::Value,
) -> Result<(), Error> {
let cb = to_v8_fn(scope, cb)?;
let realm_state_rc = JsRealm::state_from_scope(scope);
let mut realm_state = realm_state_rc.borrow_mut();
let state_rc = JsRuntime::state(scope);
let mut state = state_rc.borrow_mut();
// The callback to pass to the v8 API has to be a unit type, so it can't
// borrow or move any local variables. Therefore, we're storing the JS
// callback in a JsRuntimeState slot.
if realm_state.js_wasm_streaming_cb.is_some() {
if state.js_wasm_streaming_cb.is_some() {
return Err(type_error("op_set_wasm_streaming_callback already called"));
}
realm_state.js_wasm_streaming_cb = Some(cb);
state.js_wasm_streaming_cb = Some(cb);

scope.set_wasm_streaming_callback(|scope, arg, wasm_streaming| {
let (cb_handle, streaming_rid) = {
let realm_state_rc = JsRealm::state_from_scope(scope);
let cb_handle = realm_state_rc
.borrow()
.js_wasm_streaming_cb
.as_ref()
.unwrap()
.clone();
let state_rc = JsRuntime::state(scope);
let streaming_rid = state_rc
.borrow()
let state = state_rc.borrow();
let cb_handle = state.js_wasm_streaming_cb.as_ref().unwrap().clone();
let streaming_rid = state
.op_state
.borrow_mut()
.resource_table
Expand Down Expand Up @@ -829,11 +822,8 @@ fn op_set_format_exception_callback<'a>(
cb: serde_v8::Value<'a>,
) -> Result<Option<serde_v8::Value<'a>>, Error> {
let cb = to_v8_fn(scope, cb)?;
let realm_state_rc = JsRealm::state_from_scope(scope);
let old = realm_state_rc
.borrow_mut()
.js_format_exception_cb
.replace(cb);
let state_rc = JsRuntime::state(scope);
let old = state_rc.borrow_mut().js_format_exception_cb.replace(cb);
let old = old.map(|v| v8::Local::new(scope, v));
Ok(old.map(|v| from_v8(scope, v.into()).unwrap()))
}
Expand Down
Loading

0 comments on commit c007657

Please sign in to comment.