simplify the wit extrator

This commit is contained in:
vms 2020-06-06 01:03:49 +03:00
parent bb2bbcf35e
commit 60c9f03902
5 changed files with 20 additions and 42 deletions

View File

@ -17,7 +17,6 @@
use super::custom::WIT_SECTION_NAME;
use super::errors::WITParserError;
use fce_wit_interfaces::FCEWITInterfaces;
use walrus::{IdsToIndices, ModuleConfig};
use wasmer_wit::ast::Interfaces;
use wasmer_core::Module as WasmerModule;
@ -27,16 +26,12 @@ use std::path::PathBuf;
/// Extracts WIT section of provided Wasm binary and converts it to a string.
pub fn extract_text_wit(wasm_file_path: PathBuf) -> Result<String, WITParserError> {
let wit_section_bytes = extract_wit_section_bytes(wasm_file_path)?;
extract_wit_with_fn(
&wit_section_bytes,
|wit: Interfaces<'_>| -> Result<String, WITParserError> { Ok((&wit).to_string()) },
)
let wit = extract_wit_with_fn(&wit_section_bytes)?;
Ok((&wit).to_string())
}
/// Extracts WIT section of provided Wasm binary and converts it to a FCEWITInterfaces.
pub fn extract_fce_wit(
wasmer_module: &WasmerModule,
) -> Result<FCEWITInterfaces<'_>, WITParserError> {
pub fn extract_wit(wasmer_module: &WasmerModule) -> Result<Interfaces<'_>, WITParserError> {
let wit_sections = wasmer_module
.custom_sections(WIT_SECTION_NAME)
.ok_or_else(|| WITParserError::NoWITSection)?;
@ -45,32 +40,15 @@ pub fn extract_fce_wit(
return Err(WITParserError::MultipleWITSections);
}
extract_wit_with_fn(
&wit_sections[0],
|wit: Interfaces<'_>| -> Result<FCEWITInterfaces<'_>, WITParserError> {
Ok(FCEWITInterfaces::new(wit))
},
)
extract_wit_with_fn(&wit_sections[0])
}
fn extract_wit_with_fn<'a, F, FResultType: 'a>(
wit_section_bytes: &'a [u8],
func: F,
) -> Result<FResultType, WITParserError>
where
F: FnOnce(Interfaces<'a>) -> Result<FResultType, WITParserError>,
{
let raw_wit = match wasmer_wit::decoders::binary::parse::<()>(&wit_section_bytes) {
Ok((remainder, wit)) if remainder.is_empty() => wit,
Ok(_) => {
return Err(WITParserError::WITRemainderNotEmpty);
}
Err(_) => {
return Err(WITParserError::CorruptedWITSection);
}
};
func(raw_wit)
fn extract_wit_with_fn(wit_section_bytes: &[u8]) -> Result<Interfaces<'_>, WITParserError> {
match wasmer_wit::decoders::binary::parse::<()>(&wit_section_bytes) {
Ok((remainder, wit)) if remainder.is_empty() => Ok(wit),
Ok(_) => Err(WITParserError::WITRemainderNotEmpty),
Err(_) => Err(WITParserError::CorruptedWITSection),
}
}
fn extract_wit_section_bytes(wasm_file_path: PathBuf) -> Result<Vec<u8>, WITParserError> {

View File

@ -34,5 +34,5 @@ pub use errors::WITParserError;
pub use embedder::embed_text_wit;
pub use deleter::delete_wit_section;
pub use extractor::extract_fce_wit;
pub use extractor::extract_wit;
pub use extractor::extract_text_wit;

View File

@ -3,4 +3,4 @@
ipfs = "/usr/bin/ipfs"
[wasi]
preopened_dir = ["/tmp"]
preopened_dirs = ["/tmp"]

View File

@ -22,7 +22,7 @@ use wasmer_wit::interpreter::Interpreter;
use wasmer_runtime::{compile, ImportObject};
use wasmer_core::Instance as WasmerInstance;
use wasmer_core::import::Namespace;
use wit_parser::extract_fce_wit;
use wit_parser::extract_wit;
use std::collections::HashMap;
use std::convert::TryInto;
@ -54,11 +54,12 @@ impl FCEModule {
modules: &HashMap<String, Arc<FCEModule>>,
) -> Result<Self, FCEError> {
let wasmer_module = compile(&wasm_bytes)?;
let wit = extract_fce_wit(&wasmer_module)?;
let wit_exports = Self::instantiate_wit_exports(&wit)?;
let wit = extract_wit(&wasmer_module)?;
let fce_wit = FCEWITInterfaces::new(wit);
let wit_exports = Self::instantiate_wit_exports(&fce_wit)?;
let mut wit_instance = Arc::new_uninit();
let mut import_object = Self::adjust_wit_imports(&wit, wit_instance.clone())?;
let mut import_object = Self::adjust_wit_imports(&fce_wit, wit_instance.clone())?;
import_object.extend(imports);
let wasmer_instance = wasmer_module.instantiate(&import_object)?;
@ -67,7 +68,7 @@ impl FCEModule {
// get_mut_unchecked here is safe because currently only this modules have reference to
// it and the environment is single-threaded
*Arc::get_mut_unchecked(&mut wit_instance) =
MaybeUninit::new(WITInstance::new(&wasmer_instance, &wit, modules)?);
MaybeUninit::new(WITInstance::new(&wasmer_instance, &fce_wit, modules)?);
std::mem::transmute::<_, Arc<WITInstance>>(wit_instance)
};

View File

@ -35,8 +35,7 @@ pub trait WasmProcess {
arguments: &[IValue],
) -> Result<Vec<IValue>, FCEError>;
/// Registers new module in the FCE Service.
/// TODO:
/// Loads new module in the FCE Service.
fn load_module<S>(
&mut self,
module_name: S,
@ -46,7 +45,7 @@ pub trait WasmProcess {
where
S: Into<String>;
/// Unregisters previously registered module.
/// Unloads previously registered module.
fn unload_module(&mut self, module_name: &str) -> Result<(), FCEError>;
/// Returns signatures of all exported functions by this module.