diff options
| author | Laytan Laats <laytanlaats@hotmail.com> | 2024-06-29 23:09:49 +0200 |
|---|---|---|
| committer | Laytan Laats <laytanlaats@hotmail.com> | 2024-06-29 23:15:31 +0200 |
| commit | 604551eb2d106d64eb9159bc17aa5c57bbca0ca4 (patch) | |
| tree | 27ca9e57f7091af156f86f01311928567d01e4a9 /base | |
| parent | 476d0087c8d47102c23cf6de71eb4014b9a7b6b2 (diff) | |
wasi: make the demo run on wasi and run it in CI
Diffstat (limited to 'base')
| -rw-r--r-- | base/runtime/entry_wasm.odin | 5 | ||||
| -rw-r--r-- | base/runtime/os_specific_wasi.odin | 50 | ||||
| -rw-r--r-- | base/runtime/wasm_allocator.odin | 2 |
3 files changed, 53 insertions, 4 deletions
diff --git a/base/runtime/entry_wasm.odin b/base/runtime/entry_wasm.odin index a24c6f4b7..99cd8201d 100644 --- a/base/runtime/entry_wasm.odin +++ b/base/runtime/entry_wasm.odin @@ -22,6 +22,11 @@ when !ODIN_TEST && !ODIN_NO_ENTRY_POINT { @(link_name="_start", linkage="strong", require, export) _start :: proc "c" () { context = default_context() + + when ODIN_OS == .WASI { + _wasi_setup_args() + } + #force_no_inline _startup_runtime() intrinsics.__entry_point() } diff --git a/base/runtime/os_specific_wasi.odin b/base/runtime/os_specific_wasi.odin index 0e229ac7e..b85d7adea 100644 --- a/base/runtime/os_specific_wasi.odin +++ b/base/runtime/os_specific_wasi.odin @@ -2,10 +2,54 @@ //+private package runtime -import "core:sys/wasm/wasi" +foreign import wasi "wasi_snapshot_preview1" + +@(default_calling_convention="contextless") +foreign wasi { + fd_write :: proc( + fd: i32, + iovs: [][]byte, + n: ^uint, + ) -> u16 --- + + @(private="file") + args_sizes_get :: proc( + num_of_args: ^uint, + size_of_args: ^uint, + ) -> u16 --- + + @(private="file") + args_get :: proc( + argv: [^]cstring, + argv_buf: [^]byte, + ) -> u16 --- +} _stderr_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) { - data_iovec := (wasi.ciovec_t)(data) - n, err := wasi.fd_write(1, {data_iovec}) + n: uint + err := fd_write(1, {data}, &n) return int(n), _OS_Errno(err) } + +_wasi_setup_args :: proc() { + num_of_args, size_of_args: uint + if errno := args_sizes_get(&num_of_args, &size_of_args); errno != 0 { + return + } + + err: Allocator_Error + if args__, err = make([]cstring, num_of_args); err != nil { + return + } + + args_buf: []byte + if args_buf, err = make([]byte, size_of_args); err != nil { + delete(args__) + return + } + + if errno := args_get(raw_data(args__), raw_data(args_buf)); errno != 0 { + delete(args__) + delete(args_buf) + } +} diff --git a/base/runtime/wasm_allocator.odin b/base/runtime/wasm_allocator.odin index fb0600ea2..f4b399c47 100644 --- a/base/runtime/wasm_allocator.odin +++ b/base/runtime/wasm_allocator.odin @@ -760,7 +760,7 @@ free :: proc(a: ^WASM_Allocator, ptr: rawptr, loc := #caller_location) { defer unlock(a) size := region.size - assert(region_is_in_use(region), "double free", loc=loc) + assert(region_is_in_use(region), "double free or corrupt region", loc=loc) prev_region_size_field := ([^]uint)(region)[-1] prev_region_size := prev_region_size_field & ~uint(FREE_REGION_FLAG) |