Implement Process.cwd (#41)

* Implement `Process.cwd`
* Update `os` test suite
This commit is contained in:
Dario Vladović
2020-06-13 21:12:34 -07:00
committed by GitHub
parent 2de2d26528
commit 1ab88ed201
5 changed files with 29 additions and 1 deletions
+2
View File
@@ -25,6 +25,7 @@ extern void fileWriteBytes(WrenVM* vm);
extern void platformIsPosix(WrenVM* vm);
extern void platformName(WrenVM* vm);
extern void processAllArguments(WrenVM* vm);
extern void processCwd(WrenVM* vm);
extern void statPath(WrenVM* vm);
extern void statBlockCount(WrenVM* vm);
extern void statBlockSize(WrenVM* vm);
@@ -165,6 +166,7 @@ static ModuleRegistry modules[] =
END_CLASS
CLASS(Process)
STATIC_METHOD("allArguments", processAllArguments)
STATIC_METHOD("cwd", processCwd)
END_CLASS
END_MODULE
MODULE(repl)
+16
View File
@@ -1,4 +1,5 @@
#include "os.h"
#include "uv.h"
#include "wren.h"
#if __APPLE__
@@ -69,3 +70,18 @@ void processAllArguments(WrenVM* vm)
wrenInsertInList(vm, 0, -1, 1);
}
}
void processCwd(WrenVM* vm)
{
wrenEnsureSlots(vm, 1);
char buffer[PATH_MAX * 4];
size_t length = sizeof(buffer);
if (uv_cwd(buffer, &length) != 0)
{
wrenSetSlotString(vm, 0, "Cannot get current working directory.");
return wrenAbortFiber(vm, 0);
}
wrenSetSlotString(vm, 0, buffer);
}
+1
View File
@@ -10,4 +10,5 @@ class Process {
static arguments { allArguments[2..-1] }
foreign static allArguments
foreign static cwd
}
+1
View File
@@ -12,4 +12,5 @@ static const char* osModuleSource =
" static arguments { allArguments[2..-1] }\n"
"\n"
" foreign static allArguments\n"
" foreign static cwd\n"
"}\n";
+8
View File
@@ -0,0 +1,8 @@
import "io" for File, Directory
import "os" for Process
System.print(Process.cwd is String) // expect: true
System.print(!Process.cwd.isEmpty) // expect: true
System.print(Process.cwd.startsWith("/")) // expect: true
System.print(File.realPath(Process.cwd) == Process.cwd) // expect: true
System.print(Directory.exists(Process.cwd)) // expect: true