From: Brendan Hansen Date: Tue, 22 Mar 2022 04:28:42 +0000 (-0500) Subject: bugfixes in core reader / openal libraries X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=fd6b8e3e46073e59bd37c3e25d090f24a3c3a1a8;p=onyx.git bugfixes in core reader / openal libraries --- diff --git a/core/conv.onyx b/core/conv.onyx index 278d0a50..f9042243 100644 --- a/core/conv.onyx +++ b/core/conv.onyx @@ -78,14 +78,14 @@ str_to_i64 :: (s: str, base: u32 = 10) -> i64 { if base <= 10 do fallthrough; value *= ~~base; - value += ~~(c - #char "A"); + value += ~~((c - #char "A") + 10); } case #char "a" .. #char "z" { if base <= 10 do fallthrough; value *= ~~base; - value += ~~(c - #char "a"); + value += ~~((c - #char "a") + 10); } case #default do break break; diff --git a/core/io/io.onyx b/core/io/io.onyx index 71739d89..f6a56452 100644 --- a/core/io/io.onyx +++ b/core/io/io.onyx @@ -29,4 +29,7 @@ Error :: enum { // Not possible to unread. InvalidUnread :: 0x09; + + // When reading from a stream, no data was read. + ReadPending :: 0x0a; } \ No newline at end of file diff --git a/core/io/reader.onyx b/core/io/reader.onyx index c60ba6ed..d2cecc3a 100644 --- a/core/io/reader.onyx +++ b/core/io/reader.onyx @@ -120,6 +120,7 @@ unread_byte :: (use reader: ^Reader) -> Error { return .None; } + read_bytes :: (use reader: ^Reader, bytes: [] u8) -> (i32, Error) { n := bytes.count; if n == 0 { @@ -132,23 +133,31 @@ read_bytes :: (use reader: ^Reader, bytes: [] u8) -> (i32, Error) { if reader_empty(reader) do return 0, reader_consume_error(reader); if n >= buffer.count { - error, n = stream_read(stream, bytes); + while true { + error, n = stream_read(stream, bytes); + if error != .ReadPending do break; + } if n > 0 do last_byte = cast(i32) bytes[n - 1]; - return n, reader_consume_error(reader); } + } - start, end = 0, 0; - error, n = stream_read(stream, buffer); - if n == 0 do return 0, reader_consume_error(reader); - end += n; + write_index := 0; + while n > 0 { + if reader_read_next_chunk(reader) == .ReadPending { + return write_index, .ReadPending; + } + + to_write := math.min(n, end); + memory.copy(bytes.data + write_index, buffer.data, to_write); + n -= to_write; + write_index += to_write; + start += to_write; } - memory.copy(bytes.data, buffer.data + start, n); - start += n; - last_byte = cast(i32) buffer[start - 1]; - return n, .None; + last_byte = cast(i32) bytes[bytes.count - 1]; + return bytes.count, .None; } read_string :: (use reader: ^Reader, bytes := 1, allocator := context.allocator) -> str { @@ -245,8 +254,10 @@ read_u64 :: (use reader: ^Reader) -> u64 { return n; } -read_line :: (use reader: ^Reader, consume_newline := true, allocator := context.allocator, inplace := false) -> str { - reader_read_next_chunk(reader); +read_line :: (use reader: ^Reader, consume_newline := true, allocator := context.allocator, inplace := false, shift_buffer := true) -> str { + if shift_buffer { + while reader_read_next_chunk(reader) == .ReadPending --- + } count := start; defer start = count; @@ -266,9 +277,11 @@ read_line :: (use reader: ^Reader, consume_newline := true, allocator := context return out; } -read_word :: (use reader: ^Reader, numeric_allowed := false, allocator := context.allocator, inplace := false) -> str { +read_word :: (use reader: ^Reader, numeric_allowed := false, allocator := context.allocator, inplace := false, shift_buffer := true) -> str { skip_whitespace(reader); - reader_read_next_chunk(reader); + if shift_buffer { + while reader_read_next_chunk(reader) == .ReadPending --- + } count := start; defer start = count; @@ -292,8 +305,10 @@ read_word :: (use reader: ^Reader, numeric_allowed := false, allocator := contex return out; } -read_until :: (use reader: ^Reader, until: u8, skip: u32 = 0, allocator := context.allocator, consume_end := false, inplace := false) -> str { - reader_read_next_chunk(reader); +read_until :: (use reader: ^Reader, until: u8, skip: u32 = 0, allocator := context.allocator, consume_end := false, inplace := false, shift_buffer := true) -> str { + if shift_buffer { + while reader_read_next_chunk(reader) == .ReadPending --- + } count := start; defer start = count; @@ -410,6 +425,11 @@ skip_bytes :: (use reader: ^Reader, bytes: u32) -> (skipped: i32, err: Error) { // Try to re-read multiple times for 16 { err, n := stream_read(stream, buffer[end .. buffer.count]); + if err == .ReadPending { + error = err; + return err if end == 0 else .None; + } + end += n; if err != .None { if err == .EOF do done = true; diff --git a/core/net/net.onyx b/core/net/net.onyx index 45799c00..82bf9b04 100644 --- a/core/net/net.onyx +++ b/core/net/net.onyx @@ -144,8 +144,12 @@ socket_sendall :: (s: ^Socket, data: [] u8) { socket_recv :: (s: ^Socket, maxlen := 1024, allocator := context.allocator) -> [] u8 { buffer := alloc.from_stack(maxlen); - received := __net_recv(s.handle, .{ buffer, maxlen }); - if received < 0 { s.vtable = null; return .[]; } + would_block: bool; + received := __net_recv(s.handle, .{ buffer, maxlen }, ^would_block); + if received < 0 { + if !would_block do s.vtable = null; + return .[]; + } result := memory.make_slice(u8, received, allocator=allocator); memory.copy(result.data, buffer, received); @@ -154,8 +158,11 @@ socket_recv :: (s: ^Socket, maxlen := 1024, allocator := context.allocator) -> [ } socket_recv_into :: (s: ^Socket, buffer: [] u8) -> i32 { - received := __net_recv(s.handle, buffer); - if received < 0 { s.vtable = null; } + would_block: bool; + received := __net_recv(s.handle, buffer, ^would_block); + if received < 0 && !would_block do s.vtable = null; + if would_block do return 0; + return received; } @@ -163,8 +170,12 @@ socket_recv_into :: (s: ^Socket, buffer: [] u8) -> i32 { read = (use s: ^Socket, buffer: [] u8) -> (io.Error, u32) { if handle == 0 do return .BadFile, 0; - bytes_read := __net_recv(handle, buffer); - if bytes_read < 0 { s.vtable = null; } + would_block := false; + bytes_read := __net_recv(handle, buffer, ^would_block); + if bytes_read < 0 && !would_block do s.vtable = null; + + if would_block do return .ReadPending, bytes_read; + return .None, bytes_read; }, @@ -191,7 +202,7 @@ socket_recv_into :: (s: ^Socket, buffer: [] u8) -> i32 { #package __net_accept :: (handle: Socket.Handle, out_address: ^Socket_Address.Handle) -> Socket.Handle --- #package __net_connect :: (handle: Socket.Handle, host: str, port: u16) -> SocketError --- #package __net_send :: (handle: Socket.Handle, data: [] u8) -> i32 --- - #package __net_recv :: (handle: Socket.Handle, data: [] u8) -> i32 --- + #package __net_recv :: (handle: Socket.Handle, data: [] u8, async_would_block: ^bool) -> i32 --- #package __net_poll_recv :: (handle: [] Socket.Handle, timeout: i32, out_recv_indicies: ^i32) -> i32 --- #package __net_address_get_address :: (address: Socket_Address.Handle, out_buffer: [] u8) -> i32 --- diff --git a/docs/todo b/docs/todo index 02dbdee4..3ba0ab4c 100644 --- a/docs/todo +++ b/docs/todo @@ -217,10 +217,10 @@ Wishlist: Revamping File System: - [ ] runtime.fs should contain runtime specific file system API calls - [ ] 'os.File' should effectily be 'os.FileStream' and FileStream should go away + [x] runtime.fs should contain runtime specific file system API calls + [x] 'os.File' should effectily be 'os.FileStream' and FileStream should go away os.File :: struct { use stream: io.Stream; use data: runtime.fs.FileData; } - [ ] Most file functionality will be provided using the stream API. + [x] Most file functionality will be provided using the stream API. diff --git a/include/onyx_library.h b/include/onyx_library.h index 4361c753..d4aae4b4 100644 --- a/include/onyx_library.h +++ b/include/onyx_library.h @@ -24,7 +24,7 @@ typedef struct OnyxRuntime { // HACK HACK HACK // There should need to be this much stuff in here, but because Wasmer doesn't ship a "wasmerdll.lib" // file for windows, it is impossible for it to link successfully against the function provided in onyx.exe. - // Therefore, we must serve as the linker and do this manually. Hopefully they that library file + // Therefore, we must serve as the linker and do this manually. Hopefully that library file will be // shipped soon so this can go away... char* (*wasm_memory_data)(wasm_memory_t *wasm_memory); wasm_extern_t* (*wasm_extern_lookup_by_name)(wasm_module_t* module, wasm_instance_t* instance, const char* name); diff --git a/modules/onyx_runtime/onyx_runtime.c b/modules/onyx_runtime/onyx_runtime.c index f3527c35..41577ee9 100644 --- a/modules/onyx_runtime/onyx_runtime.c +++ b/modules/onyx_runtime/onyx_runtime.c @@ -950,11 +950,19 @@ ONYX_DEF(__net_send, (WASM_I32, WASM_I32, WASM_I32), (WASM_I32)) { return NULL; } -ONYX_DEF(__net_recv, (WASM_I32, WASM_I32, WASM_I32), (WASM_I32)) { +ONYX_DEF(__net_recv, (WASM_I32, WASM_I32, WASM_I32, WASM_I32), (WASM_I32)) { + *(i32 *) ONYX_PTR(params->data[3].of.i32) = 0; + #ifdef _BH_LINUX // TODO: The flags at the end should be controllable. int received = recv(params->data[0].of.i32, ONYX_PTR(params->data[1].of.i32), params->data[2].of.i32, 0); results->data[0] = WASM_I32_VAL(received); + + if (received < 0) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + *(i32 *) ONYX_PTR(params->data[3].of.i32) = 1; + } + } #endif return NULL; diff --git a/modules/openal/module.onyx b/modules/openal/module.onyx index 353e3629..ad5bfa50 100644 --- a/modules/openal/module.onyx +++ b/modules/openal/module.onyx @@ -1,5 +1,7 @@ package openal +use package core { cptr } + #library "onyx_openal" ALCdevice :: #distinct u64 @@ -104,8 +106,8 @@ ALCextfunc :: #distinct u64 alcCaptureSamples :: (device: ALCdevice, buf: rawptr, samples: i32) -> void --- // This returns a C-allocated string, which is not supported at the moment. - // alGetString :: (device: ALCdevice, param:i32) -> cstr --- - // alcGetString :: (device: ALCdevice, param:i32) -> cstr --- + alGetString :: (device: ALCdevice, param: i32) -> cptr(u8) --- + alcGetString :: (device: ALCdevice, param: i32) -> cptr(u8) --- } AL_FALSE :: 0 diff --git a/modules/openal/onyx_openal.c b/modules/openal/onyx_openal.c index 52d2673e..79d49249 100644 --- a/modules/openal/onyx_openal.c +++ b/modules/openal/onyx_openal.c @@ -112,6 +112,10 @@ ONYX_DEF(alDopplerFactor, (FLOAT), ()) { alDistanceModel(P(0, f32)); return NULL ONYX_DEF(alSpeedOfSound, (FLOAT), ()) { alDistanceModel(P(0, f32)); return NULL; } ONYX_DEF(alGetError, (), (INT)) { results->data[0] = WASM_I32_VAL(alGetError()); return NULL; } +ONYX_DEF(alGetString, (INT), (LONG)) { + wasm_val_init_ptr(&results->data[0], (void *) alGetString(P(0, i32))); + return NULL; +} ONYX_DEF(alcCreateContext, (LONG, PTR), (LONG)) { wasm_val_init_ptr(&results->data[0], alcCreateContext((ALCdevice *) P(0, i64), ONYX_PTR(P(1, i32)))); return NULL; } ONYX_DEF(alcMakeContextCurrent, (LONG), (BOOL)) { results->data[0] = WASM_I32_VAL(alcMakeContextCurrent((ALCcontext *) P(0, i64))); return NULL; } @@ -137,7 +141,10 @@ ONYX_DEF(alcCaptureCloseDevice, (LONG), (BOOL)) { results->data[0] = WASM_I32_VA ONYX_DEF(alcCaptureStart, (LONG), ()) { alcCaptureStart((ALCdevice *) P(0, i64)); return NULL; } ONYX_DEF(alcCaptureStop, (LONG), ()) { alcCaptureStop((ALCdevice *) P(0, i64)); return NULL; } ONYX_DEF(alcCaptureSamples, (LONG, PTR, INT), ()) { alcCaptureSamples((ALCdevice *) P(0, i64), ONYX_PTR(P(1, i32)), P(2, i32)); return NULL; } - +ONYX_DEF(alcGetString, (LONG, INT), (LONG)) { + wasm_val_init_ptr(&results->data[0], (void *) alcGetString((ALCdevice *) P(0, i64), P(1, i32))); + return NULL; +} ONYX_LIBRARY { ONYX_FUNC(alGenBuffers) diff --git a/modules/openal/onyx_openal.so b/modules/openal/onyx_openal.so index 77bd2065..7b835793 100755 Binary files a/modules/openal/onyx_openal.so and b/modules/openal/onyx_openal.so differ