Skip to content

Commit 63eb703

Browse files
committed
feat!: support decoding js arguments into structs
1 parent 48f557d commit 63eb703

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

webui.odin

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package webui
33
import "base:intrinsics"
44
import "base:runtime"
55
import "core:c"
6+
import "core:encoding/json"
67
import "core:fmt"
78
import "core:strings"
89
import "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

Comments
 (0)