Skip to content

Class Documentation: Wasm

Ashton Meuser edited this page Aug 8, 2023 · 8 revisions

Wasm

Inherits: RefCounted

Compile, instantiate, and interact with a WebAssembly module.

Description

The Wasm class is the top-level entity you'll deal with when interacting with a WebAssembly module from GDScript. Each Wasm instance contains at most one instantiated Wasm module.

At a high-level, the following steps must be completed to run a Wasm module.

  1. Compile the bytecode (via Wasm.compile())
  2. Instantiate the module (via Wasm.instantiate())

A convenience method, Wasm.load(), is provided to combine the above steps into a single method call.

The Wasm class holds a reference to an instantiated Wasm modules memory accessible via the WasmMemory class.

Properties

Type Property
Dictionary permissions
WasmMemory memory

Methods

Return Method
Error compile ( PackedByteArray bytecode )
Error instantiate ( Dictionary imports )
Error load ( PackedByteArray bytecode, Dictionary imports )
Dictionary inspect ()
Variant global ( String name )
Variant function ( String name, Array args )
bool has_permission ( String permission )

Property Descriptions

Dictionary permissions

WASI permissions for the Wasm instance.

WasmMemory memory

A StreamPeer-like interface for interacting with the memory of an instantiated Wasm module.

Method Descriptions

Error compile ( PackedByteArray bytecode )

Compile the Wasm module provided Wasm binary bytecode. This must be called before instantiating the module. Alternatively, the module can be compiled and instantiated in a single step with load.

Error instantiate ( Dictionary imports )

Instantiate a compiled Wasm module. Before this can be called, the module must be compiled via compile. Imported functions can be provided in [code]import_map[/code] in the form var imports = { "functions": { "index.function": [self, "function"] } }. Each key of the import_map.functions should be an array whose members are the object containing the imported method and a string specifying the name of the method. Alternatively, the module can be compiled and instantiated in a single step with load.

Error load ( PackedByteArray bytecode, Dictionary imports )

Compile and instantiate a Wasm module in a single step. Equivalent to calling compile and instantiate.

Dictionary inspect ()

Inspect the imports, exports, and memories of a compiled Wasm module. Note that this may be called after compiling but before instantiating a module and may even inform the imports provided instantiate.

Variant global ( String name )

Access an exported global of the instantiated Wasm module. Returns either a single float or integer.

Variant function ( String name, Array args )

Call an exported function of the instantiated Wasm module. The args argument array must be provided even if no arguments are required. Returns either a single float or integer.

bool has_permission ( String permission )

Checks if the Wasm instance has permission for the permission argument.