2019-08-02 02:46:35 +00:00
|
|
|
#![deny(
|
|
|
|
dead_code,
|
2019-08-16 02:18:29 +00:00
|
|
|
nonstandard_style,
|
2019-08-02 02:46:35 +00:00
|
|
|
unused_imports,
|
2019-08-16 02:18:29 +00:00
|
|
|
unused_mut,
|
2019-08-02 02:46:35 +00:00
|
|
|
unused_variables,
|
|
|
|
unused_unsafe,
|
|
|
|
unreachable_patterns
|
|
|
|
)]
|
2019-04-11 19:07:54 +00:00
|
|
|
#![cfg_attr(nightly, feature(unwind_attributes))]
|
|
|
|
|
2019-01-22 19:02:06 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate field_offset;
|
|
|
|
|
2019-02-07 00:26:45 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
|
2019-06-03 14:42:45 +00:00
|
|
|
#[allow(unused_imports)]
|
2019-06-03 13:23:40 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
|
2019-01-22 19:02:06 +00:00
|
|
|
#[macro_use]
|
|
|
|
mod macros;
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub mod backend;
|
|
|
|
mod backing;
|
2019-02-19 23:36:22 +00:00
|
|
|
|
2019-02-07 00:26:45 +00:00
|
|
|
pub mod cache;
|
2019-04-27 08:31:47 +00:00
|
|
|
pub mod codegen;
|
2019-01-22 19:02:06 +00:00
|
|
|
pub mod error;
|
|
|
|
pub mod export;
|
2019-01-28 19:55:44 +00:00
|
|
|
pub mod global;
|
2019-01-22 19:02:06 +00:00
|
|
|
pub mod import;
|
|
|
|
pub mod instance;
|
2019-05-14 08:04:08 +00:00
|
|
|
pub mod loader;
|
2019-01-22 19:02:06 +00:00
|
|
|
pub mod memory;
|
|
|
|
pub mod module;
|
2019-04-27 08:31:47 +00:00
|
|
|
pub mod parse;
|
2019-01-22 19:02:06 +00:00
|
|
|
mod sig_registry;
|
|
|
|
pub mod structures;
|
|
|
|
mod sys;
|
|
|
|
pub mod table;
|
2019-06-03 13:24:41 +00:00
|
|
|
#[cfg(all(unix, target_arch = "x86_64"))]
|
|
|
|
pub mod trampoline_x64;
|
2019-04-09 22:53:01 +00:00
|
|
|
pub mod typed_func;
|
2019-01-22 19:02:06 +00:00
|
|
|
pub mod types;
|
2019-01-29 23:44:15 +00:00
|
|
|
pub mod units;
|
2019-01-22 19:02:06 +00:00
|
|
|
pub mod vm;
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub mod vmcalls;
|
2019-06-03 13:23:40 +00:00
|
|
|
#[cfg(all(unix, target_arch = "x86_64"))]
|
|
|
|
pub use trampoline_x64 as trampoline;
|
2019-06-26 12:44:51 +00:00
|
|
|
#[cfg(all(unix, target_arch = "x86_64"))]
|
2019-07-03 17:27:19 +00:00
|
|
|
pub mod fault;
|
2019-06-09 13:21:18 +00:00
|
|
|
pub mod state;
|
2019-01-22 19:02:06 +00:00
|
|
|
|
|
|
|
use self::error::CompileResult;
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use self::error::Result;
|
|
|
|
#[doc(inline)]
|
2019-02-10 22:24:04 +00:00
|
|
|
pub use self::import::IsExport;
|
|
|
|
#[doc(inline)]
|
2019-04-10 01:33:29 +00:00
|
|
|
pub use self::instance::{DynFunc, Instance};
|
2019-01-22 19:02:06 +00:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use self::module::Module;
|
2019-02-02 23:28:50 +00:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use self::typed_func::Func;
|
2019-01-28 19:55:44 +00:00
|
|
|
use std::sync::Arc;
|
2019-01-22 19:02:06 +00:00
|
|
|
|
2019-05-16 17:10:21 +00:00
|
|
|
pub use wasmparser;
|
|
|
|
|
2019-02-22 01:06:49 +00:00
|
|
|
use self::cache::{Artifact, Error as CacheError};
|
2019-02-07 00:26:45 +00:00
|
|
|
|
2019-01-22 19:02:06 +00:00
|
|
|
pub mod prelude {
|
|
|
|
pub use crate::import::{ImportObject, Namespace};
|
|
|
|
pub use crate::types::{
|
|
|
|
FuncIndex, GlobalIndex, ImportedFuncIndex, ImportedGlobalIndex, ImportedMemoryIndex,
|
|
|
|
ImportedTableIndex, LocalFuncIndex, LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex,
|
|
|
|
MemoryIndex, TableIndex, Type, Value,
|
|
|
|
};
|
|
|
|
pub use crate::vm;
|
2019-01-26 00:40:07 +00:00
|
|
|
pub use crate::{func, imports};
|
2019-01-22 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 20:34:15 +00:00
|
|
|
/// Compile a [`Module`] using the provided compiler from
|
|
|
|
/// WebAssembly binary code. This function is useful if it
|
|
|
|
/// is necessary to a compile a module before it can be instantiated
|
|
|
|
/// and must be used if you wish to use a different backend from the default.
|
|
|
|
///
|
|
|
|
/// [`Module`]: struct.Module.html
|
2019-01-22 19:02:06 +00:00
|
|
|
pub fn compile_with(
|
|
|
|
wasm: &[u8],
|
|
|
|
compiler: &dyn backend::Compiler,
|
|
|
|
) -> CompileResult<module::Module> {
|
|
|
|
let token = backend::Token::generate();
|
2019-03-28 18:42:59 +00:00
|
|
|
compiler
|
|
|
|
.compile(wasm, Default::default(), token)
|
|
|
|
.map(|mut inner| {
|
|
|
|
let inner_info: &mut crate::module::ModuleInfo = &mut inner.info;
|
|
|
|
inner_info.import_custom_sections(wasm).unwrap();
|
|
|
|
module::Module::new(Arc::new(inner))
|
|
|
|
})
|
2019-01-22 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 21:08:17 +00:00
|
|
|
/// The same as `compile_with` but changes the compiler behavior
|
|
|
|
/// with the values in the `CompilerConfig`
|
2019-03-27 21:01:27 +00:00
|
|
|
pub fn compile_with_config(
|
|
|
|
wasm: &[u8],
|
|
|
|
compiler: &dyn backend::Compiler,
|
|
|
|
compiler_config: backend::CompilerConfig,
|
|
|
|
) -> CompileResult<module::Module> {
|
|
|
|
let token = backend::Token::generate();
|
|
|
|
compiler
|
|
|
|
.compile(wasm, compiler_config, token)
|
2019-01-28 19:55:44 +00:00
|
|
|
.map(|inner| module::Module::new(Arc::new(inner)))
|
2019-01-22 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 20:34:15 +00:00
|
|
|
/// Perform validation as defined by the
|
|
|
|
/// WebAssembly specification. Returns `true` if validation
|
|
|
|
/// succeeded, `false` if validation failed.
|
2019-01-22 19:02:06 +00:00
|
|
|
pub fn validate(wasm: &[u8]) -> bool {
|
2019-04-04 00:21:57 +00:00
|
|
|
validate_and_report_errors(wasm).is_ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The same as `validate` but with an Error message on failure
|
|
|
|
pub fn validate_and_report_errors(wasm: &[u8]) -> ::std::result::Result<(), String> {
|
2019-07-26 06:33:30 +00:00
|
|
|
validate_and_report_errors_with_features(wasm, Default::default())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The same as `validate_and_report_errors` but with a Features.
|
|
|
|
pub fn validate_and_report_errors_with_features(
|
|
|
|
wasm: &[u8],
|
|
|
|
features: backend::Features,
|
|
|
|
) -> ::std::result::Result<(), String> {
|
2019-01-22 19:02:06 +00:00
|
|
|
use wasmparser::WasmDecoder;
|
2019-07-26 06:33:30 +00:00
|
|
|
let config = wasmparser::ValidatingParserConfig {
|
|
|
|
operator_config: wasmparser::OperatorValidatorConfig {
|
|
|
|
enable_simd: features.simd,
|
|
|
|
enable_bulk_memory: false,
|
|
|
|
enable_multi_value: false,
|
|
|
|
enable_reference_types: false,
|
|
|
|
enable_threads: false,
|
|
|
|
},
|
2019-08-02 21:16:39 +00:00
|
|
|
mutable_global_imports: true,
|
2019-07-26 06:33:30 +00:00
|
|
|
};
|
|
|
|
let mut parser = wasmparser::ValidatingParser::new(wasm, Some(config));
|
2019-01-22 19:02:06 +00:00
|
|
|
loop {
|
|
|
|
let state = parser.read();
|
|
|
|
match *state {
|
2019-04-04 00:21:57 +00:00
|
|
|
wasmparser::ParserState::EndWasm => break Ok(()),
|
|
|
|
wasmparser::ParserState::Error(e) => break Err(format!("{}", e)),
|
2019-01-22 19:02:06 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-22 23:26:56 +00:00
|
|
|
|
2019-02-07 00:26:45 +00:00
|
|
|
pub unsafe fn load_cache_with(
|
2019-02-22 01:06:49 +00:00
|
|
|
cache: Artifact,
|
2019-02-07 00:26:45 +00:00
|
|
|
compiler: &dyn backend::Compiler,
|
|
|
|
) -> std::result::Result<module::Module, CacheError> {
|
|
|
|
let token = backend::Token::generate();
|
|
|
|
compiler
|
|
|
|
.from_cache(cache, token)
|
|
|
|
.map(|inner| module::Module::new(Arc::new(inner)))
|
|
|
|
}
|
|
|
|
|
2019-01-22 23:26:56 +00:00
|
|
|
/// The current version of this crate
|
|
|
|
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|