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

feat(cli): add support for all native return types when starting a Mun library #45

Merged
merged 1 commit into from
Nov 10, 2019
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
1 change: 1 addition & 0 deletions crates/mun/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ default-run = "mun"
[dependencies]
failure = "0.1.5"
clap = "2.33.0"
mun_abi = { path = "../mun_abi" }
mun_compiler = { path = "../mun_compiler" }
mun_compiler_daemon = { path = "../mun_compiler_daemon" }
mun_runtime = { path = "../mun_runtime" }
Expand Down
37 changes: 35 additions & 2 deletions crates/mun/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate failure;
use std::time::Duration;

use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
use mun_abi::Reflection;
use mun_compiler::PathOrInline;
use mun_runtime::{invoke_fn, Runtime, RuntimeBuilder};

Expand Down Expand Up @@ -87,9 +88,41 @@ fn start(matches: &ArgMatches) -> Result<(), failure::Error> {
let mut runtime = runtime(matches)?;

let entry_point = matches.value_of("entry").unwrap_or("main");
let fn_info = runtime.get_function_info(entry_point).ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!("Failed to obtain entry point '{}'", entry_point),
)
})?;

if let Some(ret_type) = fn_info.signature.return_type() {
let type_guid = ret_type.guid;
if type_guid == bool::type_guid() {
let result: bool =
invoke_fn!(runtime, entry_point).map_err(|e| failure::err_msg(format!("{}", e)))?;

println!("{}", result)
} else if type_guid == f64::type_guid() {
let result: f64 =
invoke_fn!(runtime, entry_point).map_err(|e| failure::err_msg(format!("{}", e)))?;

#[allow(clippy::unit_arg)]
invoke_fn!(runtime, entry_point).map_err(|e| failure::err_msg(format!("{}", e)))
println!("{}", result)
} else if type_guid == i64::type_guid() {
let result: i64 =
invoke_fn!(runtime, entry_point).map_err(|e| failure::err_msg(format!("{}", e)))?;

println!("{}", result)
} else {
return Err(failure::err_msg(format!(
"Only native Mun return types are supported for entry points. Found: {}",
ret_type.name()
)));
};
Ok(())
} else {
#[allow(clippy::unit_arg)]
invoke_fn!(runtime, entry_point).map_err(|e| failure::err_msg(format!("{}", e)))
}
}

fn compiler_options(matches: &ArgMatches) -> Result<mun_compiler::CompilerOptions, failure::Error> {
Expand Down