Skip to content

Commit

Permalink
Backport memory string test
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtonmeuser committed Sep 14, 2023
1 parent 0eb64de commit c96da4d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/wasm-test/TestGeneral.gd
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func test_load():

func test_invalid_binary():
var wasm = Wasm.new()
var buffer = "asdf".to_utf8()
var buffer = Utils.to_utf8("asdf")
var error = wasm.compile(buffer)
expect_eq(error, ERR_INVALID_DATA)
expect_error("Invalid binary")
Expand Down
13 changes: 13 additions & 0 deletions examples/wasm-test/TestMemory.gd
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ func test_memory_marshal():
var result = wasm.memory.seek(offset).get_var()
expect_eq(result, data)

func test_string_sum():
var wasm = load_wasm("memory")
var offset = wasm.global("offset")
var string = "test_string" # String to store in Wasm memory
var sum = 0 # Sum of character UTF8 values
for c in Utils.to_utf8(string): sum += c
# Write string to memory using StreamPeer interface
# Note that Godot string marshalling prepends 32-bit string length
# See https://docs.godotengine.org/en/stable/classes/class_streampeer.html#class-streampeer-method-put-string
wasm.memory.seek(offset).put_string(string)
var result = wasm.function("ascii_sum", [])
expect_eq(result, sum)

func test_resize():
var wasm = load_wasm("memory")
var memory = wasm.inspect().get("memory").get("current")
Expand Down
2 changes: 1 addition & 1 deletion examples/wasm-test/utils/TestSuite.gd
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func expect_error(s: String):
# Account for error stack trace
var regex = Utils.make_regex("^\\s+at:\\s")
var position = _log_file.get_position()
while _log_file.get_position() < _log_file.get_len():
while _log_file.get_position() < Utils.file_length(_log_file):
if !regex.search(_log_file.get_line()): break
position = _log_file.get_position()
_log_file.seek(position)
Expand Down
6 changes: 6 additions & 0 deletions examples/wasm-test/utils/Utils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ static func make_regex(pattern: String) -> RegEx:
var regex = RegEx.new()
assert(regex.compile(pattern) == OK, "Invalid regex pattern: %s" % pattern)
return regex

static func file_length(f: File) -> int:
return f.get_len()

static func to_utf8(s: String) -> PoolByteArray:
return s.to_utf8()
Binary file modified examples/wasm-test/wasm/memory.wasm
Binary file not shown.

0 comments on commit c96da4d

Please sign in to comment.