mirror of
https://github.com/fluencelabs/marine.git
synced 2025-07-02 16:11:35 +00:00
move fce to fce_wit_interfaces
This commit is contained in:
@ -10,6 +10,8 @@ path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
walrus = "0.17.0"
|
||||
wasmer-core = { package = "wasmer-runtime-core", version = "0.17.0", features = ["dynamicfunc-fat-closures"] }
|
||||
wasmer-wit = { package = "wasmer-interface-types", git = "https://github.com/fluencelabs/interface-types", branch = "master"}
|
||||
|
||||
multimap = "0.8.1"
|
||||
anyhow = "1.0.31"
|
||||
|
@ -23,39 +23,35 @@ use std::collections::HashMap;
|
||||
|
||||
pub type CoreFunctionType = u32;
|
||||
pub type AdapterFunctionType = u32;
|
||||
pub type ExportName = String;
|
||||
pub type ImportName = String;
|
||||
pub type ImportNamespace = String;
|
||||
pub type ExportName<'a> = &'a str;
|
||||
pub type ImportName<'a> = &'a str;
|
||||
pub type ImportNamespace<'a> = &'a str;
|
||||
pub type WITAstType = Type;
|
||||
|
||||
pub struct FCEWITInterfaces {
|
||||
pub struct FCEWITInterfaces<'a> {
|
||||
/// All the types.
|
||||
types: Vec<Type>,
|
||||
types: Vec<WITAstType>,
|
||||
|
||||
/// All the imported functions.
|
||||
imports: HashMap<CoreFunctionType, (ImportName, ImportNamespace)>,
|
||||
imports: HashMap<CoreFunctionType, (ImportName<'a>, ImportNamespace<'a>)>,
|
||||
|
||||
/// All the adapters.
|
||||
adapters: HashMap<AdapterFunctionType, Vec<Instruction>>,
|
||||
|
||||
/// All the exported functions.
|
||||
exports: HashMap<CoreFunctionType, ExportName>,
|
||||
exports: HashMap<CoreFunctionType, ExportName<'a>>,
|
||||
|
||||
/// All the implementations.
|
||||
adapter_type_to_core: MultiMap<AdapterFunctionType, CoreFunctionType>,
|
||||
core_type_to_adapter: MultiMap<CoreFunctionType, AdapterFunctionType>,
|
||||
}
|
||||
|
||||
impl FCEWITInterfaces {
|
||||
pub fn new(interfaces: Interfaces<'_>) -> Self {
|
||||
impl<'a> FCEWITInterfaces<'a> {
|
||||
pub fn new(interfaces: Interfaces<'a>) -> Self {
|
||||
let imports = interfaces
|
||||
.imports
|
||||
.into_iter()
|
||||
.map(|import| {
|
||||
(
|
||||
import.function_type,
|
||||
(import.namespace.to_owned(), import.name.to_owned()),
|
||||
)
|
||||
})
|
||||
.map(|import| (import.function_type, (import.namespace, import.name)))
|
||||
.collect::<HashMap<_, _>>();
|
||||
|
||||
let adapters = interfaces
|
||||
@ -67,7 +63,7 @@ impl FCEWITInterfaces {
|
||||
let exports = interfaces
|
||||
.exports
|
||||
.into_iter()
|
||||
.map(|export| (export.function_type, export.name.to_owned()))
|
||||
.map(|export| (export.function_type, export.name))
|
||||
.collect::<HashMap<_, _>>();
|
||||
|
||||
let adapter_type_to_core = interfaces
|
||||
@ -102,8 +98,8 @@ impl FCEWITInterfaces {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn type_by_idx(&self, idx: usize) -> Option<&Type> {
|
||||
self.types.get(idx)
|
||||
pub fn type_by_idx(&self, idx: u32) -> Option<&Type> {
|
||||
self.types.get(idx as usize)
|
||||
}
|
||||
|
||||
pub fn types(&self) -> impl Iterator<Item = &Type> {
|
||||
@ -113,13 +109,13 @@ impl FCEWITInterfaces {
|
||||
pub fn import_by_type(
|
||||
&self,
|
||||
import_type: CoreFunctionType,
|
||||
) -> Option<&(ImportName, ImportNamespace)> {
|
||||
) -> Option<&(ImportName<'a>, ImportNamespace<'a>)> {
|
||||
self.imports.get(&import_type)
|
||||
}
|
||||
|
||||
pub fn imports(
|
||||
&self,
|
||||
) -> impl Iterator<Item = (&CoreFunctionType, &(ImportName, ImportNamespace))> {
|
||||
) -> impl Iterator<Item = (&CoreFunctionType, &(ImportName<'a>, ImportNamespace<'a>))> {
|
||||
self.imports.iter()
|
||||
}
|
||||
|
||||
@ -127,22 +123,22 @@ impl FCEWITInterfaces {
|
||||
self.adapters.get(&adapter_type)
|
||||
}
|
||||
|
||||
pub fn export_by_type(&self, export_type: u32) -> Option<&ExportName> {
|
||||
pub fn export_by_type(&self, export_type: u32) -> Option<&ExportName<'a>> {
|
||||
self.exports.get(&export_type)
|
||||
}
|
||||
|
||||
pub fn adapter_func_implementations(
|
||||
pub fn exports(
|
||||
&self,
|
||||
) -> impl Iterator<Item = (&CoreFunctionType, &ExportName<'a>)> {
|
||||
self.exports.iter()
|
||||
}
|
||||
|
||||
pub fn implementations(
|
||||
&self,
|
||||
) -> impl Iterator<Item = (&AdapterFunctionType, &CoreFunctionType)> {
|
||||
self.adapter_type_to_core.iter()
|
||||
}
|
||||
|
||||
pub fn core_func_implementations(
|
||||
&self,
|
||||
) -> impl Iterator<Item = (&CoreFunctionType, &AdapterFunctionType)> {
|
||||
self.core_type_to_adapter.iter()
|
||||
}
|
||||
|
||||
pub fn adapter_types_by_core_type(
|
||||
&self,
|
||||
core_function_type: CoreFunctionType,
|
||||
|
@ -20,40 +20,47 @@ use crate::fce_wit_interfaces::FCEWITInterfaces;
|
||||
|
||||
use walrus::{IdsToIndices, ModuleConfig};
|
||||
use wasmer_wit::ast::Interfaces;
|
||||
use wasmer_core::Module as WasmerModule;
|
||||
|
||||
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(
|
||||
wasm_file_path,
|
||||
&wit_section_bytes,
|
||||
|wit: Interfaces<'_>| -> Result<String, WITParserError> { Ok((&wit).to_string()) },
|
||||
)
|
||||
}
|
||||
|
||||
pub fn extract_fce_wit(wasm_file_path: PathBuf) -> Result<FCEWITInterfaces, WITParserError> {
|
||||
/// Extracts WIT section of provided Wasm binary and converts it to a FCEWITInterfaces.
|
||||
pub fn extract_fce_wit(
|
||||
wasmer_module: &WasmerModule,
|
||||
) -> Result<FCEWITInterfaces<'_>, WITParserError> {
|
||||
let wit_sections = wasmer_module
|
||||
.custom_sections(WIT_SECTION_NAME)
|
||||
.ok_or_else(|| WITParserError::NoWITSection)?;
|
||||
|
||||
if wit_sections.len() > 1 {
|
||||
return Err(WITParserError::MultipleWITSections);
|
||||
}
|
||||
|
||||
extract_wit_with_fn(
|
||||
wasm_file_path,
|
||||
|wit: Interfaces<'_>| -> Result<FCEWITInterfaces, WITParserError> {
|
||||
&wit_sections[0],
|
||||
|wit: Interfaces<'_>| -> Result<FCEWITInterfaces<'_>, WITParserError> {
|
||||
Ok(FCEWITInterfaces::new(wit))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn extract_wit_with_fn<F, FResultType>(
|
||||
wasm_file_path: PathBuf,
|
||||
fn extract_wit_with_fn<'a, F, FResultType: 'a>(
|
||||
wit_section_bytes: &'a [u8],
|
||||
func: F,
|
||||
) -> Result<FResultType, WITParserError>
|
||||
where
|
||||
F: FnOnce(Interfaces<'_>) -> Result<FResultType, WITParserError>,
|
||||
F: FnOnce(Interfaces<'a>) -> Result<FResultType, WITParserError>,
|
||||
{
|
||||
let wit_section_bytes = extract_wit_section_bytes(wasm_file_path)?;
|
||||
let raw_wit = extract_raw_interfaces(&wit_section_bytes)?;
|
||||
|
||||
func(raw_wit)
|
||||
}
|
||||
|
||||
fn extract_raw_interfaces(wit_section_bytes: &[u8]) -> Result<Interfaces<'_>, WITParserError> {
|
||||
let wit = match wasmer_wit::decoders::binary::parse::<()>(&wit_section_bytes) {
|
||||
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);
|
||||
@ -63,7 +70,7 @@ fn extract_raw_interfaces(wit_section_bytes: &[u8]) -> Result<Interfaces<'_>, WI
|
||||
}
|
||||
};
|
||||
|
||||
Ok(wit)
|
||||
func(raw_wit)
|
||||
}
|
||||
|
||||
fn extract_wit_section_bytes(wasm_file_path: PathBuf) -> Result<Vec<u8>, WITParserError> {
|
@ -1,6 +1,6 @@
|
||||
mod custom;
|
||||
mod errors;
|
||||
mod extracter;
|
||||
mod extractor;
|
||||
mod embedder;
|
||||
|
||||
pub use errors::WITParserError;
|
||||
@ -8,5 +8,5 @@ pub use errors::WITParserError;
|
||||
pub use embedder::EmbedderConfig;
|
||||
pub use embedder::embed_text_wit;
|
||||
|
||||
pub use extracter::extract_fce_wit;
|
||||
pub use extracter::extract_text_wit;
|
||||
pub use extractor::extract_fce_wit;
|
||||
pub use extractor::extract_text_wit;
|
||||
|
Reference in New Issue
Block a user