Skip to content

Commit

Permalink
* idiomatic rust
Browse files Browse the repository at this point in the history
* spelling mistake
* Rust FMT
  • Loading branch information
jasonwilliams committed Apr 5, 2021
1 parent 666bd8b commit 227901c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
18 changes: 7 additions & 11 deletions examples/src/loadfile.rs
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -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);
}
}
}
37 changes: 19 additions & 18 deletions examples/src/modulehandler.rs
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -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;
}
}
}

0 comments on commit 227901c

Please sign in to comment.