Skip to content

Commit 90c1832

Browse files
[wasm] Import sys/mman.h through CoreFoundation
Importing `wasi_emulated_mman` in Swift code requires the client to define `_WASI_EMULATED_MMAN`. `@_implementationOnly import` was the only way to work around this, but it's now declared as unsafe if it's used in non-resilient moules (even though it's actually safe as long as the module does not use type layout of the imported module). The new scoped import feature always requires the transitive clients to load privately imported modules, so it's not a good fit for this tricky case where a special macro must be defined before importing the module. This patch imports `sys/mman.h` through CoreFoundation while defining `_WASI_EMULATED_MMAN` in the header exposed by modulemap. This way, the clients don't need to define it.
1 parent 514e2bd commit 90c1832

File tree

3 files changed

+5
-6
lines changed

3 files changed

+5
-6
lines changed

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

+5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
#include <termios.h>
6767
#elif TARGET_OS_WASI
6868
#include <fcntl.h>
69+
// Define _WASI_EMULATED_MMAN here to use the emulated mman functions in
70+
// Foundation-side without requiring transitive clients to define it.
71+
#undef _WASI_EMULATED_MMAN
72+
#define _WASI_EMULATED_MMAN
73+
#include <sys/mman.h>
6974
#elif TARGET_OS_LINUX
7075
#include <errno.h>
7176
#include <features.h>

Sources/Foundation/Data.swift

-5
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@
3636
@usableFromInline let memcpy = Musl.memcpy
3737
@usableFromInline let memcmp = Musl.memcmp
3838
#elseif canImport(WASILibc)
39-
#if swift(>=6.0)
40-
private import wasi_emulated_mman
41-
#else
42-
import wasi_emulated_mman
43-
#endif
4439
@usableFromInline let calloc = WASILibc.calloc
4540
@usableFromInline let malloc = WASILibc.malloc
4641
@usableFromInline let free = WASILibc.free

Sources/Foundation/FileHandle.swift

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ fileprivate let _write = Musl.write(_:_:_:)
2929
fileprivate let _close = Musl.close(_:)
3030
#elseif canImport(WASILibc)
3131
import WASILibc
32-
@_implementationOnly import wasi_emulated_mman
3332
fileprivate let _read = WASILibc.read(_:_:_:)
3433
fileprivate let _write = WASILibc.write(_:_:_:)
3534
fileprivate let _close = WASILibc.close(_:)

0 commit comments

Comments
 (0)