Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: return Rc<RefCell<Runtime>> from builder #153

Merged
merged 1 commit into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/mun/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn build(matches: &ArgMatches) -> Result<(), failure::Error> {

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

let borrowed = runtime.borrow();
let entry_point = matches.value_of("entry").unwrap_or("main");
Expand Down Expand Up @@ -168,7 +168,7 @@ fn compiler_options(matches: &ArgMatches) -> Result<mun_compiler::CompilerOption
})
}

fn runtime(matches: &ArgMatches) -> Result<Runtime, failure::Error> {
fn runtime(matches: &ArgMatches) -> Result<Rc<RefCell<Runtime>>, failure::Error> {
let builder = RuntimeBuilder::new(
matches.value_of("LIBRARY").unwrap(), // Safe because its a required arg
);
Expand Down
3 changes: 0 additions & 3 deletions crates/mun_runtime/examples/hot_reloading.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use mun_runtime::{invoke_fn, RetryResultExt, RuntimeBuilder};
use std::cell::RefCell;
use std::env;
use std::rc::Rc;

// How to run?
// 1. On the CLI, navigate to the `crates/mun_runtime/examples` directory.
Expand All @@ -15,7 +13,6 @@ fn main() {
.spawn()
.expect("Failed to spawn Runtime");

let runtime = Rc::new(RefCell::new(runtime));
loop {
let n: i64 = invoke_fn!(runtime, "nth").wait();
let result: i64 = invoke_fn!(runtime, "fibonacci", n).wait();
Expand Down
19 changes: 12 additions & 7 deletions crates/mun_runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ use memory::gc::{self, GcRuntime};
use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher};
use rustc_hash::FxHashMap;
use std::{
cell::RefCell,
collections::HashMap,
ffi, io, mem,
path::{Path, PathBuf},
ptr::NonNull,
rc::Rc,
string::ToString,
sync::{
mpsc::{channel, Receiver},
Expand Down Expand Up @@ -64,10 +66,6 @@ impl RuntimeBuilder {
user_functions: Default::default(),
},
}
.insert_fn(
"new",
new as extern "C" fn(*const abi::TypeInfo, *mut ffi::c_void) -> *const *mut ffi::c_void,
)
}

/// Sets the `delay`.
Expand All @@ -85,8 +83,8 @@ impl RuntimeBuilder {
}

/// Spawns a [`Runtime`] with the builder's options.
pub fn spawn(self) -> Result<Runtime, Error> {
Runtime::new(self.options)
pub fn spawn(self) -> Result<Rc<RefCell<Runtime>>, Error> {
Runtime::new(self.options).map(|runtime| Rc::new(RefCell::new(runtime)))
}
}

Expand Down Expand Up @@ -201,11 +199,18 @@ impl Runtime {
/// Constructs a new `Runtime` that loads the library at `library_path` and its
/// dependencies. The `Runtime` contains a file watcher that is triggered with an interval
/// of `dur`.
pub fn new(options: RuntimeOptions) -> Result<Runtime, Error> {
pub fn new(mut options: RuntimeOptions) -> Result<Runtime, Error> {
let (tx, rx) = channel();

let mut dispatch_table = DispatchTable::default();

// Add internal functions
options.user_functions.push(IntoFunctionInfo::into(
new as extern "C" fn(*const abi::TypeInfo, *mut ffi::c_void) -> *const *mut ffi::c_void,
"new",
abi::Privacy::Public,
));

let mut storages = Vec::with_capacity(options.user_functions.len());
for (info, storage) in options.user_functions.into_iter() {
dispatch_table.insert_fn(info.signature.name().to_string(), info);
Expand Down
2 changes: 1 addition & 1 deletion crates/mun_runtime/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl RuntimeOrBuilder {
let previous = std::mem::replace(self, RuntimeOrBuilder::Pending);
let runtime = match previous {
RuntimeOrBuilder::Runtime(runtime) => runtime,
RuntimeOrBuilder::Builder(builder) => Rc::new(RefCell::new(builder.spawn()?)),
RuntimeOrBuilder::Builder(builder) => builder.spawn()?,
_ => unreachable!(),
};
std::mem::replace(self, RuntimeOrBuilder::Runtime(runtime));
Expand Down
12 changes: 9 additions & 3 deletions crates/mun_runtime_capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ pub mod hub;
mod tests;

use std::ffi::{c_void, CStr, CString};
use std::os::raw::c_char;
use std::{os::raw::c_char, time::Duration};

use crate::error::ErrorHandle;
use crate::hub::HUB;
use failure::err_msg;
use mun_abi::{FunctionInfo, StructInfo, TypeInfo};
use mun_runtime::{Runtime, RuntimeBuilder};
use mun_runtime::{Runtime, RuntimeOptions};

pub(crate) type Token = usize;

Expand Down Expand Up @@ -77,7 +77,13 @@ pub unsafe extern "C" fn mun_runtime_create(
}
};

let runtime = match RuntimeBuilder::new(library_path).spawn() {
let options = RuntimeOptions {
library_path: library_path.into(),
delay: Duration::from_millis(10),
user_functions: Default::default(),
};

let runtime = match Runtime::new(options) {
Ok(runtime) => runtime,
Err(e) => return HUB.errors.register(Box::new(e)),
};
Expand Down