Crate boa_runtime

Source
Expand description

Boa’s boa_runtime crate contains an example runtime and basic runtime features and functionality for the boa_engine crate for runtime implementors.

§Example: Adding Web API’s Console Object

  1. Add boa_runtime as a dependency to your project along with boa_engine.
use boa_engine::{js_string, property::Attribute, Context, Source};
use boa_runtime::Console;

// Create the context.
let mut context = Context::default();

// Initialize the Console object.
let console = Console::init(&mut context);

// Register the console as a global property to the context.
context
    .register_global_property(js_string!(Console::NAME), console, Attribute::all())
    .expect("the console object shouldn't exist yet");

// JavaScript source for parsing.
let js_code = "console.log('Hello World from a JS code string!')";

// Parse the source code
match context.eval(Source::from_bytes(js_code)) {
    Ok(res) => {
        println!(
            "{}",
            res.to_string(&mut context).unwrap().to_std_string_escaped()
        );
    }
    Err(e) => {
        // Pretty print the error
        eprintln!("Uncaught {e}");
    }
};

§About Boa

Boa is an open-source, experimental ECMAScript Engine written in Rust for lexing, parsing and executing ECMAScript/JavaScript. Currently, Boa supports some of the language. More information can be viewed at Boa’s website.

Try out the most recent release with Boa’s live demo playground.

§Boa Crates

Modules§

url
Boa’s implementation of JavaScript’s URL Web API class.

Structs§

Console
This is the internal console object state.
ConsoleState
The current state of the console, passed to the logger backend. This should not be copied or cloned. References are only valid for the current logging call.
RegisterOptions
Options used when registering all built-in objects and functions of the WebAPI runtime.
TextDecoder
The TextDecodermdn class represents an encoder for a specific method, that is a specific character encoding, like utf-8.
TextEncoder
The TextEncodermdn class represents an encoder for a specific method, that is a specific character encoding, like utf-8.

Traits§

Logger
A trait that can be used to forward console logs to an implementation.

Functions§

register
Register all the built-in objects and functions of the WebAPI runtime.