From 5fa5345dea73017727234e38ff227d514d4f30f4 Mon Sep 17 00:00:00 2001 From: servostar Date: Sun, 4 Aug 2024 17:51:03 +0200 Subject: [PATCH] changed library IO module functions to return --- lib/src/io.gsc | 10 ++++----- lib/src/io/api.h | 10 ++++----- lib/src/io/impl.c | 44 ++++++++++++++++++++----------------- tests/stdlib/src/matrix.gsc | 13 +++++------ 4 files changed, 39 insertions(+), 38 deletions(-) diff --git a/lib/src/io.gsc b/lib/src/io.gsc index 1adb655..d57b1f9 100644 --- a/lib/src/io.gsc +++ b/lib/src/io.gsc @@ -22,33 +22,33 @@ handle: nullHandle = 0 as handle # -- Implementation note # On Linux this will return 0 as is it convention under UNIX (see: https://www.man7.org/linux/man-pages/man3/stdin.3.html) # On Windows the library will call `GetStdHandle(STD_INPUT_HANDLE)` -fun getStdinHandle(out handle: stdin) +fun handle:getStdinHandle() # Returns a handle to this processes standard input I/O handle # -- Implementation note # On Linux this will return 1 as is it convention under UNIX (see: https://www.man7.org/linux/man-pages/man3/stdout.3.html) # On Windows the library will call `GetStdHandle(STD_OUTPUT_HANDLE)` -fun getStdoutHandle(out handle: stdout) +fun handle:getStdoutHandle() # Returns a handle to this processes standard input I/O handle # -- Implementation note # On Linux this will return 1 as is it convention under UNIX (see: https://www.man7.org/linux/man-pages/man3/stderr.3.html) # On Windows the library will call `GetStdHandle(STD_OUTPUT_HANDLE)` -fun getStderrHandle(out handle: stderr) +fun handle:getStderrHandle() # Write `len` number of bytes from `buf` into the I/O resource specified # by `dev`. Returns the number of bytes written. # -- Implementation note # On Linux this will use the syscall write # On Windows this will use the WriteFile function -fun writeBytes(in handle: dev, in ref u8: buf, in u32: len)(out u32: written) +fun u32:writeBytes(in handle: dev, in ref u8: buf, in u32: len) # Read atmost `len` bytes to `buf` from the I/O resource specified by `dev` # Returns the number of read bytes in `written` # -- Implementation note # On Linux this will use the syscall read # On Windows this will use the ReadFile function -fun readBytes(in handle: dev, in ref u8: buf, in u32: len)(out u32: read) +fun u32:readBytes(in handle: dev, in ref u8: buf, in u32: len) # Flushes the buffers of the I/O resource specified by `dev` # -- Implementation note diff --git a/lib/src/io/api.h b/lib/src/io/api.h index c642185..0064b9a 100644 --- a/lib/src/io/api.h +++ b/lib/src/io/api.h @@ -6,15 +6,15 @@ typedef ptr handle; -void getStdinHandle(handle* stdin); +handle getStdinHandle(); -void getStdoutHandle(handle* stdout); +handle getStdoutHandle(); -void getStderrHandle(handle* stderr); +handle getStderrHandle(); -void writeBytes(handle dev, u8* buf, u32 len, u32* written); +u32 writeBytes(handle dev, u8* buf, u32 len); -void readBytes(handle dev, u8* buf, u32 len, u32* read); +u32 readBytes(handle dev, u8* buf, u32 len); void flush(handle dev); diff --git a/lib/src/io/impl.c b/lib/src/io/impl.c index 4de453b..5879375 100644 --- a/lib/src/io/impl.c +++ b/lib/src/io/impl.c @@ -11,24 +11,28 @@ // FIXME: error in case GetStdHandle return INVALID_HANDLE_VALUE // FIXME: error in case functions return 0 -void getStdinHandle(handle* stdin) { - *stdin = (handle) GetStdHandle(STD_INPUT_HANDLE); +handle getStdinHandle(handle* stdin) { + return GetStdHandle(STD_INPUT_HANDLE); } -void getStdoutHandle(handle* stdout) { - *stdout = (handle) GetStdHandle(STD_OUTPUT_HANDLE); +handle getStdoutHandle(handle* stdout) { + return GetStdHandle(STD_OUTPUT_HANDLE); } -void getStderrHandle(handle* stderr) { - *stderr = (handle) GetStdHandle(STD_ERROR_HANDLE); +handle getStderrHandle(handle* stderr) { + return GetStdHandle(STD_ERROR_HANDLE); } -void writeBytes(handle dev, u8* buf, u32 len, u32* bytesWritten) { - WriteFile((HANDLE) dev, buf, len, bytesRead, NULL); +u32 writeBytes(handle dev, u8* buf, u32 len) { + u32 bytesWritten = 0; + WriteFile((HANDLE) dev, buf, len, &bytesWritten, NULL); + return bytesWritten; } -void readBytes(handle dev, u8* buf, u32 len, u32* bytesRead) { - ReadFile((HANDLE) dev, buf, len, bytesRead, NULL); +u32 readBytes(handle dev, u8* buf, u32 len) { + u32 bytesRead = 0; + ReadFile((HANDLE) dev, buf, len, &bytesRead, NULL); + return bytesRead; } void flush(handle dev) { @@ -47,24 +51,24 @@ void flush(handle dev) { // which are stored as 64-bit by zero extending #define TO_INT(x) ((int)(long int)(x)) -void getStdinHandle(handle* stdin) { - *stdin = (handle) STDIN_FILENO; +handle getStdinHandle() { + return (handle) STDIN_FILENO; } -void getStdoutHandle(handle* stdout) { - *stdout = (handle) STDOUT_FILENO; +handle getStdoutHandle() { + return (handle) STDOUT_FILENO; } -void getStderrHandle(handle* stderr) { - *stderr = (handle) STDERR_FILENO; +handle getStderrHandle() { + return (handle) STDERR_FILENO; } -void writeBytes(handle dev, u8* buf, u32 len, u32* bytesWritten) { - *bytesWritten = write(TO_INT(dev), buf, len); +u32 writeBytes(handle dev, u8* buf, u32 len) { + return write(TO_INT(dev), buf, len); } -void readBytes(handle dev, u8* buf, u32 len, u32* bytesRead) { - *bytesRead = read(TO_INT(dev), buf, len); +u32 readBytes(handle dev, u8* buf, u32 len) { + return read(TO_INT(dev), buf, len); } void flush(handle dev) { diff --git a/tests/stdlib/src/matrix.gsc b/tests/stdlib/src/matrix.gsc index 21de436..0569dd9 100644 --- a/tests/stdlib/src/matrix.gsc +++ b/tests/stdlib/src/matrix.gsc @@ -50,15 +50,13 @@ fun printU32(in u32: val) u32ToCstr(val)(str, len) - handle: stdout = nullHandle - getStdoutHandle(stdout) + handle: stdout = getStdoutHandle() - u32: written = 0 as u32 - writeBytes(stdout, str, len, written) + writeBytes(stdout, str, len) heapFree(str) - writeBytes(stdout, " ", 1 as u32, written) + writeBytes(stdout, " ", 1 as u32) } fun test_matrix() @@ -67,8 +65,7 @@ fun test_matrix() heapAlloc((8 * 4) as u32)(matrix as ref u8) u32: written = 0 as u32 - handle: stdout = nullHandle - getStdoutHandle(stdout) + handle: stdout = getStdoutHandle() u32: idx = 0 as u32 while idx < 4 { @@ -82,7 +79,7 @@ fun test_matrix() idy = idy + 1 as u32 } - writeBytes(stdout, "\n", 1 as u32, written) + writeBytes(stdout, "\n", 1 as u32) heapFree(matrix[idx] as ref u8) idx = idx + 1 as u32