Skip to content

Commit

Permalink
Split examples into separate executables (#1898)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 authored Mar 7, 2022
1 parent 806c1eb commit 9535f99
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 58 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/bors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ members = [
"boa_tester",
"boa_unicode",
"boa_wasm",
"boa_examples"
"boa_examples",
]

# The release profile, used for `cargo build --release`.
Expand Down
2 changes: 1 addition & 1 deletion boa_examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ edition = "2021"
[dependencies]
boa_engine = { path = "../boa_engine", features = ["console"] }
boa_gc = { path = "../boa_gc" }
gc = { version = "0.4.1" }
gc = { version = "0.4.1" }
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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<JsValue> {
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)
Expand Down
34 changes: 0 additions & 34 deletions boa_examples/src/main.rs

This file was deleted.

0 comments on commit 9535f99

Please sign in to comment.