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
# 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

View File

@ -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);

View File

@ -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) {

View File

@ -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