-
-
Notifications
You must be signed in to change notification settings - Fork 630
Expand file tree
/
Copy pathloadfile.rs
More file actions
31 lines (24 loc) · 1007 Bytes
/
loadfile.rs
File metadata and controls
31 lines (24 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// This example shows how to load, parse and execute JS code from a source file
// (./scripts/helloworld.js)
use std::{error::Error, path::Path};
use boa_engine::{Context, Source, property::Attribute};
use boa_runtime::Console;
/// Adds the custom runtime to the context.
fn add_runtime(context: &mut Context) {
// We first add the `console` object, to be able to call `console.log()`.
let console = Console::init(context);
context
.register_global_property(Console::NAME, console, Attribute::all())
.expect("the console builtin shouldn't exist");
}
fn main() -> Result<(), Box<dyn Error>> {
let js_file_path = "./scripts/helloworld.js";
let source = Source::from_filepath(Path::new(js_file_path))?;
// Instantiate the execution context
let mut context = Context::default();
// Add the runtime intrinsics
add_runtime(&mut context);
// Parse the source code and print the result
println!("{}", context.eval(source)?.display());
Ok(())
}