diff --git a/src/cli/modules.c b/src/cli/modules.c index a3c7f6c1..b00af34b 100644 --- a/src/cli/modules.c +++ b/src/cli/modules.c @@ -28,6 +28,7 @@ extern void platformHomePath(WrenVM* vm); extern void platformIsPosix(WrenVM* vm); extern void platformName(WrenVM* vm); extern void processAllArguments(WrenVM* vm); +extern void processChdir(WrenVM* vm); extern void processCwd(WrenVM* vm); extern void processPid(WrenVM* vm); extern void processPpid(WrenVM* vm); @@ -176,6 +177,7 @@ static ModuleRegistry modules[] = END_CLASS CLASS(Process) STATIC_METHOD("allArguments", processAllArguments) + STATIC_METHOD("chdir_(_)", processChdir) STATIC_METHOD("cwd", processCwd) STATIC_METHOD("pid", processPid) STATIC_METHOD("ppid", processPpid) diff --git a/src/module/os.c b/src/module/os.c index c13cd33f..27c40c55 100644 --- a/src/module/os.c +++ b/src/module/os.c @@ -98,6 +98,19 @@ void processAllArguments(WrenVM* vm) } } +// chdir_(dir) +void processChdir(WrenVM* vm) +{ + wrenEnsureSlots(vm, 1); + const char* dir = wrenGetSlotString(vm, 1); + if (uv_chdir(dir) != 0) + { + wrenSetSlotString(vm, 0, "Cannot change directory."); + wrenAbortFiber(vm, 0); + return; + } +} + void processCwd(WrenVM* vm) { wrenEnsureSlots(vm, 1); diff --git a/src/module/os.wren b/src/module/os.wren index 4d9ec25a..90bb62b3 100644 --- a/src/module/os.wren +++ b/src/module/os.wren @@ -10,8 +10,19 @@ class Process { // TODO: This will need to be smarter when wren supports CLI options. static arguments { allArguments.count >= 2 ? allArguments[2..-1] : [] } + static chdir(dir) { + ensureString_(dir, "directory") + chdir_(dir) + } + + // TODO: Copied from `io`. Figure out good way to share this. + static ensureString_(s, name) { + if (!(s is String)) Fiber.abort("%(name) must be a string.") + } + foreign static allArguments foreign static cwd + foreign static chdir_(dir) foreign static pid foreign static ppid foreign static version diff --git a/src/module/os.wren.inc b/src/module/os.wren.inc index 8c7c8522..32ac4295 100644 --- a/src/module/os.wren.inc +++ b/src/module/os.wren.inc @@ -14,8 +14,19 @@ static const char* osModuleSource = " // TODO: This will need to be smarter when wren supports CLI options.\n" " static arguments { allArguments.count >= 2 ? allArguments[2..-1] : [] }\n" "\n" +" static chdir(dir) {\n" +" ensureString_(dir, \"directory\")\n" +" chdir_(dir)\n" +" }\n" +"\n" +" // TODO: Copied from `io`. Figure out good way to share this.\n" +" static ensureString_(s, name) {\n" +" if (!(s is String)) Fiber.abort(\"%(name) must be a string.\")\n" +" }\n" +"\n" " foreign static allArguments\n" " foreign static cwd\n" +" foreign static chdir_(dir)\n" " foreign static pid\n" " foreign static ppid\n" " foreign static version\n"