Skip to main content

wasmer_types/
lib.rs

1//! This are the common types and utility tools for using WebAssembly
2//! in a Rust environment.
3//!
4//! This crate provides common structures such as `Type` or `Value`, type indexes
5//! and native function wrappers with `Func`.
6
7#![deny(missing_docs, unused_extern_crates)]
8#![warn(unused_import_braces)]
9#![allow(clippy::new_without_default)]
10#![warn(
11    clippy::float_arithmetic,
12    clippy::mut_mut,
13    clippy::nonminimal_bool,
14    clippy::map_unwrap_or,
15    clippy::print_stdout,
16    clippy::unicode_not_nfc,
17    clippy::use_self
18)]
19#![cfg_attr(docsrs, feature(doc_cfg))]
20
21pub mod error;
22mod exception;
23mod features;
24mod indexes;
25mod initializers;
26mod libcalls;
27mod memory;
28mod module;
29mod module_hash;
30mod progress;
31mod serialize;
32mod stack;
33mod store_id;
34mod table;
35pub mod target;
36mod trapcode;
37mod types;
38mod units;
39mod utils;
40mod value;
41mod vmoffsets;
42
43pub use error::{
44    CompileError, DeserializeError, ImportError, MemoryError, MiddlewareError,
45    ParseCpuFeatureError, PreInstantiationError, SerializeError, WasmError, WasmResult,
46};
47
48/// The entity module, with common helpers for Rust structures
49pub mod entity;
50pub use crate::features::Features;
51pub use crate::indexes::{
52    CustomSectionIndex, DataIndex, ElemIndex, ExportIndex, FunctionIndex, GlobalIndex, ImportIndex,
53    LocalFunctionIndex, LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex, LocalTagIndex,
54    MemoryIndex, SignatureHash, SignatureIndex, TableIndex, Tag, TagIndex,
55};
56pub use crate::initializers::{
57    ArchivedDataInitializerLocation, ArchivedOwnedDataInitializer, DataInitializer,
58    DataInitializerLike, DataInitializerLocation, DataInitializerLocationLike,
59    OwnedDataInitializer, TableInitializer,
60};
61pub use crate::memory::{Memory32, Memory64, MemorySize};
62pub use crate::module::{ExportsIterator, ImportKey, ImportsIterator, ModuleInfo};
63pub use crate::module_hash::ModuleHash;
64pub use crate::progress::{CompilationProgress, CompilationProgressCallback, UserAbort};
65pub use crate::types::{
66    ExportType, ExternType, FunctionType, GlobalInit, GlobalType, ImportType, InitExpr, InitExprOp,
67    MemoryType, Mutability, TableType, TagKind, TagType, Type, V128,
68};
69pub use crate::units::{
70    Bytes, PageCountOutOfRange, Pages, WASM_MAX_PAGES, WASM_MIN_PAGES, WASM_PAGE_SIZE,
71};
72pub use value::{RawValue, ValueType};
73
74pub use crate::libcalls::LibCall;
75pub use crate::memory::MemoryStyle;
76pub use crate::table::TableStyle;
77pub use serialize::MetadataHeader;
78// TODO: OnCalledAction is needed for asyncify. It will be refactored with https://github.com/wasmerio/wasmer/issues/3451
79pub use crate::exception::CATCH_ALL_TAG_VALUE;
80pub use crate::stack::{FrameInfo, SourceLoc, TrapInformation};
81pub use crate::store_id::StoreId;
82pub use crate::trapcode::{OnCalledAction, TrapCode};
83pub use crate::utils::is_wasm;
84pub use crate::vmoffsets::{VMBuiltinFunctionIndex, VMOffsets, vmctx_offset};
85
86/// Offset in bytes from the beginning of the function.
87pub type CodeOffset = u32;
88
89/// Addend to add to the symbol value.
90pub type Addend = i64;
91
92/// Version number of this crate.
93pub const VERSION: &str = env!("CARGO_PKG_VERSION");
94
95mod native {
96    use super::Type;
97    use crate::memory::{Memory32, Memory64, MemorySize};
98    use std::fmt;
99
100    /// `NativeWasmType` represents a Wasm type that has a direct
101    /// representation on the host (hence the “native” term).
102    ///
103    /// It uses the Rust Type system to automatically detect the
104    /// Wasm type associated with a native Rust type.
105    ///
106    /// ```
107    /// use wasmer_types::{NativeWasmType, Type};
108    ///
109    /// let wasm_type = i32::WASM_TYPE;
110    /// assert_eq!(wasm_type, Type::I32);
111    /// ```
112    ///
113    /// > Note: This strategy will be needed later to
114    /// > automatically detect the signature of a Rust function.
115    pub trait NativeWasmType: Sized {
116        /// The ABI for this type (i32, i64, f32, f64)
117        type Abi: Copy + fmt::Debug;
118
119        /// Type for this `NativeWasmType`.
120        const WASM_TYPE: Type;
121    }
122
123    impl NativeWasmType for u32 {
124        const WASM_TYPE: Type = Type::I32;
125        type Abi = Self;
126    }
127
128    impl NativeWasmType for i32 {
129        const WASM_TYPE: Type = Type::I32;
130        type Abi = Self;
131    }
132
133    impl NativeWasmType for i64 {
134        const WASM_TYPE: Type = Type::I64;
135        type Abi = Self;
136    }
137
138    impl NativeWasmType for u64 {
139        const WASM_TYPE: Type = Type::I64;
140        type Abi = Self;
141    }
142
143    impl NativeWasmType for f32 {
144        const WASM_TYPE: Type = Type::F32;
145        type Abi = Self;
146    }
147
148    impl NativeWasmType for f64 {
149        const WASM_TYPE: Type = Type::F64;
150        type Abi = Self;
151    }
152
153    impl NativeWasmType for u128 {
154        const WASM_TYPE: Type = Type::V128;
155        type Abi = Self;
156    }
157
158    impl NativeWasmType for Memory32 {
159        const WASM_TYPE: Type = <<Self as MemorySize>::Native as NativeWasmType>::WASM_TYPE;
160        type Abi = <<Self as MemorySize>::Native as NativeWasmType>::Abi;
161    }
162
163    impl NativeWasmType for Memory64 {
164        const WASM_TYPE: Type = <<Self as MemorySize>::Native as NativeWasmType>::WASM_TYPE;
165        type Abi = <<Self as MemorySize>::Native as NativeWasmType>::Abi;
166    }
167
168    impl<T: NativeWasmType> NativeWasmType for Option<T> {
169        const WASM_TYPE: Type = T::WASM_TYPE;
170        type Abi = T::Abi;
171    }
172}
173
174pub use crate::native::*;