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 59a618e
Show file tree
Hide file tree
Showing 23 changed files with 391 additions and 480 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
16 changes: 6 additions & 10 deletions crates/mun_runtime/benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ pub fn fibonacci_benchmark(c: &mut Criterion) {
for i in [100i64, 200i64, 500i64, 1000i64, 4000i64, 8000i64].iter() {
// Run Mun fibonacci
group.bench_with_input(BenchmarkId::new("mun", i), i, |b, i| {
let runtime_ref = runtime.borrow();
b.iter(|| {
let _: i64 = runtime_ref.invoke("main", (*i,)).unwrap();
let _: i64 = runtime.invoke("main", (*i,)).unwrap();
})
});

Expand Down Expand Up @@ -78,9 +77,8 @@ pub fn empty_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("empty");

group.bench_function("mun", |b| {
let runtime_ref = runtime.borrow();
b.iter(|| {
let _: i64 = runtime_ref.invoke("empty", (black_box(20i64),)).unwrap();
let _: i64 = runtime.invoke("empty", (black_box(20i64),)).unwrap();
})
});
group.bench_function("rust", |b| b.iter(|| empty(black_box(20))));
Expand Down Expand Up @@ -114,9 +112,8 @@ struct RustParent<'a> {
pub fn get_struct_field_benchmark(c: &mut Criterion) {
// Perform setup (not part of the benchmark)
let runtime = util::runtime_from_file("struct.mun");
let runtime_ref = runtime.borrow_mut();
let mun_gc_parent: StructRef = runtime_ref.invoke("make_gc_parent", ()).unwrap();
let mun_value_parent: StructRef = runtime_ref.invoke("make_value_parent", ()).unwrap();
let mun_gc_parent: StructRef = runtime.invoke("make_gc_parent", ()).unwrap();
let mun_value_parent: StructRef = runtime.invoke("make_value_parent", ()).unwrap();

let rust_child = RustChild(-2.0, -1.0, 1.0, 2.0);
let rust_parent = RustParent {
Expand Down Expand Up @@ -203,9 +200,8 @@ pub fn get_struct_field_benchmark(c: &mut Criterion) {
pub fn set_struct_field_benchmark(c: &mut Criterion) {
// Perform setup (not part of the benchmark)
let runtime = util::runtime_from_file("struct.mun");
let runtime_ref = runtime.borrow();
let mut mun_gc_parent: StructRef = runtime_ref.invoke("make_value_parent", ()).unwrap();
let mut mun_value_parent: StructRef = runtime_ref.invoke("make_value_parent", ()).unwrap();
let mut mun_gc_parent: StructRef = runtime.invoke("make_value_parent", ()).unwrap();
let mut mun_value_parent: StructRef = runtime.invoke("make_value_parent", ()).unwrap();

let rust_child = RustChild(-2.0, -1.0, 1.0, 2.0);
let mut rust_child2 = rust_child.clone();
Expand Down
12 changes: 4 additions & 8 deletions crates/mun_runtime/benches/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use compiler::{Config, DisplayColor, Driver, OptimizationLevel, PathOrInline};
use mlua::Lua;
use mun_runtime::RuntimeBuilder;
use std::{
cell::RefCell,
path::{Path, PathBuf},
rc::Rc,
};
use mun_runtime::Runtime;
use std::path::{Path, PathBuf};
use wasmer_runtime::{instantiate, Instance};

fn compute_resource_path<P: AsRef<Path>>(p: P) -> PathBuf {
Expand All @@ -14,7 +10,7 @@ fn compute_resource_path<P: AsRef<Path>>(p: P) -> PathBuf {
.join(p)
}

pub fn runtime_from_file<P: AsRef<Path>>(p: P) -> Rc<RefCell<mun_runtime::Runtime>> {
pub fn runtime_from_file<P: AsRef<Path>>(p: P) -> Runtime {
let path = PathOrInline::Path(compute_resource_path(p));
let (mut driver, file_id) = Driver::with_file(
Config {
Expand All @@ -33,7 +29,7 @@ pub fn runtime_from_file<P: AsRef<Path>>(p: P) -> Rc<RefCell<mun_runtime::Runtim

let out_path = driver.assembly_output_path_from_file(file_id);
driver.write_all_assemblies(false).unwrap();
RuntimeBuilder::new(out_path).spawn().unwrap()
Runtime::builder(out_path).finish().unwrap()
}

pub fn lua_from_file<P: AsRef<Path>>(p: P) -> Lua {
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();
}
}
Loading

0 comments on commit 59a618e

Please sign in to comment.