Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/execution/linker.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use alloc::{
borrow::ToOwned,
collections::btree_map::{BTreeMap, Entry},
string::String,
vec::Vec,
Expand Down Expand Up @@ -138,10 +139,9 @@ impl Linker {
validation_info: &ValidationInfo,
) -> Result<Vec<ExternVal>, RuntimeError> {
validation_info
.imports
.iter()
.map(|import| {
self.get_unchecked(import.module_name.clone(), import.name.clone())
.imports()
.map(|(module_name, name, _desc)| {
self.get_unchecked(module_name.to_owned(), name.to_owned())
.ok_or(RuntimeError::UnableToResolveExternLookup)
})
.collect()
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ extern crate log_wrapper;

pub use core::error::ValidationError;
pub use core::reader::types::{
export::ExportDesc, global::GlobalType, Limits, MemType, NumType, RefType, TableType, ValType,
export::ExportDesc, global::GlobalType, ExternType, FuncType, Limits, MemType, NumType,
RefType, ResultType, TableType, ValType,
};
pub use core::rw_spinlock;
pub use execution::error::{RuntimeError, TrapError};
Expand Down
35 changes: 34 additions & 1 deletion src/validation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::iter::Map;

use alloc::collections::btree_set::{self, BTreeSet};
use alloc::vec::Vec;

Expand All @@ -9,7 +11,7 @@ use crate::core::reader::types::element::ElemType;
use crate::core::reader::types::export::Export;
use crate::core::reader::types::global::{Global, GlobalType};
use crate::core::reader::types::import::{Import, ImportDesc};
use crate::core::reader::types::{FuncType, MemType, ResultType, TableType};
use crate::core::reader::types::{ExternType, FuncType, MemType, ResultType, TableType};
use crate::core::reader::WasmReader;
use crate::core::sidetable::Sidetable;
use crate::{ExportDesc, ValidationError};
Expand Down Expand Up @@ -480,3 +482,34 @@ fn handle_section<T, F: FnOnce(&mut WasmReader, SectionHeader) -> Result<T, Vali
_ => Ok(None),
}
}

impl ValidationInfo<'_> {
/// Returns the imports of this module as an iterator. Each import consist
/// of a module name, a name and an extern type.
///
/// See: WebAssembly Specification 2.0 - 7.1.5 - module_imports
pub fn imports<'a>(
&'a self,
) -> Map<core::slice::Iter<'a, Import>, impl FnMut(&'a Import) -> (&'a str, &'a str, ExternType)>
{
self.imports.iter().map(|import| {
(
&*import.module_name,
&*import.name,
import.desc.extern_type(self),
)
})
}

/// Returns the exports of this module as an iterator. Each export consist
/// of a name, and an extern type.
///
/// See: WebAssembly Specification 2.0 - 7.1.5 - module_exports
pub fn exports<'a>(
&'a self,
) -> Map<core::slice::Iter<'a, Export>, impl FnMut(&'a Export) -> (&'a str, ExternType)> {
self.exports
.iter()
.map(|export| (&*export.name, export.desc.extern_type(self)))
}
}
103 changes: 103 additions & 0 deletions tests/module_imports_exports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use wasm::{validate, ExternType, FuncType, GlobalType, NumType, ResultType, ValType};

#[test_log::test]
fn empty_module() {
const EMPTY_MODULE: &str = r#"
(module)
"#;
let wasm_bytes = wat::parse_str(EMPTY_MODULE).unwrap();

let validation_info = validate(&wasm_bytes).unwrap();

assert_eq!(validation_info.imports().len(), 0);
assert_eq!(validation_info.exports().len(), 0);
}

#[test_log::test]
fn imports() {
const MODULE_WITH_IMPORTS: &str = r#"
(module
(import "foo" "baz" (func))
(import "bar" "bat" (global (mut i64)))
)
"#;

let wasm_bytes = wat::parse_str(MODULE_WITH_IMPORTS).unwrap();

let validation_info = validate(&wasm_bytes).unwrap();

let imports: Vec<(&str, &str, ExternType)> = validation_info.imports().collect();

assert_eq!(
&imports,
&[
(
"foo",
"baz",
ExternType::Func(FuncType {
params: ResultType {
valtypes: Vec::new()
},
returns: ResultType {
valtypes: Vec::new()
},
})
),
(
"bar",
"bat",
ExternType::Global(GlobalType {
ty: ValType::NumType(NumType::I64),
is_mut: true,
}),
)
]
);

assert_eq!(validation_info.exports().len(), 0);
}

#[test_log::test]
fn exports() {
const MODULE_WITH_EXPORTED_DEFINITIONS: &str = r#"
(module
(func $my_func (export "foo") (param i32) (result i64)
local.get 0
i64.extend_i32_u
)
(global $my_global (export "bar") (mut i32)
i32.const 123
)
)
"#;

let wasm_bytes = wat::parse_str(MODULE_WITH_EXPORTED_DEFINITIONS).unwrap();

let validation_info = validate(&wasm_bytes).unwrap();

let exports: Vec<(&str, ExternType)> = validation_info.exports().collect();

assert_eq!(
&exports,
&[
(
"foo",
ExternType::Func(FuncType {
params: ResultType {
valtypes: vec![ValType::NumType(NumType::I32)]
},
returns: ResultType {
valtypes: vec![ValType::NumType(NumType::I64)],
}
}),
),
(
"bar",
ExternType::Global(GlobalType {
ty: ValType::NumType(NumType::I32),
is_mut: true
})
)
]
)
}