Implement Platform.homePath (#48)

* fix naming and path max

Co-authored-by: ruby0x1 <ruby0x1@pm.me>
This commit is contained in:
Dario Vladović 2021-05-02 22:00:19 +02:00 committed by GitHub
parent 8227330eb6
commit 8190115b97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 0 deletions

View File

@ -24,6 +24,7 @@ extern void fileRealPath(WrenVM* vm);
extern void fileSize(WrenVM* vm);
extern void fileStat(WrenVM* vm);
extern void fileWriteBytes(WrenVM* vm);
extern void platformHomePath(WrenVM* vm);
extern void platformIsPosix(WrenVM* vm);
extern void platformName(WrenVM* vm);
extern void processAllArguments(WrenVM* vm);
@ -169,6 +170,7 @@ static ModuleRegistry modules[] =
END_MODULE
MODULE(os)
CLASS(Platform)
STATIC_METHOD("homePath", platformHomePath)
STATIC_METHOD("isPosix", platformIsPosix)
STATIC_METHOD("name", platformName)
END_CLASS

View File

@ -15,6 +15,33 @@ void osSetArguments(int argc, const char* argv[])
args = argv;
}
void platformHomePath(WrenVM* vm)
{
wrenEnsureSlots(vm, 1);
char _buffer[WREN_PATH_MAX];
char* buffer = _buffer;
size_t length = sizeof(_buffer);
int result = uv_os_homedir(buffer, &length);
if (result == UV_ENOBUFS)
{
buffer = (char*)malloc(length);
result = uv_os_homedir(buffer, &length);
}
if (result != 0)
{
wrenSetSlotString(vm, 0, "Cannot get the current user's home directory.");
wrenAbortFiber(vm, 0);
return;
}
wrenSetSlotString(vm, 0, buffer);
if (buffer != _buffer) free(buffer);
}
void platformName(WrenVM* vm)
{
wrenEnsureSlots(vm, 1);

1
src/module/os.wren vendored
View File

@ -1,4 +1,5 @@
class Platform {
foreign static homePath
foreign static isPosix
foreign static name

View File

@ -3,6 +3,7 @@
static const char* osModuleSource =
"class Platform {\n"
" foreign static homePath\n"
" foreign static isPosix\n"
" foreign static name\n"
"\n"

7
test/os/platform/homedir.wren vendored Normal file
View File

@ -0,0 +1,7 @@
import "io" for File, Directory
import "os" for Platform
System.print(Platform.homePath is String) // expect: true
System.print(!Platform.homePath.isEmpty) // expect: true
System.print(File.realPath(Platform.homePath) == Platform.homePath) // expect: true
System.print(Directory.exists(Platform.homePath)) // expect: true