mirror of
https://github.com/fluencelabs/marine.git
synced 2025-06-12 22:41:26 +00:00
feat!: decouple wasmer from Marine, replace it with generic backend interface (#219)
This commit is contained in:
@ -4,7 +4,7 @@ description = "Fluence Marine Wasm module info (manifest and version) parser"
|
||||
version = "0.5.1"
|
||||
authors = ["Fluence Labs"]
|
||||
license = "Apache-2.0"
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
name = "marine_module_info_parser"
|
||||
@ -13,7 +13,7 @@ path = "src/lib.rs"
|
||||
[dependencies]
|
||||
marine-rs-sdk-main = "0.7.1"
|
||||
|
||||
wasmer-core = { package = "wasmer-runtime-core-fl", version = "=0.17.1" }
|
||||
marine-wasm-backend-traits = { path = "../wasm-backend-traits", version = "0.1.0"}
|
||||
|
||||
anyhow = "1.0.66"
|
||||
chrono = "0.4.23"
|
||||
|
@ -30,11 +30,11 @@ pub enum ModuleInfoError {
|
||||
MultipleCustomSections(&'static str, usize),
|
||||
|
||||
/// Errors related to corrupted version.
|
||||
#[error("{0}")]
|
||||
#[error(transparent)]
|
||||
VersionError(#[from] SDKVersionError),
|
||||
|
||||
/// Errors related to corrupted manifest.
|
||||
#[error("{0}")]
|
||||
#[error(transparent)]
|
||||
ManifestError(#[from] ManifestError),
|
||||
|
||||
/// An error occurred while parsing Wasm file.
|
||||
|
@ -20,7 +20,9 @@ use crate::ModuleInfoError;
|
||||
use crate::extract_custom_sections_by_name;
|
||||
use crate::try_as_one_section;
|
||||
|
||||
use wasmer_core::Module as WasmerModule;
|
||||
use marine_wasm_backend_traits::Module as ModuleTrait;
|
||||
use marine_wasm_backend_traits::WasmBackend;
|
||||
|
||||
use marine_rs_sdk_main::MANIFEST_SECTION_NAME;
|
||||
use walrus::ModuleConfig;
|
||||
use walrus::Module;
|
||||
@ -29,7 +31,7 @@ use std::borrow::Cow;
|
||||
use std::path::Path;
|
||||
use std::convert::TryInto;
|
||||
|
||||
pub fn extract_from_path<P>(wasm_module_path: P) -> ModuleInfoResult<Option<ModuleManifest>>
|
||||
pub fn extract_from_path<P>(wasm_module_path: P) -> ModuleInfoResult<ModuleManifest>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
@ -40,12 +42,8 @@ where
|
||||
extract_from_module(&module)
|
||||
}
|
||||
|
||||
pub fn extract_from_module(wasm_module: &Module) -> ModuleInfoResult<Option<ModuleManifest>> {
|
||||
pub fn extract_from_module(wasm_module: &Module) -> ModuleInfoResult<ModuleManifest> {
|
||||
let sections = extract_custom_sections_by_name(wasm_module, MANIFEST_SECTION_NAME)?;
|
||||
if sections.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let section = try_as_one_section(§ions, MANIFEST_SECTION_NAME)?;
|
||||
|
||||
let manifest = match section {
|
||||
@ -53,20 +51,15 @@ pub fn extract_from_module(wasm_module: &Module) -> ModuleInfoResult<Option<Modu
|
||||
Cow::Owned(vec) => vec.as_slice().try_into(),
|
||||
}?;
|
||||
|
||||
Ok(Some(manifest))
|
||||
Ok(manifest)
|
||||
}
|
||||
|
||||
pub fn extract_from_wasmer_module(
|
||||
wasmer_module: &WasmerModule,
|
||||
) -> ModuleInfoResult<Option<ModuleManifest>> {
|
||||
let sections = wasmer_module.custom_sections(MANIFEST_SECTION_NAME);
|
||||
let sections = match sections {
|
||||
Some(sections) => sections,
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
pub fn extract_from_compiled_module<WB: WasmBackend>(
|
||||
module: &<WB as WasmBackend>::Module,
|
||||
) -> ModuleInfoResult<ModuleManifest> {
|
||||
let sections = module.custom_sections(MANIFEST_SECTION_NAME);
|
||||
let section = try_as_one_section(sections, MANIFEST_SECTION_NAME)?;
|
||||
let manifest = section.as_slice().try_into()?;
|
||||
|
||||
Ok(Some(manifest))
|
||||
Ok(manifest)
|
||||
}
|
||||
|
@ -23,5 +23,5 @@ mod tests;
|
||||
pub use errors::ManifestError;
|
||||
pub use manifest_extractor::extract_from_path;
|
||||
pub use manifest_extractor::extract_from_module;
|
||||
pub use manifest_extractor::extract_from_wasmer_module;
|
||||
pub use manifest_extractor::extract_from_compiled_module;
|
||||
pub use module_manifest::ModuleManifest;
|
||||
|
@ -109,7 +109,7 @@ fn test_too_big_field_len() {
|
||||
array.add_utf8_field("repository");
|
||||
|
||||
let actual: Result<ModuleManifest, _> = array.as_bytes().try_into();
|
||||
let expected = Err(ManifestError::TooBigFieldSize("version", incorrect_size));
|
||||
let expected: Result<_, _> = Err(ManifestError::TooBigFieldSize("version", incorrect_size));
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
@ -123,7 +123,7 @@ fn test_without_one_field() {
|
||||
array.add_utf8_field("description");
|
||||
|
||||
let actual: Result<ModuleManifest, _> = array.as_bytes().try_into();
|
||||
let expected = Err(ManifestError::NotEnoughBytesForPrefix("repository"));
|
||||
let expected: Result<_, _> = Err(ManifestError::NotEnoughBytesForPrefix("repository"));
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
@ -131,7 +131,7 @@ fn test_without_one_field() {
|
||||
#[test]
|
||||
fn test_with_empty_slice() {
|
||||
let actual: Result<ModuleManifest, _> = vec![].as_slice().try_into();
|
||||
let expected = Err(ManifestError::NotEnoughBytesForPrefix("authors"));
|
||||
let expected: Result<_, _> = Err(ManifestError::NotEnoughBytesForPrefix("authors"));
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
@ -148,7 +148,7 @@ fn test_not_enough_data_for_field() {
|
||||
array.add_utf8_string("repository");
|
||||
|
||||
let actual: Result<ModuleManifest, _> = array.as_bytes().try_into();
|
||||
let expected = Err(ManifestError::NotEnoughBytesForField(
|
||||
let expected: Result<_, _> = Err(ManifestError::NotEnoughBytesForField(
|
||||
"repository",
|
||||
too_big_size as usize,
|
||||
));
|
||||
|
@ -21,6 +21,6 @@ mod version_extractor;
|
||||
pub use errors::SDKVersionError;
|
||||
pub use version_extractor::extract_from_path;
|
||||
pub use version_extractor::extract_from_module;
|
||||
pub use version_extractor::extract_from_wasmer_module;
|
||||
pub use version_extractor::extract_from_compiled_module;
|
||||
pub use version_embedder::embed_from_path;
|
||||
pub use version_embedder::embed_from_module;
|
||||
|
@ -20,7 +20,9 @@ use super::SDKVersionError;
|
||||
use crate::extract_custom_sections_by_name;
|
||||
use crate::try_as_one_section;
|
||||
|
||||
use wasmer_core::Module as WasmerModule;
|
||||
use marine_wasm_backend_traits::WasmBackend;
|
||||
use marine_wasm_backend_traits::Module as WasmModule;
|
||||
|
||||
use marine_rs_sdk_main::VERSION_SECTION_NAME;
|
||||
use walrus::ModuleConfig;
|
||||
use walrus::Module;
|
||||
@ -29,7 +31,7 @@ use std::borrow::Cow;
|
||||
use std::str::FromStr;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn extract_from_path<P>(wasm_module_path: P) -> ModuleInfoResult<Option<semver::Version>>
|
||||
pub fn extract_from_path<P>(wasm_module_path: P) -> ModuleInfoResult<semver::Version>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
@ -40,12 +42,8 @@ where
|
||||
extract_from_module(&module)
|
||||
}
|
||||
|
||||
pub fn extract_from_module(wasm_module: &Module) -> ModuleInfoResult<Option<semver::Version>> {
|
||||
pub fn extract_from_module(wasm_module: &Module) -> ModuleInfoResult<semver::Version> {
|
||||
let sections = extract_custom_sections_by_name(wasm_module, VERSION_SECTION_NAME)?;
|
||||
|
||||
if sections.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let section = try_as_one_section(§ions, VERSION_SECTION_NAME)?;
|
||||
|
||||
let version = match section {
|
||||
@ -53,23 +51,17 @@ pub fn extract_from_module(wasm_module: &Module) -> ModuleInfoResult<Option<semv
|
||||
Cow::Owned(vec) => as_semver(vec),
|
||||
}?;
|
||||
|
||||
Ok(Some(version))
|
||||
Ok(version)
|
||||
}
|
||||
|
||||
pub fn extract_from_wasmer_module(
|
||||
wasmer_module: &WasmerModule,
|
||||
) -> ModuleInfoResult<Option<semver::Version>> {
|
||||
let sections = wasmer_module.custom_sections(VERSION_SECTION_NAME);
|
||||
|
||||
let sections = match sections {
|
||||
Some(sections) => sections,
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
pub fn extract_from_compiled_module<WB: WasmBackend>(
|
||||
wasm_module: &<WB as WasmBackend>::Module,
|
||||
) -> ModuleInfoResult<semver::Version> {
|
||||
let sections = wasm_module.custom_sections(VERSION_SECTION_NAME);
|
||||
let section = try_as_one_section(sections, VERSION_SECTION_NAME)?;
|
||||
let version = as_semver(section)?;
|
||||
|
||||
Ok(Some(version))
|
||||
Ok(version)
|
||||
}
|
||||
|
||||
fn as_semver(version_as_bytes: &[u8]) -> Result<semver::Version, super::SDKVersionError> {
|
||||
|
Reference in New Issue
Block a user