Skip to content

Commit

Permalink
feat: make the runtime send!
Browse files Browse the repository at this point in the history
  • Loading branch information
baszalmstra committed Feb 27, 2022
1 parent 3a481eb commit bf8800f
Show file tree
Hide file tree
Showing 21 changed files with 381 additions and 462 deletions.
17 changes: 7 additions & 10 deletions book/listings/ch01-getting-started/listing04.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use mun_runtime::RuntimeBuilder;
use mun_runtime::Runtime;
use std::{cell::RefCell, env, rc::Rc};

fn main() {
let lib_path = env::args().nth(1).expect("Expected path to a Mun library.");

let mut runtime = RuntimeBuilder::new(lib_path)
.spawn()
let mut runtime = Runtime::builder(lib_path)
.finish()
.expect("Failed to spawn Runtime");

loop {
{
let runtime_ref = runtime.borrow();
let arg: i64 = runtime_ref.invoke("arg", ()).unwrap();
let result: i64 = runtime_ref.invoke("fibonacci", (arg,)).unwrap();
println!("fibonacci({}) = {}", arg, result);
}
runtime.borrow_mut().update();
let arg: i64 = runtime.invoke("arg", ()).unwrap();
let result: i64 = runtime.invoke("fibonacci", (arg,)).unwrap();
println!("fibonacci({}) = {}", arg, result);
runtime.update();
}
}
9 changes: 4 additions & 5 deletions book/listings/ch02-basic-concepts/listing02.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use mun_runtime::RuntimeBuilder;
use mun_runtime::Runtime;
use std::{cell::RefCell, rc::Rc};

fn main() {
let runtime = RuntimeBuilder::new("main.munlib")
.spawn()
let runtime = Runtime::builder("main.munlib")
.finish()
.expect("Failed to spawn Runtime");

let runtime_ref = runtime.borrow();
let result: bool = runtime_ref.invoke("random_bool", ()).unwrap();
let result: bool = runtime.invoke("random_bool", ()).unwrap();
println!("random bool: {}", result);
}
9 changes: 4 additions & 5 deletions book/listings/ch02-basic-concepts/listing03.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mun_runtime::RuntimeBuilder;
use mun_runtime::Runtime;
use std::{cell::RefCell, rc::Rc};

