Embedding V8 in Rust

Written
  • This is based on using deno_core but a lot of it applies directly to using rusty_v8 as well, which maps somewhat to the C++ V8 API.
  • Other references on embedding V8
  • While using rusty_v8, you'll need to use scopes, isolates, locals and globals. This can seem confusing at first, especially when you need to get a local from a global, or when you need a scope and an isolate, but need to take mutable borrows on the engine for both at the same time.
  • HandleScope derefs to Isolate, so you only need to explicitly get the scope from runtime.handle_scope(), and then can use it as an isolate as well.
  • Converting a Global<Value> to a Local<Value>
    • let scope = runtime.handle_scope();
      let local = v8::Local::new(&mut scope, value);
      
    • This is useful with serde_v8, which simplifies a lot of conversion between JS types and Rust types but needs a Local.

Thanks for reading! If you have any questions or comments, please send me a note on Twitter.