2019-01-22 19:02:06 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate field_offset;
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
mod macros;
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub mod backend;
|
|
|
|
mod backing;
|
|
|
|
pub mod error;
|
|
|
|
pub mod export;
|
|
|
|
pub mod import;
|
|
|
|
pub mod instance;
|
|
|
|
pub mod memory;
|
|
|
|
pub mod module;
|
|
|
|
mod sig_registry;
|
|
|
|
pub mod structures;
|
|
|
|
mod sys;
|
|
|
|
pub mod table;
|
|
|
|
pub mod types;
|
|
|
|
pub mod vm;
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub mod vmcalls;
|
|
|
|
|
|
|
|
use self::error::CompileResult;
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use self::error::Result;
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use self::instance::Instance;
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use self::module::Module;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
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;
|
|
|
|
pub use crate::{export_func, imports};
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
compiler
|
|
|
|
.compile(wasm, token)
|
|
|
|
.map(|inner| module::Module::new(Rc::new(inner)))
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
use wasmparser::WasmDecoder;
|
|
|
|
let mut parser = wasmparser::ValidatingParser::new(wasm, None);
|
|
|
|
loop {
|
|
|
|
let state = parser.read();
|
|
|
|
match *state {
|
|
|
|
wasmparser::ParserState::EndWasm => break true,
|
|
|
|
wasmparser::ParserState::Error(_) => break false,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-22 23:26:56 +00:00
|
|
|
|
|
|
|
/// The current version of this crate
|
|
|
|
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|