@@ -103,7 +103,6 @@ CF_PRIVATE const wchar_t *_CFDLLPath(void) {
103103}
104104#endif // TARGET_OS_WIN32
105105
106- #if !TARGET_OS_WASI
107106static const char * __CFProcessPath = NULL ;
108107static 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
254277CF_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
277299CF_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
394414CF_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
673707CF_INLINE CFIndex strlen_UniChar (const UniChar * p ) {
0 commit comments