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