-
Notifications
You must be signed in to change notification settings - Fork 12
Imports & Exports
WebAssembly modules (guests) interact with the runtime (host) via imports, exports, and memory. Types of imports and exports include functions, globals, tables, and memories. This document covers import and export functions and globals, the most common external types.
Import functions, sometimes called callbacks, must be provided to modules on instantiation. These provide functions that the Wasm module can invoke. To view module import functions, use wasm.inspect()
and refer to the import_functions
key.
Under the hood, Godot Wasm uses Godot's Object.call()
method. As such, both an object and method string must be provided as a Wasm module import function.
func _ready():
var file = FileAccess.open("res://YOUR_WASM_MODULE_PATH", FileAccess.READ)
var buffer = file.get_buffer(file.get_length())
var imports = {
"functions": {
# The import function provided is the "callback" method of the current object
# Whenever the module calls this import function, the "callback" method is invoked
"index.callback": [self, "callback"],
}
}
wasm.load(buffer, imports)
file.close()
func callback(a, b, c):
# This method is invoked when the Wasm module import function is called
# The signature (arguments) of this function must match those expected by the import function
print("Callback function invoked with %s %s %s" % [a, b, c])
Note that a module may be compiled without providing imports. However, on instantiation, imports must be provided (see example below).
var file = FileAccess.open("res://YOUR_WASM_MODULE_PATH", FileAccess.READ)
var buffer = file.get_buffer(file.get_length())
var imports = { "functions": { "index.callback": [self, "callback"] } } # Set imports according to Wasm module
wasm.compile(buffer)
file.close()
print(wasm.inspect())
wasm.instantiate(imports)
As of v0.3.2, import globals are not yet supported by Godot Wasm (see #13).
Export functions are functions defined and exposed by a WebAssembly module. To view module export functions, use wasm.inspect()
and refer to the export_functions
key.
Module export functions can be invoked from GDScript using wasm.function()
. The export name and an array of arguments must be provided. When an export function takes no arguments, an empty argument array must still be provided as the second parameter of wasm.function()
. The values in the argument array must match the types expected by the export function e.g. f64
, i64
. The return value of wasm.function()
is either the single value returned by the function defined in the Wasm module or an array of values if Wasm multi-value is used by the module.
var file = FileAccess.open("res://YOUR_WASM_MODULE_PATH", FileAccess.READ)
var buffer = file.get_buffer(file.get_length())
wasm.load(buffer, {}) # Module requires no imports
file.close()
# Invoke an export function called "my_function"
# The function expects three arguments of type `i64`, `f64`, and `i64`, respectively
print(wasm.function("my_function", [1, 2.0, 3]))
As of v0.3.2, writing exported globals is not yet supported by Godot Wasm (see #13).
To view module export globals, use wasm.inspect()
and refer to the export_globals
key. To read the value of a specific export global, use wasm.global()
.