Skip to content

(enh) add OS / Process.exit #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/cli/modules.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern void processCwd(WrenVM* vm);
extern void processPid(WrenVM* vm);
extern void processPpid(WrenVM* vm);
extern void processVersion(WrenVM* vm);
extern void processExit(WrenVM* vm);
extern void statPath(WrenVM* vm);
extern void statBlockCount(WrenVM* vm);
extern void statBlockSize(WrenVM* vm);
Expand Down Expand Up @@ -180,6 +181,7 @@ static ModuleRegistry modules[] =
STATIC_METHOD("pid", processPid)
STATIC_METHOD("ppid", processPpid)
STATIC_METHOD("version", processVersion)
STATIC_METHOD("exit(_)", processExit)
END_CLASS
END_MODULE
MODULE(repl)
Expand Down
31 changes: 29 additions & 2 deletions src/cli/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,39 @@ static void initVM()
uv_loop_init(loop);
}

void on_uvClose(uv_handle_t* handle)
{
if (handle != NULL)
{
free(handle);
}
}

void on_uvWalkForShutdown(uv_handle_t* handle, void* arg)
{
if (!uv_is_closing(handle))
uv_close(handle, on_uvClose);
}

static void uvShutdown() {
uv_loop_t *loop = getLoop();
int result = uv_loop_close(loop);
if (result != UV_EBUSY) return;

// walk open handles and shut them down
uv_walk(loop, on_uvWalkForShutdown, NULL);
uv_run(loop, UV_RUN_ONCE);
result = uv_loop_close(loop);
if (result != 0) {
fprintf(stderr, "could not close UV event loop completely");
}
}

static void freeVM()
{
ioShutdown();
schedulerShutdown();

uv_loop_close(loop);
uvShutdown();
free(loop);

wrenFreeVM(vm);
Expand Down
7 changes: 7 additions & 0 deletions src/module/os.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "os.h"
#include "uv.h"
#include "wren.h"
#include "vm.h"

#if __APPLE__
#include "TargetConditionals.h"
Expand Down Expand Up @@ -98,6 +99,12 @@ void processAllArguments(WrenVM* vm)
}
}

void processExit(WrenVM* vm) {
int code = (int)wrenGetSlotDouble(vm, 1);
setExitCode(code);
uv_stop(getLoop());
}

void processCwd(WrenVM* vm)
{
wrenEnsureSlots(vm, 1);
Expand Down
1 change: 1 addition & 0 deletions src/module/os.wren
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ class Process {
foreign static pid
foreign static ppid
foreign static version
foreign static exit(code)
}
1 change: 1 addition & 0 deletions src/module/os.wren.inc
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ static const char* osModuleSource =
" foreign static pid\n"
" foreign static ppid\n"
" foreign static version\n"
" foreign static exit(code)\n"
"}\n";