extern "C" fn random() -> i64 {
Expand All @@ -8,12 +8,11 @@ extern "C" fn random() -> i64 {
}

fn main() {
let runtime = RuntimeBuilder::new("main.munlib")
let runtime = Runtime::builder("main.munlib")
.insert_fn("random", random as extern "C" fn() -> i64)
.spawn()
.finish()
.expect("Failed to spawn Runtime");

let runtime_ref = runtime.borrow();
let result: bool = runtime_ref.invoke("random_bool", ()).unwrap();
let result: bool = runtime.invoke("random_bool", ()).unwrap();
println!("random_bool: {}", result);
}
13 changes: 6 additions & 7 deletions book/listings/ch03-structs/listing11.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# extern crate mun_runtime;
use mun_runtime::{RuntimeBuilder, StructRef};
use mun_runtime::{Runtime, StructRef};
use std::{cell::RefCell, env, rc::Rc};

fn main() {
let lib_path = env::args().nth(1).expect("Expected path to a Mun library.");

let runtime = RuntimeBuilder::new(lib_path)
.spawn()
let runtime = Runtime::builder(lib_path)
.finish()
.expect("Failed to spawn Runtime");

let runtime_ref = runtime.borrow();
let a: StructRef = runtime_ref.invoke("vector2_new", (-1.0f32, 1.0f32)).unwrap();
let b: StructRef = runtime_ref.invoke("vector2_new", (1.0f32, -1.0f32)).unwrap();
let added: StructRef = runtime_ref.invoke("vector2_add", (a, b)).unwrap();
let a: StructRef = runtime.invoke("vector2_new", (-1.0f32, 1.0f32)).unwrap();
let b: StructRef = runtime.invoke("vector2_new", (1.0f32, -1.0f32)).unwrap();
let added: StructRef = runtime.invoke("vector2_add", (a, b)).unwrap();
}
11 changes: 5 additions & 6 deletions book/listings/ch03-structs/listing12.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
# extern crate mun_runtime;
# use mun_runtime::{RuntimeBuilder, StructRef};
# use mun_runtime::{Runtime, StructRef};
# use std::{cell::RefCell, env, rc::Rc};
#
# fn main() {
# let lib_path = env::args().nth(1).expect("Expected path to a Mun library.");
#
# let mut runtime =
# RuntimeBuilder::new(lib_path)
# .spawn()
# let runtime =
# Runtime::builder(lib_path)
# .finish()
# .expect("Failed to spawn Runtime");
#
let runtime_ref = runtime.borrow();
let mut xy: StructRef = runtime_ref.invoke("vector2_new", (-1.0f32, 1.0f32)).unwrap();
let mut xy: StructRef = runtime.invoke("vector2_new", (-1.0f32, 1.0f32)).unwrap();
let x: f32 = xy.get("x").unwrap();
xy.set("x", x * x).unwrap();
let y = xy.replace("y", -1.0f32).unwrap();
Expand Down
19 changes: 6 additions & 13 deletions book/listings/ch03-structs/listing14.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# extern crate mun_runtime;
use mun_runtime::{RuntimeBuilder, StructRef};
use mun_runtime::{Runtime, StructRef};
use std::{env, time};

extern "C" fn log_f32(value: f32) {
Expand All @@ -9,16 +9,12 @@ extern "C" fn log_f32(value: f32) {
fn main() {
let lib_dir = env::args().nth(1).expect("Expected path to a Mun library.");

let runtime = RuntimeBuilder::new(lib_dir)
let runtime = Runtime::builder(lib_dir)
.insert_fn("log_f32", log_f32 as extern "C" fn(f32))
.spawn()
.finish()
.expect("Failed to spawn Runtime");

let ctx = {
let runtime_ref = runtime.borrow();
let ctx: StructRef = runtime_ref.invoke("new_sim", ()).unwrap();
ctx.root(runtime.clone())
};
let ctx = runtime.invoke::<StructRef, ()>("new_sim", ()).unwrap().root();

let mut previous = time::Instant::now();
const FRAME_TIME: time::Duration = time::Duration::from_millis(40);
Expand All @@ -33,12 +29,9 @@ fn main() {
elapsed.as_secs_f32()
};

{
let runtime_ref = runtime.borrow();
let _: () = runtime_ref.invoke("sim_update", (unsafe { ctx.as_ref(&runtime_ref) }, elapsed_secs)).unwrap();
}
let _: () = runtime.invoke("sim_update", (ctx.as_ref(&runtime), elapsed_secs)).unwrap();
previous = now;

runtime.borrow_mut().update();
runtime.update();
}
}
26 changes: 11 additions & 15 deletions crates/mun/src/ops/start.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use std::{cell::RefCell, rc::Rc};

use anyhow::anyhow;
use clap::ArgMatches;
use mun_runtime::{ReturnTypeReflection, Runtime, RuntimeBuilder};
use mun_runtime::{ReturnTypeReflection, Runtime};

use crate::ExitStatus;

/// Starts the runtime with the specified library and invokes function `entry`.
pub fn start(matches: &ArgMatches) -> Result<ExitStatus, anyhow::Error> {
pub fn start(matches: &ArgMatches) -> anyhow::Result<ExitStatus> {
let runtime = runtime(matches)?;

let borrowed = runtime.borrow();
let entry_point = matches.value_of("entry").unwrap_or("main");
let fn_definition = borrowed
let fn_definition = runtime
.get_function_definition(entry_point)
.ok_or_else(|| {
std::io::Error::new(
Expand All @@ -24,19 +21,19 @@ pub fn start(matches: &ArgMatches) -> Result<ExitStatus, anyhow::Error> {
if let Some(ret_type) = fn_definition.prototype.signature.return_type() {
let type_guid = &ret_type.guid;
if *type_guid == bool::type_guid() {
let result: bool = borrowed
let result: bool = runtime
.invoke(entry_point, ())
.map_err(|e| anyhow!("{}", e))?;

println!("{}", result)
} else if *type_guid == f64::type_guid() {
let result: f64 = borrowed
let result: f64 = runtime
.invoke(entry_point, ())
.map_err(|e| anyhow!("{}", e))?;

println!("{}", result)
} else if *type_guid == i64::type_guid() {
let result: i64 = borrowed
let result: i64 = runtime
.invoke(entry_point, ())
.map_err(|e| anyhow!("{}", e))?;

Expand All @@ -50,17 +47,16 @@ pub fn start(matches: &ArgMatches) -> Result<ExitStatus, anyhow::Error> {
Ok(ExitStatus::Success)
} else {
#[allow(clippy::unit_arg)]
borrowed
runtime
.invoke(entry_point, ())
.map(|_: ()| ExitStatus::Success)
.map_err(|e| anyhow!("{}", e))
}
}

fn runtime(matches: &ArgMatches) -> Result<Rc<RefCell<Runtime>>, anyhow::Error> {
let builder = RuntimeBuilder::new(
fn runtime(matches: &ArgMatches) -> anyhow::Result<Runtime> {
Runtime::builder(
matches.value_of("LIBRARY").unwrap(), // Safe because its a required arg
);

builder.spawn()
)
.finish()
}
7 changes: 3 additions & 4 deletions crates/mun/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use mun::run_with_args;
use mun_runtime::RuntimeBuilder;
use mun_runtime::Runtime;
use std::ffi::OsString;
use std::path::Path;

Expand Down Expand Up @@ -73,8 +73,7 @@ fn build_and_run(project: &Path) {
let library_path = project.join("target/mod.munlib");
assert!(library_path.is_file());

let runtime = RuntimeBuilder::new(&library_path).spawn().unwrap();
let runtime_ref = runtime.borrow();
let result: f64 = runtime_ref.invoke("main", ()).unwrap();
let runtime = Runtime::builder(&library_path).finish().unwrap();
let result: f64 = runtime.invoke("main", ()).unwrap();
assert_eq!(result, 3.14159);
}
5 changes: 5 additions & 0 deletions crates/mun_memory/src/gc/root_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ impl<T: TypeMemory + TypeTrace, G: GcRuntime<T>> GcRootPtr<T, G> {
}
}

/// Returns the runtime that owns the memory
pub fn runtime(&self) -> &Weak<G> {
&self.runtime
}

/// Returns the handle of this instance
pub fn handle(&self) -> GcPtr {
self.handle
Expand Down
19 changes: 10 additions & 9 deletions crates/mun_runtime/examples/buoyancy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mun_runtime::{RuntimeBuilder, StructRef};
use mun_runtime::{Runtime, StructRef};
use std::{env, time};

extern "C" fn log_f32(value: f32) {
Expand All @@ -12,13 +12,15 @@ extern "C" fn log_f32(value: f32) {
fn main() {
let lib_dir = env::args().nth(1).expect("Expected path to a Mun library.");

let runtime = RuntimeBuilder::new(lib_dir)
let mut runtime = Runtime::builder(lib_dir)
.insert_fn("log_f32", log_f32 as extern "C" fn(f32))
.spawn()
.finish()
.expect("Failed to spawn Runtime");

let runtime_ref = runtime.borrow();
let ctx: StructRef = runtime_ref.invoke("new_sim", ()).unwrap();
let ctx = runtime
.invoke::<StructRef, ()>("new_sim", ())
.unwrap()
.root();

let mut previous = time::Instant::now();
const FRAME_TIME: time::Duration = time::Duration::from_millis(40);
Expand All @@ -33,12 +35,11 @@ fn main() {
elapsed.as_secs_f32()
};

let runtime_ref = runtime.borrow();
let _: () = runtime_ref
.invoke("sim_update", (ctx.clone(), elapsed_secs))
let _: () = runtime
.invoke("sim_update", (ctx.as_ref(&runtime), elapsed_secs))
.unwrap();
previous = now;

runtime.borrow_mut().update();
runtime.update();
}
}
18 changes: 8 additions & 10 deletions crates/mun_runtime/examples/fibonacci.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mun_runtime::RuntimeBuilder;
use mun_runtime::Runtime;
use std::env;

// How to run?
Expand All @@ -9,20 +9,18 @@ fn main() {
let lib_dir = env::args().nth(1).expect("Expected path to a Mun library.");
println!("lib: {}", lib_dir);

let runtime = RuntimeBuilder::new(lib_dir)
.spawn()
let mut runtime = Runtime::builder(lib_dir)
.finish()
.expect("Failed to spawn Runtime");

let mut runtime_ref = runtime.borrow_mut();

loop {
let n: i64 = runtime_ref
let n: i64 = runtime
.invoke("nth", ())
.unwrap_or_else(|e| e.wait(&mut runtime_ref));
let result: i64 = runtime_ref
.unwrap_or_else(|e| e.wait(&mut runtime));
let result: i64 = runtime
.invoke("fibonacci", (n,))
.unwrap_or_else(|e| e.wait(&mut runtime_ref));
.unwrap_or_else(|e| e.wait(&mut runtime));
println!("fibonacci({}) = {}", n, result);
runtime_ref.update();
runtime.update();
}
}
Loading

0 comments on commit bf8800f

Please sign in to comment.