Struct boa_engine::Context
source · [−]pub struct Context { /* private fields */ }
Expand description
Javascript 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::{
Context,
object::ObjectInitializer,
property::{Attribute, PropertyDescriptor}
};
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(script).unwrap();
// Create an object that can be used in eval calls.
let arg = ObjectInitializer::new(&mut context)
.property("x", 12, Attribute::READONLY)
.build();
context.register_global_property(
"arg",
arg,
Attribute::all()
);
let value = context.eval("test(arg)").unwrap();
assert_eq!(value.as_number(), Some(12.0))
Implementations
sourceimpl 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 interner_mut(&mut self) -> &mut Interner
pub fn interner_mut(&mut self) -> &mut Interner
Gets a mutable reference to the string interner.
sourcepub fn construct_object(&self) -> JsObject
pub fn construct_object(&self) -> JsObject
Constructs an object with the %Object.prototype%
prototype.
sourcepub fn parse<S>(&mut self, src: S) -> Result<StatementList, ParseError>where
S: AsRef<[u8]>,
pub fn parse<S>(&mut self, src: S) -> Result<StatementList, ParseError>where
S: AsRef<[u8]>,
Parse the given source text.
sourcepub fn global_object(&self) -> &JsObject
pub fn global_object(&self) -> &JsObject
Return the global object.
sourcepub fn construct_error<M>(&mut self, message: M) -> JsValuewhere
M: Into<Box<str>>,
pub fn construct_error<M>(&mut self, message: M) -> JsValuewhere
M: Into<Box<str>>,
Constructs a Error
with the specified message.
sourcepub fn throw_error<M, R>(&mut self, message: M) -> JsResult<R>where
M: Into<Box<str>>,
pub fn throw_error<M, R>(&mut self, message: M) -> JsResult<R>where
M: Into<Box<str>>,
Throws a Error
with the specified message.
sourcepub fn construct_range_error<M>(&mut self, message: M) -> JsValuewhere
M: Into<Box<str>>,
pub fn construct_range_error<M>(&mut self, message: M) -> JsValuewhere
M: Into<Box<str>>,
Constructs a RangeError
with the specified message.
sourcepub fn throw_range_error<M, R>(&mut self, message: M) -> JsResult<R>where
M: Into<Box<str>>,
pub fn throw_range_error<M, R>(&mut self, message: M) -> JsResult<R>where
M: Into<Box<str>>,
Throws a RangeError
with the specified message.
sourcepub fn construct_type_error<M>(&mut self, message: M) -> JsValuewhere
M: Into<Box<str>>,
pub fn construct_type_error<M>(&mut self, message: M) -> JsValuewhere
M: Into<Box<str>>,
Constructs a TypeError
with the specified message.
sourcepub fn throw_type_error<M, R>(&mut self, message: M) -> JsResult<R>where
M: Into<Box<str>>,
pub fn throw_type_error<M, R>(&mut self, message: M) -> JsResult<R>where
M: Into<Box<str>>,
Throws a TypeError
with the specified message.
sourcepub fn construct_reference_error<M>(&mut self, message: M) -> JsValuewhere
M: Into<Box<str>>,
pub fn construct_reference_error<M>(&mut self, message: M) -> JsValuewhere
M: Into<Box<str>>,
Constructs a ReferenceError
with the specified message.
sourcepub fn throw_reference_error<M, R>(&mut self, message: M) -> JsResult<R>where
M: Into<Box<str>>,
pub fn throw_reference_error<M, R>(&mut self, message: M) -> JsResult<R>where
M: Into<Box<str>>,
Throws a ReferenceError
with the specified message.
sourcepub fn construct_syntax_error<M>(&mut self, message: M) -> JsValuewhere
M: Into<Box<str>>,
pub fn construct_syntax_error<M>(&mut self, message: M) -> JsValuewhere
M: Into<Box<str>>,
Constructs a SyntaxError
with the specified message.
sourcepub fn throw_syntax_error<M, R>(&mut self, message: M) -> JsResult<R>where
M: Into<Box<str>>,
pub fn throw_syntax_error<M, R>(&mut self, message: M) -> JsResult<R>where
M: Into<Box<str>>,
Throws a SyntaxError
with the specified message.
sourcepub fn construct_eval_error<M>(&mut self, message: M) -> JsValuewhere
M: Into<Box<str>>,
pub fn construct_eval_error<M>(&mut self, message: M) -> JsValuewhere
M: Into<Box<str>>,
Constructs a EvalError
with the specified message.
sourcepub fn construct_uri_error<M>(&mut self, message: M) -> JsValuewhere
M: Into<Box<str>>,
pub fn construct_uri_error<M>(&mut self, message: M) -> JsValuewhere
M: Into<Box<str>>,
Constructs a URIError
with the specified message.
sourcepub fn throw_eval_error<M, R>(&mut self, message: M) -> JsResult<R>where
M: Into<Box<str>>,
pub fn throw_eval_error<M, R>(&mut self, message: M) -> JsResult<R>where
M: Into<Box<str>>,
Throws a EvalError
with the specified message.
sourcepub fn throw_uri_error<M>(&mut self, message: M) -> JsResult<JsValue>where
M: Into<Box<str>>,
pub fn throw_uri_error<M>(&mut self, message: M) -> JsResult<JsValue>where
M: Into<Box<str>>,
Throws a URIError
with the specified message.
sourcepub fn register_global_function(
&mut self,
name: &str,
length: usize,
body: NativeFunctionSignature
)
pub fn register_global_function(
&mut self,
name: &str,
length: usize,
body: NativeFunctionSignature
)
Register a global native function.
This is more efficient that creating a closure function, since this does not allocate, it is just a function pointer.
The function will be both constructable
(call with new
).
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 want to make a function only constructable
, or wish to bind it differently
to the global object, you can create the function object with
FunctionBuilder
. And bind it to the global
object with Context::register_global_property
method.
sourcepub fn register_global_builtin_function(
&mut self,
name: &str,
length: usize,
body: NativeFunctionSignature
)
pub fn register_global_builtin_function(
&mut self,
name: &str,
length: usize,
body: NativeFunctionSignature
)
Register a global native function that is not a constructor.
This is more efficient that creating a closure function, since this does not allocate, it is just a function pointer.
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_function
is,
that the function will not be constructable
.
Usage of the function as a constructor will produce a TypeError
.
sourcepub fn register_global_closure<F>(
&mut self,
name: &str,
length: usize,
body: F
) -> JsResult<()>where
F: Fn(&JsValue, &[JsValue], &mut Self) -> JsResult<JsValue> + Copy + 'static,
pub fn register_global_closure<F>(
&mut self,
name: &str,
length: usize,
body: F
) -> JsResult<()>where
F: Fn(&JsValue, &[JsValue], &mut Self) -> JsResult<JsValue> + Copy + 'static,
Register a global closure function.
The function will be both constructable
(call with new
).
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 #1
If you want to make a function only constructable
, or wish to bind it differently
to the global object, you can create the function object with
FunctionBuilder
. And bind it to the global
object with Context::register_global_property
method.
Note #2
This function will only accept Copy
closures, meaning you cannot
move Clone
types, just Copy
types. If you need to move Clone
types
as captures, see FunctionBuilder::closure_with_captures
.
See https://github.com/boa-dev/boa/issues/1515 for an explanation on why we need to restrict the set of accepted closures.
sourcepub fn register_global_class<T>(&mut self) -> JsResult<()>where
T: Class,
pub fn register_global_class<T>(&mut self) -> JsResult<()>where
T: Class,
Register a global class of type T
, where T
implements Class
.
Example
#[derive(Debug, Trace, Finalize)]
struct MyClass;
impl Class for MyClass {
// ...
}
context.register_global_class::<MyClass>();
sourcepub fn register_global_property<K, V>(
&mut self,
key: K,
value: V,
attribute: Attribute
)where
K: Into<PropertyKey>,
V: Into<JsValue>,
pub fn register_global_property<K, V>(
&mut self,
key: K,
value: V,
attribute: Attribute
)where
K: Into<PropertyKey>,
V: Into<JsValue>,
Register a global property.
Example
use boa_engine::{
Context,
property::{Attribute, PropertyDescriptor},
object::ObjectInitializer
};
let mut context = Context::default();
context.register_global_property(
"myPrimitiveProperty",
10,
Attribute::all()
);
let object = ObjectInitializer::new(&mut context)
.property(
"x",
0,
Attribute::all()
)
.property(
"y",
1,
Attribute::all()
)
.build();
context.register_global_property(
"myObjectProperty",
object,
Attribute::all()
);
sourcepub fn eval<S>(&mut self, src: S) -> JsResult<JsValue>where
S: AsRef<[u8]>,
pub fn eval<S>(&mut self, src: S) -> JsResult<JsValue>where
S: AsRef<[u8]>,
Evaluates the given code by compiling down to bytecode, then interpreting the bytecode into a value
Examples
let mut context = Context::default();
let value = context.eval("1 + 3").unwrap();
assert!(value.is_number());
assert_eq!(value.as_number().unwrap(), 4.0);
sourcepub fn compile(
&mut self,
statement_list: &StatementList
) -> JsResult<Gc<CodeBlock>>
pub fn compile(
&mut self,
statement_list: &StatementList
) -> JsResult<Gc<CodeBlock>>
Compile the AST into a CodeBlock
ready to be executed by the VM.
sourcepub fn execute(&mut self, code_block: Gc<CodeBlock>) -> JsResult<JsValue>
pub fn execute(&mut self, code_block: Gc<CodeBlock>) -> JsResult<JsValue>
Call the VM with a CodeBlock
and return the result.
Since this function receives a Gc<CodeBlock>
, cloning the code is very cheap, since it’s
just a pointer copy. Therefore, if you’d like to execute the same CodeBlock
multiple
times, there is no need to re-compile it, and you can just call clone()
on the
Gc<CodeBlock>
returned by the Self::compile()
function.
sourcepub fn intrinsics(&self) -> &Intrinsics
pub fn intrinsics(&self) -> &Intrinsics
Return the intrinsic constructors and objects.
sourcepub fn host_enqueue_promise_job(&mut self, job: JobCallback)
pub fn host_enqueue_promise_job(&mut self, job: JobCallback)
More information:
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
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceimpl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
sourcefn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
sourcefn 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 moresourcefn 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 moresourcefn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
sourcefn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
sourcefn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
self
, then passes self.as_ref()
into the pipe function.sourcefn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
sourceimpl<T> Tap for T
impl<T> Tap for T
sourcefn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
Borrow<B>
of a value. Read moresourcefn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
BorrowMut<B>
of a value. Read moresourcefn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
AsRef<R>
view of a value. Read moresourcefn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
AsMut<R>
view of a value. Read moresourcefn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
Deref::Target
of a value. Read moresourcefn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
Deref::Target
of a value. Read moresourcefn 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.sourcefn 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. Read moresourcefn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
.tap_borrow()
only in debug builds, and is erased in release
builds. Read moresourcefn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
.tap_borrow_mut()
only in debug builds, and is erased in release
builds. Read moresourcefn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
.tap_ref()
only in debug builds, and is erased in release
builds. Read moresourcefn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
.tap_ref_mut()
only in debug builds, and is erased in release
builds. Read more