Skip to content
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

(enh) Add Process.chdir, exposing libuv uv_chdir #119

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions src/module/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions src/module/os.wren
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/module/os.wren.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down