From e134505b9030e4d169b598890ac116b684f22d30 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 6 Dec 2019 16:19:55 -0600 Subject: [PATCH] Refactor the `types.rs` types and structures (#681) * Refactor the `types.rs` types and structures A few changes applied along the way: * Documentation added to most methods and types. * Limits are now stored with the maximum as optional rather than a sentinel u32 value for `None`. * The `Name` type was removed in favor of just using a bare `String`. * The `Extern` prefix in the varaints of `ExternType` has been removed since it was redundant. * Accessors of `ExternType` variants no longer panic, and unwrapping versions were added with "unwrap" in the name. * Fields and methods named `r#type` were renamed to `ty` to avoid requiring a raw identifier to use them. * Remove `fail-fast: false` This was left around since the development of GitHub Actions for wasmtime, but they're no longer needed! * Fix compilation of the test-programs code * Fix compilation of wasmtime-py package * Run rustfmt --- .github/workflows/main.yml | 3 - crates/api/examples/memory.rs | 2 +- crates/api/src/externals.rs | 8 +- crates/api/src/instance.rs | 6 +- crates/api/src/lib.rs | 8 +- crates/api/src/module.rs | 33 +-- crates/api/src/trampoline/memory.rs | 6 +- crates/api/src/trampoline/table.rs | 6 +- crates/api/src/types.rs | 234 +++++++++++------- crates/api/src/wasm.rs | 49 ++-- crates/fuzzing/src/oracles/dummy.rs | 10 +- crates/misc/py/src/instance.rs | 6 +- crates/misc/py/src/lib.rs | 11 +- crates/misc/rust/macro/src/lib.rs | 8 +- .../test-programs/tests/wasm_tests/runtime.rs | 4 +- src/bin/wasmtime.rs | 6 +- 16 files changed, 226 insertions(+), 174 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3b25c039fb..d00d9f7507 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -92,7 +92,6 @@ jobs: name: Test runs-on: ${{ matrix.os }} strategy: - fail-fast: false matrix: build: [stable, beta, nightly, windows, macos] include: @@ -165,7 +164,6 @@ jobs: name: Python Wheel runs-on: ${{ matrix.os }} strategy: - fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] steps: @@ -262,7 +260,6 @@ jobs: name: Build wasmtime runs-on: ${{ matrix.os }} strategy: - fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] steps: diff --git a/crates/api/examples/memory.rs b/crates/api/examples/memory.rs index 2b84d81d03..7214a95e8c 100644 --- a/crates/api/examples/memory.rs +++ b/crates/api/examples/memory.rs @@ -151,7 +151,7 @@ fn main() -> Result<(), Error> { // Create stand-alone memory. // TODO(wasm+): Once Wasm allows multiple memories, turn this into import. println!("Creating stand-alone memory..."); - let memorytype = MemoryType::new(Limits::new(5, 5)); + let memorytype = MemoryType::new(Limits::new(5, Some(5))); let mut memory2 = Memory::new(&store, memorytype); check!(memory2.size(), 5u32); check!(memory2.grow(1), false); diff --git a/crates/api/src/externals.rs b/crates/api/src/externals.rs index b02589bf17..6e1a00bbce 100644 --- a/crates/api/src/externals.rs +++ b/crates/api/src/externals.rs @@ -49,10 +49,10 @@ impl Extern { pub fn r#type(&self) -> ExternType { match self { - Extern::Func(ft) => ExternType::ExternFunc(ft.borrow().r#type().clone()), - Extern::Memory(ft) => ExternType::ExternMemory(ft.borrow().r#type().clone()), - Extern::Table(tt) => ExternType::ExternTable(tt.borrow().r#type().clone()), - Extern::Global(gt) => ExternType::ExternGlobal(gt.borrow().r#type().clone()), + Extern::Func(ft) => ExternType::Func(ft.borrow().r#type().clone()), + Extern::Memory(ft) => ExternType::Memory(ft.borrow().r#type().clone()), + Extern::Table(tt) => ExternType::Table(tt.borrow().r#type().clone()), + Extern::Global(gt) => ExternType::Global(gt.borrow().r#type().clone()), } } diff --git a/crates/api/src/instance.rs b/crates/api/src/instance.rs index e5935a8408..49eecce2ed 100644 --- a/crates/api/src/instance.rs +++ b/crates/api/src/instance.rs @@ -4,7 +4,7 @@ use crate::module::Module; use crate::r#ref::HostRef; use crate::runtime::Store; use crate::trampoline::take_api_trap; -use crate::types::{ExportType, ExternType, Name}; +use crate::types::{ExportType, ExternType}; use anyhow::Result; use std::cell::RefCell; use std::collections::{HashMap, HashSet}; @@ -122,7 +122,7 @@ impl Instance { .exports() .iter() .enumerate() - .find(|(_, e)| e.name().as_str() == name)?; + .find(|(_, e)| e.name() == name)?; Some(&self.exports()[i]) } @@ -141,7 +141,7 @@ impl Instance { let _ = store.borrow_mut().register_wasmtime_signature(signature); } let extern_type = ExternType::from_wasmtime_export(&export); - exports_types.push(ExportType::new(Name::new(name), extern_type)); + exports_types.push(ExportType::new(name, extern_type)); exports.push(Extern::from_wasmtime_export( store, instance_handle.clone(), diff --git a/crates/api/src/lib.rs b/crates/api/src/lib.rs index c817c3dcc2..cde6b0a336 100644 --- a/crates/api/src/lib.rs +++ b/crates/api/src/lib.rs @@ -1,4 +1,10 @@ -//! Wasmtime embed API. Based on wasm-c-api. +//! Wasmtime's embedding API +//! +//! This crate contains a high-level API used to interact with WebAssembly +//! modules. The API here is intended to mirror the proposed [WebAssembly C +//! API](https://github.com/WebAssembly/wasm-c-api), with small extensions here +//! and there to implement Rust idioms. This crate also defines the actual C API +//! itself for consumption from other languages. #![allow(improper_ctypes)] diff --git a/crates/api/src/module.rs b/crates/api/src/module.rs index e3d2f8bfbe..f92aee937b 100644 --- a/crates/api/src/module.rs +++ b/crates/api/src/module.rs @@ -12,10 +12,7 @@ use wasmparser::{ fn into_memory_type(mt: wasmparser::MemoryType) -> MemoryType { assert!(!mt.shared); - MemoryType::new(Limits::new( - mt.limits.initial, - mt.limits.maximum.unwrap_or(std::u32::MAX), - )) + MemoryType::new(Limits::new(mt.limits.initial, mt.limits.maximum)) } fn into_global_type(gt: &wasmparser::GlobalType) -> GlobalType { @@ -53,10 +50,7 @@ fn into_table_type(tt: wasmparser::TableType) -> TableType { tt.element_type == wasmparser::Type::AnyFunc || tt.element_type == wasmparser::Type::AnyRef ); let ty = into_valtype(&tt.element_type); - let limits = Limits::new( - tt.limits.initial, - tt.limits.maximum.unwrap_or(std::u32::MAX), - ); + let limits = Limits::new(tt.limits.initial, tt.limits.maximum); TableType::new(ty, limits) } @@ -112,31 +106,29 @@ fn read_imports_and_exports(binary: &[u8]) -> Result<(Box<[ImportType]>, Box<[Ex imports.reserve_exact(section.get_count() as usize); for entry in section { let entry = entry?; - let module = String::from(entry.module).into(); - let name = String::from(entry.field).into(); let r#type = match entry.ty { ImportSectionEntryType::Function(index) => { func_sig.push(index); let sig = &sigs[index as usize]; - ExternType::ExternFunc(sig.clone()) + ExternType::Func(sig.clone()) } ImportSectionEntryType::Table(tt) => { let table = into_table_type(tt); tables.push(table.clone()); - ExternType::ExternTable(table) + ExternType::Table(table) } ImportSectionEntryType::Memory(mt) => { let memory = into_memory_type(mt); memories.push(memory.clone()); - ExternType::ExternMemory(memory) + ExternType::Memory(memory) } ImportSectionEntryType::Global(gt) => { let global = into_global_type(>); globals.push(global.clone()); - ExternType::ExternGlobal(global) + ExternType::Global(global) } }; - imports.push(ImportType::new(module, name, r#type)); + imports.push(ImportType::new(entry.module, entry.field, r#type)); } } SectionCode::Export => { @@ -144,24 +136,23 @@ fn read_imports_and_exports(binary: &[u8]) -> Result<(Box<[ImportType]>, Box<[Ex exports.reserve_exact(section.get_count() as usize); for entry in section { let entry = entry?; - let name = String::from(entry.field).into(); let r#type = match entry.kind { ExternalKind::Function => { let sig_index = func_sig[entry.index as usize] as usize; let sig = &sigs[sig_index]; - ExternType::ExternFunc(sig.clone()) + ExternType::Func(sig.clone()) } ExternalKind::Table => { - ExternType::ExternTable(tables[entry.index as usize].clone()) + ExternType::Table(tables[entry.index as usize].clone()) } ExternalKind::Memory => { - ExternType::ExternMemory(memories[entry.index as usize].clone()) + ExternType::Memory(memories[entry.index as usize].clone()) } ExternalKind::Global => { - ExternType::ExternGlobal(globals[entry.index as usize].clone()) + ExternType::Global(globals[entry.index as usize].clone()) } }; - exports.push(ExportType::new(name, r#type)); + exports.push(ExportType::new(entry.field, r#type)); } } _ => { diff --git a/crates/api/src/trampoline/memory.rs b/crates/api/src/trampoline/memory.rs index bbe116b78a..9437876219 100644 --- a/crates/api/src/trampoline/memory.rs +++ b/crates/api/src/trampoline/memory.rs @@ -12,11 +12,7 @@ pub fn create_handle_with_memory(memory: &MemoryType) -> Result let memory = wasm::Memory { minimum: memory.limits().min(), - maximum: if memory.limits().max() == std::u32::MAX { - None - } else { - Some(memory.limits().max()) - }, + maximum: memory.limits().max(), shared: false, // TODO }; let tunable = Default::default(); diff --git a/crates/api/src/trampoline/table.rs b/crates/api/src/trampoline/table.rs index 405a54e88c..5d2441bf92 100644 --- a/crates/api/src/trampoline/table.rs +++ b/crates/api/src/trampoline/table.rs @@ -10,11 +10,7 @@ pub fn create_handle_with_table(table: &TableType) -> Result { let table = wasm::Table { minimum: table.limits().min(), - maximum: if table.limits().max() == std::u32::MAX { - None - } else { - Some(table.limits().max()) - }, + maximum: table.limits().max(), ty: match table.element() { ValType::FuncRef => wasm::TableElementType::Func, _ => wasm::TableElementType::Val(table.element().get_wasmtime_type()), diff --git a/crates/api/src/types.rs b/crates/api/src/types.rs index 0398442b3d..2084985f87 100644 --- a/crates/api/src/types.rs +++ b/crates/api/src/types.rs @@ -4,35 +4,43 @@ use wasmtime_environ::{ir, wasm}; // Type attributes +/// Indicator of whether a global is mutable or not #[derive(Debug, Clone, Copy)] pub enum Mutability { + /// The global is constant and its value does not change Const, + /// The value of the global can change over time Var, } +/// Limits of tables/memories where the units of the limits are defined by the +/// table/memory types. +/// +/// A minimum is always available but the maximum may not be present. #[derive(Debug, Clone)] pub struct Limits { min: u32, - max: u32, + max: Option, } impl Limits { - pub fn new(min: u32, max: u32) -> Limits { + /// Creates a new set of limits with the minimum and maximum both specified. + pub fn new(min: u32, max: Option) -> Limits { Limits { min, max } } + /// Creates a new `Limits` with the `min` specified and no maximum specified. pub fn at_least(min: u32) -> Limits { - Limits { - min, - max: ::std::u32::MAX, - } + Limits::new(min, None) } + /// Returns the minimum amount for these limits. pub fn min(&self) -> u32 { self.min } - pub fn max(&self) -> u32 { + /// Returs the maximum amount for these limits, if specified. + pub fn max(&self) -> Option { self.max } } @@ -90,52 +98,63 @@ impl ValType { // External Types +/// A list of all possible types which can be externally referenced from a +/// WebAssembly module. +/// +/// This list can be found in [`ImportType`] or [`ExportType`], so these types +/// can either be imported or exported. #[derive(Debug, Clone)] pub enum ExternType { - ExternFunc(FuncType), - ExternGlobal(GlobalType), - ExternTable(TableType), - ExternMemory(MemoryType), + Func(FuncType), + Global(GlobalType), + Table(TableType), + Memory(MemoryType), +} + +macro_rules! accessors { + ($(($variant:ident($ty:ty) $get:ident $unwrap:ident))*) => ($( + /// Attempt to return the underlying type of this external type, + /// returning `None` if it is a different type. + pub fn $get(&self) -> Option<&$ty> { + if let ExternType::$variant(e) = self { + Some(e) + } else { + None + } + } + + /// Returns the underlying descriptor of this [`ExternType`], panicking + /// if it is a different type. + /// + /// # Panics + /// + /// Panics if `self` is not of the right type. + pub fn $unwrap(&self) -> &$ty { + self.$get().expect(concat!("expected ", stringify!($ty))) + } + )*) } impl ExternType { - pub fn func(&self) -> &FuncType { - match self { - ExternType::ExternFunc(func) => func, - _ => panic!("ExternType::ExternFunc expected"), - } - } - pub fn global(&self) -> &GlobalType { - match self { - ExternType::ExternGlobal(func) => func, - _ => panic!("ExternType::ExternGlobal expected"), - } - } - pub fn table(&self) -> &TableType { - match self { - ExternType::ExternTable(table) => table, - _ => panic!("ExternType::ExternTable expected"), - } - } - pub fn memory(&self) -> &MemoryType { - match self { - ExternType::ExternMemory(memory) => memory, - _ => panic!("ExternType::ExternMemory expected"), - } + accessors! { + (Func(FuncType) func unwrap_func) + (Global(GlobalType) global unwrap_global) + (Table(TableType) table unwrap_table) + (Memory(MemoryType) memory unwrap_memory) } pub(crate) fn from_wasmtime_export(export: &wasmtime_runtime::Export) -> Self { match export { wasmtime_runtime::Export::Function { signature, .. } => { - ExternType::ExternFunc(FuncType::from_wasmtime_signature(signature.clone())) + ExternType::Func(FuncType::from_wasmtime_signature(signature.clone())) } wasmtime_runtime::Export::Memory { memory, .. } => { - ExternType::ExternMemory(MemoryType::from_wasmtime_memory(&memory.memory)) + ExternType::Memory(MemoryType::from_wasmtime_memory(&memory.memory)) } wasmtime_runtime::Export::Global { global, .. } => { - ExternType::ExternGlobal(GlobalType::from_wasmtime_global(&global)) + ExternType::Global(GlobalType::from_wasmtime_global(&global)) } wasmtime_runtime::Export::Table { table, .. } => { - ExternType::ExternTable(TableType::from_wasmtime_table(&table.table)) + ExternType::Table(TableType::from_wasmtime_table(&table.table)) } } } @@ -147,6 +166,9 @@ fn from_wasmtime_abiparam(param: &ir::AbiParam) -> ValType { ValType::from_wasmtime_type(param.value_type) } +/// A descriptor for a function in a WebAssembly module. +/// +/// WebAssembly functions can have 0 or more parameters and results. #[derive(Debug, Clone)] pub struct FuncType { params: Box<[ValType]>, @@ -155,6 +177,10 @@ pub struct FuncType { } impl FuncType { + /// Creates a new function descriptor from the given parameters and results. + /// + /// The function descriptor returned will represent a function which takes + /// `params` as arguments and returns `results` when it is finished. pub fn new(params: Box<[ValType]>, results: Box<[ValType]>) -> FuncType { use wasmtime_environ::ir::{types, AbiParam, ArgumentPurpose, Signature}; use wasmtime_jit::native; @@ -182,9 +208,13 @@ impl FuncType { signature, } } + + /// Returns the list of parameter types for this function. pub fn params(&self) -> &[ValType] { &self.params } + + /// Returns the list of result types for this function. pub fn results(&self) -> &[ValType] { &self.results } @@ -215,6 +245,11 @@ impl FuncType { // Global Types +/// A WebAssembly global descriptor. +/// +/// This type describes an instance of a global in a WebAssembly module. Globals +/// are local to an [`Instance`](crate::Instance) and are either immutable or +/// mutable. #[derive(Debug, Clone)] pub struct GlobalType { content: ValType, @@ -222,15 +257,21 @@ pub struct GlobalType { } impl GlobalType { + /// Creates a new global descriptor of the specified `content` type and + /// whether or not it's mutable. pub fn new(content: ValType, mutability: Mutability) -> GlobalType { GlobalType { content, mutability, } } + + /// Returns the value type of this global descriptor. pub fn content(&self) -> &ValType { &self.content } + + /// Returns whether or not this global is mutable. pub fn mutability(&self) -> Mutability { self.mutability } @@ -248,6 +289,11 @@ impl GlobalType { // Table Types +/// A descriptor for a table in a WebAssembly module. +/// +/// Tables are contiguous chunks of a specific element, typically a `funcref` or +/// an `anyref`. The most common use for tables is a function table through +/// which `call_indirect` can invoke other functions. #[derive(Debug, Clone)] pub struct TableType { element: ValType, @@ -255,12 +301,18 @@ pub struct TableType { } impl TableType { + /// Creates a new table descriptor which will contain the specified + /// `element` and have the `limits` applied to its length. pub fn new(element: ValType, limits: Limits) -> TableType { TableType { element, limits } } + + /// Returns the element value type of this table. pub fn element(&self) -> &ValType { &self.element } + + /// Returns the limits, in units of elements, of this table. pub fn limits(&self) -> &Limits { &self.limits } @@ -272,103 +324,113 @@ impl TableType { false }); let ty = ValType::FuncRef; - let limits = Limits::new(table.minimum, table.maximum.unwrap_or(::std::u32::MAX)); + let limits = Limits::new(table.minimum, table.maximum); TableType::new(ty, limits) } } // Memory Types +/// A descriptor for a WebAssembly memory type. +/// +/// Memories are described in units of pages (64KB) and represent contiguous +/// chunks of addressable memory. #[derive(Debug, Clone)] pub struct MemoryType { limits: Limits, } impl MemoryType { + /// Creates a new descriptor for a WebAssembly memory given the specified + /// limits of the memory. pub fn new(limits: Limits) -> MemoryType { MemoryType { limits } } + + /// Returns the limits (in pages) that are configured for this memory. pub fn limits(&self) -> &Limits { &self.limits } pub(crate) fn from_wasmtime_memory(memory: &wasm::Memory) -> MemoryType { - MemoryType::new(Limits::new( - memory.minimum, - memory.maximum.unwrap_or(::std::u32::MAX), - )) + MemoryType::new(Limits::new(memory.minimum, memory.maximum)) } } // Import Types -#[derive(Debug, Clone)] -pub struct Name(String); - -impl Name { - pub fn new(value: &str) -> Self { - Name(value.to_owned()) - } - - pub fn as_str(&self) -> &str { - self.0.as_str() - } -} - -impl From for Name { - fn from(s: String) -> Name { - Name(s) - } -} - -impl ToString for Name { - fn to_string(&self) -> String { - self.0.to_owned() - } -} - +/// A descriptor for an imported value into a wasm module. +/// +/// This type is primarily accessed from the +/// [`Module::imports`](crate::Module::imports) API. Each [`ImportType`] +/// describes an import into the wasm module with the module/name that it's +/// imported from as well as the type of item that's being imported. #[derive(Debug, Clone)] pub struct ImportType { - module: Name, - name: Name, - r#type: ExternType, + module: String, + name: String, + ty: ExternType, } impl ImportType { - pub fn new(module: Name, name: Name, r#type: ExternType) -> ImportType { + /// Creates a new import descriptor which comes from `module` and `name` and + /// is of type `ty`. + pub fn new(module: &str, name: &str, ty: ExternType) -> ImportType { ImportType { - module, - name, - r#type, + module: module.to_string(), + name: name.to_string(), + ty, } } - pub fn module(&self) -> &Name { + + /// Returns the module name that this import is expected to come from. + pub fn module(&self) -> &str { &self.module } - pub fn name(&self) -> &Name { + + /// Returns the field name of the module that this import is expected to + /// come from. + pub fn name(&self) -> &str { &self.name } - pub fn r#type(&self) -> &ExternType { - &self.r#type + + /// Returns the expected type of this import. + pub fn ty(&self) -> &ExternType { + &self.ty } } // Export Types +/// A descriptor for an exported WebAssembly value. +/// +/// This type is primarily accessed from the +/// [`Module::exports`](crate::Module::exports) accessor and describes what +/// names are exported from a wasm module and the type of the item that is +/// exported. #[derive(Debug, Clone)] pub struct ExportType { - name: Name, - r#type: ExternType, + name: String, + ty: ExternType, } impl ExportType { - pub fn new(name: Name, r#type: ExternType) -> ExportType { - ExportType { name, r#type } + /// Creates a new export which is exported with the given `name` and has the + /// given `ty`. + pub fn new(name: &str, ty: ExternType) -> ExportType { + ExportType { + name: name.to_string(), + ty, + } } - pub fn name(&self) -> &Name { + + /// Returns the name by which this export is known by. + pub fn name(&self) -> &str { &self.name } - pub fn r#type(&self) -> &ExternType { - &self.r#type + + /// Returns the type of this export. + pub fn ty(&self) -> &ExternType { + &self.ty } } diff --git a/crates/api/src/wasm.rs b/crates/api/src/wasm.rs index 5c58edaa10..98b65433a3 100644 --- a/crates/api/src/wasm.rs +++ b/crates/api/src/wasm.rs @@ -7,8 +7,8 @@ use super::{ AnyRef, Callable, Engine, ExportType, Extern, ExternType, Func, FuncType, Global, GlobalType, - HostInfo, HostRef, ImportType, Instance, Limits, Memory, MemoryType, Module, Name, Store, - Table, TableType, Trap, Val, ValType, + HostInfo, HostRef, ImportType, Instance, Limits, Memory, MemoryType, Module, Store, Table, + TableType, Trap, Val, ValType, }; use std::rc::Rc; use std::{mem, ptr, slice}; @@ -714,11 +714,8 @@ pub unsafe extern "C" fn wasm_module_delete(module: *mut wasm_module_t) { } impl wasm_name_t { - fn from_name(name: &Name) -> wasm_name_t { - let s = name.to_string(); - let mut buffer = Vec::new(); - buffer.extend_from_slice(s.as_bytes()); - buffer.into() + fn from_name(name: &str) -> wasm_name_t { + name.to_string().into_bytes().into() } } @@ -977,7 +974,7 @@ pub unsafe extern "C" fn wasm_importtype_type( it: *const wasm_importtype_t, ) -> *const wasm_externtype_t { let ty = Box::new(wasm_externtype_t { - ty: (*it).ty.r#type().clone(), + ty: (*it).ty.ty().clone(), cache: wasm_externtype_t_type_cache::Empty, }); Box::into_raw(ty) @@ -1004,7 +1001,7 @@ pub unsafe extern "C" fn wasm_exporttype_type( if (*et).type_cache.is_none() { let et = (et as *mut wasm_exporttype_t).as_mut().unwrap(); et.type_cache = Some(wasm_externtype_t { - ty: (*et).ty.r#type().clone(), + ty: (*et).ty.ty().clone(), cache: wasm_externtype_t_type_cache::Empty, }); } @@ -1018,10 +1015,10 @@ pub unsafe extern "C" fn wasm_exporttype_vec_delete(et: *mut wasm_exporttype_vec fn from_externtype(ty: &ExternType) -> wasm_externkind_t { match ty { - ExternType::ExternFunc(_) => 0, - ExternType::ExternGlobal(_) => 1, - ExternType::ExternTable(_) => 2, - ExternType::ExternMemory(_) => 3, + ExternType::Func(_) => 0, + ExternType::Global(_) => 1, + ExternType::Table(_) => 2, + ExternType::Memory(_) => 3, } } @@ -1044,7 +1041,7 @@ pub unsafe extern "C" fn wasm_externtype_as_functype_const( et: *const wasm_externtype_t, ) -> *const wasm_functype_t { if let wasm_externtype_t_type_cache::Empty = (*et).cache { - let functype = (*et).ty.func().clone(); + let functype = (*et).ty.unwrap_func().clone(); let f = wasm_functype_t { functype, params_cache: None, @@ -1064,7 +1061,7 @@ pub unsafe extern "C" fn wasm_externtype_as_globaltype_const( et: *const wasm_externtype_t, ) -> *const wasm_globaltype_t { if let wasm_externtype_t_type_cache::Empty = (*et).cache { - let globaltype = (*et).ty.global().clone(); + let globaltype = (*et).ty.unwrap_global().clone(); let g = wasm_globaltype_t { globaltype, content_cache: None, @@ -1083,7 +1080,7 @@ pub unsafe extern "C" fn wasm_externtype_as_tabletype_const( et: *const wasm_externtype_t, ) -> *const wasm_tabletype_t { if let wasm_externtype_t_type_cache::Empty = (*et).cache { - let tabletype = (*et).ty.table().clone(); + let tabletype = (*et).ty.unwrap_table().clone(); let t = wasm_tabletype_t { tabletype, element_cache: None, @@ -1103,7 +1100,7 @@ pub unsafe extern "C" fn wasm_externtype_as_memorytype_const( et: *const wasm_externtype_t, ) -> *const wasm_memorytype_t { if let wasm_externtype_t_type_cache::Empty = (*et).cache { - let memorytype = (*et).ty.memory().clone(); + let memorytype = (*et).ty.unwrap_memory().clone(); let m = wasm_memorytype_t { memorytype, limits_cache: None, @@ -1210,7 +1207,7 @@ pub unsafe extern "C" fn wasm_memorytype_limits( let limits = (*mt).memorytype.limits(); mt.limits_cache = Some(wasm_limits_t { min: limits.min(), - max: limits.max(), + max: limits.max().unwrap_or(u32::max_value()), }); } (*mt).limits_cache.as_ref().unwrap() @@ -1270,7 +1267,7 @@ pub unsafe extern "C" fn wasm_tabletype_limits( let limits = (*tt).tabletype.limits(); tt.limits_cache = Some(wasm_limits_t { min: limits.min(), - max: limits.max(), + max: limits.max().unwrap_or(u32::max_value()), }); } (*tt).limits_cache.as_ref().unwrap() @@ -1469,7 +1466,12 @@ pub unsafe extern "C" fn wasm_memorytype_delete(mt: *mut wasm_memorytype_t) { pub unsafe extern "C" fn wasm_memorytype_new( limits: *const wasm_limits_t, ) -> *mut wasm_memorytype_t { - let limits = Limits::new((*limits).min, (*limits).max); + let max = if (*limits).max == u32::max_value() { + None + } else { + Some((*limits).max) + }; + let limits = Limits::new((*limits).min, max); let mt = Box::new(wasm_memorytype_t { memorytype: MemoryType::new(limits), limits_cache: None, @@ -1619,7 +1621,12 @@ pub unsafe extern "C" fn wasm_tabletype_new( limits: *const wasm_limits_t, ) -> *mut wasm_tabletype_t { let ty = Box::from_raw(ty).ty; - let limits = Limits::new((*limits).min, (*limits).max); + let max = if (*limits).max == u32::max_value() { + None + } else { + Some((*limits).max) + }; + let limits = Limits::new((*limits).min, max); let tt = Box::new(wasm_tabletype_t { tabletype: TableType::new(ty, limits), element_cache: None, diff --git a/crates/fuzzing/src/oracles/dummy.rs b/crates/fuzzing/src/oracles/dummy.rs index 58a8605e88..b20212a1db 100644 --- a/crates/fuzzing/src/oracles/dummy.rs +++ b/crates/fuzzing/src/oracles/dummy.rs @@ -13,17 +13,17 @@ pub fn dummy_imports( ) -> Result, HostRef> { let mut imports = Vec::with_capacity(import_tys.len()); for imp in import_tys { - imports.push(match imp.r#type() { - ExternType::ExternFunc(func_ty) => { + imports.push(match imp.ty() { + ExternType::Func(func_ty) => { Extern::Func(HostRef::new(DummyFunc::new(&store, func_ty.clone()))) } - ExternType::ExternGlobal(global_ty) => { + ExternType::Global(global_ty) => { Extern::Global(HostRef::new(dummy_global(&store, global_ty.clone())?)) } - ExternType::ExternTable(table_ty) => { + ExternType::Table(table_ty) => { Extern::Table(HostRef::new(dummy_table(&store, table_ty.clone())?)) } - ExternType::ExternMemory(mem_ty) => { + ExternType::Memory(mem_ty) => { Extern::Memory(HostRef::new(dummy_memory(&store, mem_ty.clone()))) } }); diff --git a/crates/misc/py/src/instance.rs b/crates/misc/py/src/instance.rs index a8c5781f49..5975371cd4 100644 --- a/crates/misc/py/src/instance.rs +++ b/crates/misc/py/src/instance.rs @@ -22,8 +22,8 @@ impl Instance { let exports = PyDict::new(py); let module = self.instance.borrow().module().clone(); for (i, e) in module.borrow().exports().iter().enumerate() { - match e.r#type() { - wasmtime::ExternType::ExternFunc(ft) => { + match e.ty() { + wasmtime::ExternType::Func(ft) => { let mut args_types = Vec::new(); for ty in ft.params().iter() { args_types.push(ty.clone()); @@ -39,7 +39,7 @@ impl Instance { )?; exports.set_item(e.name().to_string(), f)?; } - wasmtime::ExternType::ExternMemory(_) => { + wasmtime::ExternType::Memory(_) => { let f = Py::new( py, Memory { diff --git a/crates/misc/py/src/lib.rs b/crates/misc/py/src/lib.rs index ca584a4ed9..7e56b06977 100644 --- a/crates/misc/py/src/lib.rs +++ b/crates/misc/py/src/lib.rs @@ -111,21 +111,18 @@ pub fn instantiate( let mut imports: Vec = Vec::new(); for i in module.borrow().imports() { - let module_name = i.module().as_str(); + let module_name = i.module(); if let Some(m) = import_obj.get_item(module_name) { - let e = find_export_in(m, &store, i.name().as_str())?; + let e = find_export_in(m, &store, i.name())?; imports.push(e); } else if wasi.is_some() && module_name == wasi.as_ref().unwrap().0 { let e = wasi .as_ref() .unwrap() .1 - .find_export_by_name(i.name().as_str()) + .find_export_by_name(i.name()) .ok_or_else(|| { - PyErr::new::(format!( - "wasi export {} is not found", - i.name().as_str() - )) + PyErr::new::(format!("wasi export {} is not found", i.name(),)) })?; imports.push(e.clone()); } else { diff --git a/crates/misc/rust/macro/src/lib.rs b/crates/misc/rust/macro/src/lib.rs index 9327e55f9d..03d167eef8 100644 --- a/crates/misc/rust/macro/src/lib.rs +++ b/crates/misc/rust/macro/src/lib.rs @@ -69,13 +69,13 @@ fn generate_load(item: &syn::ItemTrait) -> syn::Result { let wasi_instance = #root::wasmtime_wasi::create_wasi_instance(&store, &[], &[], &[]) .map_err(|e| format_err!("wasm instantiation error: {:?}", e))?; for i in module.borrow().imports().iter() { - if i.module().as_str() != module_name { - bail!("unknown import module {}", i.module().as_str()); + if i.module() != module_name { + bail!("unknown import module {}", i.module()); } - if let Some(export) = wasi_instance.find_export_by_name(i.name().as_str()) { + if let Some(export) = wasi_instance.find_export_by_name(i.name()) { imports.push(export.clone()); } else { - bail!("unknown import {}:{}", i.module().as_str(), i.name().as_str()) + bail!("unknown import {}:{}", i.module(), i.name()) } } } diff --git a/crates/test-programs/tests/wasm_tests/runtime.rs b/crates/test-programs/tests/wasm_tests/runtime.rs index 52b79a657e..2d21c29e88 100644 --- a/crates/test-programs/tests/wasm_tests/runtime.rs +++ b/crates/test-programs/tests/wasm_tests/runtime.rs @@ -68,9 +68,9 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any .imports() .iter() .map(|i| { - let module_name = i.module().as_str(); + let module_name = i.module(); if let Some(instance) = module_registry.get(module_name) { - let field_name = i.name().as_str(); + let field_name = i.name(); if let Some(export) = instance.find_export_by_name(field_name) { Ok(export.clone()) } else { diff --git a/src/bin/wasmtime.rs b/src/bin/wasmtime.rs index 3ab3217180..a0abb06fa6 100644 --- a/src/bin/wasmtime.rs +++ b/src/bin/wasmtime.rs @@ -341,9 +341,9 @@ fn instantiate_module( .imports() .iter() .map(|i| { - let module_name = i.module().as_str(); + let module_name = i.module(); if let Some(instance) = module_registry.get(module_name) { - let field_name = i.name().as_str(); + let field_name = i.name(); if let Some(export) = instance.borrow().find_export_by_name(field_name) { Ok(export.clone()) } else { @@ -380,7 +380,7 @@ fn handle_module( .borrow() .exports() .iter() - .find(|export| export.name().as_str().is_empty()) + .find(|export| export.name().is_empty()) .is_some() { // Launch the default command export.