ashtonmeuser / godot-wasm

Interact with WebAssembly modules from Godot
https://github.com/ashtonmeuser/godot-wasm/wiki
MIT License
193 stars 11 forks source link

Crash when import function target is freed #69

Closed ashtonmeuser closed 4 months ago

ashtonmeuser commented 4 months ago

Freeing the target of an import function before said import function is called results in a crash.

func _ready():
var imports = { "functions": {
    "import.import_int": [self, "import_function"],
    "import.import_float": [$Child, "import_function"],
} }
var bytecode = FileAccess.get_file_as_bytes("res://wasm/import.wasm")
var wasm = Wasm.new()
wasm.compile(bytecode)
wasm.instantiate(imports)
wasm.function("callback", [])
$Child.free()
wasm.function("callback", [])

Both parent and child nodes should implement a function to be supplied as import functions:

func import_function(a):
    print("Import function %s" % a)

The callback export function calls import_function. Before freeing $Child, both import functions should be successfully called. After freeing $Child, the program crashes.

Possible solution is to use a WeakRef to ensure the import function target is still valid.

Godot v4.2.2 Godot Wasm v0.3.6 Using import.wasm test Wasm module