Struct boa_engine::context::Context
source · pub struct Context { /* private fields */ }
Expand description
ECMAScript context. It is the primary way to interact with the runtime.
Context
s constructed in a thread share the same runtime, therefore it
is possible to share objects from one context to another context, but they
have to be in the same thread.
§Examples
§Execute Function of Script File
use boa_engine::{
js_string,
object::ObjectInitializer,
property::{Attribute, PropertyDescriptor},
Context, Source,
};
let script = r#"
function test(arg1) {
if(arg1 != null) {
return arg1.x;
}
return 112233;
}
"#;
let mut context = Context::default();
// Populate the script definition to the context.
context.eval(Source::from_bytes(script)).unwrap();
// Create an object that can be used in eval calls.
let arg = ObjectInitializer::new(&mut context)
.property(js_string!("x"), 12, Attribute::READONLY)
.build();
context
.register_global_property(js_string!("arg"), arg, Attribute::all())
.expect("property shouldn't exist");
let value = context.eval(Source::from_bytes("test(arg)")).unwrap();
assert_eq!(value.as_number(), Some(12.0))
Implementations§
source§impl Context
impl Context
sourcepub fn builder() -> ContextBuilder
pub fn builder() -> ContextBuilder
Create a new ContextBuilder
to specify the Interner
and/or
the icu data provider.
sourcepub fn eval<R: ReadChar>(&mut self, src: Source<'_, R>) -> JsResult<JsValue>
pub fn eval<R: ReadChar>(&mut self, src: Source<'_, R>) -> JsResult<JsValue>
Evaluates the given source by compiling down to bytecode, then interpreting the bytecode into a value.
§Examples
let mut context = Context::default();
let source = Source::from_bytes("1 + 3");
let value = context.eval(source).unwrap();
assert!(value.is_number());
assert_eq!(value.as_number().unwrap(), 4.0);
Note that this won’t run any scheduled promise jobs; you need to call Context::run_jobs
on the context or JobQueue::run_jobs
on the provided queue to run them.
sourcepub fn optimize_statement_list(
&mut self,
statement_list: &mut StatementList
) -> OptimizerStatistics
pub fn optimize_statement_list( &mut self, statement_list: &mut StatementList ) -> OptimizerStatistics
Applies optimizations to the StatementList
inplace.
sourcepub fn register_global_property<K, V>(
&mut self,
key: K,
value: V,
attribute: Attribute
) -> JsResult<()>
pub fn register_global_property<K, V>( &mut self, key: K, value: V, attribute: Attribute ) -> JsResult<()>
Register a global property.
It will return an error if the property is already defined.
§Example
use boa_engine::{
js_string,
object::ObjectInitializer,
property::{Attribute, PropertyDescriptor},
Context,
};
let mut context = Context::default();
context
.register_global_property(
js_string!("myPrimitiveProperty"),
10,
Attribute::all(),
)
.expect("property shouldn't exist");
let object = ObjectInitializer::new(&mut context)
.property(js_string!("x"), 0, Attribute::all())
.property(js_string!("y"), 1, Attribute::all())
.build();
context
.register_global_property(
js_string!("myObjectProperty"),
object,
Attribute::all(),
)
.expect("property shouldn't exist");
sourcepub fn register_global_callable(
&mut self,
name: JsString,
length: usize,
body: NativeFunction
) -> JsResult<()>
pub fn register_global_callable( &mut self, name: JsString, length: usize, body: NativeFunction ) -> JsResult<()>
Register a global native callable.
The function will be both constructable
(call with new <name>()
) and callable
(call
with <name>()
).
The function will be bound to the global object with writable
, non-enumerable
and configurable
attributes. The same as when you create a function in JavaScript.
§Note
If you wish to only create the function object without binding it to the global object, you
can use the FunctionObjectBuilder
API.
sourcepub fn register_global_builtin_callable(
&mut self,
name: JsString,
length: usize,
body: NativeFunction
) -> JsResult<()>
pub fn register_global_builtin_callable( &mut self, name: JsString, length: usize, body: NativeFunction ) -> JsResult<()>
Register a global native function that is not a constructor.
The function will be bound to the global object with writable
, non-enumerable
and configurable
attributes. The same as when you create a function in JavaScript.
§Note
The difference to Context::register_global_callable
is, that the function will not be
constructable
. Usage of the function as a constructor will produce a TypeError
.
sourcepub fn register_global_class<C: Class>(&mut self) -> JsResult<()>
pub fn register_global_class<C: Class>(&mut self) -> JsResult<()>
sourcepub fn unregister_global_class<C: Class>(
&mut self
) -> JsResult<Option<StandardConstructor>>
pub fn unregister_global_class<C: Class>( &mut self ) -> JsResult<Option<StandardConstructor>>
Removes the global class C
from the currently active realm, returning the constructor
and prototype of the class if C
was registered.
§Note
This makes the constructor return an error on further calls, but note that this won’t protect
static properties from being accessed within variables that stored the constructor before being
unregistered. If you need that functionality, you can use a static accessor that first checks
if the class is registered (Context::has_global_class
) before returning the static value.
§Example
#[derive(Debug, Trace, Finalize)]
struct MyClass;
impl Class for MyClass {
// ...
}
context.register_global_class::<MyClass>()?;
// ... code
context.unregister_global_class::<MyClass>()?;
sourcepub fn has_global_class<C: Class>(&self) -> bool
pub fn has_global_class<C: Class>(&self) -> bool
Checks if the currently active realm has the global class C
registered.
sourcepub fn get_global_class<C: Class>(&self) -> Option<StandardConstructor>
pub fn get_global_class<C: Class>(&self) -> Option<StandardConstructor>
Gets the constructor and prototype of the global class C
if the currently active realm has
that class registered.
sourcepub fn interner_mut(&mut self) -> &mut Interner
pub fn interner_mut(&mut self) -> &mut Interner
Gets a mutable reference to the string interner.
sourcepub fn global_object(&self) -> JsObject
pub fn global_object(&self) -> JsObject
Returns the global object.
sourcepub fn intrinsics(&self) -> &Intrinsics
pub fn intrinsics(&self) -> &Intrinsics
Returns the currently active intrinsic constructors and objects.
sourcepub const fn optimizer_options(&self) -> OptimizerOptions
pub const fn optimizer_options(&self) -> OptimizerOptions
Get optimizer options.
sourcepub fn set_optimizer_options(&mut self, optimizer_options: OptimizerOptions)
pub fn set_optimizer_options(&mut self, optimizer_options: OptimizerOptions)
Enable or disable optimizations
sourcepub fn enqueue_job(&mut self, job: NativeJob)
pub fn enqueue_job(&mut self, job: NativeJob)
sourcepub async fn run_jobs_async(&mut self)
pub async fn run_jobs_async(&mut self)
sourcepub fn clear_kept_objects(&mut self)
pub fn clear_kept_objects(&mut self)
Abstract operation ClearKeptObjects
.
Clears all objects maintained alive by calls to the AddToKeptObjects
abstract
operation, used within the WeakRef
constructor.
sourcepub fn stack_trace(&self) -> impl Iterator<Item = &CallFrame>
pub fn stack_trace(&self) -> impl Iterator<Item = &CallFrame>
Retrieves the current stack trace of the context.
sourcepub fn enter_realm(&mut self, realm: Realm) -> Realm
pub fn enter_realm(&mut self, realm: Realm) -> Realm
Replaces the currently active realm with realm
, and returns the old realm.
sourcepub fn create_realm(&mut self) -> JsResult<Realm>
pub fn create_realm(&mut self) -> JsResult<Realm>
Create a new Realm with the default global bindings.
sourcepub const fn root_shape(&self) -> &RootShape
pub const fn root_shape(&self) -> &RootShape
Get the RootShape
.
sourcepub fn host_hooks(&self) -> &'static dyn HostHooks
pub fn host_hooks(&self) -> &'static dyn HostHooks
Gets the host hooks.
sourcepub fn module_loader(&self) -> Rc<dyn ModuleLoader>
pub fn module_loader(&self) -> Rc<dyn ModuleLoader>
Gets the module loader.
sourcepub const fn runtime_limits(&self) -> RuntimeLimits
pub const fn runtime_limits(&self) -> RuntimeLimits
Get the RuntimeLimits
.
sourcepub fn set_runtime_limits(&mut self, runtime_limits: RuntimeLimits)
pub fn set_runtime_limits(&mut self, runtime_limits: RuntimeLimits)
Set the RuntimeLimits
.
sourcepub fn runtime_limits_mut(&mut self) -> &mut RuntimeLimits
pub fn runtime_limits_mut(&mut self) -> &mut RuntimeLimits
Get a mutable reference to the RuntimeLimits
.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Context
impl !Send for Context
impl !Sync for Context
impl Unpin for Context
impl !UnwindSafe for Context
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> R
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.