Skip to content

Commit 095b12f

Browse files
[wasm] Port ProcessInfo for WASI platform
1 parent b5988f5 commit 095b12f

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ CF_PRIVATE const wchar_t *_CFDLLPath(void) {
103103
}
104104
#endif // TARGET_OS_WIN32
105105

106-
#if !TARGET_OS_WASI
107106
static const char *__CFProcessPath = NULL;
108107
static const char *__CFprogname = NULL;
109108

@@ -186,6 +185,31 @@ const char *_CFProcessPath(void) {
186185
__CFprogname = __CFProcessPath;
187186
}
188187
return __CFProcessPath;
188+
#elif TARGET_OS_WASI
189+
__wasi_errno_t err;
190+
size_t argc;
191+
size_t argv_buf_size;
192+
err = __wasi_args_sizes_get(&argc, &argv_buf_size);
193+
if (err != 0) {
194+
__CFProcessPath = "";
195+
__CFprogname = __CFProcessPath;
196+
return __CFProcessPath;
197+
}
198+
char *argv_buf = malloc(argv_buf_size);
199+
char **argv = calloc(argc, sizeof(char *));
200+
err = __wasi_args_get((uint8_t **)argv, (uint8_t *)argv_buf);
201+
if (err != 0) {
202+
__CFProcessPath = "";
203+
__CFprogname = __CFProcessPath;
204+
free(argv_buf);
205+
free(argv);
206+
return __CFProcessPath;
207+
}
208+
_CFSetProgramNameFromPath(argv[0]);
209+
free(argv_buf);
210+
free(argv);
211+
return __CFProcessPath;
212+
189213
#else // TARGET_OS_BSD
190214
char *argv0 = NULL;
191215

@@ -248,7 +272,6 @@ const char *_CFProcessPath(void) {
248272
return __CFProcessPath;
249273
#endif
250274
}
251-
#endif // TARGET_OS_WASI
252275

