add ability to use IT in host imports

This commit is contained in:
vms 2020-07-18 18:05:07 +03:00
parent 43b765e61a
commit 33d9320d47
4 changed files with 13 additions and 19 deletions

View File

@ -26,8 +26,15 @@ use fluence_sdk_wit::ParsedType;
use wasmer_wit::interpreter::Instruction; use wasmer_wit::interpreter::Instruction;
use crate::instructions_generator::utils::wtype_to_itype; use crate::instructions_generator::utils::wtype_to_itype;
const HOST_NAMESPACE_NAME: &str = "host";
impl WITGenerator for AstExternModItem { impl WITGenerator for AstExternModItem {
fn generate_wit<'a>(&'a self, interfaces: &mut Interfaces<'a>) { fn generate_wit<'a>(&'a self, interfaces: &mut Interfaces<'a>) {
// host imports should be left as is
if self.namespace == HOST_NAMESPACE_NAME {
return;
}
for import in &self.imports { for import in &self.imports {
generate_wit_for_import(import, &self.namespace, interfaces); generate_wit_for_import(import, &self.namespace, interfaces);
} }

View File

@ -39,7 +39,7 @@ pub fn put(file_path: String) -> String {
let timeout = std::env::var(TIMEOUT_ENV_NAME).unwrap_or_else(|_| "1s".to_string()); let timeout = std::env::var(TIMEOUT_ENV_NAME).unwrap_or_else(|_| "1s".to_string());
let cmd = format!("add --timeout {} -Q {}", timeout, file_path); let cmd = format!("add --timeout {} -Q {}", timeout, file_path);
call_ipfs(cmd) ipfs(cmd)
} }
/// Get file by provided hash from IPFS, saves it to a temporary file and returns a path to it. /// Get file by provided hash from IPFS, saves it to a temporary file and returns a path to it.
@ -55,7 +55,7 @@ pub fn get(hash: String) -> String {
timeout, result_file_path, hash timeout, result_file_path, hash
); );
call_ipfs(cmd); ipfs(cmd);
RESULT_FILE_PATH.to_string() RESULT_FILE_PATH.to_string()
} }
@ -71,22 +71,9 @@ pub fn get_address() -> String {
} }
} }
fn call_ipfs(cmd: String) -> String { #[fce]
unsafe {
// TODO: better error handling
match ipfs(cmd.as_ptr() as _, cmd.len() as _) {
0 => String::from_raw_parts(
fluence::internal::get_result_ptr() as _,
fluence::internal::get_result_size(),
fluence::internal::get_result_size(),
),
_ => "host ipfs call failed".to_string(),
}
}
}
#[link(wasm_import_module = "host")] #[link(wasm_import_module = "host")]
extern "C" { extern "C" {
/// Put a file to ipfs, returns ipfs hash of the file. /// Execute provided cmd as a parameters of ipfs cli, return result.
fn ipfs(ptr: i32, size: i32) -> i32; pub fn ipfs(cmd: String) -> String;
} }

View File

@ -93,7 +93,7 @@ where
}; };
DynamicFunc::new( DynamicFunc::new(
std::sync::Arc::new(FuncSig::new(vec![Type::I32, Type::I32], vec![Type::I32])), std::sync::Arc::new(FuncSig::new(vec![Type::I32, Type::I32], vec![])),
func, func,
) )
} }