Skip to content

Commit b74092a

Browse files
authored
feat: make the runtime send (#370)
1 parent 3a481eb commit b74092a

23 files changed

Lines changed: 391 additions & 480 deletions

File tree

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
use mun_runtime::RuntimeBuilder;
1+
use mun_runtime::Runtime;
22
use std::{cell::RefCell, env, rc::Rc};
33

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

7-
let mut runtime = RuntimeBuilder::new(lib_path)
8-
.spawn()
7+
let mut runtime = Runtime::builder(lib_path)
8+
.finish()
99
.expect("Failed to spawn Runtime");
1010

1111
loop {
12-
{
13-
let runtime_ref = runtime.borrow();
14-
let arg: i64 = runtime_ref.invoke("arg", ()).unwrap();
15-
let result: i64 = runtime_ref.invoke("fibonacci", (arg,)).unwrap();
16-
println!("fibonacci({}) = {}", arg, result);
17-
}
18-
runtime.borrow_mut().update();
12+
let arg: i64 = runtime.invoke("arg", ()).unwrap();
13+
let result: i64 = runtime.invoke("fibonacci", (arg,)).unwrap();
14+
println!("fibonacci({}) = {}", arg, result);
15+
runtime.update();
1916
}
2017
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
use mun_runtime::RuntimeBuilder;
1+
use mun_runtime::Runtime;
22
use std::{cell::RefCell, rc::Rc};
33

44
fn main() {
5-
let runtime = RuntimeBuilder::new("main.munlib")
6-
.spawn()
5+
let runtime = Runtime::builder("main.munlib")
6+
.finish()
77
.expect("Failed to spawn Runtime");
88

9-
let runtime_ref = runtime.borrow();
10-
let result: bool = runtime_ref.invoke("random_bool", ()).unwrap();
9+
let result: bool = runtime.invoke("random_bool", ()).unwrap();
1110
println!("random bool: {}", result);
1211
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use mun_runtime::RuntimeBuilder;
1+
use mun_runtime::Runtime;
22
use std::{cell::RefCell, rc::Rc};
33

44
extern "C" fn random() -> i64 {
@@ -8,12 +8,11 @@ extern "C" fn random() -> i64 {
88
}
99

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

16-
let runtime_ref = runtime.borrow();
17-
let result: bool = runtime_ref.invoke("random_bool", ()).unwrap();
16+
let result: bool = runtime.invoke("random_bool", ()).unwrap();
1817
println!("random_bool: {}", result);
1918
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
# extern crate mun_runtime;
2-
use mun_runtime::{RuntimeBuilder, StructRef};
2+
use mun_runtime::{Runtime, StructRef};
33
use std::{cell::RefCell, env, rc::Rc};
44

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

8-
let runtime = RuntimeBuilder::new(lib_path)
9-
.spawn()
8+
let runtime = Runtime::builder(lib_path)
9+
.finish()
1010
.expect("Failed to spawn Runtime");
1111

12-
let runtime_ref = runtime.borrow();
13-
let a: StructRef = runtime_ref.invoke("vector2_new", (-1.0f32, 1.0f32)).unwrap();
14-
let b: StructRef = runtime_ref.invoke("vector2_new", (1.0f32, -1.0f32)).unwrap();
15-
let added: StructRef = runtime_ref.invoke("vector2_add", (a, b)).unwrap();
12+
let a: StructRef = runtime.invoke("vector2_new", (-1.0f32, 1.0f32)).unwrap();
13+
let b: StructRef = runtime.invoke("vector2_new", (1.0f32, -1.0f32)).unwrap();
14+
let added: StructRef = runtime.invoke("vector2_add", (a, b)).unwrap();
1615
}

book/listings/ch03-structs/listing12.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
# extern crate mun_runtime;
2-
# use mun_runtime::{RuntimeBuilder, StructRef};
2+
# use mun_runtime::{Runtime, StructRef};
33
# use std::{cell::RefCell, env, rc::Rc};
44
#
55
# fn main() {
66
# let lib_path = env::args().nth(1).expect("Expected path to a Mun library.");
77
#
8-
# let mut runtime =
9-
# RuntimeBuilder::new(lib_path)
10-
# .spawn()
8+
# let runtime =
9+
# Runtime::builder(lib_path)
10+
# .finish()
1111
# .expect("Failed to spawn Runtime");
1212
#
13-
let runtime_ref = runtime.borrow();
14-
let mut xy: StructRef = runtime_ref.invoke("vector2_new", (-1.0f32, 1.0f32)).unwrap();
13+
let mut xy: StructRef = runtime.invoke("vector2_new", (-1.0f32, 1.0f32)).unwrap();
1514
let x: f32 = xy.get("x").unwrap();
1615
xy.set("x", x * x).unwrap();
1716
let y = xy.replace("y", -1.0f32).unwrap();
Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# extern crate mun_runtime;
2-
use mun_runtime::{RuntimeBuilder, StructRef};
2+
use mun_runtime::{Runtime, StructRef};
33
use std::{env, time};
44

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

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

17-
let ctx = {
18-
let runtime_ref = runtime.borrow();
19-
let ctx: StructRef = runtime_ref.invoke("new_sim", ()).unwrap();
20-
ctx.root(runtime.clone())
21-
};
17+
let ctx = runtime.invoke::<StructRef, ()>("new_sim", ()).unwrap().root();
2218

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

36-
{
37-
let runtime_ref = runtime.borrow();
38-
let _: () = runtime_ref.invoke("sim_update", (unsafe { ctx.as_ref(&runtime_ref) }, elapsed_secs)).unwrap();
39-
}
32+
let _: () = runtime.invoke("sim_update", (ctx.as_ref(&runtime), elapsed_secs)).unwrap();
4033
previous = now;
4134

42-
runtime.borrow_mut().update();
35+
runtime.update();
4336
}
4437
}

