Skip to content

Commit 8a134e8

Browse files
[wasm] Grab wasi-libc constants manually
Unfortunately they are defined in the way ClangImporter can't handle them, so we need to grab them manually through function calls. Those calls are usually inlined, so the performance impact should be minimal.
1 parent 778dde9 commit 8a134e8

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
#include <sys/stat.h>
6565
#include <sys/syscall.h>
6666
#include <termios.h>
67+
#elif TARGET_OS_WASI
68+
#include <fcntl.h>
6769
#elif TARGET_OS_LINUX
6870
#include <errno.h>
6971
#include <features.h>
@@ -712,6 +714,15 @@ typedef struct _REPARSE_DATA_BUFFER {
712714
} REPARSE_DATA_BUFFER;
713715
#endif
714716

717+
#if TARGET_OS_WASI
718+
static inline uint8_t _getConst_DT_DIR(void) { return DT_DIR; }
719+
static inline int32_t _getConst_O_CREAT(void) { return O_CREAT; }
720+
static inline int32_t _getConst_O_DIRECTORY(void) { return O_DIRECTORY; }
721+
static inline int32_t _getConst_O_EXCL(void) { return O_EXCL; }
722+
static inline int32_t _getConst_O_TRUNC(void) { return O_TRUNC; }
723+
static inline int32_t _getConst_O_WRONLY(void) { return O_WRONLY; }
724+
#endif
725+
715726
#if !TARGET_OS_WIN32
716727
typedef void * _CFPosixSpawnFileActionsRef;
717728
typedef void * _CFPosixSpawnAttrRef;

Sources/Foundation/FileManager+POSIX.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ internal func &(left: UInt32, right: mode_t) -> mode_t {
1313
}
1414
#endif
1515

16+
#if os(WASI)
17+
import WASILibc
18+
// wasi-libc defines the following constants in a way that Clang Importer can't
19+
// understand, so we need to grab them manually through ForSwiftFoundationOnly.h
20+
internal var DT_DIR: UInt8 { _getConst_DT_DIR() }
21+
internal var O_CREAT: Int32 { _getConst_O_CREAT() }
22+
internal var O_DIRECTORY: Int32 { _getConst_O_DIRECTORY() }
23+
internal var O_EXCL: Int32 { _getConst_O_EXCL() }
24+
internal var O_TRUNC: Int32 { _getConst_O_TRUNC() }
25+
internal var O_WRONLY: Int32 { _getConst_O_WRONLY() }
26+
#endif
27+
1628
@_implementationOnly import CoreFoundation
1729

1830
extension FileManager {

0 commit comments

Comments
 (0)