Add a Platform class.

- Rename the "process" module to "os".
- Add Platform to it.
- Static "name" method.
- Static "isPosix" method.
- Docs and tests!
This commit is contained in:
Bob Nystrom
2016-05-21 12:44:17 -07:00
parent 6bcc64eb61
commit 06731f66d4
20 changed files with 170 additions and 65 deletions
+2 -2
View File
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <string.h>
#include "process.h"
#include "os.h"
#include "vm.h"
#include "wren.h"
@@ -14,7 +14,7 @@ int main(int argc, const char* argv[])
return 0;
}
processSetArguments(argc, argv);
osSetArguments(argc, argv);
if (argc == 1)
{
+8 -2
View File
@@ -4,7 +4,7 @@
#include "modules.h"
#include "io.wren.inc"
#include "process.wren.inc"
#include "os.wren.inc"
#include "scheduler.wren.inc"
#include "timer.wren.inc"
@@ -21,6 +21,8 @@ extern void fileRealPath(WrenVM* vm);
extern void fileSize(WrenVM* vm);
extern void fileStat(WrenVM* vm);
extern void fileWriteBytes(WrenVM* vm);
extern void platformIsPosix(WrenVM* vm);
extern void platformName(WrenVM* vm);
extern void processAllArguments(WrenVM* vm);
extern void statPath(WrenVM* vm);
extern void statBlockCount(WrenVM* vm);
@@ -149,7 +151,11 @@ static ModuleRegistry modules[] =
STATIC_METHOD("readStop_()", stdinReadStop)
END_CLASS
END_MODULE
MODULE(process)
MODULE(os)
CLASS(Platform)
STATIC_METHOD("isPosix", platformIsPosix)
STATIC_METHOD("name", platformName)
END_CLASS
CLASS(Process)
STATIC_METHOD("allArguments", processAllArguments)
END_CLASS
+71
View File
@@ -0,0 +1,71 @@
#include "os.h"
#include "wren.h"
#if __APPLE__
#include "TargetConditionals.h"
#endif
int numArgs;
const char** args;
void osSetArguments(int argc, const char* argv[])
{
numArgs = argc;
args = argv;
}
void platformName(WrenVM* vm)
{
wrenEnsureSlots(vm, 1);
#ifdef _WIN32
wrenSetSlotString(vm, 0, "Windows");
#elif __APPLE__
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
wrenSetSlotString(vm, 0, "iOS");
#elif TARGET_OS_MAC
wrenSetSlotString(vm, 0, "OS X");
#else
wrenSetSlotString(vm, 0, "Unknown");
#endif
#elif __linux__
wrenSetSlotString(vm, 0, "Linux");
#elif __unix__
wrenSetSlotString(vm, 0, "Unix");
#elif defined(_POSIX_VERSION)
wrenSetSlotString(vm, 0, "POSIX");
#else
wrenSetSlotString(vm, 0, "Unknown");
#endif
}
void platformIsPosix(WrenVM* vm)
{
wrenEnsureSlots(vm, 1);
#ifdef _WIN32
wrenSetSlotBool(vm, 0, false);
#elif __APPLE__
wrenSetSlotBool(vm, 0, true);
#elif __linux__
wrenSetSlotBool(vm, 0, true);
#elif __unix__
wrenSetSlotBool(vm, 0, true);
#elif defined(_POSIX_VERSION)
wrenSetSlotBool(vm, 0, true);
#else
wrenSetSlotString(vm, 0, false);
#endif
}
void processAllArguments(WrenVM* vm)
{
wrenEnsureSlots(vm, 2);
wrenSetSlotNewList(vm, 0);
for (int i = 0; i < numArgs; i++)
{
wrenSetSlotString(vm, 1, args[i]);
wrenInsertInList(vm, 0, -1, 1);
}
}
+1 -1
View File
@@ -4,6 +4,6 @@
#include "wren.h"
// Stores the command line arguments passed to the CLI.
void processSetArguments(int argc, const char* argv[]);
void osSetArguments(int argc, const char* argv[]);
#endif
@@ -1,3 +1,8 @@
class Platform {
foreign static isPosix
foreign static name
}
class Process {
// TODO: This will need to be smarter when wren supports CLI options.
static arguments { allArguments[2..-1] }
+13
View File
@@ -0,0 +1,13 @@
// Generated automatically from src/module/os.wren. Do not edit.
static const char* osModuleSource =
"class Platform {\n"
" foreign static isPosix\n"
" foreign static name\n"
"}\n"
"\n"
"class Process {\n"
" // TODO: This will need to be smarter when wren supports CLI options.\n"
" static arguments { allArguments[2..-1] }\n"
"\n"
" foreign static allArguments\n"
"}\n";
-23
View File
@@ -1,23 +0,0 @@
#include "process.h"
#include "wren.h"
int numArgs;
const char** args;
void processSetArguments(int argc, const char* argv[])
{
numArgs = argc;
args = argv;
}
void processAllArguments(WrenVM* vm)
{
wrenEnsureSlots(vm, 2);
wrenSetSlotNewList(vm, 0);
for (int i = 0; i < numArgs; i++)
{
wrenSetSlotString(vm, 1, args[i]);
wrenInsertInList(vm, 0, -1, 1);
}
}
-7
View File
@@ -1,7 +0,0 @@
// Generated automatically from src/module/process.wren. Do not edit.
static const char* processModuleSource =
"class Process {\n"
" static arguments { allArguments[2..-1] }\n"
"\n"
" foreign static allArguments\n"
"}\n";