1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

refactor(core): Use &mut Isolate as an argument in JsRealm methods (#15093)

Currently almost every `JsRealm` method has a `&mut JsRuntime`
argument. This argument, however, is only used to get the runtime's
corresponding isolate. Given that a mutable reference to the
corresponding `v8::Isolate` can be reached from many more places than
a mutable reference to the `JsRuntime` (for example, by derefing a V8
scope), changing that will make `JsRealm` usable from many more places
than it currently is.
This commit is contained in:
Andreu Botella 2022-07-06 00:45:10 +02:00 committed by GitHub
parent cdba53a73d
commit f0ef15ff07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -479,7 +479,7 @@ impl JsRuntime {
} }
pub fn handle_scope(&mut self) -> v8::HandleScope { pub fn handle_scope(&mut self) -> v8::HandleScope {
self.global_realm().handle_scope(self) self.global_realm().handle_scope(self.v8_isolate())
} }
fn setup_isolate(mut isolate: v8::OwnedIsolate) -> v8::OwnedIsolate { fn setup_isolate(mut isolate: v8::OwnedIsolate) -> v8::OwnedIsolate {
@ -512,7 +512,7 @@ impl JsRuntime {
let js_files = m.init_js(); let js_files = m.init_js();
for (filename, source) in js_files { for (filename, source) in js_files {
// TODO(@AaronO): use JsRuntime::execute_static() here to move src off heap // TODO(@AaronO): use JsRuntime::execute_static() here to move src off heap
realm.execute_script(self, filename, source)?; realm.execute_script(self.v8_isolate(), filename, source)?;
} }
} }
// Restore extensions // Restore extensions
@ -666,7 +666,9 @@ impl JsRuntime {
name: &str, name: &str,
source_code: &str, source_code: &str,
) -> Result<v8::Global<v8::Value>, Error> { ) -> Result<v8::Global<v8::Value>, Error> {
self.global_realm().execute_script(self, name, source_code) self
.global_realm()
.execute_script(self.v8_isolate(), name, source_code)
} }
/// Takes a snapshot. The isolate should have been created with will_snapshot /// Takes a snapshot. The isolate should have been created with will_snapshot
@ -1877,7 +1879,7 @@ impl JsRuntime {
/// # Panics /// # Panics
/// ///
/// Every method of [`JsRealm`] will panic if you call if with a reference to a /// Every method of [`JsRealm`] will panic if you call if with a reference to a
/// [`JsRuntime`] other than the one that corresponds to the current context. /// [`v8::Isolate`] other than the one that corresponds to the current context.
/// ///
/// # Lifetime of the realm /// # Lifetime of the realm
/// ///
@ -1896,16 +1898,16 @@ impl JsRealm {
pub fn handle_scope<'s>( pub fn handle_scope<'s>(
&self, &self,
runtime: &'s mut JsRuntime, isolate: &'s mut v8::Isolate,
) -> v8::HandleScope<'s> { ) -> v8::HandleScope<'s> {
v8::HandleScope::with_context(runtime.v8_isolate(), &self.0) v8::HandleScope::with_context(isolate, &self.0)
} }
pub fn global_object<'s>( pub fn global_object<'s>(
&self, &self,
runtime: &'s mut JsRuntime, isolate: &'s mut v8::Isolate,
) -> v8::Local<'s, v8::Object> { ) -> v8::Local<'s, v8::Object> {
let scope = &mut self.handle_scope(runtime); let scope = &mut self.handle_scope(isolate);
self.0.open(scope).global(scope) self.0.open(scope).global(scope)
} }
@ -1923,11 +1925,11 @@ impl JsRealm {
/// `Error` can usually be downcast to `JsError`. /// `Error` can usually be downcast to `JsError`.
pub fn execute_script( pub fn execute_script(
&self, &self,
runtime: &mut JsRuntime, isolate: &mut v8::Isolate,
name: &str, name: &str,
source_code: &str, source_code: &str,
) -> Result<v8::Global<v8::Value>, Error> { ) -> Result<v8::Global<v8::Value>, Error> {
let scope = &mut self.handle_scope(runtime); let scope = &mut self.handle_scope(isolate);
let source = v8::String::new(scope, source_code).unwrap(); let source = v8::String::new(scope, source_code).unwrap();
let name = v8::String::new(scope, name).unwrap(); let name = v8::String::new(scope, name).unwrap();
@ -3441,11 +3443,12 @@ assertEquals(1, notify_return_value);
let realm = runtime.create_realm().unwrap(); let realm = runtime.create_realm().unwrap();
assert_ne!(realm.context(), &main_context); assert_ne!(realm.context(), &main_context);
assert_ne!(realm.global_object(&mut runtime), main_global); assert_ne!(realm.global_object(runtime.v8_isolate()), main_global);
let main_object = runtime.execute_script("", "Object").unwrap(); let main_object = runtime.execute_script("", "Object").unwrap();
let realm_object = let realm_object = realm
realm.execute_script(&mut runtime, "", "Object").unwrap(); .execute_script(runtime.v8_isolate(), "", "Object")
.unwrap();
assert_ne!(main_object, realm_object); assert_ne!(main_object, realm_object);
} }
@ -3462,10 +3465,10 @@ assertEquals(1, notify_return_value);
}); });
let realm = runtime.create_realm().unwrap(); let realm = runtime.create_realm().unwrap();
let ret = realm let ret = realm
.execute_script(&mut runtime, "", "Deno.core.opSync('op_test')") .execute_script(runtime.v8_isolate(), "", "Deno.core.opSync('op_test')")
.unwrap(); .unwrap();
let scope = &mut realm.handle_scope(&mut runtime); let scope = &mut realm.handle_scope(runtime.v8_isolate());
assert_eq!(ret, serde_v8::to_v8(scope, "Test").unwrap()); assert_eq!(ret, serde_v8::to_v8(scope, "Test").unwrap());
} }
@ -3492,10 +3495,10 @@ assertEquals(1, notify_return_value);
}); });
let realm = runtime.create_realm().unwrap(); let realm = runtime.create_realm().unwrap();
let ret = realm let ret = realm
.execute_script(&mut runtime, "", "Deno.core.opSync('op_test')") .execute_script(runtime.v8_isolate(), "", "Deno.core.opSync('op_test')")
.unwrap(); .unwrap();
let scope = &mut realm.handle_scope(&mut runtime); let scope = &mut realm.handle_scope(runtime.v8_isolate());
assert_eq!(ret, serde_v8::to_v8(scope, "Test").unwrap()); assert_eq!(ret, serde_v8::to_v8(scope, "Test").unwrap());
} }
@ -3528,7 +3531,7 @@ assertEquals(1, notify_return_value);
for realm in [runtime.global_realm(), new_realm].into_iter() { for realm in [runtime.global_realm(), new_realm].into_iter() {
let ret = realm let ret = realm
.execute_script( .execute_script(
&mut runtime, runtime.v8_isolate(),
"", "",
r#" r#"
const buf = Deno.core.opSync("op_test", false); const buf = Deno.core.opSync("op_test", false);