pub struct Context {
pub trace: bool,
/* 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::{Context, object::ObjectInitializer, property::{Attribute, PropertyDescriptor}};
let script = r#"
function test(arg1) {
if(arg1 != null) {
return arg1.x;
}
return 112233;
}
"#;
let mut context = Context::new();
// 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))
Fields
trace: bool
Whether or not to show trace of instructions being ran
Implementations
sourceimpl Context
impl Context
pub fn executor(&mut self) -> &mut Interpreter
sourcepub fn set_strict_mode_off(&mut self)
pub fn set_strict_mode_off(&mut self)
Disable the strict mode.
sourcepub fn set_strict_mode_global(&mut self)
pub fn set_strict_mode_global(&mut self)
Enable the global strict mode.
sourcepub fn construct_object(&self) -> JsObject
pub fn construct_object(&self) -> JsObject
Construct an empty object.
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>(&mut self, message: M) -> JsResult<JsValue>where
M: Into<Box<str>>,
pub fn throw_error<M>(&mut self, message: M) -> JsResult<JsValue>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>(&mut self, message: M) -> JsResult<JsValue>where
M: Into<Box<str>>,
pub fn throw_range_error<M>(&mut self, message: M) -> JsResult<JsValue>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>(&mut self, message: M) -> JsResult<JsValue>where
M: Into<Box<str>>,
pub fn throw_type_error<M>(&mut self, message: M) -> JsResult<JsValue>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>(&mut self, message: M) -> JsResult<JsValue>where
M: Into<Box<str>>,
pub fn throw_reference_error<M>(&mut self, message: M) -> JsResult<JsValue>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>(&mut self, message: M) -> JsResult<JsValue>where
M: Into<Box<str>>,
pub fn throw_syntax_error<M>(&mut self, message: M) -> JsResult<JsValue>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>(&mut self, message: M) -> JsResult<JsValue>where
M: Into<Box<str>>,
pub fn throw_eval_error<M>(&mut self, message: M) -> JsResult<JsValue>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: NativeFunction
) -> JsResult<()>
pub fn register_global_function(
&mut self,
name: &str,
length: usize,
body: NativeFunction
) -> JsResult<()>
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_closure<F>(
&mut self,
name: &str,
length: usize,
body: F
) -> JsResult<()>where
F: Fn(&JsValue, &[JsValue], &mut Context) -> 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 Context) -> 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::{Context, property::{Attribute, PropertyDescriptor}, object::ObjectInitializer};
let mut context = Context::new();
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<T: AsRef<[u8]>>(&mut self, src: T) -> JsResult<JsValue>
pub fn eval<T: AsRef<[u8]>>(&mut self, src: T) -> JsResult<JsValue>
Evaluates the given code.
Examples
let mut context = Context::new();
let value = context.eval("1 + 3").unwrap();
assert!(value.is_number());
assert_eq!(value.as_number().unwrap(), 4.0);
sourcepub fn iterator_prototypes(&self) -> &IteratorPrototypes
pub fn iterator_prototypes(&self) -> &IteratorPrototypes
Return the cached iterator prototypes.
sourcepub fn standard_objects(&self) -> &StandardObjects
pub fn standard_objects(&self) -> &StandardObjects
Return the core standard objects.