Skip to content

Commit 45cb803

Browse files
authored
Merge pull request #47 from tryandromeda/feat/flexibilize-macro-tasks-api
feat: Flexibilize macro tasks api
2 parents faf668d + cdfe44b commit 45cb803

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

cli/src/main.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This Source Code Form is subject to the terms of the Mozilla Public
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4-
use andromeda_core::{Runtime, RuntimeConfig};
4+
use andromeda_core::{HostData, Runtime, RuntimeConfig};
55
use andromeda_runtime::{
66
recommended_builtins, recommended_eventloop_handler, recommended_extensions,
77
};
@@ -49,14 +49,20 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4949
no_strict,
5050
paths,
5151
} => {
52-
let runtime = Runtime::new(RuntimeConfig {
53-
no_strict,
54-
paths,
55-
verbose,
56-
extensions: recommended_extensions(),
57-
builtins: recommended_builtins(),
58-
eventloop_handler: recommended_eventloop_handler,
59-
});
52+
let (macro_task_tx, macro_task_rx) = std::sync::mpsc::channel();
53+
let host_data = HostData::new(macro_task_tx);
54+
let runtime = Runtime::new(
55+
RuntimeConfig {
56+
no_strict,
57+
paths,
58+
verbose,
59+
extensions: recommended_extensions(),
60+
builtins: recommended_builtins(),
61+
eventloop_handler: recommended_eventloop_handler,
62+
macro_task_rx,
63+
},
64+
host_data,
65+
);
6066
let mut runtime_output = runtime.run();
6167

6268
match runtime_output.result {

core/src/host_data.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
sync::{
66
Arc,
77
atomic::{AtomicU32, Ordering},
8-
mpsc::{Receiver, Sender},
8+
mpsc::Sender,
99
},
1010
};
1111

@@ -33,18 +33,14 @@ pub struct HostData<UserMacroTask> {
3333
}
3434

3535
impl<UserMacroTask> HostData<UserMacroTask> {
36-
pub fn new() -> (Self, Receiver<MacroTask<UserMacroTask>>) {
37-
let (macro_task_tx, rx) = std::sync::mpsc::channel();
38-
(
39-
Self {
40-
storage: RefCell::new(AnyMap::new()),
41-
macro_task_tx,
42-
macro_task_count: Arc::new(AtomicU32::new(0)),
43-
tasks: RefCell::default(),
44-
task_count: Arc::default(),
45-
},
46-
rx,
47-
)
36+
pub fn new(macro_task_tx: Sender<MacroTask<UserMacroTask>>) -> Self {
37+
Self {
38+
storage: RefCell::new(AnyMap::new()),
39+
macro_task_tx,
40+
macro_task_count: Arc::new(AtomicU32::new(0)),
41+
tasks: RefCell::default(),
42+
task_count: Arc::default(),
43+
}
4844
}
4945

5046
/// Get an owned senderto the macro tasks event loop.

core/src/runtime.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,23 @@ pub struct RuntimeConfig<UserMacroTask: 'static> {
7979
pub builtins: Vec<&'static str>,
8080
/// User event loop handler.
8181
pub eventloop_handler: EventLoopHandler<UserMacroTask>,
82+
/// Macro tasks eventloop receiver.
83+
pub macro_task_rx: Receiver<MacroTask<UserMacroTask>>,
8284
}
8385

8486
pub struct Runtime<UserMacroTask: 'static> {
8587
pub config: RuntimeConfig<UserMacroTask>,
8688
pub agent: GcAgent,
8789
pub realm_root: RealmRoot,
8890
pub host_hooks: &'static RuntimeHostHooks<UserMacroTask>,
89-
pub macro_task_rx: Receiver<MacroTask<UserMacroTask>>,
9091
}
9192

9293
impl<UserMacroTask> Runtime<UserMacroTask> {
9394
/// Create a new [Runtime] given a [RuntimeConfig]. Use [Runtime::run] to run it.
94-
pub fn new(mut config: RuntimeConfig<UserMacroTask>) -> Self {
95-
let (host_data, macro_task_rx) = HostData::new();
95+
pub fn new(
96+
mut config: RuntimeConfig<UserMacroTask>,
97+
host_data: HostData<UserMacroTask>,
98+
) -> Self {
9699
let host_hooks = RuntimeHostHooks::new(host_data);
97100

98101
let host_hooks: &RuntimeHostHooks<UserMacroTask> = &*Box::leak(Box::new(host_hooks));
@@ -125,7 +128,6 @@ impl<UserMacroTask> Runtime<UserMacroTask> {
125128
agent,
126129
realm_root,
127130
host_hooks,
128-
macro_task_rx,
129131
}
130132
}
131133

@@ -204,7 +206,7 @@ impl<UserMacroTask> Runtime<UserMacroTask> {
204206

205207
// Listen for pending macro tasks and resolve one by one
206208
pub fn handle_macro_task(&mut self) {
207-
match self.macro_task_rx.recv() {
209+
match self.config.macro_task_rx.recv() {
208210
Ok(MacroTask::ResolvePromise(root_value)) => {
209211
self.agent.run_in_realm(&self.realm_root, |agent, gc| {
210212
let value = root_value.take(agent);

0 commit comments

Comments
 (0)