pub struct JsRealm(_);
Expand description
A representation of a JavaScript realm tied to a [JsRuntime
], that allows
execution in the realm’s context.
A JsRealm
instance is a reference to an already existing realm, which
does not hold ownership of it, so instances can be created and dropped as
needed. As such, calling JsRealm::new
doesn’t create a new realm, and
cloning a JsRealm
only creates a new reference. See
[JsRuntime::create_realm
] to create new realms instead.
Despite JsRealm
instances being references, multiple instances that
point to the same realm won’t overlap because every operation requires
passing a mutable reference to the v8::Isolate
. Therefore, no operation
on two JsRealm
instances tied to the same isolate can be run at the same
time, regardless of whether they point to the same realm.
Panics
Every method of JsRealm
will panic if you call it with a reference to a
v8::Isolate
other than the one that corresponds to the current context.
In other words, the v8::Isolate
parameter for all the related JsRealm
methods
must be extracted from the pre-existing [JsRuntime
].
Example usage with the JsRealm::execute_script
method:
use deno_core::JsRuntime;
use deno_core::RuntimeOptions;
let mut runtime = JsRuntime::new(RuntimeOptions::default());
let new_realm = runtime
.create_realm()
.expect("Handle the error properly");
let source_code = "var a = 0; a + 1";
let result = new_realm
.execute_script_static(runtime.v8_isolate(), "<anon>", source_code)
.expect("Handle the error properly");
Lifetime of the realm
As long as the corresponding isolate is alive, a JsRealm
instance will
keep the underlying V8 context alive even if it would have otherwise been
garbage collected.
Implementations§
source§impl JsRealm
impl JsRealm
pub fn new(context: Global<Context>) -> Self
pub fn context(&self) -> &Global<Context>
sourcepub fn handle_scope<'s>(&self, isolate: &'s mut Isolate) -> HandleScope<'s>
pub fn handle_scope<'s>(&self, isolate: &'s mut Isolate) -> HandleScope<'s>
For info on the v8::Isolate
parameter, check JsRealm
.
sourcepub fn global_object<'s>(&self, isolate: &'s mut Isolate) -> Local<'s, Object>
pub fn global_object<'s>(&self, isolate: &'s mut Isolate) -> Local<'s, Object>
For info on the v8::Isolate
parameter, check JsRealm
.
sourcepub fn execute_script_static(
&self,
isolate: &mut Isolate,
name: &'static str,
source_code: &'static str
) -> Result<Global<Value>, Error>
pub fn execute_script_static( &self, isolate: &mut Isolate, name: &'static str, source_code: &'static str ) -> Result<Global<Value>, Error>
Executes traditional JavaScript code (traditional = not ES modules) in the realm’s context.
For info on the v8::Isolate
parameter, check JsRealm
.
The name
parameter can be a filepath or any other string. E.g.:
- “/some/file/path.js”
- “
” - “[native code]”
The same name
value can be used for multiple executions.
Error
can usually be downcast to JsError
.
sourcepub fn execute_script(
&self,
isolate: &mut Isolate,
name: &'static str,
source_code: ModuleCode
) -> Result<Global<Value>, Error>
pub fn execute_script( &self, isolate: &mut Isolate, name: &'static str, source_code: ModuleCode ) -> Result<Global<Value>, Error>
Executes traditional JavaScript code (traditional = not ES modules) in the realm’s context.
For info on the v8::Isolate
parameter, check JsRealm
.
The name
parameter can be a filepath or any other string. E.g.:
- “/some/file/path.js”
- “
” - “[native code]”
The same name
value can be used for multiple executions.
Error
can usually be downcast to JsError
.