Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtonmeuser committed Sep 2, 2023
1 parent aa010b2 commit 50dca18
Show file tree
Hide file tree
Showing 8 changed files with 415 additions and 960 deletions.
2 changes: 1 addition & 1 deletion examples/wasm-consume/Benchmark.gd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extends RefCounted
extends Reference
class_name Benchmark

static func fibonacci(n: int):
Expand Down
49 changes: 25 additions & 24 deletions examples/wasm-consume/Main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ extends Control

const info_template := "[b]Import Globals[/b]\n%s[b]Imports Functions[/b]\n%s[b]Export Globals[/b]\n%s[b]Export Functions[/b]\n%s[b]Memory[/b]\n[indent]%s[/indent]"
var callback_count: int
@onready var wasm: Wasm = Wasm.new()
onready var wasm: Wasm = Wasm.new()

func _ready():
$"%PrimeLimit".connect("value_changed", Callable(self, "_benchmark"))
$"%MemoryType".connect("item_selected", Callable(self, "_update_memory_type"))
$"%CallbackButton".connect("pressed", Callable(wasm, "function").bind("invoke_callback", []))
$"%PrimeLimit".connect("value_changed", self, "_benchmark")
$"%MemoryType".connect("item_selected", self, "_update_memory_type")
$"%CallbackButton".connect("pressed", wasm, "function", ["invoke_callback", []])
for node in $"%MemoryInput".get_children() + [$"%MemoryOffset"]:
node.connect("value_changed" if node is Range else "text_changed", Callable(self, "_update_memory"))
node.connect("value_changed" if node is Range else "text_changed", self, "_update_memory")
for item in ["Int", "Float", "String"]: $"%MemoryType".add_item(item)

_load_wasm("res://example.wasm")
Expand All @@ -18,12 +18,13 @@ func _ready():

func _gui_input(event: InputEvent): # Unfocus input
if event is InputEventMouseButton and event.pressed:
var focus_owner = get_viewport().gui_get_focus_owner()
var focus_owner = get_focus_owner()
if focus_owner: focus_owner.release_focus()

func _load_wasm(path: String):
var file = FileAccess.open(path, FileAccess.READ)
var buffer = file.get_buffer(file.get_length())
var file = File.new()
file.open(path, File.READ)
var buffer = file.get_buffer(file.get_len())
var imports = { # Import format module.name
"functions": { "index.callback": [self, "callback"] },
}
Expand All @@ -39,14 +40,14 @@ func callback(value: int):

func _update_info():
var info = wasm.inspect()
if info.is_empty():
if info.empty():
$"%InfoText".set("text", "Error")
return
var memory_info = ""
if info.has("memory_min"): memory_info += "\nMin %s" % _pretty_bytes(info.memory_min)
if info.has("memory_max"): memory_info += "\nMax %s" % _pretty_bytes(info.memory_max)
if info.has("memory_current"): memory_info += "\nCurrent %s" % _pretty_bytes(info.memory_current)
$"%InfoText".text = info_template % [
$"%InfoText".bbcode_text = info_template % [
_pretty_signatures({}),
_pretty_signatures(info.import_functions),
_pretty_signatures(info.export_globals),
Expand All @@ -66,7 +67,7 @@ func _update_memory(_value = 0):
match(input.get_index()):
0: wasm.memory.put_64(int(input.value))
1: wasm.memory.put_double(input.value)
2: wasm.memory.put_data(input.text.to_utf8_buffer())
2: wasm.memory.put_data(input.text.to_utf8())
wasm.function("update_memory", [])
$"%GlobalValue".text = _hex(wasm.global("memory_value"))
$"%ReadValue".text = _hex(wasm.memory.seek(0).get_64()) # Seek allows chaining
Expand All @@ -77,22 +78,22 @@ func _hex(i: int) -> String: # Format bytes without leading negative sign
return "%X%015X" % [(i >> 60) | 0x8, i & 0x0FFFFFFFFFFFFFFF]

func _pretty_signatures(signatures: Dictionary) -> String: # Indented, line-separated string
if signatures.keys().is_empty(): return ""
var rows = PackedStringArray()
if !signatures.keys(): return ""
var rows = PoolStringArray()
for key in signatures.keys():
var signature = signatures[key]
assert(signature is Array and len(signature) == 2) #,"Invalid signature")
assert(signature is Array and len(signature) == 2, "Invalid signature")
if signature[0] is Array and signature[1] is Array: # Function signature (param and result types)
var func_signature = ""
if signature[0].is_empty(): func_signature += "V"
if !signature[0]: func_signature += "V"
else: for type in signature[0]: func_signature += "I" if type == TYPE_INT else "F"
func_signature += "→"
if signature[1].is_empty(): func_signature += "V"
if !signature[1]: func_signature += "V"
else: for type in signature[1]: func_signature += "I" if type == TYPE_INT else "F"
rows.append("%s [code][color=#FFF5]%s[/color][/code]" % [key, func_signature])
rows.append("%s [code][color=#5FFF]%s[/color][/code]" % [key, func_signature])
elif signature[0] is int and signature[1] is bool: # Global signature (type and mutability)
rows.append("%s [code][color=#FFF5]%s(%s)[/color][/code]" % [key, "I" if signature[0] == TYPE_INT else "F", "M" if signature[1] else "C"])
return "[indent]%s[/indent]\n" % "\n".join(rows)
rows.append("%s [code][color=#5FFF]%s(%s)[/color][/code]" % [key, "I" if signature[0] == TYPE_INT else "F", "M" if signature[1] else "C"])
return "[indent]%s[/indent]\n" % rows.join("\n")

func _pretty_bytes(i: int) -> String: # Format bytes without leading negative sign
for unit in ["", "Ki", "Mi", "Gi"]:
Expand All @@ -102,12 +103,12 @@ func _pretty_bytes(i: int) -> String: # Format bytes without leading negative si

func _benchmark(_value = 0):
var limit: int = $"%PrimeLimit".value
var t_gdscript = Time.get_ticks_usec()
var t_gdscript = OS.get_ticks_usec()
var v_gdscript = Benchmark.sieve(limit)
t_gdscript = Time.get_ticks_usec() - t_gdscript
var t_wasm = Time.get_ticks_usec()
t_gdscript = OS.get_ticks_usec() - t_gdscript
var t_wasm = OS.get_ticks_usec()
var v_wasm = wasm.function("sieve", [limit])
t_wasm = Time.get_ticks_usec() - t_wasm
$"%PrimeAnswer".text = ("%d" % v_gdscript) if v_gdscript == v_wasm else "?"
t_wasm = OS.get_ticks_usec() - t_wasm
$"%PrimeAnswer".text = String(v_gdscript) if v_gdscript == v_wasm else "?"
$"%TimeGDScript".text = "%.3f ms" % (t_gdscript / 1000.0)
$"%TimeWasm".text = "%.3f ms" % (t_wasm / 1000.0)
Loading

0 comments on commit 50dca18

Please sign in to comment.