Crate boa_engine
source ·Expand description
Boa’s boa_engine
crate implements ECMAScript’s standard library of builtin objects
and an ECMAScript context, bytecompiler, and virtual machine for code execution.
§Example usage
You can find multiple examples of the usage of Boa in the boa_examples
crate. In
order to use Boa in your project, you will need to add the boa_engine
crate to your
Cargo.toml
file. You will need to use a Source
structure to handle the JavaScript code
to execute, and a Context
structure to execute the code:
use boa_engine::{Context, Source};
let js_code = r#"
let two = 1 + 1;
let definitely_not_four = two + "2";
definitely_not_four
"#;
// Instantiate the execution context
let mut context = Context::default();
// 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}");
}
};
§Crate Features
- serde - Enables serialization and deserialization of the AST (Abstract Syntax Tree).
- profiler - Enables profiling with measureme (this is mostly internal).
- intl - Enables
boa
’s ECMA-402 Internationalization API (Intl
object)
§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
boa_cli
- Boa’s CLI && REPL implementationboa_ast
- Boa’s ECMAScript Abstract Syntax Tree.boa_engine
- Boa’s implementation of ECMAScript builtin objects and execution.boa_gc
- Boa’s garbage collector.boa_icu_provider
- Boa’s ICU4X data provider.boa_interner
- Boa’s string interner.boa_macros
- Boa’s macros.boa_parser
- Boa’s lexer and parser.boa_profiler
- Boa’s code profiler.boa_runtime
- Boa’s WebAPI features.boa_string
- Boa’s ECMAScript string implementation.
Re-exports§
pub use crate::bigint::JsBigInt;
pub use crate::context::Context;
pub use crate::error::JsError;
pub use crate::error::JsNativeError;
pub use crate::error::JsNativeErrorKind;
pub use crate::module::Module;
pub use crate::native_function::NativeFunction;
pub use crate::object::JsData;
pub use crate::object::JsObject;
pub use crate::object::NativeObject;
pub use crate::script::Script;
pub use crate::symbol::JsSymbol;
pub use crate::value::JsValue;
pub use boa_ast as ast;
pub use boa_gc as gc;
pub use boa_interner as interner;
pub use boa_parser as parser;
Modules§
- Boa’s implementation of ECMAScript’s bigint primitive type.
- Boa’s ECMAScript built-in object implementations, e.g. Object, String, Math, Array, etc.
- This module contains the bytecode compiler.
- Traits and structs for implementing native classes.
- The ECMAScript context.
- Boa’s implementation of ECMAScript’s
Environment Records
. - Error-related types and conversions.
- Boa’s API to create and customize
ECMAScript
jobs and job queues. - Boa’s implementation of the ECMAScript’s module system.
- Boa’s wrappers for native Rust functions to be compatible with ECMAScript calls.
- Boa’s representation of a JavaScript object and builtin object wrappers
- Implements optimizations.
- A convenience module that re-exports the most commonly-used Boa APIs
- Boa’s implementation of ECMAScript’s Property Descriptor.
- Boa’s implementation of ECMAScript’s
Realm Records
- Boa’s implementation of ECMAScript’s Scripts.
- Boa’s implementation of ECMAScript’s global
Symbol
object. - Boa’s ECMAScript Value implementation.
- Boa’s ECMAScript Virtual Machine
Macros§
- Convert a utf8 string literal to a
JsString
- Utility macro to create a
JsString
.
Structs§
- This represents a
ECMASCript
specification [HostDefined
] field. - This is equivalent to Rust’s
&str
. - A Latin1 or UTF-16–encoded, reference counted, immutable string.
- A source of ECMAScript code.
Traits§
- Substitute for the
Drop
trait for garbage collected types. - A utility trait to make working with function arguments easier.
- The Trace trait, which needs to be implemented on garbage-collected objects.
Type Aliases§
- The result of a Javascript expression is represented like this so it can succeed (
Ok
) or fail (Err
)
Derive Macros§
- Derive the
Finalize
trait. - Derive the
JsData
trait. - Derive the
Trace
trait.