Browse Source

Add basic support for wasi in package os

gingerBill 3 years ago
parent
commit
22982586f1
2 changed files with 36 additions and 10 deletions
  1. 34 8
      core/os/os_wasi.odin
  2. 2 2
      core/os/stream.odin

+ 34 - 8
core/os/os_js_wasm32.odin → core/os/os_wasi.odin

@@ -1,9 +1,11 @@
 package os
 
+import "core:sys/wasm/wasi"
+
 Handle :: distinct i32
 Errno :: distinct i32
 
-ERROR_NONE :: Errno(0)
+ERROR_NONE :: Errno(wasi.errno_t.SUCCESS)
 
 O_RDONLY   :: 0x00000
 O_WRONLY   :: 0x00001
@@ -18,16 +20,30 @@ O_SYNC     :: 0x01000
 O_ASYNC    :: 0x02000
 O_CLOEXEC  :: 0x80000
 
-stdout: Handle
-stderr: Handle
-stdin: Handle
+stdin:  Handle = 0
+stdout: Handle = 1
+stderr: Handle = 2
 
 
 write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
-	return 0, 0
+	iovs := wasi.ciovec_t(data)
+	n, err := wasi.fd_write(wasi.fd_t(fd), &iovs, 1)
+	return int(n), Errno(err)
 }
 read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
-	return 0, 0
+	iovs := wasi.iovec_t(data)
+	n, err := wasi.fd_read(wasi.fd_t(fd), &iovs, 1)
+	return int(n), Errno(err)
+}
+write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Errno) {
+	iovs := wasi.ciovec_t(data)
+	n, err := wasi.fd_pwrite(wasi.fd_t(fd), &iovs, 1, wasi.filesize_t(offset))
+	return int(n), Errno(err)
+}
+read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Errno) {
+	iovs := wasi.iovec_t(data)
+	n, err := wasi.fd_pread(wasi.fd_t(fd), &iovs, 1, wasi.filesize_t(offset))
+	return int(n), Errno(err)
 }
 open :: proc(path: string, mode: int = O_RDONLY, perm: int = 0) -> (Handle, Errno) {
 	return 0, 0
@@ -36,7 +52,8 @@ close :: proc(fd: Handle) -> Errno {
 	return 0
 }
 seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
-	return 0, 0
+	n, err := wasi.fd_seek(wasi.fd_t(fd), wasi.filedelta_t(offset), wasi.whence_t(whence))
+	return i64(n), Errno(err)
 }
 current_thread_id :: proc "contextless" () -> int {
 	return 0
@@ -44,7 +61,11 @@ current_thread_id :: proc "contextless" () -> int {
 
 
 file_size :: proc(fd: Handle) -> (i64, Errno) {
-	return 0, 0
+	stat, err := wasi.fd_filestat_get(wasi.fd_t(fd))
+	if err != nil {
+		return 0, Errno(err)
+	}
+	return i64(stat.size), 0
 }
 
 
@@ -68,3 +89,8 @@ heap_free :: proc(ptr: rawptr) {
 		return
 	}
 }
+
+
+exit :: proc "contextless" (code: int) -> ! {
+	wasi.proc_exit(wasi.exitcode_t(code))
+}

+ 2 - 2
core/os/stream.odin

@@ -19,7 +19,7 @@ _file_stream_vtable := &io.Stream_VTable{
 		return
 	},
 	impl_read_at = proc(s: io.Stream, p: []byte, offset: i64) -> (n: int, err: io.Error) {
-		when ODIN_OS == "windows" {
+		when ODIN_OS == "windows" || ODIN_OS == "wasi" {
 			fd := Handle(uintptr(s.stream_data))
 			os_err: Errno
 			n, os_err = read_at(fd, p, offset)
@@ -33,7 +33,7 @@ _file_stream_vtable := &io.Stream_VTable{
 		return
 	},
 	impl_write_at = proc(s: io.Stream, p: []byte, offset: i64) -> (n: int, err: io.Error) {
-		when ODIN_OS == "windows" {
+		when ODIN_OS == "windows" || ODIN_OS == "wasi" {
 			fd := Handle(uintptr(s.stream_data))
 			os_err: Errno
 			n, os_err = write_at(fd, p, offset)