From a9ef4670dba84b719e1251b166e62804a626a629 Mon Sep 17 00:00:00 2001 From: marc2332 Date: Sat, 19 Apr 2025 23:52:06 +0200 Subject: [PATCH 1/4] feat: Flexibilize macro tasks api --- core/src/host_data.rs | 22 +++++++++------------- core/src/runtime.rs | 22 ++++++++++++++++++---- 2 files changed, 27 insertions(+), 17 deletions(-) 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..3cf3d283 100644 --- a/core/src/runtime.rs +++ b/core/src/runtime.rs @@ -3,7 +3,10 @@ use std::{ borrow::BorrowMut, cell::RefCell, collections::VecDeque, - sync::{atomic::Ordering, mpsc::Receiver}, + sync::{ + atomic::Ordering, + mpsc::{Receiver, Sender}, + }, }; use nova_vm::{ @@ -79,6 +82,17 @@ pub struct RuntimeConfig { pub builtins: Vec<&'static str>, /// User event loop handler. pub eventloop_handler: EventLoopHandler, + /// Macro tasks sender. + pub macro_task_tx: Sender>, + /// Macro tasks receiver. + pub macro_task_rx: Receiver>, +} + +pub struct RuntimeData { + /// Macro tasks sender. + pub macro_task_tx: Sender>, + /// Macro tasks receiver. + pub macro_task_rx: Receiver>, } pub struct Runtime { @@ -91,8 +105,8 @@ pub struct Runtime { 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, data: RuntimeData) -> Self { + let host_data = HostData::new(data.macro_task_tx); let host_hooks = RuntimeHostHooks::new(host_data); let host_hooks: &RuntimeHostHooks = &*Box::leak(Box::new(host_hooks)); @@ -125,7 +139,7 @@ impl Runtime { agent, realm_root, host_hooks, - macro_task_rx, + macro_task_rx: data.macro_task_rx, } } From f42fdb44747c9d3e3fa9ee4d0662ffe1fd393c24 Mon Sep 17 00:00:00 2001 From: marc2332 Date: Sat, 19 Apr 2025 23:54:07 +0200 Subject: [PATCH 2/4] clean up --- core/src/runtime.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/src/runtime.rs b/core/src/runtime.rs index 3cf3d283..88829e69 100644 --- a/core/src/runtime.rs +++ b/core/src/runtime.rs @@ -82,10 +82,6 @@ pub struct RuntimeConfig { pub builtins: Vec<&'static str>, /// User event loop handler. pub eventloop_handler: EventLoopHandler, - /// Macro tasks sender. - pub macro_task_tx: Sender>, - /// Macro tasks receiver. - pub macro_task_rx: Receiver>, } pub struct RuntimeData { From abd51f09d1af3fd0bdbb960d82670adbe0830232 Mon Sep 17 00:00:00 2001 From: marc2332 Date: Sun, 20 Apr 2025 09:40:50 +0200 Subject: [PATCH 3/4] clean up --- cli/src/main.rs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 41bedece..b27c342a 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::{Runtime, RuntimeConfig, RuntimeData}; use andromeda_runtime::{ recommended_builtins, recommended_eventloop_handler, recommended_extensions, }; @@ -49,14 +49,21 @@ 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 runtime = Runtime::new( + RuntimeConfig { + no_strict, + paths, + verbose, + extensions: recommended_extensions(), + builtins: recommended_builtins(), + eventloop_handler: recommended_eventloop_handler, + }, + RuntimeData { + macro_task_tx, + macro_task_rx, + }, + ); let mut runtime_output = runtime.run(); match runtime_output.result { From cdfe44be7f364509279e6ab367363970d4a3cb42 Mon Sep 17 00:00:00 2001 From: marc2332 Date: Sun, 20 Apr 2025 09:44:34 +0200 Subject: [PATCH 4/4] clean up --- cli/src/main.rs | 7 +++---- core/src/runtime.rs | 22 +++++++--------------- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index b27c342a..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, RuntimeData}; +use andromeda_core::{HostData, Runtime, RuntimeConfig}; use andromeda_runtime::{ recommended_builtins, recommended_eventloop_handler, recommended_extensions, }; @@ -50,6 +50,7 @@ fn main() -> Result<(), Box> { paths, } => { 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, @@ -58,11 +59,9 @@ fn main() -> Result<(), Box> { extensions: recommended_extensions(), builtins: recommended_builtins(), eventloop_handler: recommended_eventloop_handler, - }, - RuntimeData { - macro_task_tx, macro_task_rx, }, + host_data, ); let mut runtime_output = runtime.run(); diff --git a/core/src/runtime.rs b/core/src/runtime.rs index 88829e69..998434a8 100644 --- a/core/src/runtime.rs +++ b/core/src/runtime.rs @@ -3,10 +3,7 @@ use std::{ borrow::BorrowMut, cell::RefCell, collections::VecDeque, - sync::{ - atomic::Ordering, - mpsc::{Receiver, Sender}, - }, + sync::{atomic::Ordering, mpsc::Receiver}, }; use nova_vm::{ @@ -82,12 +79,7 @@ pub struct RuntimeConfig { pub builtins: Vec<&'static str>, /// User event loop handler. pub eventloop_handler: EventLoopHandler, -} - -pub struct RuntimeData { - /// Macro tasks sender. - pub macro_task_tx: Sender>, - /// Macro tasks receiver. + /// Macro tasks eventloop receiver. pub macro_task_rx: Receiver>, } @@ -96,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, data: RuntimeData) -> Self { - let host_data = HostData::new(data.macro_task_tx); + 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)); @@ -135,7 +128,6 @@ impl Runtime { agent, realm_root, host_hooks, - macro_task_rx: data.macro_task_rx, } } @@ -214,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);