From 34085a64fccb9edca01c7708f678f2ea27799616 Mon Sep 17 00:00:00 2001 From: Wodann Date: Sat, 2 Nov 2019 20:35:20 +0100 Subject: [PATCH] feat(runtime_example): add example of hot reloading in the Mun Runtime --- crates/mun_runtime/examples/hot_reloading.rs | 22 +++++++++++++++++++ .../examples/resources/fibonacci.mun | 11 ++++++++++ 2 files changed, 33 insertions(+) create mode 100644 crates/mun_runtime/examples/hot_reloading.rs create mode 100644 crates/mun_runtime/examples/resources/fibonacci.mun diff --git a/crates/mun_runtime/examples/hot_reloading.rs b/crates/mun_runtime/examples/hot_reloading.rs new file mode 100644 index 000000000..7f544d14e --- /dev/null +++ b/crates/mun_runtime/examples/hot_reloading.rs @@ -0,0 +1,22 @@ +use mun_runtime::{invoke_fn, RetryResultExt, RuntimeBuilder}; +use std::env; + +// How to run? +// 1. On the CLI, navigate to the `crates/mun_runtime/examples` directory. +// 2. Run the compiler daemon from the CLI: `/path/to/mun build resources/fibonacci.mun --watch` +// 3. Run the application from the CLI: cargo run --example hot_reloading -- fibonacci.dll +fn main() { + let lib_dir = env::args().nth(1).expect("Expected path to a Mun library."); + println!("lib: {}", lib_dir); + + let mut runtime = RuntimeBuilder::new(lib_dir) + .spawn() + .expect("Failed to spawn Runtime"); + + loop { + let n: i64 = invoke_fn!(runtime, "nth").wait(); + let result: i64 = invoke_fn!(runtime, "fibonacci", n).wait(); + println!("fibonacci({}) = {}", n, result); + runtime.update(); + } +} diff --git a/crates/mun_runtime/examples/resources/fibonacci.mun b/crates/mun_runtime/examples/resources/fibonacci.mun new file mode 100644 index 000000000..4645748e7 --- /dev/null +++ b/crates/mun_runtime/examples/resources/fibonacci.mun @@ -0,0 +1,11 @@ +fn nth(): int { + 3 +} + +fn fibonacci(n:int):int { + if n <= 1 { + n + } else { + fibonacci(n-1) + fibonacci(n-2) + } +}