defined the return value of __spawn_process
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 2 Dec 2021 14:08:14 +0000 (08:08 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 2 Dec 2021 14:08:14 +0000 (08:08 -0600)
core/runtime/onyx_run.onyx
include/small_windows.h
src/wasm_runtime.c

index 4b2fd6d4dd4ac90cd7926bd868d07e77bc2c42a7..a60156241feae9ac4c28f53890ad8afc021d4a63 100644 (file)
@@ -17,4 +17,10 @@ use package wasi
     #export "_thread_exit"  _thread_exit
 }
 
-__spawn_process :: (path: str, args: [] str) -> i32 #foreign "env" "spawn_process" ---
\ No newline at end of file
+#local SpawnProcessResult :: enum {
+    Success     :: 0x00;
+    FailedToRun :: 0x01;
+    Error       :: 0x02;
+}
+
+__spawn_process :: (path: str, args: [] str) -> SpawnProcessResult #foreign "env" "spawn_process" ---
\ No newline at end of file
index 9aa075f5a174d08647de3581a46f17e9b8f6989a..4f3114dda44f4acffae143e1c7c4ead413c11f51 100644 (file)
@@ -306,6 +306,7 @@ GB_DLL_IMPORT BOOL    WINAPI CreateProcessA     (char const * lpApplicationName,
                                                  BOOL bInheritHandles, DWORD dwCreationFlags, void* lpEnvironment,
                                                  char const * lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo,
                                                  LPPROCESS_INFORMATION lpProcessInformation);
+GB_DLL_IMPORT BOOL    WINAPI GetExitCodeProcess (HANDLE hProcess, DWORD *lpExitCode);
 
 GB_DLL_IMPORT BOOL      WINAPI GetLogicalProcessorInformation(SYSTEM_LOGICAL_PROCESSOR_INFORMATION *buffer, DWORD *return_length);
 GB_DLL_IMPORT DWORD_PTR WINAPI SetThreadAffinityMask(HANDLE thread, DWORD_PTR check_mask);
index b3d8d4c23b1dfb65c2589d1c8315c735a6e13efe..862904f8659bfff6fc52a211823b6d43b06d22ad 100644 (file)
@@ -201,13 +201,25 @@ static wasm_trap_t* onyx_spawn_process_impl(const wasm_val_vec_t* params, wasm_v
         process_args[0] = process_path;
         process_args[args_len + 1] = NULL;
         
-        if (fork() == 0) {
-            execv(process_path, process_args);
+        switch (fork()) {
+            case -1: // Bad fork
+                results->data[0] = WASM_I32_VAL(1); // Failed to run
+                break;
+
+            case 0: // Child process
+                execv(process_path, process_args);
+            
+            default: {
+                i32 status;
+                wait(&status);
+
+                i32 exit_status = WEXITSTATUS(status);
+            
+                results->data[0] = WASM_I32_VAL(exit_status != 0 ? 2 : 0); // Error if non-zero exit, Success if zero.
+                break;
+            }
         }
 
-        i32 status;
-        wait(&status);
-        results->data[0] = WASM_I32_VAL(WEXITSTATUS(status));
     #endif
 
     #ifdef _BH_WINDOWS
@@ -232,8 +244,15 @@ static wasm_trap_t* onyx_spawn_process_impl(const wasm_val_vec_t* params, wasm_v
 
         PROCESS_INFORMATION proc_info;
         BOOL success = CreateProcessA(process_path, cmdLine, NULL, NULL, 1, 0, NULL, NULL, &startup, &proc_info);
+        if (!success) {
+            results->data[0] = WASM_I32_VAL(1); // Failed to run
+            return NULL;
+        }
+
         DWORD result = WaitForSingleObject(proc_info.hProcess, INFINITE);
-        results->data[0] = WASM_I32_VAL(result);
+        DWORD exitCode;
+        GetExitCodeProcess(proc_info.hProcess, &exitCode);
+        results->data[0] = WASM_I32_VAL(exitCode != 0 ? 2 : 0);
     #endif
 
     return NULL;