Struct boa::context::Context [−][src]
pub struct Context {
pub trace: bool,
// some fields omitted
}Expand description
Javascript context. It is the primary way to interact with the runtime.
Contexts 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: boolWhether or not to show trace of instructions being ran
Implementations
Disable the strict mode.
Enable the global strict mode.
Construct an empty object.
Return the global object.
Constructs a Error with the specified message.
Throws a Error with the specified message.
Constructs a RangeError with the specified message.
Throws a RangeError with the specified message.
Constructs a TypeError with the specified message.
Throws a TypeError with the specified message.
Constructs a ReferenceError with the specified message.
Throws a ReferenceError with the specified message.
Constructs a SyntaxError with the specified message.
Throws a SyntaxError with the specified message.
Constructs a EvalError with the specified message.
Constructs a URIError with the specified message.
Throws a EvalError with the specified message.
Throws a URIError with the specified message.
pub 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.
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.
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>();pub 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()
);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);Return the cached iterator prototypes.
Return the core standard objects.