diff --git a/cli/src/main.rs b/cli/src/main.rs index 41bedece..60a3cda7 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,7 +1,7 @@ // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -use andromeda_core::{Runtime, RuntimeConfig}; +use andromeda_core::{HostData, Runtime, RuntimeConfig}; use andromeda_runtime::{ recommended_builtins, recommended_eventloop_handler, recommended_extensions, }; @@ -49,14 +49,20 @@ fn main() -> Result<(), Box> { no_strict, paths, } => { - let runtime = Runtime::new(RuntimeConfig { - no_strict, - paths, - verbose, - extensions: recommended_extensions(), - builtins: recommended_builtins(), - eventloop_handler: recommended_eventloop_handler, - }); + let (macro_task_tx, macro_task_rx) = std::sync::mpsc::channel(); + let host_data = HostData::new(macro_task_tx); + let runtime = Runtime::new( + RuntimeConfig { + no_strict, + paths, + verbose, + extensions: recommended_extensions(), + builtins: recommended_builtins(), + eventloop_handler: recommended_eventloop_handler, + macro_task_rx, + }, + host_data, + ); let mut runtime_output = runtime.run(); match runtime_output.result { diff --git a/core/src/host_data.rs b/core/src/host_data.rs index 37661af6..97f5c288 100644 --- a/core/src/host_data.rs +++ b/core/src/host_data.rs @@ -5,7 +5,7 @@ use std::{ sync::{ Arc, atomic::{AtomicU32, Ordering}, - mpsc::{Receiver, Sender}, + mpsc::Sender, }, }; @@ -33,18 +33,14 @@ pub struct HostData { } impl HostData { - pub fn new() -> (Self, Receiver>) { - let (macro_task_tx, rx) = std::sync::mpsc::channel(); - ( - Self { - storage: RefCell::new(AnyMap::new()), - macro_task_tx, - macro_task_count: Arc::new(AtomicU32::new(0)), - tasks: RefCell::default(), - task_count: Arc::default(), - }, - rx, - ) + pub fn new(macro_task_tx: Sender>) -> Self { + Self { + storage: RefCell::new(AnyMap::new()), + macro_task_tx, + macro_task_count: Arc::new(AtomicU32::new(0)), + tasks: RefCell::default(), + task_count: Arc::default(), + } } /// Get an owned senderto the macro tasks event loop. diff --git a/core/src/runtime.rs b/core/src/runtime.rs index ed1087f6..998434a8 100644 --- a/core/src/runtime.rs +++ b/core/src/runtime.rs @@ -79,6 +79,8 @@ pub struct RuntimeConfig { pub builtins: Vec<&'static str>, /// User event loop handler. pub eventloop_handler: EventLoopHandler, + /// Macro tasks eventloop receiver. + pub macro_task_rx: Receiver>, } pub struct Runtime { @@ -86,13 +88,14 @@ pub struct Runtime { pub agent: GcAgent, pub realm_root: RealmRoot, pub host_hooks: &'static RuntimeHostHooks, - pub macro_task_rx: Receiver>, } impl Runtime { /// Create a new [Runtime] given a [RuntimeConfig]. Use [Runtime::run] to run it. - pub fn new(mut config: RuntimeConfig) -> Self { - let (host_data, macro_task_rx) = HostData::new(); + pub fn new( + mut config: RuntimeConfig, + host_data: HostData, + ) -> Self { let host_hooks = RuntimeHostHooks::new(host_data); let host_hooks: &RuntimeHostHooks = &*Box::leak(Box::new(host_hooks)); @@ -125,7 +128,6 @@ impl Runtime { agent, realm_root, host_hooks, - macro_task_rx, } } @@ -204,7 +206,7 @@ impl Runtime { // Listen for pending macro tasks and resolve one by one pub fn handle_macro_task(&mut self) { - match self.macro_task_rx.recv() { + match self.config.macro_task_rx.recv() { Ok(MacroTask::ResolvePromise(root_value)) => { self.agent.run_in_realm(&self.realm_root, |agent, gc| { let value = root_value.take(agent);