changed library IO module functions to return

This commit is contained in:
Sven Vogel 2024-08-04 17:51:03 +02:00
parent b786b3e156
commit 5fa5345dea
4 changed files with 39 additions and 38 deletions

View File

@ -22,33 +22,33 @@ handle: nullHandle = 0 as handle
# -- Implementation note # -- 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 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)` # 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 # Returns a handle to this processes standard input I/O handle
# -- Implementation note # -- 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 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)` # 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 # Returns a handle to this processes standard input I/O handle
# -- Implementation note # -- 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 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)` # 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 # Write `len` number of bytes from `buf` into the I/O resource specified
# by `dev`. Returns the number of bytes written. # by `dev`. Returns the number of bytes written.
# -- Implementation note # -- Implementation note
# On Linux this will use the syscall write # On Linux this will use the syscall write
# On Windows this will use the WriteFile function # 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` # Read atmost `len` bytes to `buf` from the I/O resource specified by `dev`
# Returns the number of read bytes in `written` # Returns the number of read bytes in `written`
# -- Implementation note # -- Implementation note
# On Linux this will use the syscall read # On Linux this will use the syscall read
# On Windows this will use the ReadFile function # 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` # Flushes the buffers of the I/O resource specified by `dev`
# -- Implementation note # -- Implementation note

View File

@ -6,15 +6,15 @@
typedef ptr handle; 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); void flush(handle dev);

View File

@ -11,24 +11,28 @@
// FIXME: error in case GetStdHandle return INVALID_HANDLE_VALUE // FIXME: error in case GetStdHandle return INVALID_HANDLE_VALUE
// FIXME: error in case functions return 0 // FIXME: error in case functions return 0
void getStdinHandle(handle* stdin) { handle getStdinHandle(handle* stdin) {
*stdin = (handle) GetStdHandle(STD_INPUT_HANDLE); return GetStdHandle(STD_INPUT_HANDLE);
} }
void getStdoutHandle(handle* stdout) { handle getStdoutHandle(handle* stdout) {
*stdout = (handle) GetStdHandle(STD_OUTPUT_HANDLE); return GetStdHandle(STD_OUTPUT_HANDLE);
} }
void getStderrHandle(handle* stderr) { handle getStderrHandle(handle* stderr) {
*stderr = (handle) GetStdHandle(STD_ERROR_HANDLE); return GetStdHandle(STD_ERROR_HANDLE);
} }
void writeBytes(handle dev, u8* buf, u32 len, u32* bytesWritten) { u32 writeBytes(handle dev, u8* buf, u32 len) {
WriteFile((HANDLE) dev, buf, len, bytesRead, NULL); u32 bytesWritten = 0;
WriteFile((HANDLE) dev, buf, len, &bytesWritten, NULL);
return bytesWritten;
} }
void readBytes(handle dev, u8* buf, u32 len, u32* bytesRead) { u32 readBytes(handle dev, u8* buf, u32 len) {
ReadFile((HANDLE) dev, buf, len, bytesRead, NULL); u32 bytesRead = 0;
ReadFile((HANDLE) dev, buf, len, &bytesRead, NULL);
return bytesRead;
} }
void flush(handle dev) { void flush(handle dev) {
@ -47,24 +51,24 @@ void flush(handle dev) {
// which are stored as 64-bit by zero extending // which are stored as 64-bit by zero extending
#define TO_INT(x) ((int)(long int)(x)) #define TO_INT(x) ((int)(long int)(x))
void getStdinHandle(handle* stdin) { handle getStdinHandle() {
*stdin = (handle) STDIN_FILENO; return (handle) STDIN_FILENO;
} }
void getStdoutHandle(handle* stdout) { handle getStdoutHandle() {
*stdout = (handle) STDOUT_FILENO; return (handle) STDOUT_FILENO;
} }
void getStderrHandle(handle* stderr) { handle getStderrHandle() {
*stderr = (handle) STDERR_FILENO; return (handle) STDERR_FILENO;
} }
void writeBytes(handle dev, u8* buf, u32 len, u32* bytesWritten) { u32 writeBytes(handle dev, u8* buf, u32 len) {
*bytesWritten = write(TO_INT(dev), buf, len); return write(TO_INT(dev), buf, len);
} }
void readBytes(handle dev, u8* buf, u32 len, u32* bytesRead) { u32 readBytes(handle dev, u8* buf, u32 len) {
*bytesRead = read(TO_INT(dev), buf, len); return read(TO_INT(dev), buf, len);
} }
void flush(handle dev) { void flush(handle dev) {

View File

@ -50,15 +50,13 @@ fun printU32(in u32: val)
u32ToCstr(val)(str, len) u32ToCstr(val)(str, len)
handle: stdout = nullHandle handle: stdout = getStdoutHandle()
getStdoutHandle(stdout)
u32: written = 0 as u32 writeBytes(stdout, str, len)
writeBytes(stdout, str, len, written)
heapFree(str) heapFree(str)
writeBytes(stdout, " ", 1 as u32, written) writeBytes(stdout, " ", 1 as u32)
} }
fun test_matrix() fun test_matrix()
@ -67,8 +65,7 @@ fun test_matrix()
heapAlloc((8 * 4) as u32)(matrix as ref u8) heapAlloc((8 * 4) as u32)(matrix as ref u8)
u32: written = 0 as u32 u32: written = 0 as u32
handle: stdout = nullHandle handle: stdout = getStdoutHandle()
getStdoutHandle(stdout)
u32: idx = 0 as u32 u32: idx = 0 as u32
while idx < 4 { while idx < 4 {
@ -82,7 +79,7 @@ fun test_matrix()
idy = idy + 1 as u32 idy = idy + 1 as u32
} }
writeBytes(stdout, "\n", 1 as u32, written) writeBytes(stdout, "\n", 1 as u32)
heapFree(matrix[idx] as ref u8) heapFree(matrix[idx] as ref u8)
idx = idx + 1 as u32 idx = idx + 1 as u32