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};
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, vec,
49        };
50    }
51}
52
53pub mod error;
54mod features;
55mod indexes;
56mod initializers;
57mod libcalls;
58mod memory;
59mod module;
60mod module_hash;
61mod serialize;
62mod stack;
63mod store_id;
64mod table;
65pub mod target;
66mod trapcode;
67mod types;
68mod units;
69mod utils;
70mod value;
71mod vmoffsets;
72
73pub use error::{
74    CompileError, DeserializeError, ImportError, MemoryError, MiddlewareError,
75    ParseCpuFeatureError, PreInstantiationError, SerializeError, WasmError, WasmResult,
76};
77
78/// The entity module, with common helpers for Rust structures
79pub mod entity;
80pub use crate::features::Features;
81pub use crate::indexes::{
82    CustomSectionIndex, DataIndex, ElemIndex, ExportIndex, FunctionIndex, GlobalIndex, ImportIndex,
83    LocalFunctionIndex, LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex, LocalTagIndex,
84    MemoryIndex, SignatureIndex, TableIndex, Tag, TagIndex,
85};
86pub use crate::initializers::{
87    ArchivedDataInitializerLocation, ArchivedOwnedDataInitializer, DataInitializer,
88    DataInitializerLike, DataInitializerLocation, DataInitializerLocationLike,
89    OwnedDataInitializer, TableInitializer,
90};
91pub use crate::memory::{Memory32, Memory64, MemorySize};
92pub use crate::module::{ExportsIterator, ImportKey, ImportsIterator, ModuleInfo};
93pub use crate::module_hash::{HashAlgorithm, ModuleHash};
94pub use crate::types::{
95    ExportType, ExternType, FunctionType, GlobalInit, GlobalType, ImportType, MemoryType,
96    Mutability, TableType, TagKind, TagType, Type, V128,
97};
98pub use crate::units::{
99    Bytes, PageCountOutOfRange, Pages, WASM_MAX_PAGES, WASM_MIN_PAGES, WASM_PAGE_SIZE,
100};
101pub use value::{RawValue, ValueType};
102
103pub use crate::libcalls::LibCall;
104pub use crate::memory::MemoryStyle;
105pub use crate::table::TableStyle;
106pub use serialize::MetadataHeader;
107// TODO: OnCalledAction is needed for asyncify. It will be refactored with https://github.com/wasmerio/wasmer/issues/3451
108pub use crate::stack::{FrameInfo, SourceLoc, TrapInformation};
109pub use crate::store_id::StoreId;
110pub use crate::trapcode::{OnCalledAction, TrapCode};
111pub use crate::utils::is_wasm;
112pub use crate::vmoffsets::{TargetSharedSignatureIndex, VMBuiltinFunctionIndex, VMOffsets};
113
114/// Offset in bytes from the beginning of the function.
115pub type CodeOffset = u32;
116
117/// Addend to add to the symbol value.
118pub type Addend = i64;
119
120/// Version number of this crate.
121pub const VERSION: &str = env!("CARGO_PKG_VERSION");
122
123mod native {
124    use super::Type;
125    use crate::memory::{Memory32, Memory64, MemorySize};
126    use std::fmt;
127
128    /// `NativeWasmType` represents a Wasm type that has a direct
129    /// representation on the host (hence the “native” term).
130    ///
131    /// It uses the Rust Type system to automatically detect the
132    /// Wasm type associated with a native Rust type.
133    ///
134    /// ```
135    /// use wasmer_types::{NativeWasmType, Type};
136    ///
137    /// let wasm_type = i32::WASM_TYPE;
138    /// assert_eq!(wasm_type, Type::I32);
139    /// ```
140    ///
141    /// > Note: This strategy will be needed later to
142    /// > automatically detect the signature of a Rust function.
143    pub trait NativeWasmType: Sized {
144        /// The ABI for this type (i32, i64, f32, f64)
145        type Abi: Copy + fmt::Debug;
146
147        /// Type for this `NativeWasmType`.
148        const WASM_TYPE: Type;
149    }
150
151    impl NativeWasmType for u32 {
152        const WASM_TYPE: Type = Type::I32;
153        type Abi = Self;
154    }
155
156    impl NativeWasmType for i32 {
157        const WASM_TYPE: Type = Type::I32;
158        type Abi = Self;
159    }
160
161    impl NativeWasmType for i64 {
162        const WASM_TYPE: Type = Type::I64;
163        type Abi = Self;
164    }
165
166    impl NativeWasmType for u64 {
167        const WASM_TYPE: Type = Type::I64;
168        type Abi = Self;
169    }
170
171    impl NativeWasmType for f32 {
172        const WASM_TYPE: Type = Type::F32;
173        type Abi = Self;
174    }
175
176    impl NativeWasmType for f64 {
177        const WASM_TYPE: Type = Type::F64;
178        type Abi = Self;
179    }
180
181    impl NativeWasmType for u128 {
182        const WASM_TYPE: Type = Type::V128;
183        type Abi = Self;
184    }
185
186    impl NativeWasmType for Memory32 {
187        const WASM_TYPE: Type = <<Self as MemorySize>::Native as NativeWasmType>::WASM_TYPE;
188        type Abi = <<Self as MemorySize>::Native as NativeWasmType>::Abi;
189    }
190
191    impl NativeWasmType for Memory64 {
192        const WASM_TYPE: Type = <<Self as MemorySize>::Native as NativeWasmType>::WASM_TYPE;
193        type Abi = <<Self as MemorySize>::Native as NativeWasmType>::Abi;
194    }
195
196    impl<T: NativeWasmType> NativeWasmType for Option<T> {
197        const WASM_TYPE: Type = T::WASM_TYPE;
198        type Abi = T::Abi;
199    }
200}
201
202pub use crate::native::*;