denoland/rusty_v8

Holding &mut HandleScope and Isolate at the same time

graham opened this issue · 2 comments

I realize this is more of a Rust programming question, but the context is specific enough it seems reasonable to ask here.

I have a struct that currently owns the Isolate I am using:

struct Container {
  isolate: v8::OwnedIsolate,
}

However, I don't understand how I can hold a reference to a HandleScope and the isolate at the same time, as a result, this is common at the top of many of my functions:

impl Container {
  pub fn run_some_code(&mut self, src: String) {
    let mut handle_scope: v8::HandleScope<'_, ()> = v8::HandleScope::new(&mut self.isolate); 
    let mut context = v8::Context::new(&mut handle_scope);                                   
    let mut context_scope = v8::ContextScope::new(&mut handle_scope, context);

    // more code here...
  }
}

I'm struggling to understand how (if at all) I can retain the HandleScope beyond the scope of a single function call. I expect this solution to not be Sync, but I'm just trying to figure out how to improve performance by not regenerating a HandleScope each time I run one of my functions.

Thank you!

In general this will be hard to have a reference to both HandleScope and Isolate - the good part is that HandleScope derefs to Isolate so you shouldn't need to have both at the same time.

As for the question of not creating so many HandleScopes - you can create it once and pass it to functions as needed, eg. some examples from deno_core:

https://github.com/denoland/deno_core/blob/8e3e21be672550390da4815eb34da31832c91eac/core/runtime/jsruntime.rs#L479-L485

Understood, thank you for the explanation and links!