add -dir option; refactor Config

This commit is contained in:
vms 2020-04-26 20:36:31 +03:00
parent e1e873b2fc
commit 978bb90669
3 changed files with 94 additions and 27 deletions

View File

@ -34,6 +34,7 @@ use clap::{App, AppSettings, Arg, SubCommand};
use exitfailure::ExitFailure;
use failure::err_msg;
use std::fs;
use std::path::PathBuf;
const VERSION: &str = env!("CARGO_PKG_VERSION");
const AUTHORS: &str = env!("CARGO_PKG_AUTHORS");
@ -41,8 +42,9 @@ const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
const IN_MODULE_PATH: &str = "in-wasm-path";
const INVOKE_ARG: &str = "arg";
const WASI_DIR: &str = "dir";
fn prepare_args<'a, 'b>() -> [Arg<'a, 'b>; 2] {
fn prepare_args<'a, 'b>() -> [Arg<'a, 'b>; 3] {
[
Arg::with_name(IN_MODULE_PATH)
.required(true)
@ -54,6 +56,13 @@ fn prepare_args<'a, 'b>() -> [Arg<'a, 'b>; 2] {
.takes_value(true)
.short("a")
.help("argument for the invoke function in the Wasm module"),
Arg::with_name(WASI_DIR)
.required(true)
.takes_value(true)
.multiple(true)
.number_of_values(1)
.short("d")
.help("preopened directory for wasi subsystem"),
]
}
@ -73,11 +82,19 @@ fn main() -> Result<(), ExitFailure> {
match app.get_matches().subcommand() {
("execute", Some(arg)) => {
let config = Config::default();
let in_module_path = arg.value_of(IN_MODULE_PATH).unwrap();
let wasm_code = fs::read(in_module_path)?;
let invoke_arg = arg.value_of(INVOKE_ARG).unwrap();
let preopened_dirs: Vec<_> = arg.values_of(WASI_DIR).unwrap().collect();
let config = Config::default().with_wasi_preopened_files(
preopened_dirs
.into_iter()
.map(PathBuf::from)
.collect::<Vec<PathBuf>>(),
);
let mut frank = Frank::new(&wasm_code, config)?;
let result = frank.invoke(invoke_arg.as_bytes())?;

View File

@ -55,19 +55,19 @@ pub struct Config {
pub logger_enabled: bool,
/// The name of the main module handler function.
pub invoke_function_name: String,
pub invoke_fn_name: String,
/// The name of function that should be called for allocation memory. This function
/// is used for passing array of bytes to the main module.
pub allocate_function_name: String,
pub allocate_fn_name: String,
/// The name of function that should be called for deallocation of
/// previously allocated memory by allocateFunction.
pub deallocate_function_name: String,
pub deallocate_fn_name: String,
/// Config for WASI subsystem initialization. None means that module should be loaded
/// without WASI.
pub wasi_config: Option<WASIConfig>,
pub wasi_config: WASIConfig,
}
impl Default for Config {
@ -76,11 +76,67 @@ impl Default for Config {
Self {
// 65536*1600 ~ 100 Mb
mem_pages_count: 1600,
invoke_function_name: "invoke".to_string(),
allocate_function_name: "allocate".to_string(),
deallocate_function_name: "deallocate".to_string(),
invoke_fn_name: "invoke".to_string(),
allocate_fn_name: "allocate".to_string(),
deallocate_fn_name: "deallocate".to_string(),
logger_enabled: true,
wasi_config: Some(WASIConfig::default()),
wasi_config: WASIConfig::default(),
}
}
}
impl Config {
#[allow(dead_code)]
pub fn with_mem_pages_count(mut self, mem_pages_count: u32) -> Self {
self.mem_pages_count = mem_pages_count;
self
}
#[allow(dead_code)]
pub fn with_invoke_fn_name(mut self, invoke_fn_name: String) -> Self {
self.invoke_fn_name = invoke_fn_name;
self
}
#[allow(dead_code)]
pub fn with_allocate_fn_name(mut self, allocate_fn_name: String) -> Self {
self.allocate_fn_name = allocate_fn_name;
self
}
#[allow(dead_code)]
pub fn with_deallocate_fn_name(mut self, deallocate_fn_name: String) -> Self {
self.deallocate_fn_name = deallocate_fn_name;
self
}
#[allow(dead_code)]
pub fn with_logger_enable(mut self, logger_enable: bool) -> Self {
self.logger_enabled = logger_enable;
self
}
#[allow(dead_code)]
pub fn with_wasi_version(mut self, wasi_version: WasiVersion) -> Self {
self.wasi_config.version = wasi_version;
self
}
#[allow(dead_code)]
pub fn with_wasi_envs(mut self, envs: Vec<Vec<u8>>) -> Self {
self.wasi_config.envs = envs;
self
}
#[allow(dead_code)]
pub fn with_wasi_preopened_files(mut self, preopened_files: Vec<PathBuf>) -> Self {
self.wasi_config.preopened_files = preopened_files;
self
}
#[allow(dead_code)]
pub fn with_wasi_mapped_dirs(mut self, mapped_dirs: Vec<(String, PathBuf)>) -> Self {
self.wasi_config.mapped_dirs = mapped_dirs;
self
}
}

View File

@ -93,29 +93,23 @@ impl Frank {
},
};
let import_object = match config.wasi_config {
Some(wasi_config) => {
let mut wasi_import_object = generate_import_object_for_version(
wasi_config.version,
vec![],
wasi_config.envs,
wasi_config.preopened_files,
wasi_config.mapped_dirs,
);
wasi_import_object.extend(logger_imports);
wasi_import_object
}
None => logger_imports,
};
let mut import_object = generate_import_object_for_version(
config.wasi_config.version,
vec![],
config.wasi_config.envs,
config.wasi_config.preopened_files,
config.wasi_config.mapped_dirs,
);
import_object.extend(logger_imports);
let instance = compile(&prepared_wasm_bytes)?.instantiate(&import_object)?;
let instance: &'static mut Instance = Box::leak(Box::new(instance));
Ok(Self {
instance,
allocate: Some(instance.exports.get(&config.allocate_function_name)?),
deallocate: Some(instance.exports.get(&config.deallocate_function_name)?),
invoke: Some(instance.exports.get(&config.invoke_function_name)?),
allocate: Some(instance.exports.get(&config.allocate_fn_name)?),
deallocate: Some(instance.exports.get(&config.deallocate_fn_name)?),
invoke: Some(instance.exports.get(&config.invoke_fn_name)?),
})
}
}