@@ -2,14 +2,14 @@ extends Control
22
33const 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]"
44var callback_count : int
5- @ onready var wasm : Wasm = Wasm .new ()
5+ onready var wasm : Wasm = Wasm .new ()
66
77func _ready ():
8- $ "% PrimeLimit" .connect ("value_changed" , Callable ( self , "_benchmark" ) )
9- $ "% MemoryType" .connect ("item_selected" , Callable ( self , "_update_memory_type" ) )
10- $ "% CallbackButton" .connect ("pressed" , Callable ( wasm , "function" ). bind ( "invoke_callback" , []) )
8+ $ "% PrimeLimit" .connect ("value_changed" , self , "_benchmark" )
9+ $ "% MemoryType" .connect ("item_selected" , self , "_update_memory_type" )
10+ $ "% CallbackButton" .connect ("pressed" , wasm , "function" , [ "invoke_callback" , []] )
1111 for node in $ "% MemoryInput" .get_children () + [$ "% MemoryOffset" ]:
12- node .connect ("value_changed" if node is Range else "text_changed" , Callable ( self , "_update_memory" ) )
12+ node .connect ("value_changed" if node is Range else "text_changed" , self , "_update_memory" )
1313 for item in ["Int" , "Float" , "String" ]: $ "% MemoryType" .add_item (item )
1414
1515 _load_wasm ("res://example.wasm" )
@@ -18,12 +18,13 @@ func _ready():
1818
1919func _gui_input (event : InputEvent ): # Unfocus input
2020 if event is InputEventMouseButton and event .pressed :
21- var focus_owner = get_viewport (). gui_get_focus_owner ()
21+ var focus_owner = get_focus_owner ()
2222 if focus_owner : focus_owner .release_focus ()
2323
2424func _load_wasm (path : String ):
25- var file = FileAccess .open (path , FileAccess .READ )
26- var buffer = file .get_buffer (file .get_length ())
25+ var file = File .new ()
26+ file .open (path , File .READ )
27+ var buffer = file .get_buffer (file .get_len ())
2728 var imports = { # Import format module.name
2829 "functions" : { "index.callback" : [self , "callback" ] },
2930 }
@@ -39,15 +40,15 @@ func callback(value: int):
3940
4041func _update_info ():
4142 var info = wasm .inspect ()
42- if info .is_empty ():
43+ if info .empty ():
4344 $ "% InfoText" .set ("text" , "Error" )
4445 return
4546 if ! info .has ("memory" ): info ["memory" ] = {}
4647 var memory_info = ""
4748 if info .memory .has ("min" ): memory_info += "\n Min %s " % _pretty_bytes (info .memory .min )
4849 if info .memory .has ("max" ): memory_info += "\n Max %s " % _pretty_bytes (info .memory .max )
4950 if info .memory .has ("current" ): memory_info += "\n Current %s " % _pretty_bytes (info .memory .current )
50- $ "% InfoText" .text = info_template % [
51+ $ "% InfoText" .bbcode_text = info_template % [
5152 _pretty_signatures ({}),
5253 _pretty_signatures (info .import_functions ),
5354 _pretty_signatures (info .export_globals ),
@@ -67,7 +68,7 @@ func _update_memory(_value = 0):
6768 match (input .get_index ()):
6869 0 : wasm .memory .put_64 (int (input .value ))
6970 1 : wasm .memory .put_double (input .value )
70- 2 : wasm .memory .put_data (input .text .to_utf8_buffer ())
71+ 2 : wasm .memory .put_data (input .text .to_utf8 ())
7172 wasm .function ("update_memory" , [])
7273 $ "% GlobalValue" .text = _hex (wasm .global ("memory_value" ))
7374 $ "% ReadValue" .text = _hex (wasm .memory .seek (0 ).get_64 ()) # Seek allows chaining
@@ -78,22 +79,22 @@ func _hex(i: int) -> String: # Format bytes without leading negative sign
7879 return "%X%015X " % [(i >> 60 ) | 0x8 , i & 0x0FFFFFFFFFFFFFFF ]
7980
8081func _pretty_signatures (signatures : Dictionary ) -> String : # Indented, line-separated string
81- if signatures .keys (). is_empty (): return ""
82- var rows = PackedStringArray ()
82+ if ! signatures .keys (): return ""
83+ var rows = PoolStringArray ()
8384 for key in signatures .keys ():
8485 var signature = signatures [key ]
85- assert (signature is Array and len (signature ) == 2 ) # , "Invalid signature")
86+ assert (signature is Array and len (signature ) == 2 , "Invalid signature" )
8687 if signature [0 ] is Array and signature [1 ] is Array : # Function signature (param and result types)
8788 var func_signature = ""
88- if signature [0 ]. is_empty () : func_signature += "V"
89+ if ! signature [0 ]: func_signature += "V"
8990 else : for type in signature [0 ]: func_signature += "I" if type == TYPE_INT else "F"
9091 func_signature += "→"
91- if signature [1 ]. is_empty () : func_signature += "V"
92+ if ! signature [1 ]: func_signature += "V"
9293 else : for type in signature [1 ]: func_signature += "I" if type == TYPE_INT else "F"
93- rows .append ("%s [code][color=#FFF5 ]%s [/color][/code]" % [key , func_signature ])
94+ rows .append ("%s [code][color=#5FFF ]%s [/color][/code]" % [key , func_signature ])
9495 elif signature [0 ] is int and signature [1 ] is bool : # Global signature (type and mutability)
95- 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" ])
96- return "[indent]%s [/indent]\n " % "\n " . join ( rows )
96+ 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" ])
97+ return "[indent]%s [/indent]\n " % rows . join ( "\n " )
9798
9899func _pretty_bytes (i : int ) -> String : # Format bytes without leading negative sign
99100 for unit in ["" , "Ki" , "Mi" , "Gi" ]:
@@ -103,12 +104,12 @@ func _pretty_bytes(i: int) -> String: # Format bytes without leading negative si
103104
104105func _benchmark (_value = 0 ):
105106 var limit : int = $ "% PrimeLimit" .value
106- var t_gdscript = Time .get_ticks_usec ()
107+ var t_gdscript = OS .get_ticks_usec ()
107108 var v_gdscript = Benchmark .sieve (limit )
108- t_gdscript = Time .get_ticks_usec () - t_gdscript
109- var t_wasm = Time .get_ticks_usec ()
109+ t_gdscript = OS .get_ticks_usec () - t_gdscript
110+ var t_wasm = OS .get_ticks_usec ()
110111 var v_wasm = wasm .function ("sieve" , [limit ])
111- t_wasm = Time .get_ticks_usec () - t_wasm
112- $ "% PrimeAnswer" .text = ( " %d " % v_gdscript ) if v_gdscript == v_wasm else "?"
112+ t_wasm = OS .get_ticks_usec () - t_wasm
113+ $ "% PrimeAnswer" .text = String ( v_gdscript ) if v_gdscript == v_wasm else "?"
113114 $ "% TimeGDScript" .text = "%.3f ms" % (t_gdscript / 1000.0 )
114115 $ "% TimeWasm" .text = "%.3f ms" % (t_wasm / 1000.0 )
0 commit comments