feat: add Process.pid and Process.ppid foreign static methods to os module

Implement uv_os_getpid() and uv_os_getppid() bindings in process.c, register them in the CLI module table, expose as foreign statics in os.wren, and add a test suite verifying both return positive integers with pid greater than ppid.
This commit is contained in:
Dario Vladović
2021-04-28 21:51:23 +00:00
parent 7b56ba281b
commit 5118fb6561
5 changed files with 38 additions and 10 deletions
+6 -2
View File
@@ -25,8 +25,10 @@ extern void fileWriteBytes(WrenVM* vm);
extern void platformIsPosix(WrenVM* vm);
extern void platformName(WrenVM* vm);
extern void processAllArguments(WrenVM* vm);
extern void processVersion(WrenVM* vm);
extern void processCwd(WrenVM* vm);
extern void processPid(WrenVM* vm);
extern void processPpid(WrenVM* vm);
extern void processVersion(WrenVM* vm);
extern void statPath(WrenVM* vm);
extern void statBlockCount(WrenVM* vm);
extern void statBlockSize(WrenVM* vm);
@@ -168,8 +170,10 @@ static ModuleRegistry modules[] =
END_CLASS
CLASS(Process)
STATIC_METHOD("allArguments", processAllArguments)
STATIC_METHOD("version", processVersion)
STATIC_METHOD("cwd", processCwd)
STATIC_METHOD("pid", processPid)
STATIC_METHOD("ppid", processPpid)
STATIC_METHOD("version", processVersion)
END_CLASS
END_MODULE
MODULE(repl)
+15 -6
View File
@@ -71,12 +71,6 @@ void processAllArguments(WrenVM* vm)
}
}
void processVersion(WrenVM* vm) {
wrenEnsureSlots(vm, 1);
wrenSetSlotString(vm, 0, WREN_VERSION_STRING);
}
void processCwd(WrenVM* vm)
{
wrenEnsureSlots(vm, 1);
@@ -92,3 +86,18 @@ void processCwd(WrenVM* vm)
wrenSetSlotString(vm, 0, buffer);
}
void processPid(WrenVM* vm) {
wrenEnsureSlots(vm, 1);
wrenSetSlotDouble(vm, 0, uv_os_getpid());
}
void processPpid(WrenVM* vm) {
wrenEnsureSlots(vm, 1);
wrenSetSlotDouble(vm, 0, uv_os_getppid());
}
void processVersion(WrenVM* vm) {
wrenEnsureSlots(vm, 1);
wrenSetSlotString(vm, 0, WREN_VERSION_STRING);
}
+3 -1
View File
@@ -10,6 +10,8 @@ class Process {
static arguments { allArguments.count >= 2 ? allArguments[2..-1] : [] }
foreign static allArguments
foreign static version
foreign static cwd
foreign static pid
foreign static ppid
foreign static version
}
+3 -1
View File
@@ -14,6 +14,8 @@ static const char* osModuleSource =
" static arguments { allArguments.count >= 2 ? allArguments[2..-1] : [] }\n"
"\n"
" foreign static allArguments\n"
" foreign static version\n"
" foreign static cwd\n"
" foreign static pid\n"
" foreign static ppid\n"
" foreign static version\n"
"}\n";
+11
View File
@@ -0,0 +1,11 @@
import "os" for Process
System.print(Process.pid is Num) // expect: true
System.print(Process.pid.isInteger) // expect: true
System.print(Process.pid > 0) // expect: true
System.print(Process.ppid is Num) // expect: true
System.print(Process.ppid.isInteger) // expect: true
System.print(Process.ppid > 0) // expect: true
System.print(Process.pid > Process.ppid) // expect: true