Struct Context

Source
pub struct Context {
    pub trace: bool,
    /* private fields */
}
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: bool

Whether or not to show trace of instructions being ran

Implementations§

Source§

impl Context

Source

pub fn new() -> Self

Create a new Context.

Source

pub fn executor(&mut self) -> &mut Interpreter

Source

pub fn strict(&self) -> bool

Returns if strict mode is currently active.

Source

pub fn set_strict_mode_off(&mut self)

Disable the strict mode.

Source

pub fn set_strict_mode_global(&mut self)

Enable the global strict mode.

Source

pub fn construct_object(&self) -> JsObject

Construct an empty object.

Source

pub fn global_object(&self) -> JsObject

Return the global object.

Source

pub fn construct_error<M>(&mut self, message: M) -> JsValue
where M: Into<Box<str>>,

Constructs a Error with the specified message.

Source

pub fn throw_error<M>(&mut self, message: M) -> JsResult<JsValue>
where M: Into<Box<str>>,

Throws a Error with the specified message.

Source

pub fn construct_range_error<M>(&mut self, message: M) -> JsValue
where M: Into<Box<str>>,

Constructs a RangeError with the specified message.

Source

pub fn throw_range_error<M>(&mut self, message: M) -> JsResult<JsValue>
where M: Into<Box<str>>,

Throws a RangeError with the specified message.

Source

pub fn construct_type_error<M>(&mut self, message: M) -> JsValue
where M: Into<Box<str>>,

Constructs a TypeError with the specified message.

Source

pub fn throw_type_error<M>(&mut self, message: M) -> JsResult<JsValue>
where M: Into<Box<str>>,

Throws a TypeError with the specified message.

Source

pub fn construct_reference_error<M>(&mut self, message: M) -> JsValue
where M: Into<Box<str>>,

Constructs a ReferenceError with the specified message.

Source

pub fn throw_reference_error<M>(&mut self, message: M) -> JsResult<JsValue>
where M: Into<Box<str>>,

Throws a ReferenceError with the specified message.

Source

pub fn construct_syntax_error<M>(&mut self, message: M) -> JsValue
where M: Into<Box<str>>,

Constructs a SyntaxError with the specified message.

Source

pub fn throw_syntax_error<M>(&mut self, message: M) -> JsResult<JsValue>
where M: Into<Box<str>>,

Throws a SyntaxError with the specified message.

Source

pub fn construct_eval_error<M>(&mut self, message: M) -> JsValue
where M: Into<Box<str>>,

Constructs a EvalError with the specified message.

Source

pub fn construct_uri_error<M>(&mut self, message: M) -> JsValue
where M: Into<Box<str>>,

Constructs a URIError with the specified message.

Source

pub fn throw_eval_error<M>(&mut self, message: M) -> JsResult<JsValue>
where M: Into<Box<str>>,

Throws a EvalError with the specified message.

Source

pub fn throw_uri_error<M>(&mut self, message: M) -> JsResult<JsValue>
where M: Into<Box<str>>,

Throws a URIError with the specified message.

Source

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.

Source

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.

Source

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>();
Source

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()
);
Source

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);
Source

pub fn iterator_prototypes(&self) -> &IteratorPrototypes

Return the cached iterator prototypes.

Source

pub fn standard_objects(&self) -> &StandardObjects

Return the core standard objects.

Source

pub fn set_trace(&mut self, trace: bool)

Set the value of trace on the context

Trait Implementations§

Source§

impl Debug for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Context

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !Freeze for Context

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V