crates/mun/src/ops/start.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
use std::{cell::RefCell, rc::Rc};
2-
31
use anyhow::anyhow;
42
use clap::ArgMatches;
5-
use mun_runtime::{ReturnTypeReflection, Runtime, RuntimeBuilder};
3+
use mun_runtime::{ReturnTypeReflection, Runtime};
64

75
use crate::ExitStatus;
86

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

13-
let borrowed = runtime.borrow();
1411
let entry_point = matches.value_of("entry").unwrap_or("main");
15-
let fn_definition = borrowed
12+
let fn_definition = runtime
1613
.get_function_definition(entry_point)
1714
.ok_or_else(|| {
1815
std::io::Error::new(
@@ -24,19 +21,19 @@ pub fn start(matches: &ArgMatches) -> Result<ExitStatus, anyhow::Error> {
2421
if let Some(ret_type) = fn_definition.prototype.signature.return_type() {
2522
let type_guid = &ret_type.guid;
2623
if *type_guid == bool::type_guid() {
27-
let result: bool = borrowed
24+
let result: bool = runtime
2825
.invoke(entry_point, ())
2926
.map_err(|e| anyhow!("{}", e))?;
3027

3128
println!("{}", result)
3229
} else if *type_guid == f64::type_guid() {
33-
let result: f64 = borrowed
30+
let result: f64 = runtime
3431
.invoke(entry_point, ())
3532
.map_err(|e| anyhow!("{}", e))?;
3633

3734
println!("{}", result)
3835
} else if *type_guid == i64::type_guid() {
39-
let result: i64 = borrowed
36+
let result: i64 = runtime
4037
.invoke(entry_point, ())
4138
.map_err(|e| anyhow!("{}", e))?;
4239

@@ -50,17 +47,16 @@ pub fn start(matches: &ArgMatches) -> Result<ExitStatus, anyhow::Error> {
5047
Ok(ExitStatus::Success)
5148
} else {
5249
#[allow(clippy::unit_arg)]
53-
borrowed
50+
runtime
5451
.invoke(entry_point, ())
5552
.map(|_: ()| ExitStatus::Success)
5653
.map_err(|e| anyhow!("{}", e))
5754
}
5855
}
5956

60-
fn runtime(matches: &ArgMatches) -> Result<Rc<RefCell<Runtime>>, anyhow::Error> {
61-
let builder = RuntimeBuilder::new(
57+
fn runtime(matches: &ArgMatches) -> anyhow::Result<Runtime> {
58+
Runtime::builder(
6259
matches.value_of("LIBRARY").unwrap(), // Safe because its a required arg
63-
);
64-
65-
builder.spawn()
60+
)
61+
.finish()
6662
}

crates/mun/tests/integration.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use mun::run_with_args;
2-
use mun_runtime::RuntimeBuilder;
2+
use mun_runtime::Runtime;
33
use std::ffi::OsString;
44
use std::path::Path;
55

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

76-
let runtime = RuntimeBuilder::new(&library_path).spawn().unwrap();
77-
let runtime_ref = runtime.borrow();
78-
let result: f64 = runtime_ref.invoke("main", ()).unwrap();
76+
let runtime = Runtime::builder(&library_path).finish().unwrap();
77+
let result: f64 = runtime.invoke("main", ()).unwrap();
7978
assert_eq!(result, 3.14159);
8079
}

crates/mun_memory/src/gc/root_ptr.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ impl<T: TypeMemory + TypeTrace, G: GcRuntime<T>> GcRootPtr<T, G> {
3636
}
3737
}
3838

39+
/// Returns the runtime that owns the memory
40+
pub fn runtime(&self) -> &Weak<G> {
41+
&self.runtime
42+
}
43+
3944
/// Returns the handle of this instance
4045
pub fn handle(&self) -> GcPtr {
4146
self.handle

crates/mun_runtime/benches/benchmarks.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ pub fn fibonacci_benchmark(c: &mut Criterion) {
1717
for i in [100i64, 200i64, 500i64, 1000i64, 4000i64, 8000i64].iter() {
1818
// Run Mun fibonacci
1919
group.bench_with_input(BenchmarkId::new("mun", i), i, |b, i| {
20-
let runtime_ref = runtime.borrow();
2120
b.iter(|| {
22-
let _: i64 = runtime_ref.invoke("main", (*i,)).unwrap();
21+
let _: i64 = runtime.invoke("main", (*i,)).unwrap();
2322
})
2423
});
2524

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

8079
group.bench_function("mun", |b| {
81-
let runtime_ref = runtime.borrow();
8280
b.iter(|| {
83-
let _: i64 = runtime_ref.invoke("empty", (black_box(20i64),)).unwrap();
81+
let _: i64 = runtime.invoke("empty", (black_box(20i64),)).unwrap();
8482
})
8583
});
8684
group.bench_function("rust", |b| b.iter(|| empty(black_box(20))));
@@ -114,9 +112,8 @@ struct RustParent<'a> {
114112
pub fn get_struct_field_benchmark(c: &mut Criterion) {
115113
// Perform setup (not part of the benchmark)
116114
let runtime = util::runtime_from_file("struct.mun");
117-
let runtime_ref = runtime.borrow_mut();
118-
let mun_gc_parent: StructRef = runtime_ref.invoke("make_gc_parent", ()).unwrap();
119-
let mun_value_parent: StructRef = runtime_ref.invoke("make_value_parent", ()).unwrap();
115+
let mun_gc_parent: StructRef = runtime.invoke("make_gc_parent", ()).unwrap();
116+
let mun_value_parent: StructRef = runtime.invoke("make_value_parent", ()).unwrap();
120117

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

210206
let rust_child = RustChild(-2.0, -1.0, 1.0, 2.0);
211207
let mut rust_child2 = rust_child.clone();

0 commit comments

Comments
 (0)