Skip to content

Commit 47b8ca9

Browse files
committed
Backport default empty arguments array
1 parent aae9767 commit 47b8ca9

File tree

6 files changed

+23
-6
lines changed

6 files changed

+23
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ See the [Usage wiki page](https://github.com/ashtonmeuser/godot-wasm/wiki/Gettin
5555
5656
1. A small subset of [WASI](https://wasmbyexample.dev/examples/wasi-introduction/wasi-introduction.all.en-us.html) bindings are provided to the Wasm module by default. These can be overridden by the imports supplied on module instantiation. The guest Wasm module has no access to the host machines filesystem, etc. Pros for this are simplicity and increased security. Cons include more work required to run Wasm modules created in ways that require a larger set of WASI bindings e.g. [TinyGo](https://tinygo.org/docs/guides/webassembly/) (see relevant [issue](https://github.com/tinygo-org/tinygo/issues/3068)).
5757
1. Only `int` and `float` return values are supported. While workarounds could be used, this limitation is because the only [concrete types supported by Wasm](https://webassembly.github.io/spec/core/syntax/types.html#number-types) are integers and floating point.
58-
1. A default empty `args` parameter for `function(name, args)` can not be supplied. Default `Array` parameters in GDNative seem to retain values between calls. Calling methods of this addon without expected arguments produces undefined behaviour. This is reliant on [godotengine/godot-cpp#209](https://github.com/godotengine/godot-cpp/issues/209).
58+
1. Default empty `args` parameter for `function(name, args)` is not supported in Godot 3.x using Godot Wasm as an addon e.g. via the Godot Asset Library. Default `Array` parameters in GDNative seem to retain values between calls. Calling methods of this addon without expected arguments produces undefined behaviour. Default empty arguments *are* supported in Godot 4.x and Godot 3.x when using Godot Wasm as a module.
5959
1. Web/HTML5 export is not supported (see [#15](https://github.com/ashtonmeuser/godot-wasm/issues/15) and [#18](https://github.com/ashtonmeuser/godot-wasm/issues/18)).
6060
6161
## Relevant Discussion

examples/wasm-test/TestGeneral.gd

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func test_function():
5252
var wasm = load_wasm("simple")
5353
var result = wasm.function("add", [1, 2])
5454
expect_eq(result, 3)
55+
expect_empty()
5556

5657
func test_invalid_function():
5758
var wasm = load_wasm("simple")
@@ -81,6 +82,17 @@ func test_invalid_function_arg_count():
8182
expect_eq(result, null)
8283
expect_error("Incorrect number of arguments supplied")
8384

85+
func test_function_default_args():
86+
if !Utils.godot_4(): return # Default arguments are unsupported in GDNative
87+
var wasm = load_wasm("simple")
88+
var result = wasm.function("count") # Expects no arguments
89+
expect_eq(result, 1)
90+
result = wasm.function("add", [1, 2])
91+
expect_eq(result, 3)
92+
result = wasm.function("add") # Expects arguments
93+
expect_eq(result, null)
94+
expect_error("Incorrect number of arguments supplied")
95+
8496
func test_global():
8597
var wasm = load_wasm("simple")
8698
var global_const = wasm.global("global_const")
@@ -122,8 +134,9 @@ func test_inspect():
122134
},
123135
"export_functions": {
124136
"_initialize": [[], []],
125-
"add": [[TYPE_INT, TYPE_INT], [TYPE_INT]]
137+
"add": [[TYPE_INT, TYPE_INT], [TYPE_INT]],
138+
"count": [[], [TYPE_INT]],
126139
},
127-
"memory": {}
140+
"memory": {},
128141
}
129142
expect_eq(inspect, expected)

examples/wasm-test/TestMemory.gd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ func test_inspect_compiled():
1414
},
1515
"export_functions": {
1616
"_initialize": [[], []],
17-
"add": [[TYPE_INT, TYPE_INT], [TYPE_INT]]
17+
"add": [[TYPE_INT, TYPE_INT], [TYPE_INT]],
18+
"count": [[], [TYPE_INT]],
1819
},
19-
"memory": {}
20+
"memory": {},
2021
}
2122
expect_eq(inspect, expected)
2223

examples/wasm-test/utils/Utils.gd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ static func file_length(f: File) -> int:
1515

1616
static func to_utf8(s: String) -> PoolByteArray:
1717
return s.to_utf8()
18+
19+
static func godot_4() -> bool:
20+
return Engine.get_version_info()["major"] == 4
30 Bytes
Binary file not shown.

src/wasm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ namespace godot {
230230
ClassDB::bind_method(D_METHOD("load", "bytecode", "import_map"), &Wasm::load);
231231
ClassDB::bind_method(D_METHOD("inspect"), &Wasm::inspect);
232232
ClassDB::bind_method(D_METHOD("global", "name"), &Wasm::global);
233-
ClassDB::bind_method(D_METHOD("function", "name", "args"), &Wasm::function);
233+
ClassDB::bind_method(D_METHOD("function", "name", "args"), &Wasm::function, DEFVAL(Array()));
234234
ClassDB::bind_method(D_METHOD("set_permissions"), &Wasm::set_permissions);
235235
ClassDB::bind_method(D_METHOD("get_permissions"), &Wasm::get_permissions);
236236
ClassDB::bind_method(D_METHOD("has_permission", "permission"), &Wasm::has_permission);

0 commit comments

Comments
 (0)