added: getting/setting current directory
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 21 Nov 2023 16:16:36 +0000 (10:16 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 21 Nov 2023 16:16:36 +0000 (10:16 -0600)
core/encoding/kdl/kql.onyx
core/encoding/kdl/utils.onyx
core/os/dir.onyx
core/runtime/platform/onyx/fs.onyx
core/runtime/platform/wasi/wasi_fs.onyx
core/runtime/platform/wasi/wasix_defs.onyx
core/runtime/platform/wasi/wasix_misc.onyx
runtime/onyx_runtime.c
runtime/src/ort_directories.h
shared/include/small_windows.h

index 7d534e274b44870de1df3f9132a3628f9ce03cb8..60fe8dc5defe1dbac87be478da6ed2fcf16551bf 100644 (file)
@@ -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;
index 8c7ba06873bb8215291aef8eaeae54d81e994e7a..1c738e6b43891ce51e33f67e70be146067c9fe82 100644 (file)
@@ -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 {
index 903c14bbd3af83e7d88af3308d0be13eaca26b76..c423bb80672250a17de00098dee44b5dc3e7a5bc 100644 (file)
@@ -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;
index 64a1da3d28184fc216263a7cfa7ea6fbf3471bc0..ccc1398cebf5517b627278492e099f67df64a1b5 100644 (file)
@@ -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);
index 61a5679e35d28cfa4300d68f28f2b8dd3f4ef2f4..de1c23a348da5578aff1ba9751704b12b42fa0ea 100644 (file)
@@ -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
index 3a2d3cb7818448bbc211dbded21c837708baa42f..e839238e696b6fabc1076bcaa21fa1ed5e760d08 100644 (file)
@@ -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 ---
index 43e835b8f3690d701d35b7127d2c5bc5d5034a82..98f16bcb5779a254eafa3a5be1fedc115d7f16d6 100644 (file)
@@ -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
index 1e75c3b807068e4c69c0658ad873063bcd69467e..799aa82e78372836287d5bc3e612100d3cdc6972 100644 (file)
@@ -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)
index c067a30981f67c71cfd10e0441f22992025d1c2e..36e6406a84d14b1fe0beb8d69ccd7549e052edc5 100644 (file)
@@ -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
+}
+
index ea292338da460bcafd05124f4ed9f76cd63aa885..d25a08dcf4e1b63a9d06e85ca2521ade8f81bd16 100644 (file)
@@ -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);