mirror of
https://github.com/fluencelabs/marine.git
synced 2025-03-15 22:10:49 +00:00
add -dir option; refactor Config
This commit is contained in:
parent
e1e873b2fc
commit
978bb90669
21
src/main.rs
21
src/main.rs
@ -34,6 +34,7 @@ use clap::{App, AppSettings, Arg, SubCommand};
|
|||||||
use exitfailure::ExitFailure;
|
use exitfailure::ExitFailure;
|
||||||
use failure::err_msg;
|
use failure::err_msg;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
const AUTHORS: &str = env!("CARGO_PKG_AUTHORS");
|
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 IN_MODULE_PATH: &str = "in-wasm-path";
|
||||||
const INVOKE_ARG: &str = "arg";
|
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)
|
Arg::with_name(IN_MODULE_PATH)
|
||||||
.required(true)
|
.required(true)
|
||||||
@ -54,6 +56,13 @@ fn prepare_args<'a, 'b>() -> [Arg<'a, 'b>; 2] {
|
|||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.short("a")
|
.short("a")
|
||||||
.help("argument for the invoke function in the Wasm module"),
|
.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() {
|
match app.get_matches().subcommand() {
|
||||||
("execute", Some(arg)) => {
|
("execute", Some(arg)) => {
|
||||||
let config = Config::default();
|
|
||||||
let in_module_path = arg.value_of(IN_MODULE_PATH).unwrap();
|
let in_module_path = arg.value_of(IN_MODULE_PATH).unwrap();
|
||||||
let wasm_code = fs::read(in_module_path)?;
|
let wasm_code = fs::read(in_module_path)?;
|
||||||
|
|
||||||
let invoke_arg = arg.value_of(INVOKE_ARG).unwrap();
|
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 mut frank = Frank::new(&wasm_code, config)?;
|
||||||
let result = frank.invoke(invoke_arg.as_bytes())?;
|
let result = frank.invoke(invoke_arg.as_bytes())?;
|
||||||
|
|
||||||
|
@ -55,19 +55,19 @@ pub struct Config {
|
|||||||
pub logger_enabled: bool,
|
pub logger_enabled: bool,
|
||||||
|
|
||||||
/// The name of the main module handler function.
|
/// 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
|
/// The name of function that should be called for allocation memory. This function
|
||||||
/// is used for passing array of bytes to the main module.
|
/// 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
|
/// The name of function that should be called for deallocation of
|
||||||
/// previously allocated memory by allocateFunction.
|
/// 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
|
/// Config for WASI subsystem initialization. None means that module should be loaded
|
||||||
/// without WASI.
|
/// without WASI.
|
||||||
pub wasi_config: Option<WASIConfig>,
|
pub wasi_config: WASIConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
@ -76,11 +76,67 @@ impl Default for Config {
|
|||||||
Self {
|
Self {
|
||||||
// 65536*1600 ~ 100 Mb
|
// 65536*1600 ~ 100 Mb
|
||||||
mem_pages_count: 1600,
|
mem_pages_count: 1600,
|
||||||
invoke_function_name: "invoke".to_string(),
|
invoke_fn_name: "invoke".to_string(),
|
||||||
allocate_function_name: "allocate".to_string(),
|
allocate_fn_name: "allocate".to_string(),
|
||||||
deallocate_function_name: "deallocate".to_string(),
|
deallocate_fn_name: "deallocate".to_string(),
|
||||||
logger_enabled: true,
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -93,29 +93,23 @@ impl Frank {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let import_object = match config.wasi_config {
|
let mut import_object = generate_import_object_for_version(
|
||||||
Some(wasi_config) => {
|
config.wasi_config.version,
|
||||||
let mut wasi_import_object = generate_import_object_for_version(
|
vec![],
|
||||||
wasi_config.version,
|
config.wasi_config.envs,
|
||||||
vec![],
|
config.wasi_config.preopened_files,
|
||||||
wasi_config.envs,
|
config.wasi_config.mapped_dirs,
|
||||||
wasi_config.preopened_files,
|
);
|
||||||
wasi_config.mapped_dirs,
|
import_object.extend(logger_imports);
|
||||||
);
|
|
||||||
wasi_import_object.extend(logger_imports);
|
|
||||||
wasi_import_object
|
|
||||||
}
|
|
||||||
None => logger_imports,
|
|
||||||
};
|
|
||||||
|
|
||||||
let instance = compile(&prepared_wasm_bytes)?.instantiate(&import_object)?;
|
let instance = compile(&prepared_wasm_bytes)?.instantiate(&import_object)?;
|
||||||
let instance: &'static mut Instance = Box::leak(Box::new(instance));
|
let instance: &'static mut Instance = Box::leak(Box::new(instance));
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
instance,
|
instance,
|
||||||
allocate: Some(instance.exports.get(&config.allocate_function_name)?),
|
allocate: Some(instance.exports.get(&config.allocate_fn_name)?),
|
||||||
deallocate: Some(instance.exports.get(&config.deallocate_function_name)?),
|
deallocate: Some(instance.exports.get(&config.deallocate_fn_name)?),
|
||||||
invoke: Some(instance.exports.get(&config.invoke_function_name)?),
|
invoke: Some(instance.exports.get(&config.invoke_fn_name)?),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user