8 releases (4 breaking)
Uses new Rust 2024
| 0.21.0 | Oct 21, 2025 |
|---|---|
| 0.20.0 | Dec 5, 2024 |
| 0.19.1 | Sep 12, 2024 |
| 0.19.0 | Jul 11, 2024 |
| 0.17.0 | Jul 8, 2023 |
#765 in Web programming
1,382 downloads per month
Used in 6 crates
(4 directly)
5.5MB
97K
SLoC
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
- 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;
use boa_runtime::console::DefaultLogger;
// Create the context.
let mut context = Context::default();
// Register the Console object to the context. The DefaultLogger simply
// write errors to STDERR and all other logs to STDOUT.
Console::register_with_logger(DefaultLogger, &mut context)
.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}");
# panic!("An error occured in boa_runtime's js_code");
}
};
Example: Add all supported Boa's Runtime Web API to your context
use boa_engine::{js_string, property::Attribute, Context, Source};
// Create the context.
let mut context = Context::default();
// Register all objects in the context. To conditionally register extensions,
// call `register()` directly on the extension.
boa_runtime::register(
(
// Register the default logger.
boa_runtime::extensions::ConsoleExtension::default(),
// A fetcher can be added if the `fetch` feature flag is enabled.
// This fetcher uses the Reqwest blocking API to allow fetching using HTTP.
boa_runtime::extensions::FetchExtension(
boa_runtime::fetch::BlockingReqwestFetcher::default()
),
),
None,
&mut context,
);
// JavaScript source for parsing.
let js_code = r#"
fetch("https://google.com/")
.then(response => response.text())
.then(html => console.log(html))
"#;
// Parse the source code
match context.eval(Source::from_bytes(js_code)) {
Ok(res) => {
// The result is a promise, so we need to await it.
res
.as_promise()
.expect("Should be a promise")
.await_blocking(&mut context)
.expect("Should resolve()");
println!(
"{}",
res.to_string(&mut context).unwrap().to_std_string_escaped()
);
}
Err(e) => {
// Pretty print the error
eprintln!("Uncaught {e}");
# panic!("An error occured in boa_runtime's js_code");
}
};
Dependencies
~14–49MB
~715K SLoC