Skip to content

Commit

Permalink
Fixed the Boa examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Razican committed Mar 30, 2023
1 parent e4390c7 commit b545934
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions boa_examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ boa_ast.workspace = true
boa_interner.workspace = true
boa_gc.workspace = true
boa_parser.workspace = true
boa_runtime.workspace = true
smol = "1.3.0"
futures-util = "0.3.27"
15 changes: 13 additions & 2 deletions boa_examples/src/bin/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use boa_engine::{
};

use boa_gc::{Finalize, Trace};
use boa_runtime::Console;

// We create a new struct that is going to represent a person.
//
Expand Down Expand Up @@ -124,12 +125,22 @@ impl Class for Person {
}
}

/// Adds the custom runtime to the context.
fn add_runtime(context: &mut Context<'_>) {
// We first add the `console` object, to be able to call `console.log()`.
let console = Console::init(context);
context.register_global_property(Console::NAME, console, Attribute::all());

// Then we need to register the global class `Person` inside `context`.
context.register_global_class::<Person>().unwrap();
}

fn main() {
// First we need to create a Javascript context.
let mut context = Context::default();

// Then we need to register the global class `Person` inside `context`.
context.register_global_class::<Person>().unwrap();
// Then, we add our custom runtime.
add_runtime(&mut context);

// Having done all of that, we can execute Javascript code with `eval`,
// and access the `Person` class defined in Rust!
Expand Down
16 changes: 14 additions & 2 deletions boa_examples/src/bin/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use boa_engine::{
context::ContextBuilder,
job::{FutureJob, JobQueue, NativeJob},
native_function::NativeFunction,
property::Attribute,
Context, JsArgs, JsResult, JsValue, Source,
};
use boa_runtime::Console;
use futures_util::{stream::FuturesUnordered, Future};
use smol::{future, stream::StreamExt, LocalExecutor};

Expand Down Expand Up @@ -128,14 +130,24 @@ fn delay(
}
}

/// Adds the custom runtime to the context.
fn add_runtime(context: &mut Context<'_>) {
// We first add the `console` object, to be able to call `console.log()`.
let console = Console::init(context);
context.register_global_property(Console::NAME, console, Attribute::all());

// Then, we bind the defined async function to the ECMAScript function "delay".
context.register_global_builtin_callable("delay", 1, NativeFunction::from_async_fn(delay));
}

fn main() {
// Initialize the required executors and the context
let executor = LocalExecutor::new();
let queue = Queue::new(executor);
let context = &mut ContextBuilder::new().job_queue(&queue).build().unwrap();

// Bind the defined async function to the ECMAScript function "delay".
context.register_global_builtin_callable("delay", 1, NativeFunction::from_async_fn(delay));
// Then, we add our custom runtime.
add_runtime(context);

// Multiple calls to multiple async timers.
let script = r#"
Expand Down
2 changes: 1 addition & 1 deletion boa_examples/src/bin/loadstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use boa_engine::{Context, Source};

fn main() {
let js_code = "console.log('Hello World from a JS code string!')";
let js_code = "'Hello World ' + 'from a JS code ' + 'string!'";

// Instantiate the execution context
let mut context = Context::default();
Expand Down

0 comments on commit b545934

Please sign in to comment.