@@ -3,6 +3,7 @@ package webui
33import " base:intrinsics"
44import " base:runtime"
55import " core:c"
6+ import " core:encoding/json"
67import " core:fmt"
78import " core:strings"
89import " core:time"
@@ -214,17 +215,28 @@ script :: proc(
214215 return strings.string_from_ptr (buf, int (buffer_len)), .None
215216}
216217
218+ GetArgError :: union {
219+ enum {
220+ None,
221+ No_Argument,
222+ },
223+ json.Unmarshal_Error,
224+ }
225+
217226// Parse a JS argument as Odin data type.
218- get_arg :: proc ($T : typeid , e: ^Event, idx: uint = 0 ) -> T {
227+ get_arg :: proc ($T : typeid , e: ^Event, idx: uint = 0 ) -> (res: T, err: GetArgError) {
228+ if get_size_at (e, idx) == 0 {
229+ return res, .No_Argument
230+ }
219231 when intrinsics.type_is_numeric (T) {
220- return auto_cast get_int_at (e, idx)
232+ return auto_cast get_int_at (e, idx), nil
221233 } else when T == string {
222- return string (get_string_at (e, idx))
234+ return string (get_string_at (e, idx)), nil
223235 } else when T == bool {
224- return get_bool_at (e, idx)
236+ return get_bool_at (e, idx), nil
225237 }
226- // TODO: unmarshal other types from JSON
227- return {}
238+ json. unmarshal_string ( string ( get_string_at (e, idx)), &res) or_return
239+ return
228240}
229241
230242// Return the response to JavaScript.
0 commit comments