From 1501e7b0a395485516f5cc5d52eb7127d4d9635e Mon Sep 17 00:00:00 2001 From: Wodann Date: Sat, 9 Nov 2019 12:40:44 +0100 Subject: [PATCH] feat(cli): add support for all native return types when starting a Mun library --- crates/mun/Cargo.toml | 1 + crates/mun/src/main.rs | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/crates/mun/Cargo.toml b/crates/mun/Cargo.toml index 40b3ccb82..d2973d57d 100644 --- a/crates/mun/Cargo.toml +++ b/crates/mun/Cargo.toml @@ -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" } diff --git a/crates/mun/src/main.rs b/crates/mun/src/main.rs index 0b315e078..cc26f8731 100644 --- a/crates/mun/src/main.rs +++ b/crates/mun/src/main.rs @@ -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}; @@ -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 {