253276
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_BSD
254277
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void) {
@@ -273,7 +296,6 @@ Boolean _CFIsMainThread(void) {
273296
}
274297
#endif // TARGET_OS_LINUX
275298

276-
#if !TARGET_OS_WASI
277299
CF_PRIVATE CFStringRef _CFProcessNameString(void) {
278300
static CFStringRef __CFProcessNameString = NULL;
279301
if (!__CFProcessNameString) {
@@ -292,7 +314,6 @@ CF_PRIVATE CFStringRef _CFProcessNameString(void) {
292314
}
293315
return __CFProcessNameString;
294316
}
295-
#endif // !TARGET_OS_WASI
296317

297318
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
298319

@@ -387,16 +408,20 @@ static CFURLRef _CFCopyHomeDirURLForUser(const char *username, bool fallBackToHo
387408

388409
#endif
389410

390-
#if !TARGET_OS_WASI
391411
#define CFMaxHostNameLength 256
392412
#define CFMaxHostNameSize (CFMaxHostNameLength+1)
393413

394414
CF_PRIVATE CFStringRef _CFStringCreateHostName(void) {
415+
#if TARGET_OS_WASI
416+
// WASI doesn't have a concept of a hostname
417+
return CFSTR("");
418+
#else
395419
char myName[CFMaxHostNameSize];
396420

397421
// return @"" instead of nil a la CFUserName() and Ali Ozer
398422
if (0 != gethostname(myName, CFMaxHostNameSize)) return CFSTR("");
399423
return CFStringCreateWithCString(kCFAllocatorSystemDefault, myName, kCFPlatformInterfaceStringEncoding);
424+
#endif
400425
}
401426

402427
/* These are sanitized versions of the above functions. We might want to eliminate the above ones someday.
@@ -433,6 +458,8 @@ CF_EXPORT CFStringRef CFCopyUserName(void) {
433458
result = CFStringCreateWithCString(kCFAllocatorSystemDefault, cname, kCFPlatformInterfaceStringEncoding);
434459
}
435460
}
461+
#elif TARGET_OS_WASI
462+
// WASI does not have user concept
436463
#else
437464
#error "Please add an implementation for CFCopyUserName() that copies the account username"
438465
#endif
@@ -462,6 +489,8 @@ CF_CROSS_PLATFORM_EXPORT CFStringRef CFCopyFullUserName(void) {
462489
GetUserNameExW(NameDisplay, (LPWSTR)wszBuffer, &ulLength);
463490

464491
result = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (UniChar *)wszBuffer, ulLength);
492+
#elif TARGET_OS_WASI
493+
// WASI does not have user concept
465494
#else
466495
#error "Please add an implementation for CFCopyFullUserName() that copies the full (display) user name"
467496
#endif
@@ -528,6 +557,9 @@ CFURLRef CFCopyHomeDirectoryURL(void) {
528557
if (testPath) CFRelease(testPath);
529558

530559
return retVal;
560+
#elif TARGET_OS_WASI
561+
// WASI does not have user concept
562+
return NULL;
531563
#else
532564
#error Dont know how to compute users home directories on this platform
533565
#endif
@@ -659,6 +691,9 @@ CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName) {
659691
CFAllocatorDeallocate(kCFAllocatorSystemDefault, pwszUserName);
660692

661693
return url;
694+
#elif TARGET_OS_WASI
695+
// WASI does not have user concept
696+
return NULL;
662697
#else
663698
#error Dont know how to compute users home directories on this platform
664699
#endif
@@ -667,7 +702,6 @@ CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName) {
667702

668703
#undef CFMaxHostNameLength
669704
#undef CFMaxHostNameSize
670-
#endif // !TARGET_OS_WASI
671705

672706
#if TARGET_OS_WIN32
673707
CF_INLINE CFIndex strlen_UniChar(const UniChar* p) {

CoreFoundation/Base.subproj/CFPriv.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,17 @@ CF_EXTERN_C_BEGIN
5757

5858
CF_EXPORT void _CFRuntimeSetCFMPresent(void *a);
5959

60-
#if !TARGET_OS_WASI
6160
CF_EXPORT const char *_CFProcessPath(void);
6261
CF_EXPORT const char **_CFGetProcessPath(void);
6362
CF_EXPORT const char **_CFGetProgname(void);
6463

65-
#if !TARGET_OS_WIN32
64+
#if !TARGET_OS_WIN32 && !TARGET_OS_WASI
6665
#include <sys/types.h>
6766

6867
CF_EXPORT void _CFGetUGIDs(uid_t *euid, gid_t *egid);
6968
CF_EXPORT uid_t _CFGetEUID(void);
7069
CF_EXPORT uid_t _CFGetEGID(void);
7170
#endif
72-
#endif
7371

7472
#if (TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_LINUX))
7573
CF_EXPORT void _CFRunLoopSetCurrent(CFRunLoopRef rl);
@@ -166,7 +164,6 @@ CF_EXPORT Boolean _CFStringGetFileSystemRepresentation(CFStringRef string, UInt8
166164
/* If this is publicized, we might need to create a GetBytesPtr type function as well. */
167165
CF_EXPORT CFStringRef _CFStringCreateWithBytesNoCopy(CFAllocatorRef alloc, const UInt8 *bytes, CFIndex numBytes, CFStringEncoding encoding, Boolean externalFormat, CFAllocatorRef contentsDeallocator);
168166

169-
#if !TARGET_OS_WASI
170167
/* These return NULL on MacOS 8 */
171168
// This one leaks the returned string in order to be thread-safe.
172169
// CF cannot help you in this matter if you continue to use this SPI.
@@ -178,7 +175,6 @@ CFStringRef CFCopyUserName(void);
178175

179176
CF_EXPORT
180177
CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName); /* Pass NULL for the current user's home directory */
181-
#endif
182178

183179

184180
/*

0 commit comments

Comments
 (0)