Struct wasmer_ruby::Wasmer::Module
source · pub struct Module;
Expand description
A WebAssembly module contains stateless WebAssembly code that has already been compiled and can be instantiated multiple times.
Creates a new WebAssembly Module
given the configuration in the
store.
If the provided bytes are not WebAssembly-like (start with b“\0asm“), this function will try to to convert the bytes assuming they correspond to the WebAssembly text format.
Security
Before the code is compiled, it will be validated using the store features.
Example
store = Wasmer::Store.new
# Let's compile WebAssembly from bytes.
wasm_bytes = "\x00asm\x01\x00\x00\x00";
module_ = Wasmer::Module.new store, wasm_bytes
# Let's compile WebAssembly from WAT.
module_ = Wasmer::Module.new store, "(module)"
Implementations§
source§impl Module
impl Module
sourcepub fn validate(store: Store, bytes: String) -> Boolean
pub fn validate(store: Store, bytes: String) -> Boolean
Validates a new WebAssembly Module given the configuration
in the Store
.
This validation is normally pretty fast and checks the
enabled WebAssembly features in the Store engine to assure
deterministic validation of the Module
.
Example
wasm_bytes = "\x00asm\x01\x00\x00\x00";
Wasmer::Module.validate Wasmer::Store.new, wasm_bytes
sourcepub fn name(&self, name: String)
pub fn name(&self, name: String)
Get or set the current name of the module.
This name is normally set in the WebAssembly bytecode by some compilers, but can be also overwritten.
Not all modules have a name.
Example
store = Wasmer::Store.new
# Module with an existing name.
assert { Wasmer::Module.new(store, "(module $moduleName)").name == "moduleName" }
# Module with no name.
assert { Wasmer::Module.new(store, "(module)").name.nil? }
# Change the module's name.
module_ = Wasmer::Module.new store, "(module $moduleName)"
module_.name = "hello"
assert { module_.name == "hello" }
sourcepub fn exports(&self) -> Array<ExportType>
pub fn exports(&self) -> Array<ExportType>
Returns a list of ExportType
objects, which represents
all the exports of this module.
The order of the exports is guaranteed to be the same as in the WebAssembly bytecode.
Example
See the ExportType
class to learn more.
sourcepub fn imports(&self) -> Array<ImportType>
pub fn imports(&self) -> Array<ImportType>
Returns a list of ImportType
objects, which represents
all the imports of this module.
The order of the imports is guaranteed to be the same as in the WebAssembly bytecode.
Example
See the ImportType
class to learn more.
sourcepub fn custom_sections(&self, name: String) -> Array<String>
pub fn custom_sections(&self, name: String) -> Array<String>
Get the custom sections of the module given a name
.
Important
Following the WebAssembly specification, one name can have multiple custom sections. That’s why a list of bytes is returned rather than bytes.
Consequently, the empty list represents the absence of a custom section for the given name.
Example
bytes = IO.read "custom_sections.wasm", mode: "rb"
module_ = Wasmer::Module.new Wasmer::Store.new, bytes
assert { module_.custom_sections("easter_egg") == ["Wasmer"] }
assert { module_.custom_sections("hello") == ["World!"] }
assert { module_.custom_sections("foo") == [] }
sourcepub fn serialize(&self) -> String
pub fn serialize(&self) -> String
Serializes a module into a binary representation that the
engine can later process via Module::deserialize
.
Example
module_ = Wasmer::Module.new Wasmer::Store.new, "(module)"
assert { module_.serialize.is_a?(String) }
sourcepub fn deserialize(bytes: String) -> Self
pub fn deserialize(bytes: String) -> Self
Deserializes a serialized module binary into a Module.
Note: the module has to be serialized before with the serialize method.
Safety
This function is inherently unsafe as the provided bytes:
- Are going to be deserialized directly into Rust objects.
- Contains the function assembly bodies and, if intercepted, a malicious actor could inject code into executable memory.
And as such, the deserialize method is unsafe.
Example
store = Wasmer::Store.new
serialized_module = Wasmer::Module.new(
store,
(<<~WAST)
(module
(func (export "function") (param i32 i64)))
WAST
).serialize
module_ = Wasmer::Module.deserialize store, serialized_module
serialized_module = nil
exports = module_.exports
assert { exports.length() == 1 }
assert { exports[0].name == "function" }
assert { exports[0].type.is_a?(Wasmer::FunctionType) }
assert { exports[0].type.params == [Wasmer::Type::I32, Wasmer::Type::I64] }
assert { exports[0].type.results == [] }