From: Brendan Hansen Date: Tue, 21 Nov 2023 16:16:36 +0000 (-0600) Subject: added: getting/setting current directory X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=f48409c845a0e89a527483dfeb1ff3595e79d94c;p=onyx.git added: getting/setting current directory --- diff --git a/core/encoding/kdl/kql.onyx b/core/encoding/kdl/kql.onyx index 7d534e27..60fe8dc5 100644 --- a/core/encoding/kdl/kql.onyx +++ b/core/encoding/kdl/kql.onyx @@ -47,6 +47,8 @@ QueryStack :: struct { query_next :: ctx => { while true { if !ctx.stack { + if !ctx.d do break; + // If the stack is empty, populate with a node ctx.top_level_node += 1; if ctx.top_level_node >= ctx.d.nodes.length do break; diff --git a/core/encoding/kdl/utils.onyx b/core/encoding/kdl/utils.onyx index 8c7ba068..1c738e6b 100644 --- a/core/encoding/kdl/utils.onyx +++ b/core/encoding/kdl/utils.onyx @@ -15,6 +15,10 @@ use core {string} as_float :: (v: Value) -> ? f64 { return v.data.Number?.Float; } + + as_bool :: (v: Value) -> ? bool { + return v.data.Boolean; + } } #inject Document { diff --git a/core/os/dir.onyx b/core/os/dir.onyx index 903c14bb..c423bb80 100644 --- a/core/os/dir.onyx +++ b/core/os/dir.onyx @@ -39,6 +39,9 @@ dir_exists :: fs.__file_exists dir_remove :: fs.__dir_remove dir_rename :: fs.__file_rename +chdir :: fs.__chdir +getcwd :: fs.__getcwd + list_directory :: (path: str) -> Iterator(DirectoryEntry) { Context :: struct { dir: Directory; diff --git a/core/runtime/platform/onyx/fs.onyx b/core/runtime/platform/onyx/fs.onyx index 64a1da3d..ccc1398c 100644 --- a/core/runtime/platform/onyx/fs.onyx +++ b/core/runtime/platform/onyx/fs.onyx @@ -38,6 +38,8 @@ PollDescription :: struct { __dir_read :: (dir: DirectoryData, out_entry: &os.DirectoryEntry) -> bool --- __dir_create :: (path: str) -> bool --- __dir_remove :: (path: str) -> bool --- + __dir_cwd :: (pathbuf: [] u8) -> i32 --- + __dir_chdir :: (path: cstr) -> bool --- } } @@ -60,6 +62,23 @@ __dir_read :: __dir_read __dir_create :: __dir_create __dir_remove :: __dir_remove +__chdir :: (path: str) -> bool { + #persist buf: [1024] u8; + memory.copy(~~buf, path.data, math.min(1023, path.length)); + + return __dir_chdir(~~ buf); +} + +__getcwd :: () -> str { + #persist buf: [1024] u8; + length := __dir_cwd(.{ ~~buf, 1024 }); + if length < 0 { + return ""; + } + + return buf[0 .. length]; +} + __file_stream_vtable := io.Stream_Vtable.{ seek = (use fs: &os.File, to: i32, whence: io.SeekFrom) -> io.Error { now := __file_seek(data, to, whence); diff --git a/core/runtime/platform/wasi/wasi_fs.onyx b/core/runtime/platform/wasi/wasi_fs.onyx index 61a5679e..de1c23a3 100644 --- a/core/runtime/platform/wasi/wasi_fs.onyx +++ b/core/runtime/platform/wasi/wasi_fs.onyx @@ -351,3 +351,9 @@ __dir_remove :: (path: str) -> bool { return removed; } + + +#if !#defined(runtime.vars.WASIX) { + __chdir :: (path: str) => false + __getcwd :: () => "" +} \ No newline at end of file diff --git a/core/runtime/platform/wasi/wasix_defs.onyx b/core/runtime/platform/wasi/wasix_defs.onyx index 3a2d3cb7..e839238e 100644 --- a/core/runtime/platform/wasi/wasix_defs.onyx +++ b/core/runtime/platform/wasi/wasix_defs.onyx @@ -146,7 +146,7 @@ SockOption :: enum { tty_set :: (state: &TTY) -> Errno --- getcwd :: (path: [&] u8, pathlen: &u32) -> Errno --- - chdir :: (path: cstr) -> Errno --- + chdir :: (path: str) -> Errno --- thread_spawn_v2 :: (args: rawptr, tid: &u32) -> Errno --- thread_sleep :: (duration: Timestamp) -> Errno --- diff --git a/core/runtime/platform/wasi/wasix_misc.onyx b/core/runtime/platform/wasi/wasix_misc.onyx index 43e835b8..98f16bcb 100644 --- a/core/runtime/platform/wasi/wasix_misc.onyx +++ b/core/runtime/platform/wasi/wasix_misc.onyx @@ -55,4 +55,18 @@ __tty_set :: (tty: &os.TTY_State) -> bool { wasi.tty_set(&state); return true; +} + +__chdir :: (path: str) -> bool { + return wasi.chdir(path) == .Success; +} + +__getcwd :: () -> str { + #persist buf: [1024] u8; + len := 1024; + if wasi.getcwd(~~buf, &len) == .Success { + return buf[0 .. len]; + } + + return ""; } \ No newline at end of file diff --git a/runtime/onyx_runtime.c b/runtime/onyx_runtime.c index 1e75c3b8..799aa82e 100644 --- a/runtime/onyx_runtime.c +++ b/runtime/onyx_runtime.c @@ -69,6 +69,8 @@ ONYX_LIBRARY { ONYX_FUNC(__dir_close) ONYX_FUNC(__dir_create) ONYX_FUNC(__dir_remove) + ONYX_FUNC(__dir_cwd) + ONYX_FUNC(__dir_chdir) ONYX_FUNC(__spawn_thread) ONYX_FUNC(__kill_thread) diff --git a/runtime/src/ort_directories.h b/runtime/src/ort_directories.h index c067a309..36e6406a 100644 --- a/runtime/src/ort_directories.h +++ b/runtime/src/ort_directories.h @@ -166,3 +166,42 @@ ONYX_DEF(__dir_remove, (WASM_I32, WASM_I32), (WASM_I32)) { return NULL; #endif } + +ONYX_DEF(__dir_cwd, (WASM_I32, WASM_I32), (WASM_I32)) { +#if defined(_BH_LINUX) || defined(_BH_DARWIN) + char *dir = getcwd(ONYX_PTR(params->data[0].of.i32), params->data[1].of.i32); + if (!dir) { + results->data[0] = WASM_I32_VAL(-1); + return NULL; + } + + results->data[0] = WASM_I32_VAL( strlen(dir) ); + return NULL; +#endif + +#if defined(_BH_WINDOWS) + int length = GetCurrentDirectory(params->data[1].of.i32, ONYX_PTR(params->data[0].of.i32)); + if (length == 0 || length > params->data[1].of.i32) { + results->data[0] = WASM_I32_VAL(-1); + return NULL; + } + + results->data[0] = WASM_I32_VAL( length ); + return NULL; +#endif +} + +ONYX_DEF(__dir_chdir, (WASM_I32), (WASM_I32)) { +#if defined(_BH_LINUX) || defined(_BH_DARWIN) + int result = chdir(ONYX_PTR(params->data[0].of.i32)); + results->data[0] = WASM_I32_VAL(result ? 0 : 1); + return NULL; +#endif + +#if defined(_BH_WINDOWS) + int result = SetCurrentDirectory(ONYX_PTR(params->data[0].of.i32)); + results->data[0] = WASM_I32_VAL(result ? 1 : 0); + return NULL; +#endif +} + diff --git a/shared/include/small_windows.h b/shared/include/small_windows.h index ea292338..d25a08dc 100644 --- a/shared/include/small_windows.h +++ b/shared/include/small_windows.h @@ -467,3 +467,5 @@ GB_DLL_IMPORT int BCryptCloseAlgorithmProvider( GB_DLL_IMPORT void WINAPI WakeByAddressSingle(void * Address); GB_DLL_IMPORT BOOL WINAPI WaitOnAddress(volatile void * Address, void * compareAddress, size_t addressSize, DWORD milliseconds); +GB_DLL_IMPORT DWORD GetCurrentDirectory(DWORD nBufferLength, char *lpBuffer); +GB_DLL_IMPORT BOOL SetCurrentDirectory(char *lpPathName);