diff --git a/Cargo.lock b/Cargo.lock index e44b5e7c90d..88fce419289 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -429,6 +429,7 @@ dependencies = [ "boa_gc", "boa_interner", "boa_parser", + "boa_runtime", "futures-util", "smol", ] diff --git a/boa_examples/Cargo.toml b/boa_examples/Cargo.toml index 01531077ba8..30b2ee3843b 100644 --- a/boa_examples/Cargo.toml +++ b/boa_examples/Cargo.toml @@ -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" diff --git a/boa_examples/src/bin/classes.rs b/boa_examples/src/bin/classes.rs index 3782ba87d94..4be0450a038 100644 --- a/boa_examples/src/bin/classes.rs +++ b/boa_examples/src/bin/classes.rs @@ -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. // @@ -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::().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::().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! diff --git a/boa_examples/src/bin/futures.rs b/boa_examples/src/bin/futures.rs index cbc6cf5281f..1e3938630bb 100644 --- a/boa_examples/src/bin/futures.rs +++ b/boa_examples/src/bin/futures.rs @@ -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}; @@ -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#" diff --git a/boa_examples/src/bin/loadstring.rs b/boa_examples/src/bin/loadstring.rs index cd394df6f8a..52fc116a6ee 100644 --- a/boa_examples/src/bin/loadstring.rs +++ b/boa_examples/src/bin/loadstring.rs @@ -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();