From 9cea644bbf1116cf753dcac769e159e7a8465018 Mon Sep 17 00:00:00 2001 From: elasmojs <66966290+elasmojs@users.noreply.github.com> Date: Fri, 14 Aug 2020 22:23:44 +0530 Subject: [PATCH 1/9] Added boa examples Added boa examples as per issue #446 --- Cargo.toml | 1 + examples/Cargo.toml | 10 +++++ examples/scripts/calc.js | 14 +++++++ examples/scripts/calctest.js | 9 +++++ examples/scripts/enhancedglobal.js | 11 +++++ examples/scripts/helloworld.js | 1 + examples/src/enhancedglobal.rs | 59 +++++++++++++++++++++++++++ examples/src/loadfile.rs | 29 +++++++++++++ examples/src/loadstring.rs | 21 ++++++++++ examples/src/main.rs | 29 +++++++++++++ examples/src/modulehandler.rs | 65 ++++++++++++++++++++++++++++++ examples/src/returnval.rs | 32 +++++++++++++++ 12 files changed, 281 insertions(+) create mode 100644 examples/Cargo.toml create mode 100644 examples/scripts/calc.js create mode 100644 examples/scripts/calctest.js create mode 100644 examples/scripts/enhancedglobal.js create mode 100644 examples/scripts/helloworld.js create mode 100644 examples/src/enhancedglobal.rs create mode 100644 examples/src/loadfile.rs create mode 100644 examples/src/loadstring.rs create mode 100644 examples/src/main.rs create mode 100644 examples/src/modulehandler.rs create mode 100644 examples/src/returnval.rs diff --git a/Cargo.toml b/Cargo.toml index 2a92388ffc5..14d5f080f13 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ members = [ "boa_tester", "boa_unicode", "boa_wasm", + "boa_examples" ] # The release profile, used for `cargo build --release`. diff --git a/examples/Cargo.toml b/examples/Cargo.toml new file mode 100644 index 00000000000..f71562eae44 --- /dev/null +++ b/examples/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "boa-examples" +version = "0.1.0" +authors = ["elasmojs <66966290+elasmojs@users.noreply.github.com>"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +Boa = { path = "../boa", features = ["serde"] } \ No newline at end of file diff --git a/examples/scripts/calc.js b/examples/scripts/calc.js new file mode 100644 index 00000000000..edaad6a64b4 --- /dev/null +++ b/examples/scripts/calc.js @@ -0,0 +1,14 @@ +module.exports = { + add: function(a, b){ + return a + b; + }, + subtract: function(a, b){ + return a - b; + }, + multiply: function(a, b){ + return a * b; + }, + divide: function(a, b){ + return a/b; + } +} \ No newline at end of file diff --git a/examples/scripts/calctest.js b/examples/scripts/calctest.js new file mode 100644 index 00000000000..dcb4d7970d3 --- /dev/null +++ b/examples/scripts/calctest.js @@ -0,0 +1,9 @@ + +//load module +let calc = require('./scripts/calc.js'); + +console.log('Using calc module'); +console.log('Add: ' + calc.add(3, 3)); +console.log('Subtract: ' + calc.subtract(3, 3)); +console.log('Multiply: ' + calc.multiply(3, 3)); +console.log('Divide: ' + calc.divide(3, 3)); \ No newline at end of file diff --git a/examples/scripts/enhancedglobal.js b/examples/scripts/enhancedglobal.js new file mode 100644 index 00000000000..8d53f694995 --- /dev/null +++ b/examples/scripts/enhancedglobal.js @@ -0,0 +1,11 @@ +//access custom global variable +console.log("Custom global: " + customstring); + +//call a custom global function with arguments +console.log("Custom function: " + rusty_hello("Boa! Boa!")); + +//access a custom global object and call a member function of that object +let a = 5; +let b = 5; +let result = rusty_obj.add(a, b); +console.log("Custom object: Result from rusty_obj.add() : " + result); \ No newline at end of file diff --git a/examples/scripts/helloworld.js b/examples/scripts/helloworld.js new file mode 100644 index 00000000000..97c29079268 --- /dev/null +++ b/examples/scripts/helloworld.js @@ -0,0 +1 @@ +console.log("Hello World from JS file!"); \ No newline at end of file diff --git a/examples/src/enhancedglobal.rs b/examples/src/enhancedglobal.rs new file mode 100644 index 00000000000..13c2e031150 --- /dev/null +++ b/examples/src/enhancedglobal.rs @@ -0,0 +1,59 @@ +use std::{fs::read_to_string}; + +use boa::{ + exec::Interpreter, + forward, + realm::Realm, + builtins::value::Value, + builtins::value::ResultValue, + builtins::function::Function +}; + +pub fn run(){ + let js_file_path = "./scripts/enhancedglobal.js"; + let buffer = read_to_string(js_file_path); + + if buffer.is_err(){ + println!("Error: {}", buffer.unwrap_err()); + return; + } + + //Creating the execution context + let ctx = Realm::create(); + + //Adding a custom global variable + ctx.global_obj.set_field("customstring", "Hello! I am a custom global variable"); + + //Adding a custom global function + let rfn = Function::builtin(Vec::new(), rusty_hello); + ctx.global_obj.set_field("rusty_hello", Value::from_func(rfn)); + + //Adding s custom object + let gobj = Value::new_object(Some(&ctx.global_obj)); + let addfn = Function::builtin(Vec::new(), add); + gobj.set_field("add", Value::from_func(addfn)); + ctx.global_obj.set_field("rusty_obj", gobj); + + //Instantiating the engien 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"{ + println!("Error parsing script: {}", error_string); + } +} + +//Custom function callable from JS +fn rusty_hello(_:&Value, args:&[Value], _:&mut Interpreter) -> ResultValue{ + let arg = args.get(0).unwrap(); + let val = format!("Hello from Rust! You passed {}", arg); + return ResultValue::from(Ok(Value::from(val))); +} + +//Function appended as property of a custom global object, callable from JS +fn add(_:&Value, args:&[Value], _engine:&mut Interpreter) -> ResultValue{ + let arg0 = args.get(0).unwrap(); + let arg1 = args.get(1).unwrap(); + return ResultValue::from(Ok(Value::from(arg0.to_integer() + arg1.to_integer()))); +} \ No newline at end of file diff --git a/examples/src/loadfile.rs b/examples/src/loadfile.rs new file mode 100644 index 00000000000..6a53302b7cf --- /dev/null +++ b/examples/src/loadfile.rs @@ -0,0 +1,29 @@ +use std::{fs::read_to_string}; + +use boa::{ + exec::Interpreter, + forward, + realm::Realm +}; + +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()); + return; + } + + //Create the execution context + let ctx = Realm::create(); + + //Instantiate the engien with the execution context + let mut engine = Interpreter::new(ctx); + + //Load, parse and execute the JS code read from the source file + let error_string = forward(&mut engine, &buffer.unwrap()); + if error_string != "undefined"{ + println!("Error parsing script: {}", error_string); + } +} \ No newline at end of file diff --git a/examples/src/loadstring.rs b/examples/src/loadstring.rs new file mode 100644 index 00000000000..aeee8ded839 --- /dev/null +++ b/examples/src/loadstring.rs @@ -0,0 +1,21 @@ +use boa::{ + exec::Interpreter, + forward, + realm::Realm +}; + +pub fn run(){ + let js_code = "console.log('Hello World from a JS code string!')"; + + //Create the execution context + let ctx = Realm::create(); + + //Instantiate the engien with the execution context + let mut engine = Interpreter::new(ctx); + + //Load, parse and execute the given JS String + let error_string = forward(&mut engine, js_code); + if error_string != "undefined"{ + println!("Error parsing script: {}", error_string); + } +} \ No newline at end of file diff --git a/examples/src/main.rs b/examples/src/main.rs new file mode 100644 index 00000000000..bff68875582 --- /dev/null +++ b/examples/src/main.rs @@ -0,0 +1,29 @@ +mod loadstring; +mod loadfile; +mod returnval; +mod enhancedglobal; +mod modulehandler; + +fn main() { + println!("\r\n"); + + //example that loads, parses and executs a JS code string + loadstring::run(); + println!("\r\n"); + + //example that loads, parses and executs JS code from a source file (./scripts/helloworld.js) + loadfile::run(); + println!("\r\n"); + + //example that loads, parses and executs JS code and uses the return value + returnval::run(); + println!("\r\n"); + + //example that enhances the global object with custom values, objects, functions + enhancedglobal::run(); + println!("\r\n"); + + //example that implements a custom module handler which mimics (require / module.exports) pattern + modulehandler::run(); + println!("\r\n"); +} diff --git a/examples/src/modulehandler.rs b/examples/src/modulehandler.rs new file mode 100644 index 00000000000..4ac1aa11e8a --- /dev/null +++ b/examples/src/modulehandler.rs @@ -0,0 +1,65 @@ +use std::fs::read_to_string; + +use boa::{ + exec::Interpreter, + forward, + realm::Realm, + builtins::value::Value, + builtins::value::ResultValue, + builtins::function::Function +}; + +pub fn run(){ + let js_file_path = "./scripts/calctest.js"; + let buffer = read_to_string(js_file_path); + + if buffer.is_err(){ + println!("Error: {}", buffer.unwrap_err()); + return; + } + + //Creating the execution context + let ctx = Realm::create(); + + //Adding custom implementation that mimics 'require' + let requirefn = Function::builtin(Vec::new(), require); + 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 + 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"{ + println!("Error parsing script: {}", error_string); + } +} + +//Custom implementation that mimics 'require' module loader +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(){ + println!("Error: {}", buffer.unwrap_err()); + return ResultValue::from(Ok(Value::from(-1))); + }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 return_value = ResultValue::from(Ok(Value::from(module_exports))); + return return_value; + } +} \ No newline at end of file diff --git a/examples/src/returnval.rs b/examples/src/returnval.rs new file mode 100644 index 00000000000..8acec9b9f24 --- /dev/null +++ b/examples/src/returnval.rs @@ -0,0 +1,32 @@ +use boa::{ + exec::Interpreter, + forward_val, + realm::Realm +}; + +pub fn run(){ + let js_string = r" + function add(a, b){ + return a + b; + } + let a = 3; + let b = 3; + add(a,b); + "; + + //Create the execution context + let ctx = Realm::create(); + + //Instantiate the engien with the execution context + let mut engine = Interpreter::new(ctx); + + //Load, parse, execute the given JS String and returns the result of the execution as a Boa Value + match forward_val(&mut engine, js_string){ + Ok(v) => { + println!("Script returned: {}", v); + }, + Err(e) =>{ + println!("Script error: {}", e); + } + } +} \ No newline at end of file From bf6db70e73e41cfffff03cf77efb960813d21b6c Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Mon, 5 Apr 2021 15:35:13 +0100 Subject: [PATCH 2/9] updating details and aligning with other crates --- examples/Cargo.toml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/Cargo.toml b/examples/Cargo.toml index f71562eae44..b7a6235f4a7 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -1,10 +1,12 @@ [package] -name = "boa-examples" -version = "0.1.0" -authors = ["elasmojs <66966290+elasmojs@users.noreply.github.com>"] +name = "boa_examples" +version = "0.11.0" +authors = ["boa-dev"] +repository = "https://github.com/boa-dev/boa" +license = "Unlicense/MIT" edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -Boa = { path = "../boa", features = ["serde"] } \ No newline at end of file +Boa = { path = "../boa", features = ["serde"] } From 31a106f4ac29420ce6851258ab6ce4214e612d03 Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Mon, 5 Apr 2021 15:41:44 +0100 Subject: [PATCH 3/9] * idiomatic rust * spelling mistake * Rust FMT --- examples/src/loadfile.rs | 18 +++++++---------- examples/src/modulehandler.rs | 37 ++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 29 deletions(-) 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 +} From 726b8acdf28269fb526659f4950332e6ced96ea9 Mon Sep 17 00:00:00 2001 From: "Iban Eguia (Razican)" Date: Sat, 22 May 2021 19:18:27 +0200 Subject: [PATCH 4/9] Updated some code --- Cargo.lock | 7 ++++ {examples => boa_examples}/Cargo.toml | 2 +- {examples => boa_examples}/scripts/calc.js | 0 .../scripts/calctest.js | 0 .../scripts/enhancedglobal.js | 0 .../scripts/helloworld.js | 0 .../src/enhancedglobal.rs | 0 boa_examples/src/loadfile.rs | 38 +++++++++++++++++++ boa_examples/src/loadstring.rs | 31 +++++++++++++++ {examples => boa_examples}/src/main.rs | 0 .../src/modulehandler.rs | 0 examples/src/loadfile.rs | 25 ------------ examples/src/loadstring.rs | 21 ---------- examples/src/returnval.rs | 32 ---------------- 14 files changed, 77 insertions(+), 79 deletions(-) rename {examples => boa_examples}/Cargo.toml (85%) rename {examples => boa_examples}/scripts/calc.js (100%) rename {examples => boa_examples}/scripts/calctest.js (100%) rename {examples => boa_examples}/scripts/enhancedglobal.js (100%) rename {examples => boa_examples}/scripts/helloworld.js (100%) rename {examples => boa_examples}/src/enhancedglobal.rs (100%) create mode 100644 boa_examples/src/loadfile.rs create mode 100644 boa_examples/src/loadstring.rs rename {examples => boa_examples}/src/main.rs (100%) rename {examples => boa_examples}/src/modulehandler.rs (100%) delete mode 100644 examples/src/loadfile.rs delete mode 100644 examples/src/loadstring.rs delete mode 100644 examples/src/returnval.rs diff --git a/Cargo.lock b/Cargo.lock index 35b1b82dc30..c59e64389f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -134,6 +134,13 @@ dependencies = [ "once_cell", ] +[[package]] +name = "boa_examples" +version = "0.11.0" +dependencies = [ + "boa_engine", +] + [[package]] name = "boa_tester" version = "0.13.0" diff --git a/examples/Cargo.toml b/boa_examples/Cargo.toml similarity index 85% rename from examples/Cargo.toml rename to boa_examples/Cargo.toml index b7a6235f4a7..527eaba2744 100644 --- a/examples/Cargo.toml +++ b/boa_examples/Cargo.toml @@ -9,4 +9,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -Boa = { path = "../boa", features = ["serde"] } +Boa = { path = "../boa" } diff --git a/examples/scripts/calc.js b/boa_examples/scripts/calc.js similarity index 100% rename from examples/scripts/calc.js rename to boa_examples/scripts/calc.js diff --git a/examples/scripts/calctest.js b/boa_examples/scripts/calctest.js similarity index 100% rename from examples/scripts/calctest.js rename to boa_examples/scripts/calctest.js diff --git a/examples/scripts/enhancedglobal.js b/boa_examples/scripts/enhancedglobal.js similarity index 100% rename from examples/scripts/enhancedglobal.js rename to boa_examples/scripts/enhancedglobal.js diff --git a/examples/scripts/helloworld.js b/boa_examples/scripts/helloworld.js similarity index 100% rename from examples/scripts/helloworld.js rename to boa_examples/scripts/helloworld.js diff --git a/examples/src/enhancedglobal.rs b/boa_examples/src/enhancedglobal.rs similarity index 100% rename from examples/src/enhancedglobal.rs rename to boa_examples/src/enhancedglobal.rs diff --git a/boa_examples/src/loadfile.rs b/boa_examples/src/loadfile.rs new file mode 100644 index 00000000000..bead31613d5 --- /dev/null +++ b/boa_examples/src/loadfile.rs @@ -0,0 +1,38 @@ +use std::fs::read_to_string; + +use boa::{exec::Executable, parse, Context}; + +pub fn run() { + let js_file_path = "./scripts/helloworld.js"; + + match read_to_string(js_file_path) { + Ok(src) => { + // Instantiate the execution context + let mut context = Context::new(); + + // Parse the source code + let expr = match parse(src, false) { + Ok(res) => res, + Err(e) => { + // Pretty print the error + eprintln!( + "Uncaught {}", + context + .throw_syntax_error(e.to_string()) + .expect_err("interpreter.throw_syntax_error() did not return an error") + .display() + ); + + return; + } + }; + + // Execute the JS code read from the source file + match expr.run(&mut context) { + Ok(v) => println!("{}", v.display()), + Err(e) => eprintln!("Uncaught {}", e.display()), + } + } + Err(msg) => eprintln!("Error: {}", msg), + } +} diff --git a/boa_examples/src/loadstring.rs b/boa_examples/src/loadstring.rs new file mode 100644 index 00000000000..17e2d2ddd46 --- /dev/null +++ b/boa_examples/src/loadstring.rs @@ -0,0 +1,31 @@ +use boa::{exec::Executable, parse, Context}; + +pub fn run() { + let js_code = "console.log('Hello World from a JS code string!')"; + + // Instantiate the execution context + let mut context = Context::new(); + + // Parse the source code + let expr = match parse(js_code, false) { + Ok(res) => res, + Err(e) => { + // Pretty print the error + eprintln!( + "Uncaught {}", + context + .throw_syntax_error(e.to_string()) + .expect_err("interpreter.throw_syntax_error() did not return an error") + .display() + ); + + return; + } + }; + + // Execute the JS code read from the source file + match expr.run(&mut context) { + Ok(v) => println!("{}", v.display()), + Err(e) => eprintln!("Uncaught {}", e.display()), + } +} diff --git a/examples/src/main.rs b/boa_examples/src/main.rs similarity index 100% rename from examples/src/main.rs rename to boa_examples/src/main.rs diff --git a/examples/src/modulehandler.rs b/boa_examples/src/modulehandler.rs similarity index 100% rename from examples/src/modulehandler.rs rename to boa_examples/src/modulehandler.rs diff --git a/examples/src/loadfile.rs b/examples/src/loadfile.rs deleted file mode 100644 index 73e70fe3ca6..00000000000 --- a/examples/src/loadfile.rs +++ /dev/null @@ -1,25 +0,0 @@ -use std::fs::read_to_string; - -use boa::{exec::Interpreter, forward, realm::Realm}; - -pub fn run() { - let js_file_path = "./scripts/helloworld.js"; - let buffer = read_to_string(js_file_path); - - if let Err(msg) = buffer { - println!("Error: {}", msg); - return; - } - - //Create the execution context - let ctx = Realm::create(); - - //Instantiate the engien with the execution context - let mut engine = Interpreter::new(ctx); - - //Load, parse and execute the JS code read from the source file - let error_string = forward(&mut engine, &buffer.unwrap()); - if error_string != "undefined" { - println!("Error parsing script: {}", error_string); - } -} diff --git a/examples/src/loadstring.rs b/examples/src/loadstring.rs deleted file mode 100644 index aeee8ded839..00000000000 --- a/examples/src/loadstring.rs +++ /dev/null @@ -1,21 +0,0 @@ -use boa::{ - exec::Interpreter, - forward, - realm::Realm -}; - -pub fn run(){ - let js_code = "console.log('Hello World from a JS code string!')"; - - //Create the execution context - let ctx = Realm::create(); - - //Instantiate the engien with the execution context - let mut engine = Interpreter::new(ctx); - - //Load, parse and execute the given JS String - let error_string = forward(&mut engine, js_code); - if error_string != "undefined"{ - println!("Error parsing script: {}", error_string); - } -} \ No newline at end of file diff --git a/examples/src/returnval.rs b/examples/src/returnval.rs deleted file mode 100644 index 8acec9b9f24..00000000000 --- a/examples/src/returnval.rs +++ /dev/null @@ -1,32 +0,0 @@ -use boa::{ - exec::Interpreter, - forward_val, - realm::Realm -}; - -pub fn run(){ - let js_string = r" - function add(a, b){ - return a + b; - } - let a = 3; - let b = 3; - add(a,b); - "; - - //Create the execution context - let ctx = Realm::create(); - - //Instantiate the engien with the execution context - let mut engine = Interpreter::new(ctx); - - //Load, parse, execute the given JS String and returns the result of the execution as a Boa Value - match forward_val(&mut engine, js_string){ - Ok(v) => { - println!("Script returned: {}", v); - }, - Err(e) =>{ - println!("Script error: {}", e); - } - } -} \ No newline at end of file From 7287d1529985cbfdc5c1e7398d25c2b23e3b6c3c Mon Sep 17 00:00:00 2001 From: "Iban Eguia (Razican)" Date: Sat, 22 May 2021 19:27:03 +0200 Subject: [PATCH 5/9] Fixed style with Prettier --- boa_examples/scripts/calc.js | 26 +++++++++++++------------- boa_examples/scripts/calctest.js | 13 ++++++------- boa_examples/scripts/enhancedglobal.js | 2 +- boa_examples/scripts/helloworld.js | 2 +- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/boa_examples/scripts/calc.js b/boa_examples/scripts/calc.js index edaad6a64b4..017534fab27 100644 --- a/boa_examples/scripts/calc.js +++ b/boa_examples/scripts/calc.js @@ -1,14 +1,14 @@ module.exports = { - add: function(a, b){ - return a + b; - }, - subtract: function(a, b){ - return a - b; - }, - multiply: function(a, b){ - return a * b; - }, - divide: function(a, b){ - return a/b; - } -} \ No newline at end of file + add: function (a, b) { + return a + b; + }, + subtract: function (a, b) { + return a - b; + }, + multiply: function (a, b) { + return a * b; + }, + divide: function (a, b) { + return a / b; + }, +}; diff --git a/boa_examples/scripts/calctest.js b/boa_examples/scripts/calctest.js index dcb4d7970d3..660453a1c11 100644 --- a/boa_examples/scripts/calctest.js +++ b/boa_examples/scripts/calctest.js @@ -1,9 +1,8 @@ - //load module -let calc = require('./scripts/calc.js'); +let calc = require("./scripts/calc.js"); -console.log('Using calc module'); -console.log('Add: ' + calc.add(3, 3)); -console.log('Subtract: ' + calc.subtract(3, 3)); -console.log('Multiply: ' + calc.multiply(3, 3)); -console.log('Divide: ' + calc.divide(3, 3)); \ No newline at end of file +console.log("Using calc module"); +console.log("Add: " + calc.add(3, 3)); +console.log("Subtract: " + calc.subtract(3, 3)); +console.log("Multiply: " + calc.multiply(3, 3)); +console.log("Divide: " + calc.divide(3, 3)); diff --git a/boa_examples/scripts/enhancedglobal.js b/boa_examples/scripts/enhancedglobal.js index 8d53f694995..6870f79bf9b 100644 --- a/boa_examples/scripts/enhancedglobal.js +++ b/boa_examples/scripts/enhancedglobal.js @@ -8,4 +8,4 @@ console.log("Custom function: " + rusty_hello("Boa! Boa!")); let a = 5; let b = 5; let result = rusty_obj.add(a, b); -console.log("Custom object: Result from rusty_obj.add() : " + result); \ No newline at end of file +console.log("Custom object: Result from rusty_obj.add() : " + result); diff --git a/boa_examples/scripts/helloworld.js b/boa_examples/scripts/helloworld.js index 97c29079268..74e66195d18 100644 --- a/boa_examples/scripts/helloworld.js +++ b/boa_examples/scripts/helloworld.js @@ -1 +1 @@ -console.log("Hello World from JS file!"); \ No newline at end of file +console.log("Hello World from JS file!"); From 75495e4ed1e34563c6b0ddfe2bbaac0bda4ba891 Mon Sep 17 00:00:00 2001 From: jasonwilliams Date: Thu, 3 Mar 2022 18:37:26 +0000 Subject: [PATCH 6/9] update references --- Cargo.lock | 14 +++++++------- boa_examples/Cargo.toml | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c59e64389f1..ad8cce0cc8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -109,6 +109,13 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "boa_examples" +version = "0.11.0" +dependencies = [ + "boa_engine", +] + [[package]] name = "boa_gc" version = "0.13.0" @@ -134,13 +141,6 @@ dependencies = [ "once_cell", ] -[[package]] -name = "boa_examples" -version = "0.11.0" -dependencies = [ - "boa_engine", -] - [[package]] name = "boa_tester" version = "0.13.0" diff --git a/boa_examples/Cargo.toml b/boa_examples/Cargo.toml index 527eaba2744..cbf1d057f28 100644 --- a/boa_examples/Cargo.toml +++ b/boa_examples/Cargo.toml @@ -9,4 +9,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -Boa = { path = "../boa" } +boa_engine = { path = "../boa_engine" } From 5196b96331a6f818e4af8243dc5ddf1a2073d931 Mon Sep 17 00:00:00 2001 From: jasonwilliams Date: Fri, 4 Mar 2022 19:32:43 +0000 Subject: [PATCH 7/9] updates to boa_examples --- boa_examples/Cargo.toml | 6 +- .../examples => boa_examples/src}/classes.rs | 4 +- .../examples => boa_examples/src}/closures.rs | 2 +- boa_examples/src/enhancedglobal.rs | 59 ------------------- .../examples => boa_examples/src}/jsarray.rs | 2 +- boa_examples/src/loadfile.rs | 27 +++------ boa_examples/src/loadstring.rs | 26 +++----- boa_examples/src/main.rs | 21 ++++--- boa_examples/src/modulehandler.rs | 52 +++++++--------- 9 files changed, 56 insertions(+), 143 deletions(-) rename {boa_engine/examples => boa_examples/src}/classes.rs (99%) rename {boa_engine/examples => boa_examples/src}/closures.rs (99%) delete mode 100644 boa_examples/src/enhancedglobal.rs rename {boa_engine/examples => boa_examples/src}/jsarray.rs (98%) diff --git a/boa_examples/Cargo.toml b/boa_examples/Cargo.toml index cbf1d057f28..908760924c6 100644 --- a/boa_examples/Cargo.toml +++ b/boa_examples/Cargo.toml @@ -4,9 +4,11 @@ version = "0.11.0" authors = ["boa-dev"] repository = "https://github.com/boa-dev/boa" license = "Unlicense/MIT" -edition = "2018" +edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -boa_engine = { path = "../boa_engine" } +boa_engine = { path = "../boa_engine", features = ["console"] } +boa_gc = { path = "../boa_gc" } +gc = { version = "0.4.1" } \ No newline at end of file diff --git a/boa_engine/examples/classes.rs b/boa_examples/src/classes.rs similarity index 99% rename from boa_engine/examples/classes.rs rename to boa_examples/src/classes.rs index 55ea749f264..ee417fefb2a 100644 --- a/boa_engine/examples/classes.rs +++ b/boa_examples/src/classes.rs @@ -1,10 +1,10 @@ // NOTE: this example requires the `console` feature to run correctly. - use boa_engine::{ class::{Class, ClassBuilder}, property::Attribute, Context, JsResult, JsValue, }; + use boa_gc::{Finalize, Trace}; // We create a new struct that is going to represent a person. @@ -123,7 +123,7 @@ impl Class for Person { } } -fn main() { +pub fn run() { // First we need to create a Javascript context. let mut context = Context::default(); diff --git a/boa_engine/examples/closures.rs b/boa_examples/src/closures.rs similarity index 99% rename from boa_engine/examples/closures.rs rename to boa_examples/src/closures.rs index 9eae78b4b8d..5a65f62782d 100644 --- a/boa_engine/examples/closures.rs +++ b/boa_examples/src/closures.rs @@ -8,7 +8,7 @@ use boa_engine::{ }; use boa_gc::{Finalize, Trace}; -fn main() -> Result<(), JsValue> { +pub fn run() -> Result<(), JsValue> { // We create a new `Context` to create a new Javascript executor. let mut context = Context::default(); diff --git a/boa_examples/src/enhancedglobal.rs b/boa_examples/src/enhancedglobal.rs deleted file mode 100644 index 13c2e031150..00000000000 --- a/boa_examples/src/enhancedglobal.rs +++ /dev/null @@ -1,59 +0,0 @@ -use std::{fs::read_to_string}; - -use boa::{ - exec::Interpreter, - forward, - realm::Realm, - builtins::value::Value, - builtins::value::ResultValue, - builtins::function::Function -}; - -pub fn run(){ - let js_file_path = "./scripts/enhancedglobal.js"; - let buffer = read_to_string(js_file_path); - - if buffer.is_err(){ - println!("Error: {}", buffer.unwrap_err()); - return; - } - - //Creating the execution context - let ctx = Realm::create(); - - //Adding a custom global variable - ctx.global_obj.set_field("customstring", "Hello! I am a custom global variable"); - - //Adding a custom global function - let rfn = Function::builtin(Vec::new(), rusty_hello); - ctx.global_obj.set_field("rusty_hello", Value::from_func(rfn)); - - //Adding s custom object - let gobj = Value::new_object(Some(&ctx.global_obj)); - let addfn = Function::builtin(Vec::new(), add); - gobj.set_field("add", Value::from_func(addfn)); - ctx.global_obj.set_field("rusty_obj", gobj); - - //Instantiating the engien 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"{ - println!("Error parsing script: {}", error_string); - } -} - -//Custom function callable from JS -fn rusty_hello(_:&Value, args:&[Value], _:&mut Interpreter) -> ResultValue{ - let arg = args.get(0).unwrap(); - let val = format!("Hello from Rust! You passed {}", arg); - return ResultValue::from(Ok(Value::from(val))); -} - -//Function appended as property of a custom global object, callable from JS -fn add(_:&Value, args:&[Value], _engine:&mut Interpreter) -> ResultValue{ - let arg0 = args.get(0).unwrap(); - let arg1 = args.get(1).unwrap(); - return ResultValue::from(Ok(Value::from(arg0.to_integer() + arg1.to_integer()))); -} \ No newline at end of file diff --git a/boa_engine/examples/jsarray.rs b/boa_examples/src/jsarray.rs similarity index 98% rename from boa_engine/examples/jsarray.rs rename to boa_examples/src/jsarray.rs index 86c5bda1836..af4ffa25f62 100644 --- a/boa_engine/examples/jsarray.rs +++ b/boa_examples/src/jsarray.rs @@ -3,7 +3,7 @@ use boa_engine::{ Context, JsValue, }; -fn main() -> Result<(), JsValue> { +pub fn run() -> Result<(), JsValue> { // We create a new `Context` to create a new Javascript executor. let context = &mut Context::default(); diff --git a/boa_examples/src/loadfile.rs b/boa_examples/src/loadfile.rs index bead31613d5..fe8db6f0cc2 100644 --- a/boa_examples/src/loadfile.rs +++ b/boa_examples/src/loadfile.rs @@ -1,6 +1,6 @@ use std::fs::read_to_string; -use boa::{exec::Executable, parse, Context}; +use boa_engine::Context; pub fn run() { let js_file_path = "./scripts/helloworld.js"; @@ -8,30 +8,17 @@ pub fn run() { match read_to_string(js_file_path) { Ok(src) => { // Instantiate the execution context - let mut context = Context::new(); - + let mut context = Context::default(); // Parse the source code - let expr = match parse(src, false) { - Ok(res) => res, + match context.eval(src) { + Ok(res) => { + println!("{}", res.to_string(&mut context).unwrap()); + } Err(e) => { // Pretty print the error - eprintln!( - "Uncaught {}", - context - .throw_syntax_error(e.to_string()) - .expect_err("interpreter.throw_syntax_error() did not return an error") - .display() - ); - - return; + eprintln!("Uncaught {}", e.display()); } }; - - // Execute the JS code read from the source file - match expr.run(&mut context) { - Ok(v) => println!("{}", v.display()), - Err(e) => eprintln!("Uncaught {}", e.display()), - } } Err(msg) => eprintln!("Error: {}", msg), } diff --git a/boa_examples/src/loadstring.rs b/boa_examples/src/loadstring.rs index 17e2d2ddd46..a60d46a689f 100644 --- a/boa_examples/src/loadstring.rs +++ b/boa_examples/src/loadstring.rs @@ -1,31 +1,19 @@ -use boa::{exec::Executable, parse, Context}; +use boa_engine::Context; pub fn run() { let js_code = "console.log('Hello World from a JS code string!')"; // Instantiate the execution context - let mut context = Context::new(); + let mut context = Context::default(); // Parse the source code - let expr = match parse(js_code, false) { - Ok(res) => res, + match context.eval(js_code) { + Ok(res) => { + println!("{}", res.to_string(&mut context).unwrap()); + } Err(e) => { // Pretty print the error - eprintln!( - "Uncaught {}", - context - .throw_syntax_error(e.to_string()) - .expect_err("interpreter.throw_syntax_error() did not return an error") - .display() - ); - - return; + eprintln!("Uncaught {}", e.display()); } }; - - // Execute the JS code read from the source file - match expr.run(&mut context) { - Ok(v) => println!("{}", v.display()), - Err(e) => eprintln!("Uncaught {}", e.display()), - } } diff --git a/boa_examples/src/main.rs b/boa_examples/src/main.rs index bff68875582..8316e691540 100644 --- a/boa_examples/src/main.rs +++ b/boa_examples/src/main.rs @@ -1,12 +1,13 @@ -mod loadstring; +mod classes; +mod closures; +mod jsarray; mod loadfile; -mod returnval; -mod enhancedglobal; +mod loadstring; mod modulehandler; fn main() { println!("\r\n"); - + //example that loads, parses and executs a JS code string loadstring::run(); println!("\r\n"); @@ -15,12 +16,16 @@ fn main() { loadfile::run(); println!("\r\n"); - //example that loads, parses and executs JS code and uses the return value - returnval::run(); + //example that implements classes in Rust and exposes them to JS + classes::run(); + println!("\r\n"); + + //example that implements closures in Rust and exposes them to JS + closures::run().expect("closures failed to excecute"); println!("\r\n"); - //example that enhances the global object with custom values, objects, functions - enhancedglobal::run(); + //example that shows array manipulation in Rust + jsarray::run().expect("closures failed to excecute"); println!("\r\n"); //example that implements a custom module handler which mimics (require / module.exports) pattern diff --git a/boa_examples/src/modulehandler.rs b/boa_examples/src/modulehandler.rs index 292854ff972..3d60ee7bfb5 100644 --- a/boa_examples/src/modulehandler.rs +++ b/boa_examples/src/modulehandler.rs @@ -1,10 +1,6 @@ +use boa_engine::{prelude::JsObject, property::Attribute, Context, JsResult, JsValue}; use std::fs::read_to_string; -use boa::{ - builtins::function::Function, builtins::value::ResultValue, builtins::value::Value, - exec::Interpreter, forward, realm::Realm, -}; - pub fn run() { let js_file_path = "./scripts/calctest.js"; let buffer = read_to_string(js_file_path); @@ -15,52 +11,46 @@ pub fn run() { } //Creating the execution context - let ctx = Realm::create(); + let mut ctx = Context::default(); //Adding custom implementation that mimics 'require' - let requirefn = Function::builtin(Vec::new(), require); - ctx.global_obj - .set_field("require", Value::from_func(requirefn)); + ctx.register_global_function("require", 0, require); - //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); + //Adding custom object that mimics 'module.exports' + let moduleobj = JsObject::default(); + moduleobj + .set("exports", JsValue::from(" "), false, &mut ctx) + .unwrap(); + ctx.register_global_property("module", JsValue::from(moduleobj), Attribute::default()); //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" { - println!("Error parsing script: {}", error_string); - } + ctx.eval(&buffer.unwrap()).unwrap(); } //Custom implementation that mimics 'require' module loader -fn require(_: &Value, args: &[Value], engine: &mut Interpreter) -> ResultValue { +fn require(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult { let arg = args.get(0).unwrap(); //BUG: Dev branch seems to be passing string arguments along with quotes - let libfile = arg.to_string().replace("\"", ""); + let libfile = arg + .to_string(ctx) + .expect("Failed to convert to string") + .replace('\"', ""); //Read the module source file println!("Loading: {}", libfile); let buffer = read_to_string(libfile); - if buffer.is_err() { + if let Err(..) = buffer { println!("Error: {}", buffer.unwrap_err()); - return ResultValue::from(Ok(Value::from(-1))); + Ok(JsValue::Rational(-1.0)) } else { //Load and parse the module source - forward(engine, &buffer.unwrap()); + ctx.eval(&buffer.unwrap()).unwrap(); //Access module.exports and return as ResultValue - 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; + let global_obj = ctx.global_object().to_owned(); + let module = global_obj.get("module", ctx).unwrap(); + module.as_object().unwrap().get("exports", ctx) } } From 806c1ebbf88b50ec83dd9c34df7278d0c7bea1eb Mon Sep 17 00:00:00 2001 From: jasonwilliams Date: Fri, 4 Mar 2022 19:32:53 +0000 Subject: [PATCH 8/9] include gc --- Cargo.lock | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index ad8cce0cc8a..a7f7d43fff9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -114,6 +114,8 @@ name = "boa_examples" version = "0.11.0" dependencies = [ "boa_engine", + "boa_gc", + "gc", ] [[package]] From 9535f99b12cdff22343725c846b12532060a9f19 Mon Sep 17 00:00:00 2001 From: jedel1043 Date: Mon, 7 Mar 2022 17:44:22 -0600 Subject: [PATCH 9/9] Split examples into separate executables (#1898) --- .github/workflows/bors.yml | 4 +-- .github/workflows/rust.yml | 4 +-- Cargo.toml | 2 +- boa_examples/Cargo.toml | 2 +- boa_examples/src/{ => bin}/classes.rs | 2 +- boa_examples/src/{ => bin}/closures.rs | 2 +- boa_examples/src/{ => bin}/jsarray.rs | 6 ++-- boa_examples/src/{ => bin}/loadfile.rs | 5 ++- boa_examples/src/{ => bin}/loadstring.rs | 4 ++- boa_examples/src/{ => bin}/modulehandler.rs | 27 ++++++++-------- boa_examples/src/main.rs | 34 --------------------- 11 files changed, 34 insertions(+), 58 deletions(-) rename boa_examples/src/{ => bin}/classes.rs (99%) rename boa_examples/src/{ => bin}/closures.rs (99%) rename boa_examples/src/{ => bin}/jsarray.rs (96%) rename boa_examples/src/{ => bin}/loadfile.rs (85%) rename boa_examples/src/{ => bin}/loadstring.rs (86%) rename boa_examples/src/{ => bin}/modulehandler.rs (65%) delete mode 100644 boa_examples/src/main.rs diff --git a/.github/workflows/bors.yml b/.github/workflows/bors.yml index e7dc2980ac4..144d2e3000c 100644 --- a/.github/workflows/bors.yml +++ b/.github/workflows/bors.yml @@ -126,16 +126,16 @@ jobs: ~/.cargo/git ~/.cargo/registry key: ${{ runner.os }}-cargo-examples-${{ hashFiles('**/Cargo.lock') }} + - run: cd boa_examples - name: Build examples uses: actions-rs/cargo@v1 with: command: build - args: --examples -v - name: Run example classes uses: actions-rs/cargo@v1 with: command: run - args: --example classes + args: --bin classes doc: name: Documentation diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 65b2c6666d6..aec41fae7ea 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -130,16 +130,16 @@ jobs: ~/.cargo/git ~/.cargo/registry key: ${{ runner.os }}-cargo-examples-${{ hashFiles('**/Cargo.lock') }} + - run: cd boa_examples - name: Build examples uses: actions-rs/cargo@v1 with: command: build - args: --examples -v - name: Run example classes uses: actions-rs/cargo@v1 with: command: run - args: --example classes + args: --bin classes doc: name: Documentation diff --git a/Cargo.toml b/Cargo.toml index 14d5f080f13..d3517716906 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ members = [ "boa_tester", "boa_unicode", "boa_wasm", - "boa_examples" + "boa_examples", ] # The release profile, used for `cargo build --release`. diff --git a/boa_examples/Cargo.toml b/boa_examples/Cargo.toml index 908760924c6..97596905f36 100644 --- a/boa_examples/Cargo.toml +++ b/boa_examples/Cargo.toml @@ -11,4 +11,4 @@ edition = "2021" [dependencies] boa_engine = { path = "../boa_engine", features = ["console"] } boa_gc = { path = "../boa_gc" } -gc = { version = "0.4.1" } \ No newline at end of file +gc = { version = "0.4.1" } diff --git a/boa_examples/src/classes.rs b/boa_examples/src/bin/classes.rs similarity index 99% rename from boa_examples/src/classes.rs rename to boa_examples/src/bin/classes.rs index ee417fefb2a..88c19e2d495 100644 --- a/boa_examples/src/classes.rs +++ b/boa_examples/src/bin/classes.rs @@ -123,7 +123,7 @@ impl Class for Person { } } -pub fn run() { +fn main() { // First we need to create a Javascript context. let mut context = Context::default(); diff --git a/boa_examples/src/closures.rs b/boa_examples/src/bin/closures.rs similarity index 99% rename from boa_examples/src/closures.rs rename to boa_examples/src/bin/closures.rs index 5a65f62782d..9eae78b4b8d 100644 --- a/boa_examples/src/closures.rs +++ b/boa_examples/src/bin/closures.rs @@ -8,7 +8,7 @@ use boa_engine::{ }; use boa_gc::{Finalize, Trace}; -pub fn run() -> Result<(), JsValue> { +fn main() -> Result<(), JsValue> { // We create a new `Context` to create a new Javascript executor. let mut context = Context::default(); diff --git a/boa_examples/src/jsarray.rs b/boa_examples/src/bin/jsarray.rs similarity index 96% rename from boa_examples/src/jsarray.rs rename to boa_examples/src/bin/jsarray.rs index af4ffa25f62..2b369e109dc 100644 --- a/boa_examples/src/jsarray.rs +++ b/boa_examples/src/bin/jsarray.rs @@ -1,9 +1,11 @@ +// This example shows how to manipulate a Javascript array using Rust code. + use boa_engine::{ object::{FunctionBuilder, JsArray}, - Context, JsValue, + Context, JsResult, JsValue, }; -pub fn run() -> Result<(), JsValue> { +fn main() -> JsResult<()> { // We create a new `Context` to create a new Javascript executor. let context = &mut Context::default(); diff --git a/boa_examples/src/loadfile.rs b/boa_examples/src/bin/loadfile.rs similarity index 85% rename from boa_examples/src/loadfile.rs rename to boa_examples/src/bin/loadfile.rs index fe8db6f0cc2..1692aecf92c 100644 --- a/boa_examples/src/loadfile.rs +++ b/boa_examples/src/bin/loadfile.rs @@ -1,8 +1,11 @@ +// This example shows how to load, parse and execute JS code from a source file +// (./scripts/helloworld.js) + use std::fs::read_to_string; use boa_engine::Context; -pub fn run() { +fn main() { let js_file_path = "./scripts/helloworld.js"; match read_to_string(js_file_path) { diff --git a/boa_examples/src/loadstring.rs b/boa_examples/src/bin/loadstring.rs similarity index 86% rename from boa_examples/src/loadstring.rs rename to boa_examples/src/bin/loadstring.rs index a60d46a689f..f3c50098e61 100644 --- a/boa_examples/src/loadstring.rs +++ b/boa_examples/src/bin/loadstring.rs @@ -1,6 +1,8 @@ +// This example loads, parses and executes a JS code string + use boa_engine::Context; -pub fn run() { +fn main() { let js_code = "console.log('Hello World from a JS code string!')"; // Instantiate the execution context diff --git a/boa_examples/src/modulehandler.rs b/boa_examples/src/bin/modulehandler.rs similarity index 65% rename from boa_examples/src/modulehandler.rs rename to boa_examples/src/bin/modulehandler.rs index 3d60ee7bfb5..e482447e331 100644 --- a/boa_examples/src/modulehandler.rs +++ b/boa_examples/src/bin/modulehandler.rs @@ -1,7 +1,10 @@ +// This example implements a custom module handler which mimics +// the require/module.exports pattern + use boa_engine::{prelude::JsObject, property::Attribute, Context, JsResult, JsValue}; use std::fs::read_to_string; -pub fn run() { +fn main() { let js_file_path = "./scripts/calctest.js"; let buffer = read_to_string(js_file_path); @@ -10,45 +13,45 @@ pub fn run() { return; } - //Creating the execution context + // Creating the execution context let mut ctx = Context::default(); - //Adding custom implementation that mimics 'require' + // Adding custom implementation that mimics 'require' ctx.register_global_function("require", 0, require); - //Adding custom object that mimics 'module.exports' + // Adding custom object that mimics 'module.exports' let moduleobj = JsObject::default(); moduleobj .set("exports", JsValue::from(" "), false, &mut ctx) .unwrap(); ctx.register_global_property("module", JsValue::from(moduleobj), Attribute::default()); - //Instantiating the engine with the execution context - //Loading, parsing and executing the JS code from the source file + // Instantiating the engine with the execution context + // Loading, parsing and executing the JS code from the source file ctx.eval(&buffer.unwrap()).unwrap(); } -//Custom implementation that mimics 'require' module loader +// Custom implementation that mimics the 'require' module loader fn require(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult { let arg = args.get(0).unwrap(); - //BUG: Dev branch seems to be passing string arguments along with quotes + // BUG: Dev branch seems to be passing string arguments along with quotes let libfile = arg .to_string(ctx) .expect("Failed to convert to string") - .replace('\"', ""); + .to_string(); - //Read the module source file + // Read the module source file println!("Loading: {}", libfile); let buffer = read_to_string(libfile); if let Err(..) = buffer { println!("Error: {}", buffer.unwrap_err()); Ok(JsValue::Rational(-1.0)) } else { - //Load and parse the module source + // Load and parse the module source ctx.eval(&buffer.unwrap()).unwrap(); - //Access module.exports and return as ResultValue + // Access module.exports and return as ResultValue let global_obj = ctx.global_object().to_owned(); let module = global_obj.get("module", ctx).unwrap(); module.as_object().unwrap().get("exports", ctx) diff --git a/boa_examples/src/main.rs b/boa_examples/src/main.rs deleted file mode 100644 index 8316e691540..00000000000 --- a/boa_examples/src/main.rs +++ /dev/null @@ -1,34 +0,0 @@ -mod classes; -mod closures; -mod jsarray; -mod loadfile; -mod loadstring; -mod modulehandler; - -fn main() { - println!("\r\n"); - - //example that loads, parses and executs a JS code string - loadstring::run(); - println!("\r\n"); - - //example that loads, parses and executs JS code from a source file (./scripts/helloworld.js) - loadfile::run(); - println!("\r\n"); - - //example that implements classes in Rust and exposes them to JS - classes::run(); - println!("\r\n"); - - //example that implements closures in Rust and exposes them to JS - closures::run().expect("closures failed to excecute"); - println!("\r\n"); - - //example that shows array manipulation in Rust - jsarray::run().expect("closures failed to excecute"); - println!("\r\n"); - - //example that implements a custom module handler which mimics (require / module.exports) pattern - modulehandler::run(); - println!("\r\n"); -}