diff --git a/examples/src/loadfile.rs b/examples/src/loadfile.rs index 6a53302b7cf..73e70fe3ca6 100644 --- a/examples/src/loadfile.rs +++ b/examples/src/loadfile.rs @@ -1,17 +1,13 @@ -use std::{fs::read_to_string}; +use std::fs::read_to_string; -use boa::{ - exec::Interpreter, - forward, - realm::Realm -}; +use boa::{exec::Interpreter, forward, realm::Realm}; -pub fn run(){ +pub fn run() { let js_file_path = "./scripts/helloworld.js"; let buffer = read_to_string(js_file_path); - if buffer.is_err(){ - println!("Error: {}", buffer.unwrap_err()); + if let Err(msg) = buffer { + println!("Error: {}", msg); return; } @@ -23,7 +19,7 @@ pub fn run(){ //Load, parse and execute the JS code read from the source file let error_string = forward(&mut engine, &buffer.unwrap()); - if error_string != "undefined"{ + if error_string != "undefined" { println!("Error parsing script: {}", error_string); } -} \ No newline at end of file +} diff --git a/examples/src/modulehandler.rs b/examples/src/modulehandler.rs index 4ac1aa11e8a..292854ff972 100644 --- a/examples/src/modulehandler.rs +++ b/examples/src/modulehandler.rs @@ -1,19 +1,15 @@ use std::fs::read_to_string; use boa::{ - exec::Interpreter, - forward, - realm::Realm, - builtins::value::Value, - builtins::value::ResultValue, - builtins::function::Function + builtins::function::Function, builtins::value::ResultValue, builtins::value::Value, + exec::Interpreter, forward, realm::Realm, }; -pub fn run(){ +pub fn run() { let js_file_path = "./scripts/calctest.js"; let buffer = read_to_string(js_file_path); - if buffer.is_err(){ + if buffer.is_err() { println!("Error: {}", buffer.unwrap_err()); return; } @@ -23,43 +19,48 @@ pub fn run(){ //Adding custom implementation that mimics 'require' let requirefn = Function::builtin(Vec::new(), require); - ctx.global_obj.set_field("require", Value::from_func(requirefn)); + ctx.global_obj + .set_field("require", Value::from_func(requirefn)); //Addming custom object that mimics 'module.exports' let moduleobj = Value::new_object(Some(&ctx.global_obj)); moduleobj.set_field("exports", Value::from(" ")); ctx.global_obj.set_field("module", moduleobj); - //Instantiating the engien with the execution context + //Instantiating the engine with the execution context let mut engine = Interpreter::new(ctx); //Loading, parsing and executing the JS code from the source file let error_string = forward(&mut engine, &buffer.unwrap()); - if error_string != "undefined"{ + if error_string != "undefined" { println!("Error parsing script: {}", error_string); } } //Custom implementation that mimics 'require' module loader -fn require(_:&Value, args:&[Value], engine:&mut Interpreter) -> ResultValue{ +fn require(_: &Value, args: &[Value], engine: &mut Interpreter) -> ResultValue { let arg = args.get(0).unwrap(); - + //BUG: Dev branch seems to be passing string arguments along with quotes let libfile = arg.to_string().replace("\"", ""); - + //Read the module source file println!("Loading: {}", libfile); let buffer = read_to_string(libfile); - if buffer.is_err(){ + if buffer.is_err() { println!("Error: {}", buffer.unwrap_err()); return ResultValue::from(Ok(Value::from(-1))); - }else{ + } else { //Load and parse the module source forward(engine, &buffer.unwrap()); //Access module.exports and return as ResultValue - let module_exports = engine.realm.global_obj.get_field("module").get_field("exports"); + let module_exports = engine + .realm + .global_obj + .get_field("module") + .get_field("exports"); let return_value = ResultValue::from(Ok(Value::from(module_exports))); return return_value; } -} \ No newline at end of file +}