deno_core icon indicating copy to clipboard operation
deno_core copied to clipboard

Realms should be constructable from a `v8::HandleScope`

Open andreubotella opened this issue 1 year ago • 0 comments

In order to make the ShadowRealm constructor work, it must be possible to create a new realm in the same way as JsRuntime::create_realm, but with a &mut v8::HandleScope rather than a &mut JsRuntime. This implies:

  • Making JsRuntime::create_realm and the methods it depends on static methods taking a scope or an isolate reference. In the case of create_realm, since it's public, it can stay with the current API, but the implementation would move to JsRuntime::create_realm_from_scope.
  • The extensions, global_template_middlewares, global_object_middlewares, preserve_snapshotted_modules and init_mode fields of JsRuntime must be available from the JsRuntimeState. @mmastrac suggested making a RuntimeExtensionInfo struct with those fields, which would also make things easier for #52.
  • Don't run the entire isolate event loop in JsRuntime::init_extension_js. #99 takes care of this.
  • Currently JsRealm::execute_script and module loading and evaluation use the scope returned by JsRealm::handle_scope. However, rusty_v8 needs each scope to take in a reference to the previous scope in order to keep track of the lifetimes, and JsRealm::handle_scope creates a new root scope. If this scope is created when the stack is not empty, as would happen when constructing a new realm from inside a JS execution context, handle_scope will panic. We would need to somehow make these JsRealm methods accept a parent scope, and use v8::ContextScope to switch into the realm.

andreubotella avatar Jul 28 '23 19:07 andreubotella