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