mirror of https://github.com/boa-dev/boa.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
1.0 KiB
32 lines
1.0 KiB
3 years ago
|
// This example shows how to load, parse and execute JS code from a source file
|
||
|
// (./scripts/helloworld.js)
|
||
|
|
||
1 year ago
|
use std::{error::Error, path::Path};
|
||
3 years ago
|
|
||
1 year ago
|
use boa_engine::{js_string, property::Attribute, Context, Source};
|
||
|
use boa_runtime::Console;
|
||
3 years ago
|
|
||
1 year ago
|
/// Adds the custom runtime to the context.
|
||
1 year ago
|
fn add_runtime(context: &mut Context) {
|
||
1 year ago
|
// We first add the `console` object, to be able to call `console.log()`.
|
||
|
let console = Console::init(context);
|
||
|
context
|
||
|
.register_global_property(js_string!(Console::NAME), console, Attribute::all())
|
||
|
.expect("the console builtin shouldn't exist");
|
||
|
}
|
||
|
|
||
|
fn main() -> Result<(), Box<dyn Error>> {
|
||
3 years ago
|
let js_file_path = "./scripts/helloworld.js";
|
||
|
|
||
1 year ago
|
let source = Source::from_filepath(Path::new(js_file_path))?;
|
||
|
|
||
|
// Instantiate the execution context
|
||
|
let mut context = Context::default();
|
||
|
// Add the runtime intrisics
|
||
|
add_runtime(&mut context);
|
||
|
// Parse the source code and print the result
|
||
|
println!("{}", context.eval(source)?.display());
|
||
|
|
||
|
Ok(())
|
||
3 years ago
|
}
|