From 71eb21aab7bd572c95feb21533eefeb328874a6f Mon Sep 17 00:00:00 2001 From: JopStro Date: Mon, 31 Oct 2022 21:21:10 +0000 Subject: [PATCH 1/4] implement open for wasi_wasm32 target --- core/os/os_wasi.odin | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/core/os/os_wasi.odin b/core/os/os_wasi.odin index 7bab1b949..95e37771e 100644 --- a/core/os/os_wasi.odin +++ b/core/os/os_wasi.odin @@ -24,7 +24,7 @@ O_CLOEXEC :: 0x80000 stdin: Handle = 0 stdout: Handle = 1 stderr: Handle = 2 - +default_dir: Handle = 3 write :: proc(fd: Handle, data: []byte) -> (int, Errno) { iovs := wasi.ciovec_t(data) @@ -47,7 +47,36 @@ read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Errno) { return int(n), Errno(err) } open :: proc(path: string, mode: int = O_RDONLY, perm: int = 0) -> (Handle, Errno) { - return 0, -1 + oflags: wasi.oflags_t + if mode & O_CREATE == O_CREATE { + oflags += {.CREATE} + } + if mode & O_EXCL == O_EXCL { + oflags += {.EXCL} + } + if mode & O_TRUNC == O_TRUNC { + oflags += {.TRUNC} + } + + rights: wasi.rights_t = {.FD_SEEK} + switch mode & (O_RDONLY|O_WRONLY|O_RDWR) { + case O_RDONLY: rights += {.FD_READ} + case O_WRONLY: rights += {.FD_WRITE} + case O_RDWR: rights += {.FD_READ, .FD_WRITE} + } + + fdflags: wasi.fdflags_t + if mode & O_APPEND == O_APPEND { + fdflags += {.APPEND} + } + if mode & O_NONBLOCK == O_NONBLOCK { + fdflags += {.NONBLOCK} + } + if mode & O_SYNC == O_SYNC { + fdflags += {.SYNC} + } + fd, err := wasi.path_open(wasi.fd_t(default_dir),{.SYMLINK_FOLLOW},path,oflags,rights,{},fdflags) + return Handle(fd), Errno(err) } close :: proc(fd: Handle) -> Errno { err := wasi.fd_close(wasi.fd_t(fd)) @@ -96,4 +125,4 @@ heap_free :: proc(ptr: rawptr) { exit :: proc "contextless" (code: int) -> ! { runtime._cleanup_runtime_contextless() wasi.proc_exit(wasi.exitcode_t(code)) -} \ No newline at end of file +} From dad10ef80052d668ad374b99773fa3f7d2fd2f05 Mon Sep 17 00:00:00 2001 From: JopStro Date: Mon, 31 Oct 2022 21:22:55 +0000 Subject: [PATCH 2/4] create _yeild stub for wasi_wasm32 target to avoid compile error --- core/time/time_wasi.odin | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/time/time_wasi.odin b/core/time/time_wasi.odin index 9360e3591..dacf911fc 100644 --- a/core/time/time_wasi.odin +++ b/core/time/time_wasi.odin @@ -22,3 +22,5 @@ _tick_now :: proc "contextless" () -> Tick { return {} } +_yield :: proc "contextless" () { +} From 91ad6b42c5232ebbca22887efffa3f3776011797 Mon Sep 17 00:00:00 2001 From: JopStro Date: Mon, 31 Oct 2022 21:46:47 +0000 Subject: [PATCH 3/4] rename default_dir to current_dir --- core/os/os_wasi.odin | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/os/os_wasi.odin b/core/os/os_wasi.odin index 95e37771e..5d77b4d7c 100644 --- a/core/os/os_wasi.odin +++ b/core/os/os_wasi.odin @@ -24,7 +24,7 @@ O_CLOEXEC :: 0x80000 stdin: Handle = 0 stdout: Handle = 1 stderr: Handle = 2 -default_dir: Handle = 3 +current_dir: Handle = 3 write :: proc(fd: Handle, data: []byte) -> (int, Errno) { iovs := wasi.ciovec_t(data) @@ -75,7 +75,7 @@ open :: proc(path: string, mode: int = O_RDONLY, perm: int = 0) -> (Handle, Errn if mode & O_SYNC == O_SYNC { fdflags += {.SYNC} } - fd, err := wasi.path_open(wasi.fd_t(default_dir),{.SYMLINK_FOLLOW},path,oflags,rights,{},fdflags) + fd, err := wasi.path_open(wasi.fd_t(current_dir),{.SYMLINK_FOLLOW},path,oflags,rights,{},fdflags) return Handle(fd), Errno(err) } close :: proc(fd: Handle) -> Errno { From 18d7ecc1a5237b7844579492fef9a8abce89e0d8 Mon Sep 17 00:00:00 2001 From: JopStro Date: Tue, 1 Nov 2022 12:56:36 +0000 Subject: [PATCH 4/4] wasi: Add FD_FILESTAT_GET to default file open rights --- core/os/os_wasi.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/os/os_wasi.odin b/core/os/os_wasi.odin index 5d77b4d7c..59eac74a9 100644 --- a/core/os/os_wasi.odin +++ b/core/os/os_wasi.odin @@ -58,7 +58,7 @@ open :: proc(path: string, mode: int = O_RDONLY, perm: int = 0) -> (Handle, Errn oflags += {.TRUNC} } - rights: wasi.rights_t = {.FD_SEEK} + rights: wasi.rights_t = {.FD_SEEK, .FD_FILESTAT_GET} switch mode & (O_RDONLY|O_WRONLY|O_RDWR) { case O_RDONLY: rights += {.FD_READ} case O_WRONLY: rights += {.FD_WRITE}