Skip to content

Commit 44ee4ed

Browse files
Merge pull request swiftlang#4891 from kateinoigakukun/pr-b292e9c5b58de43b6a3abcc49f4aabdc22e5b14c
[wasm] Port ProcessInfo for WASI platform
2 parents 106cf08 + 095b12f commit 44ee4ed

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

+40-6
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ CF_PRIVATE const wchar_t *_CFDLLPath(void) {
105105
}
106106
#endif // TARGET_OS_WIN32
107107

108-
#if !TARGET_OS_WASI
109108
static const char *__CFProcessPath = NULL;
110109
static const char *__CFprogname = NULL;
111110

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

@@ -250,7 +274,6 @@ const char *_CFProcessPath(void) {
250274
return __CFProcessPath;
251275
#endif
252276
}
253-
#endif // TARGET_OS_WASI
254277

255278
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_BSD
256279
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void) {
@@ -275,7 +298,6 @@ Boolean _CFIsMainThread(void) {
275298
}
276299
#endif // TARGET_OS_LINUX
277300

278-
#if !TARGET_OS_WASI
279301
CF_PRIVATE CFStringRef _CFProcessNameString(void) {
280302
static CFStringRef __CFProcessNameString = NULL;
281303
if (!__CFProcessNameString) {
@@ -294,7 +316,6 @@ CF_PRIVATE CFStringRef _CFProcessNameString(void) {
294316
}
295317
return __CFProcessNameString;
296318
}
297-
#endif // !TARGET_OS_WASI
298319

299320
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
300321

@@ -389,16 +410,20 @@ static CFURLRef _CFCopyHomeDirURLForUser(const char *username, bool fallBackToHo
389410

390411
#endif
391412

392-
#if !TARGET_OS_WASI
393413
#define CFMaxHostNameLength 256
394414
#define CFMaxHostNameSize (CFMaxHostNameLength+1)
395415

396416
CF_PRIVATE CFStringRef _CFStringCreateHostName(void) {
417+
#if TARGET_OS_WASI
418+
// WASI doesn't have a concept of a hostname
419+
return CFSTR("");
420+
#else
397421
char myName[CFMaxHostNameSize];
398422

399423
// return @"" instead of nil a la CFUserName() and Ali Ozer
400424
if (0 != gethostname(myName, CFMaxHostNameSize)) return CFSTR("");
401425
return CFStringCreateWithCString(kCFAllocatorSystemDefault, myName, kCFPlatformInterfaceStringEncoding);
426+
#endif
402427
}
403428

404429
/* These are sanitized versions of the above functions. We might want to eliminate the above ones someday.
@@ -435,6 +460,8 @@ CF_EXPORT CFStringRef CFCopyUserName(void) {
435460
result = CFStringCreateWithCString(kCFAllocatorSystemDefault, cname, kCFPlatformInterfaceStringEncoding);
436461
}
437462
}
463+
#elif TARGET_OS_WASI
464+
// WASI does not have user concept
438465
#else
439466
#error "Please add an implementation for CFCopyUserName() that copies the account username"
440467
#endif
@@ -464,6 +491,8 @@ CF_CROSS_PLATFORM_EXPORT CFStringRef CFCopyFullUserName(void) {
464491
GetUserNameExW(NameDisplay, (LPWSTR)wszBuffer, &ulLength);
465492

466493
result = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (UniChar *)wszBuffer, ulLength);
494+
#elif TARGET_OS_WASI
495+
// WASI does not have user concept
467496
#else
468497
#error "Please add an implementation for CFCopyFullUserName() that copies the full (display) user name"
469498
#endif
@@ -530,6 +559,9 @@ CFURLRef CFCopyHomeDirectoryURL(void) {
530559
if (testPath) CFRelease(testPath);
531560

532561
return retVal;
562+
#elif TARGET_OS_WASI
563+
// WASI does not have user concept
564+
return NULL;
533565
#else
534566
#error Dont know how to compute users home directories on this platform
535567
#endif
@@ -661,6 +693,9 @@ CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName) {
661693
CFAllocatorDeallocate(kCFAllocatorSystemDefault, pwszUserName);
662694

663695
return url;
696+
#elif TARGET_OS_WASI
697+
// WASI does not have user concept
698+
return NULL;
664699
#else
665700
#error Dont know how to compute users home directories on this platform
666701
#endif
@@ -669,7 +704,6 @@ CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName) {
669704

670705
#undef CFMaxHostNameLength
671706
#undef CFMaxHostNameSize
672-
#endif // !TARGET_OS_WASI
673707

674708
#if TARGET_OS_WIN32
675709
CF_INLINE CFIndex strlen_UniChar(const UniChar* p) {

CoreFoundation/Base.subproj/CFPriv.h

+1-5
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)