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"); -}