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#![cfg_attr(not(feature = "std"), no_std)]
10#![allow(clippy::new_without_default)]
11#![warn(
12    clippy::float_arithmetic,
13    clippy::mut_mut,
14    clippy::nonminimal_bool,
15    clippy::map_unwrap_or,
16    clippy::print_stdout,
17    clippy::unicode_not_nfc,
18    clippy::use_self
19)]
20#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
21
22#[cfg(all(feature = "std", feature = "core"))]
23compile_error!(
24    "The `std` and `core` features are both enabled, which is an error. Please enable only once."
25);
26
27#[cfg(all(not(feature = "std"), not(feature = "core")))]
28compile_error!("Both the `std` and `core` features are disabled. Please enable one of them.");
29
30#[cfg(feature = "core")]
31extern crate alloc;
32
33/// The `lib` module defines a `std` module that is identical whether
34/// the `core` or the `std` feature is enabled.
35pub mod lib {
36    /// Custom `std` module.
37    #[cfg(feature = "core")]
38    pub mod std {
39        pub use alloc::{borrow, boxed, format, iter, rc, slice, string, vec};
40        pub use core::{any, cell, cmp, convert, fmt, hash, marker, mem, ops, ptr, sync, u32};
41    }
42
43    /// Custom `std` module.
44    #[cfg(feature = "std")]
45    pub mod std {
46        pub use std::{
47            any, borrow, boxed, cell, cmp, convert, fmt, format, hash, iter, marker, mem, ops, ptr,
48            rc, slice, string, sync, u32, vec,
49        };
50    }
51}
52
53pub mod compilation;
54pub mod error;
55mod features;
56mod indexes;
57mod initializers;
58mod libcalls;
59mod memory;
60mod module;
61mod module_hash;
62mod serialize;
63mod stack;
64mod store_id;
65mod table;
66mod trapcode;
67mod types;
68mod units;
69mod utils;
70mod value;
71mod vmoffsets;
72
73pub use crate::compilation::target::{
74    Aarch64Architecture, Architecture, BinaryFormat, CallingConvention, CpuFeature, Endianness,
75    Environment, OperatingSystem, PointerWidth, Target, Triple, Vendor,
76};
77pub use crate::serialize::{
78    ArchivedSerializableCompilation, ArchivedSerializableModule, MetadataHeader,
79    SerializableCompilation, SerializableModule,
80};
81pub use error::{
82    CompileError, DeserializeError, ImportError, MemoryError, MiddlewareError,
83    ParseCpuFeatureError, PreInstantiationError, SerializeError, WasmError, WasmResult,
84};
85
86/// The entity module, with common helpers for Rust structures
87pub mod entity;
88pub use crate::features::Features;
89pub use crate::indexes::{
90    CustomSectionIndex, DataIndex, ElemIndex, ExportIndex, FunctionIndex, GlobalIndex, ImportIndex,
91    LocalFunctionIndex, LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex, MemoryIndex,
92    SignatureIndex, TableIndex,
93};
94pub use crate::initializers::{
95    ArchivedDataInitializerLocation, ArchivedOwnedDataInitializer, DataInitializer,
96    DataInitializerLike, DataInitializerLocation, DataInitializerLocationLike,
97    OwnedDataInitializer, TableInitializer,
98};
99pub use crate::memory::{Memory32, Memory64, MemorySize};
100pub use crate::module::{ExportsIterator, ImportKey, ImportsIterator, ModuleInfo};
101pub use crate::module_hash::{HashAlgorithm, ModuleHash};
102pub use crate::units::{
103    Bytes, PageCountOutOfRange, Pages, WASM_MAX_PAGES, WASM_MIN_PAGES, WASM_PAGE_SIZE,
104};
105pub use types::{
106    ExportType, ExternType, FunctionType, GlobalInit, GlobalType, ImportType, MemoryType,
107    Mutability, TableType, Type, V128,
108};
109pub use value::{RawValue, ValueType};
110
111pub use crate::libcalls::LibCall;
112pub use crate::memory::MemoryStyle;
113pub use crate::table::TableStyle;
114// TODO: OnCalledAction is needed for asyncify. It will be refactored with https://github.com/wasmerio/wasmer/issues/3451
115pub use crate::trapcode::{OnCalledAction, TrapCode};
116pub use crate::vmoffsets::{TargetSharedSignatureIndex, VMBuiltinFunctionIndex, VMOffsets};
117
118pub use crate::utils::is_wasm;
119
120pub use crate::compilation::relocation::{
121    ArchivedRelocation, Relocation, RelocationKind, RelocationLike, RelocationTarget, Relocations,
122};
123pub use crate::compilation::section::{
124    ArchivedCustomSection, CustomSection, CustomSectionLike, CustomSectionProtection, SectionBody,
125    SectionIndex,
126};
127
128pub use crate::compilation::address_map::{FunctionAddressMap, InstructionAddressMap};
129pub use crate::compilation::function::{
130    ArchivedFunctionBody, Compilation, CompiledFunction, CompiledFunctionFrameInfo, CustomSections,
131    Dwarf, FunctionBody, FunctionBodyLike, Functions,
132};
133pub use crate::compilation::module::CompileModuleInfo;
134pub use crate::compilation::symbols::{Symbol, SymbolRegistry};
135pub use crate::compilation::unwind::{
136    ArchivedCompiledFunctionUnwindInfo, CompiledFunctionUnwindInfo, CompiledFunctionUnwindInfoLike,
137    CompiledFunctionUnwindInfoReference,
138};
139
140pub use crate::stack::{FrameInfo, SourceLoc, TrapInformation};
141pub use crate::store_id::StoreId;
142
143/// Offset in bytes from the beginning of the function.
144pub type CodeOffset = u32;
145
146/// Addend to add to the symbol value.
147pub type Addend = i64;
148
149/// Version number of this crate.
150pub const VERSION: &str = env!("CARGO_PKG_VERSION");
151
152mod native {
153    use super::Type;
154    use crate::memory::{Memory32, Memory64, MemorySize};
155    use std::fmt;
156
157    /// `NativeWasmType` represents a Wasm type that has a direct
158    /// representation on the host (hence the “native” term).
159    ///
160    /// It uses the Rust Type system to automatically detect the
161    /// Wasm type associated with a native Rust type.
162    ///
163    /// ```
164    /// use wasmer_types::{NativeWasmType, Type};
165    ///
166    /// let wasm_type = i32::WASM_TYPE;
167    /// assert_eq!(wasm_type, Type::I32);
168    /// ```
169    ///
170    /// > Note: This strategy will be needed later to
171    /// > automatically detect the signature of a Rust function.
172    pub trait NativeWasmType: Sized {
173        /// The ABI for this type (i32, i64, f32, f64)
174        type Abi: Copy + fmt::Debug;
175
176        /// Type for this `NativeWasmType`.
177        const WASM_TYPE: Type;
178    }
179
180    impl NativeWasmType for u32 {
181        const WASM_TYPE: Type = Type::I32;
182        type Abi = Self;
183    }
184
185    impl NativeWasmType for i32 {
186        const WASM_TYPE: Type = Type::I32;
187        type Abi = Self;
188    }
189
190    impl NativeWasmType for i64 {
191        const WASM_TYPE: Type = Type::I64;
192        type Abi = Self;
193    }
194
195    impl NativeWasmType for u64 {
196        const WASM_TYPE: Type = Type::I64;
197        type Abi = Self;
198    }
199
200    impl NativeWasmType for f32 {
201        const WASM_TYPE: Type = Type::F32;
202        type Abi = Self;
203    }
204
205    impl NativeWasmType for f64 {
206        const WASM_TYPE: Type = Type::F64;
207        type Abi = Self;
208    }
209
210    impl NativeWasmType for u128 {
211        const WASM_TYPE: Type = Type::V128;
212        type Abi = Self;
213    }
214
215    impl NativeWasmType for Memory32 {
216        const WASM_TYPE: Type = <<Self as MemorySize>::Native as NativeWasmType>::WASM_TYPE;
217        type Abi = <<Self as MemorySize>::Native as NativeWasmType>::Abi;
218    }
219
220    impl NativeWasmType for Memory64 {
221        const WASM_TYPE: Type = <<Self as MemorySize>::Native as NativeWasmType>::WASM_TYPE;
222        type Abi = <<Self as MemorySize>::Native as NativeWasmType>::Abi;
223    }
224
225    impl<T: NativeWasmType> NativeWasmType for Option<T> {
226        const WASM_TYPE: Type = T::WASM_TYPE;
227        type Abi = T::Abi;
228    }
229}
230
231pub use crate::native::*;