From: Brendan Hansen Date: Fri, 3 Dec 2021 21:44:30 +0000 (-0600) Subject: moved process.onyx to core.os X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=81060aabdc0ab1ee6ba8de68975b65956f5a42c3;p=onyx.git moved process.onyx to core.os --- diff --git a/core/io/process.onyx b/core/io/process.onyx deleted file mode 100644 index e1e7dfda..00000000 --- a/core/io/process.onyx +++ /dev/null @@ -1,65 +0,0 @@ -package core.io - -// Some thoughts about processes and the API. -// -// Should processes ever directly dump to standard output? -// Or should their output always be buffered through a pipe to the user? -// -// - - -#local runtime :: package runtime -#if runtime.Runtime != runtime.Runtime_Onyx { - #error "This file can only be included in the 'onyx' runtime, because Wasi has not defined how to spawn and manage processes."; -} - -Process :: struct { - Handle :: #distinct i64; - - use stream: Stream; - process_handle: Handle; -} - -process_spawn :: (path: str, args: [] str, non_blocking_io := false) -> Process { - handle := runtime.__process_spawn(path, args, non_blocking_io); - - return .{ - .{ ^process_stream_vtable }, - handle, - }; -} - -process_kill :: (use p: ^Process) -> bool { - return runtime.__process_kill(process_handle); -} - -process_wait :: (use p: ^Process) => { - return runtime.__process_wait(process_handle); -} - -process_destroy :: (use p: ^Process) => { - runtime.__process_destroy(process_handle); -} - -#local process_stream_vtable := Stream_Vtable.{ - read = (use p: ^Process, buffer: [] u8) -> (Error, u32) { - // Read from the process stdout - if cast(i64) process_handle == 0 do return .BadFile, 0; - - bytes_read := runtime.__process_read(process_handle, buffer); - return .None, bytes_read; - }, - - write = (use p: ^Process, buffer: [] u8) -> (Error, u32) { - // Write to the process stdin - if cast(i64) process_handle == 0 do return .BadFile, 0; - - bytes_written := runtime.__process_write(process_handle, buffer); - return .None, bytes_written; - }, - - close = (use p: ^Process) -> Error { - process_kill(p); - return .None; - } -} diff --git a/core/os/process.onyx b/core/os/process.onyx new file mode 100644 index 00000000..f22aee04 --- /dev/null +++ b/core/os/process.onyx @@ -0,0 +1,69 @@ +package core.os + +// Some thoughts about processes and the API. +// +// Should processes ever directly dump to standard output? +// Or should their output always be buffered through a pipe to the user? +// +// + + +#if runtime.Runtime != runtime.Runtime_Onyx { + #error "This file can only be included in the 'onyx' runtime, because Wasi has not defined how to spawn and manage processes."; +} + +#local { + runtime :: package runtime + io :: package core.io +} + +Process :: struct { + Handle :: #distinct i64; + + use stream: io.Stream; + process_handle: Handle; +} + +process_spawn :: (path: str, args: [] str, non_blocking_io := false) -> Process { + handle := runtime.__process_spawn(path, args, non_blocking_io); + + return .{ + .{ ^process_stream_vtable }, + handle, + }; +} + +process_kill :: (use p: ^Process) -> bool { + return runtime.__process_kill(process_handle); +} + +process_wait :: (use p: ^Process) => { + return runtime.__process_wait(process_handle); +} + +process_destroy :: (use p: ^Process) => { + runtime.__process_destroy(process_handle); +} + +#local process_stream_vtable := io.Stream_Vtable.{ + read = (use p: ^Process, buffer: [] u8) -> (io.Error, u32) { + // Read from the process stdout + if cast(i64) process_handle == 0 do return .BadFile, 0; + + bytes_read := runtime.__process_read(process_handle, buffer); + return .None, bytes_read; + }, + + write = (use p: ^Process, buffer: [] u8) -> (io.Error, u32) { + // Write to the process stdin + if cast(i64) process_handle == 0 do return .BadFile, 0; + + bytes_written := runtime.__process_write(process_handle, buffer); + return .None, bytes_written; + }, + + close = (use p: ^Process) -> io.Error { + process_kill(p); + return .None; + } +} diff --git a/core/runtime/onyx_run.onyx b/core/runtime/onyx_run.onyx index 2faeea00..07a0e3ae 100644 --- a/core/runtime/onyx_run.onyx +++ b/core/runtime/onyx_run.onyx @@ -17,7 +17,7 @@ use package wasi #export "_thread_exit" _thread_exit } -#load "core/io/process" +#load "core/os/process" #local ProcessResult :: enum { Success :: 0x00; @@ -26,9 +26,9 @@ use package wasi InternalErr :: 0x03; } -__process_spawn :: (path: str, args: [] str, non_blocking_io: bool) -> io.Process.Handle #foreign "env" "process_spawn" --- -__process_read :: (handle: io.Process.Handle, buffer: [] u8) -> i32 #foreign "env" "process_read" --- -__process_write :: (handle: io.Process.Handle, buffer: [] u8) -> i32 #foreign "env" "process_write" --- -__process_kill :: (handle: io.Process.Handle) -> bool #foreign "env" "process_kill" --- -__process_wait :: (handle: io.Process.Handle) -> ProcessResult #foreign "env" "process_wait" --- -__process_destroy :: (handle: io.Process.Handle) -> void #foreign "env" "process_destroy" --- \ No newline at end of file +__process_spawn :: (path: str, args: [] str, non_blocking_io: bool) -> os.Process.Handle #foreign "env" "process_spawn" --- +__process_read :: (handle: os.Process.Handle, buffer: [] u8) -> i32 #foreign "env" "process_read" --- +__process_write :: (handle: os.Process.Handle, buffer: [] u8) -> i32 #foreign "env" "process_write" --- +__process_kill :: (handle: os.Process.Handle) -> bool #foreign "env" "process_kill" --- +__process_wait :: (handle: os.Process.Handle) -> ProcessResult #foreign "env" "process_wait" --- +__process_destroy :: (handle: os.Process.Handle) -> void #foreign "env" "process_destroy" --- \ No newline at end of file diff --git a/scripts/run_tests.onyx b/scripts/run_tests.onyx index c029768b..0c2b53a1 100644 --- a/scripts/run_tests.onyx +++ b/scripts/run_tests.onyx @@ -123,14 +123,14 @@ test_cases :: (cases) => { for #no_close *cases { printf("[{}] Running test {}...\n", context.thread_id, it.source_file); - proc := io.process_spawn(onyx_cmd, .["run", it.source_file]); - defer io.process_destroy(^proc); + proc := os.process_spawn(onyx_cmd, .["run", it.source_file]); + defer os.process_destroy(^proc); proc_reader := io.reader_make(^proc); output := io.read_all(^proc_reader); defer memory.free_slice(^output); - if exit := io.process_wait(^proc); exit != .Success { + if exit := os.process_wait(^proc); exit != .Success { // Error running the test case print_color(.Red, "[{}] Error '{}' in test case {}.\n{}", context.thread_id, exit, it.source_file, output); at_least_one_test_failed = true;