Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -49,14 +49,20 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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);
Comment on lines +52 to +53

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it easier to embed

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 {
Expand Down
22 changes: 9 additions & 13 deletions core/src/host_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
sync::{
Arc,
atomic::{AtomicU32, Ordering},
mpsc::{Receiver, Sender},
mpsc::Sender,
},
};

Expand Down Expand Up @@ -33,18 +33,14 @@ pub struct HostData<UserMacroTask> {
}

impl<UserMacroTask> HostData<UserMacroTask> {
pub fn new() -> (Self, Receiver<MacroTask<UserMacroTask>>) {
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<MacroTask<UserMacroTask>>) -> 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.
Expand Down
12 changes: 7 additions & 5 deletions core/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,23 @@ pub struct RuntimeConfig<UserMacroTask: 'static> {
pub builtins: Vec<&'static str>,
/// User event loop handler.
pub eventloop_handler: EventLoopHandler<UserMacroTask>,
/// Macro tasks eventloop receiver.
pub macro_task_rx: Receiver<MacroTask<UserMacroTask>>,
}

pub struct Runtime<UserMacroTask: 'static> {
pub config: RuntimeConfig<UserMacroTask>,
pub agent: GcAgent,
pub realm_root: RealmRoot,
pub host_hooks: &'static RuntimeHostHooks<UserMacroTask>,
pub macro_task_rx: Receiver<MacroTask<UserMacroTask>>,
}

impl<UserMacroTask> Runtime<UserMacroTask> {
/// Create a new [Runtime] given a [RuntimeConfig]. Use [Runtime::run] to run it.
pub fn new(mut config: RuntimeConfig<UserMacroTask>) -> Self {
let (host_data, macro_task_rx) = HostData::new();
pub fn new(
mut config: RuntimeConfig<UserMacroTask>,
host_data: HostData<UserMacroTask>,
) -> Self {
let host_hooks = RuntimeHostHooks::new(host_data);

let host_hooks: &RuntimeHostHooks<UserMacroTask> = &*Box::leak(Box::new(host_hooks));
Expand Down Expand Up @@ -125,7 +128,6 @@ impl<UserMacroTask> Runtime<UserMacroTask> {
agent,
realm_root,
host_hooks,
macro_task_rx,
}
}

Expand Down Expand Up @@ -204,7 +206,7 @@ impl<UserMacroTask> Runtime<UserMacroTask> {

// 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);
Expand Down