Skip to content

Commit 8190115

Browse files
vladimyrruby0x1
andauthored
Implement Platform.homePath (#48)
* fix naming and path max Co-authored-by: ruby0x1 <[email protected]>
1 parent 8227330 commit 8190115

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

src/cli/modules.c

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extern void fileRealPath(WrenVM* vm);
2424
extern void fileSize(WrenVM* vm);
2525
extern void fileStat(WrenVM* vm);
2626
extern void fileWriteBytes(WrenVM* vm);
27+
extern void platformHomePath(WrenVM* vm);
2728
extern void platformIsPosix(WrenVM* vm);
2829
extern void platformName(WrenVM* vm);
2930
extern void processAllArguments(WrenVM* vm);
@@ -169,6 +170,7 @@ static ModuleRegistry modules[] =
169170
END_MODULE
170171
MODULE(os)
171172
CLASS(Platform)
173+
STATIC_METHOD("homePath", platformHomePath)
172174
STATIC_METHOD("isPosix", platformIsPosix)
173175
STATIC_METHOD("name", platformName)
174176
END_CLASS

src/module/os.c

+27
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,33 @@ void osSetArguments(int argc, const char* argv[])
1515
args = argv;
1616
}
1717

18+
void platformHomePath(WrenVM* vm)
19+
{
20+
wrenEnsureSlots(vm, 1);
21+
22+
char _buffer[WREN_PATH_MAX];
23+
char* buffer = _buffer;
24+
size_t length = sizeof(_buffer);
25+
int result = uv_os_homedir(buffer, &length);
26+
27+
if (result == UV_ENOBUFS)
28+
{
29+
buffer = (char*)malloc(length);
30+
result = uv_os_homedir(buffer, &length);
31+
}
32+
33+
if (result != 0)
34+
{
35+
wrenSetSlotString(vm, 0, "Cannot get the current user's home directory.");
36+
wrenAbortFiber(vm, 0);
37+
return;
38+
}
39+
40+
wrenSetSlotString(vm, 0, buffer);
41+
42+
if (buffer != _buffer) free(buffer);
43+
}
44+
1845
void platformName(WrenVM* vm)
1946
{
2047
wrenEnsureSlots(vm, 1);

src/module/os.wren

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Platform {
2+
foreign static homePath
23
foreign static isPosix
34
foreign static name
45

src/module/os.wren.inc

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
static const char* osModuleSource =
55
"class Platform {\n"
6+
" foreign static homePath\n"
67
" foreign static isPosix\n"
78
" foreign static name\n"
89
"\n"

test/os/platform/homedir.wren

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import "io" for File, Directory
2+
import "os" for Platform
3+
4+
System.print(Platform.homePath is String) // expect: true
5+
System.print(!Platform.homePath.isEmpty) // expect: true
6+
System.print(File.realPath(Platform.homePath) == Platform.homePath) // expect: true
7+
System.print(Directory.exists(Platform.homePath)) // expect: true

0 commit comments

Comments
 (0)