-
-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added boa examples as per issue #446 Overtaken #634 Somehow screwed that branch up by rebasing it and losing access pings @elasmojs This Pull Request fixes/closes #446 . Co-authored-by: Jason Williams <[email protected]> Co-authored-by: Iban Eguia (Razican) <[email protected]> Co-authored-by: jasonwilliams <[email protected]> Co-authored-by: jedel1043 <[email protected]>
- Loading branch information
1 parent
6ff36fb
commit 44b5617
Showing
15 changed files
with
175 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "boa_examples" | ||
version = "0.11.0" | ||
authors = ["boa-dev"] | ||
repository = "https://github.com/boa-dev/boa" | ||
license = "Unlicense/MIT" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
boa_engine = { path = "../boa_engine", features = ["console"] } | ||
boa_gc = { path = "../boa_gc" } | ||
gc = { version = "0.4.1" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("Hello World from JS file!"); |
2 changes: 1 addition & 1 deletion
2
boa_engine/examples/classes.rs → boa_examples/src/bin/classes.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
6 changes: 4 additions & 2 deletions
6
boa_engine/examples/jsarray.rs → boa_examples/src/bin/jsarray.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 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; | ||
|
||
fn main() { | ||
let js_file_path = "./scripts/helloworld.js"; | ||
|
||
match read_to_string(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()); | ||
} | ||
Err(e) => { | ||
// Pretty print the error | ||
eprintln!("Uncaught {}", e.display()); | ||
} | ||
}; | ||
} | ||
Err(msg) => eprintln!("Error: {}", msg), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// This example loads, parses and executes a JS code string | ||
|
||
use boa_engine::Context; | ||
|
||
fn main() { | ||
let js_code = "console.log('Hello World from a JS code string!')"; | ||
|
||
// Instantiate the execution context | ||
let mut context = Context::default(); | ||
|
||
// Parse the source code | ||
match context.eval(js_code) { | ||
Ok(res) => { | ||
println!("{}", res.to_string(&mut context).unwrap()); | ||
} | ||
Err(e) => { | ||
// Pretty print the error | ||
eprintln!("Uncaught {}", e.display()); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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; | ||
|
||
fn main() { | ||
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 mut ctx = Context::default(); | ||
|
||
// Adding custom implementation that mimics 'require' | ||
ctx.register_global_function("require", 0, require); | ||
|
||
// 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 | ||
ctx.eval(&buffer.unwrap()).unwrap(); | ||
} | ||
|
||
// Custom implementation that mimics the 'require' module loader | ||
fn require(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> { | ||
let arg = args.get(0).unwrap(); | ||
|
||
// BUG: Dev branch seems to be passing string arguments along with quotes | ||
let libfile = arg | ||
.to_string(ctx) | ||
.expect("Failed to convert to string") | ||
.to_string(); | ||
|
||
// 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 | ||
ctx.eval(&buffer.unwrap()).unwrap(); | ||
|
||
// 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) | ||
} | ||
} |