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

source

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
source

pub fn new(store: Store, bytes: String) -> Self

Creates a new Module.

source

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" }
source

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.

source

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.

source

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") == [] }
source

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) }
source

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:

  1. Are going to be deserialized directly into Rust objects.
  2. 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 == [] }

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> ArchivePointee for T

§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Pointee for T

§

type Metadata = ()

The type for metadata in pointers and references to Self.
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.