@@ -105,7 +105,6 @@ CF_PRIVATE const wchar_t *_CFDLLPath(void) {
105105}
106106#endif // TARGET_OS_WIN32
107107
108- #if !TARGET_OS_WASI
109108static const char * __CFProcessPath = NULL ;
110109static 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
256279CF_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
279301CF_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
396416CF_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
675709CF_INLINE CFIndex strlen_UniChar (const UniChar * p ) {
0 commit comments