Skip to content

Commit

Permalink
refactor: simplified invoke syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
baszalmstra committed Aug 24, 2021
1 parent 5953a7b commit 80610c0
Show file tree
Hide file tree
Showing 19 changed files with 350 additions and 382 deletions.
6 changes: 3 additions & 3 deletions book/listings/ch01-getting-started/listing04.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mun_runtime::{invoke_fn, RuntimeBuilder};
use mun_runtime::RuntimeBuilder;
use std::{cell::RefCell, env, rc::Rc};

fn main() {
Expand All @@ -11,8 +11,8 @@ fn main() {
loop {
{
let runtime_ref = runtime.borrow();
let arg: i64 = invoke_fn!(runtime_ref, "arg").unwrap();
let result: i64 = invoke_fn!(runtime_ref, "fibonacci", arg).unwrap();
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();
Expand Down
4 changes: 2 additions & 2 deletions book/listings/ch02-basic-concepts/listing02.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mun_runtime::{invoke_fn, RuntimeBuilder};
use mun_runtime::RuntimeBuilder;
use std::{cell::RefCell, rc::Rc};

fn main() {
Expand All @@ -7,6 +7,6 @@ fn main() {
.expect("Failed to spawn Runtime");

let runtime_ref = runtime.borrow();
let result: bool = invoke_fn!(runtime_ref, "random_bool").unwrap();
let result: bool = runtime_ref.invoke("random_bool", ()).unwrap();
println!("random bool: {}", result);
}
4 changes: 2 additions & 2 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::{invoke_fn, RuntimeBuilder};
use mun_runtime::RuntimeBuilder;
use std::{cell::RefCell, rc::Rc};

extern "C" fn random() -> i64 {
Expand All @@ -14,6 +14,6 @@ fn main() {
.expect("Failed to spawn Runtime");

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

fn main() {
Expand All @@ -10,7 +10,7 @@ fn main() {
.expect("Failed to spawn Runtime");

let runtime_ref = runtime.borrow();
let a: StructRef = invoke_fn!(runtime_ref, "vector2_new", -1.0f32, 1.0f32).unwrap();
let b: StructRef = invoke_fn!(runtime_ref, "vector2_new", 1.0f32, -1.0f32).unwrap();
let added: StructRef = invoke_fn!(runtime_ref, "vector2_add", a, b).unwrap();
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();
}
4 changes: 2 additions & 2 deletions book/listings/ch03-structs/listing12.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# extern crate mun_runtime;
# use mun_runtime::{invoke_fn, RuntimeBuilder, StructRef};
# use mun_runtime::{RuntimeBuilder, StructRef};
# use std::{cell::RefCell, env, rc::Rc};
#
# fn main() {
Expand All @@ -11,7 +11,7 @@
# .expect("Failed to spawn Runtime");
#
let runtime_ref = runtime.borrow();
let mut xy: StructRef = invoke_fn!(runtime_ref, "vector2_new", -1.0f32, 1.0f32).unwrap();
let mut xy: StructRef = runtime_ref.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
4 changes: 2 additions & 2 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::{invoke_fn, RuntimeBuilder, StructRef};
use mun_runtime::{RuntimeBuilder, StructRef};
use std::{env, time};

extern "C" fn log_f32(value: f32) {
Expand Down Expand Up @@ -35,7 +35,7 @@ fn main() {

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

Expand Down
17 changes: 12 additions & 5 deletions crates/mun/src/ops/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{cell::RefCell, rc::Rc};

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

use crate::ExitStatus;

Expand All @@ -24,15 +24,21 @@ 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 = invoke_fn!(borrowed, entry_point).map_err(|e| anyhow!("{}", e))?;
let result: bool = borrowed
.invoke(entry_point, ())
.map_err(|e| anyhow!("{}", e))?;

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

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

println!("{}", result)
} else {
Expand All @@ -44,7 +50,8 @@ pub fn start(matches: &ArgMatches) -> Result<ExitStatus, anyhow::Error> {
Ok(ExitStatus::Success)
} else {
#[allow(clippy::unit_arg)]
invoke_fn!(borrowed, entry_point)
borrowed
.invoke(entry_point, ())
.map(|_: ()| ExitStatus::Success)
.map_err(|e| anyhow!("{}", e))
}
Expand Down
4 changes: 2 additions & 2 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::{invoke_fn, RuntimeBuilder};
use mun_runtime::RuntimeBuilder;
use std::ffi::OsString;
use std::path::Path;

Expand Down Expand Up @@ -75,6 +75,6 @@ fn build_and_run(project: &Path) {

let runtime = RuntimeBuilder::new(&library_path).spawn().unwrap();
let runtime_ref = runtime.borrow();
let result: f64 = invoke_fn!(runtime_ref, "main").unwrap();
let result: f64 = runtime_ref.invoke("main", ()).unwrap();
assert_eq!(result, 3.14159);
}
1 change: 1 addition & 0 deletions crates/mun_runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ notify = "4.0.12"
once_cell = "1.4.0"
parking_lot = "0.11.1"
rustc-hash = "1.1"
seq-macro = "0.2.2"

[dev-dependencies]
compiler = { version="=0.3.0", path="../mun_compiler", package = "mun_compiler" }
Expand Down
8 changes: 5 additions & 3 deletions crates/mun_runtime/examples/buoyancy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mun_runtime::{invoke_fn, RuntimeBuilder, StructRef};
use mun_runtime::{RuntimeBuilder, StructRef};
use std::{env, time};

extern "C" fn log_f32(value: f32) {
Expand All @@ -18,7 +18,7 @@ fn main() {
.expect("Failed to spawn Runtime");

let runtime_ref = runtime.borrow();
let ctx: StructRef = invoke_fn!(runtime_ref, "new_sim").unwrap();
let ctx: StructRef = runtime_ref.invoke("new_sim", ()).unwrap();

let mut previous = time::Instant::now();
const FRAME_TIME: time::Duration = time::Duration::from_millis(40);
Expand All @@ -34,7 +34,9 @@ fn main() {
};

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

runtime.borrow_mut().update();
Expand Down
11 changes: 7 additions & 4 deletions crates/mun_runtime/examples/fibonacci.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mun_runtime::{invoke_fn, RuntimeBuilder};
use mun_runtime::RuntimeBuilder;
use std::env;

// How to run?
Expand All @@ -16,9 +16,12 @@ fn main() {
let mut runtime_ref = runtime.borrow_mut();

loop {
let n: i64 = invoke_fn!(runtime_ref, "nth").unwrap_or_else(|e| e.wait(&mut runtime_ref));
let result: i64 =
invoke_fn!(runtime_ref, "fibonacci", n).unwrap_or_else(|e| e.wait(&mut runtime_ref));
let n: i64 = runtime_ref
.invoke("nth", ())
.unwrap_or_else(|e| e.wait(&mut runtime_ref));
let result: i64 = runtime_ref
.invoke("fibonacci", (n,))
.unwrap_or_else(|e| e.wait(&mut runtime_ref));
println!("fibonacci({}) = {}", n, result);
runtime_ref.update();
}
Expand Down
1 change: 0 additions & 1 deletion crates/mun_runtime/src/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ impl<'s> Marshal<'s> for StructRef<'s> {

fn marshal_from<'r>(value: Self::MunType, runtime: &'r Runtime) -> Self
where
Self: 's,
'r: 's,
{
StructRef::new(value, runtime)
Expand Down
Loading

0 comments on commit 80610c0

Please sign in to comment.