mirror of https://github.com/boa-dev/boa.git
Browse Source
* Improve CI testing * Bail on error * cargo fmt * Invert names * Split misc action * Rename some actionspull/3337/head
José Julián Espina
1 year ago
committed by
GitHub
19 changed files with 262 additions and 178 deletions
@ -1,31 +1,31 @@ |
|||||||
// This example shows how to load, parse and execute JS code from a source file
|
// This example shows how to load, parse and execute JS code from a source file
|
||||||
// (./scripts/helloworld.js)
|
// (./scripts/helloworld.js)
|
||||||
|
|
||||||
use std::path::Path; |
use std::{error::Error, path::Path}; |
||||||
|
|
||||||
use boa_engine::{Context, Source}; |
use boa_engine::{js_string, property::Attribute, Context, Source}; |
||||||
|
use boa_runtime::Console; |
||||||
|
|
||||||
fn main() { |
/// 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(js_string!(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 js_file_path = "./scripts/helloworld.js"; |
||||||
|
|
||||||
match Source::from_filepath(Path::new(js_file_path)) { |
let source = Source::from_filepath(Path::new(js_file_path))?; |
||||||
Ok(src) => { |
|
||||||
// Instantiate the execution context
|
// Instantiate the execution context
|
||||||
let mut context = Context::default(); |
let mut context = Context::default(); |
||||||
// Parse the source code
|
// Add the runtime intrisics
|
||||||
match context.eval(src) { |
add_runtime(&mut context); |
||||||
Ok(res) => { |
// Parse the source code and print the result
|
||||||
println!( |
println!("{}", context.eval(source)?.display()); |
||||||
"{}", |
|
||||||
res.to_string(&mut context).unwrap().to_std_string_escaped() |
Ok(()) |
||||||
); |
|
||||||
} |
|
||||||
Err(e) => { |
|
||||||
// Pretty print the error
|
|
||||||
eprintln!("Uncaught {e}"); |
|
||||||
} |
|
||||||
}; |
|
||||||
} |
|
||||||
Err(msg) => eprintln!("Error: {msg}"), |
|
||||||
} |
|
||||||
} |
} |
||||||
|
Loading…
Reference in new issue