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
12 months 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
|
||||
// (./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"; |
||||
|
||||
match Source::from_filepath(Path::new(js_file_path)) { |
||||
Ok(src) => { |
||||
// Instantiate the execution context
|
||||
let mut context = Context::default(); |
||||
// Parse the source code
|
||||
match context.eval(src) { |
||||
Ok(res) => { |
||||
println!( |
||||
"{}", |
||||
res.to_string(&mut context).unwrap().to_std_string_escaped() |
||||
); |
||||
} |
||||
Err(e) => { |
||||
// Pretty print the error
|
||||
eprintln!("Uncaught {e}"); |
||||
} |
||||
}; |
||||
} |
||||
Err(msg) => eprintln!("Error: {msg}"), |
||||
} |
||||
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(()) |
||||
} |
||||
|
Loading…
Reference in new issue