logo
pub struct Context { /* 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_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

Create a new Context.

Gets the string interner.

Gets a mutable reference to the string interner.

Returns if strict mode is currently active.

Set the global strict mode of the context.

Constructs an object with the %Object.prototype% prototype.

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.

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 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.

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

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

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

Compile the AST into a CodeBlock ready to be executed by the VM.

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.

Return the intrinsic constructors and objects.

Set the value of trace on the context

Trait Implementations

Formats the value using the given formatter. Read more

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Converts self into T using Into<T>. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Pipes by value. This is generally the method you want to use. Read more

Borrows self and passes that borrow into the pipe function. Read more

Mutably borrows self and passes that borrow into the pipe function. Read more

Borrows self, then passes self.borrow() into the pipe function. Read more

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more

Borrows self, then passes self.as_ref() into the pipe function.

Mutably borrows self, then passes self.as_mut() into the pipe function. Read more

Borrows self, then passes self.deref() into the pipe function.

Mutably borrows self, then passes self.deref_mut() into the pipe function. Read more

Immutable access to a value. Read more

Mutable access to a value. Read more

Immutable access to the Borrow<B> of a value. Read more

Mutable access to the BorrowMut<B> of a value. Read more

Immutable access to the AsRef<R> view of a value. Read more

Mutable access to the AsMut<R> view of a value. Read more

Immutable access to the Deref::Target of a value. Read more

Mutable access to the Deref::Target of a value. Read more

Calls .tap() only in debug builds, and is erased in release builds.

Calls .tap_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_borrow() only in debug builds, and is erased in release builds. Read more

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_ref() only in debug builds, and is erased in release builds. Read more

Calls .tap_ref_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_deref() only in debug builds, and is erased in release builds. Read more

Calls .tap_deref_mut() only in debug builds, and is erased in release builds. Read more

Attempts to convert self into T using TryInto<T>. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.