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