Skip to content

Commit 663b397

Browse files
committed
More fixes for Emscripten
1 parent cdd954b commit 663b397

19 files changed

Lines changed: 82 additions & 42 deletions

Sources/Foundation/FileHandle.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ import WASILibc
3434
fileprivate let _read = WASILibc.read(_:_:_:)
3535
fileprivate let _write = WASILibc.write(_:_:_:)
3636
fileprivate let _close = WASILibc.close(_:)
37+
#elseif canImport(EmscriptenLibc)
38+
@preconcurrency import EmscriptenLibc
39+
fileprivate let _read = EmscriptenLibc.read(_:_:_:)
40+
fileprivate let _write = EmscriptenLibc.write(_:_:_:)
41+
fileprivate let _close = EmscriptenLibc.close(_:)
3742
#elseif canImport(Android)
3843
@preconcurrency import Android
3944
fileprivate let _read = Android.read(_:_:_:)
@@ -1074,7 +1079,7 @@ open class Pipe: NSObject, @unchecked Sendable {
10741079
closeOnDealloc: true)
10751080
self.fileHandleForWriting = FileHandle(handle: hWritePipe!,
10761081
closeOnDealloc: true)
1077-
#elseif os(WASI)
1082+
#elseif os(WASI) || os(Emscripten)
10781083
NSUnsupported()
10791084
#else
10801085
/// the `pipe` system call creates two `fd` in a malloc'ed area

Sources/Foundation/FileManager+POSIX.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,11 @@ extension FileManager {
156156

157157
internal func _attributesOfFileSystemIncludingBlockSize(forPath path: String) throws -> (attributes: [FileAttributeKey : Any], blockSize: UInt64?) {
158158
#if os(WASI)
159-
// WASI doesn't have statvfs
159+
// WASI does not have statvfs
160160
throw _NSErrorWithErrno(ENOTSUP, reading: true, path: path)
161+
#elseif os(Emscripten)
162+
// Emscripten does not have statvfs
163+
throw _NSErrorWithErrno(EmscriptenLibc.ENOTSUP, reading: true, path: path)
161164
#else
162165
var result: [FileAttributeKey:Any] = [:]
163166
var finalBlockSize: UInt64?
@@ -318,8 +321,10 @@ extension FileManager {
318321
var _url : URL
319322
var _options : FileManager.DirectoryEnumerationOptions
320323
var _errorHandler : ((URL, Error) -> Bool)?
324+
#if !os(Emscripten) // Emscripten doesn't have fts.h
321325
var _stream : UnsafeMutablePointer<FTS>? = nil
322326
var _current : UnsafeMutablePointer<FTSENT>? = nil
327+
#endif
323328
var _rootError : Error? = nil
324329
var _gotRoot : Bool = false
325330

@@ -330,6 +335,9 @@ extension FileManager {
330335
_options = options
331336
_errorHandler = errorHandler
332337

338+
#if os(Emscripten)
339+
_rootError = _NSErrorWithErrno(ENOSYS, reading: true, url: url)
340+
#else
333341
let fm = FileManager.default
334342
do {
335343
guard fm.fileExists(atPath: _url.path) else { throw _NSErrorWithErrno(ENOENT, reading: true, url: url) }
@@ -346,15 +354,26 @@ extension FileManager {
346354
} catch {
347355
_rootError = error
348356
}
357+
#endif
349358
}
350359

351360
deinit {
361+
#if !os(Emscripten)
352362
if let stream = _stream {
353363
fts_close(stream)
354364
}
365+
#endif
355366
}
356367

357368
override func nextObject() -> Any? {
369+
#if os(Emscripten)
370+
if let error = _rootError {
371+
if let handler = _errorHandler {
372+
let _ = handler(_url, error)
373+
}
374+
}
375+
return nil
376+
#else
358377
func match(filename: String, to options: DirectoryEnumerationOptions, isDir: Bool) -> (Bool, Bool) {
359378
var showFile = true
360379
var skipDescendants = false
@@ -427,16 +446,23 @@ extension FileManager {
427446
}
428447
}
429448
return nil
449+
#endif // !os(Emscripten)
430450
}
431451

432452
override var level: Int {
453+
#if os(Emscripten)
454+
return 0
455+
#else
433456
return Int(_current?.pointee.fts_level ?? 0)
457+
#endif
434458
}
435459

436460
override func skipDescendants() {
461+
#if !os(Emscripten)
437462
if let stream = _stream, let current = _current {
438463
fts_set(stream, current, FTS_SKIP)
439464
}
465+
#endif
440466
}
441467

442468
override var directoryAttributes : [FileAttributeKey : Any]? {

Sources/Foundation/FileManager.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import WinSDK
2121

2222
#if os(WASI)
2323
import WASILibc
24+
#elseif os(Emscripten)
25+
@preconcurrency import EmscriptenLibc
2426
#elseif canImport(Bionic)
2527
@preconcurrency import Bionic
2628
#endif

Sources/Foundation/Host.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ open class Host: NSObject {
9595
return "localhost"
9696
}
9797
return String(cString: hostname)
98-
#elseif os(WASI) // WASI does not have uname
98+
#elseif os(WASI) || os(Emscripten) // WASI/Emscripten do not have uname
9999
return "localhost"
100100
#else
101101
let hname = UnsafeMutablePointer<Int8>.allocate(capacity: Int(NI_MAXHOST))
@@ -176,7 +176,7 @@ open class Host: NSObject {
176176
}
177177
_names = [info]
178178
_resolved = true
179-
#elseif os(WASI) // WASI does not have getifaddrs
179+
#elseif os(WASI) || os(Emscripten) // WASI/Emscripten do not have getifaddrs
180180
_names = [info]
181181
_resolved = true
182182
#else
@@ -276,7 +276,7 @@ open class Host: NSObject {
276276

277277
_resolved = true
278278
}
279-
#elseif os(WASI) // WASI does not have getaddrinfo
279+
#elseif os(WASI) || os(Emscripten) // WASI/Emscripten do not have getaddrinfo
280280
if let info = _info {
281281
_names = [info]
282282
_resolved = true

Sources/Foundation/JSONSerialization.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ open class JSONSerialization : NSObject {
274274

275275
}
276276

277-
#if !os(WASI)
277+
#if !os(WASI) && !os(Emscripten)
278278
/* Write JSON data into a stream. The stream should be opened and configured. The return value is the number of bytes written to the stream, or 0 on error. All other behavior of this method is the same as the dataWithJSONObject:options:error: method.
279279
*/
280280
open class func writeJSONObject(_ obj: Any, toStream stream: OutputStream, options opt: WritingOptions) throws -> Int {

Sources/Foundation/NSArray.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
446446
return objects
447447
}
448448

449-
#if !os(WASI)
449+
#if !os(WASI) && !os(Emscripten)
450450
open func write(to url: URL) throws {
451451
let pListData = try PropertyListSerialization.data(fromPropertyList: self, format: .xml, options: 0)
452452
try pListData.write(to: url, options: .atomic)

Sources/Foundation/NSCalendar.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding {
866866

867867
}
868868

869-
#if !os(WASI)
869+
#if !os(WASI) && !os(Emscripten)
870870
// This notification is posted through [NSNotificationCenter defaultCenter]
871871
// when the system day changes. Register with "nil" as the object of this
872872
// notification. If the computer/device is asleep when the day changed,

Sources/Foundation/NSData.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,10 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
430430
}
431431

432432
let fm = FileManager.default
433-
#if os(WASI)
434-
// WASI does not have permission concept
433+
#if os(WASI) || os(Emscripten)
434+
// WASI/Emscripten do not have permission concept
435435
let permissions: Int? = nil
436-
// ReadingOptions.atomic won't be specified on WASI as it's marked unavailable
436+
// ReadingOptions.atomic won't be specified as it's marked unavailable
437437
var atomicWrite: Bool { false }
438438
#else
439439
let permissions = try? fm.attributesOfItem(atPath: path)[.posixPermissions] as? Int
@@ -475,6 +475,8 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
475475
let createMode = Int(Musl.S_IRUSR) | Int(Musl.S_IWUSR) | Int(Musl.S_IRGRP) | Int(Musl.S_IWGRP) | Int(Musl.S_IROTH) | Int(Musl.S_IWOTH)
476476
#elseif canImport(WASILibc)
477477
let createMode = Int(WASILibc.S_IRUSR) | Int(WASILibc.S_IWUSR) | Int(WASILibc.S_IRGRP) | Int(WASILibc.S_IWGRP) | Int(WASILibc.S_IROTH) | Int(WASILibc.S_IWOTH)
478+
#elseif canImport(EmscriptenLibc)
479+
let createMode = Int(EmscriptenLibc.S_IRUSR) | Int(EmscriptenLibc.S_IWUSR) | Int(EmscriptenLibc.S_IRGRP) | Int(EmscriptenLibc.S_IWGRP) | Int(EmscriptenLibc.S_IROTH) | Int(EmscriptenLibc.S_IWOTH)
478480
#elseif canImport(Android)
479481
let createMode = Int(Android.S_IRUSR) | Int(Android.S_IWUSR) | Int(Android.S_IRGRP) | Int(Android.S_IWGRP) | Int(Android.S_IROTH) | Int(Android.S_IWOTH)
480482
#endif
@@ -490,11 +492,11 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
490492

491493
/// Writes the data object's bytes to the file specified by a given path.
492494
/// NOTE: the 'atomically' flag is ignored if the url is not of a type the supports atomic writes
493-
#if os(WASI)
494-
@available(*, unavailable, message: "WASI does not support atomic file-writing as it does not have temporary directories")
495+
#if os(WASI) || os(Emscripten)
496+
@available(*, unavailable, message: "Atomic file-writing is not available on this platform")
495497
#endif
496498
open func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool {
497-
#if os(WASI)
499+
#if os(WASI) || os(Emscripten)
498500
// WASI does not support atomic file-writing as it does not have temporary directories
499501
return false
500502
#else
@@ -509,11 +511,11 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
509511

510512
/// Writes the data object's bytes to the location specified by a given URL.
511513
/// NOTE: the 'atomically' flag is ignored if the url is not of a type the supports atomic writes
512-
#if os(WASI)
513-
@available(*, unavailable, message: "WASI does not support atomic file-writing as it does not have temporary directories")
514+
#if os(WASI) || os(Emscripten)
515+
@available(*, unavailable, message: "Atomic file-writing is not available on this platform")
514516
#endif
515517
open func write(to url: URL, atomically: Bool) -> Bool {
516-
#if os(WASI)
518+
#if os(WASI) || os(Emscripten)
517519
// WASI does not support atomic file-writing as it does not have temporary directories
518520
return false
519521
#else

Sources/Foundation/NSDictionary.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
505505
return objects
506506
}
507507

508-
#if !os(WASI)
508+
#if !os(WASI) && !os(Emscripten)
509509
open func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool {
510510
return write(to: URL(fileURLWithPath: path), atomically: useAuxiliaryFile)
511511
}

Sources/Foundation/NSKeyedUnarchiver.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ open class NSKeyedUnarchiver : NSCoder {
5454

5555
private enum Stream {
5656
case data(Data)
57-
#if !os(WASI)
57+
#if !os(WASI) && !os(Emscripten)
5858
case stream(CFReadStream)
5959
#endif
6060
}
@@ -96,7 +96,7 @@ open class NSKeyedUnarchiver : NSCoder {
9696
return try? unarchiveTopLevelObjectWithData(data)
9797
}
9898

99-
#if !os(WASI)
99+
#if !os(WASI) && !os(Emscripten)
100100
@available(swift, deprecated: 9999, renamed: "unarchivedObject(ofClass:from:)")
101101
open class func unarchiveObject(withFile path: String) -> Any? {
102102
let url = URL(fileURLWithPath: path)
@@ -150,7 +150,7 @@ open class NSKeyedUnarchiver : NSCoder {
150150
switch self._stream {
151151
case .data(let data):
152152
try plist = PropertyListSerialization.propertyList(from: data, options: [], format: &format)
153-
#if !os(WASI)
153+
#if !os(WASI) && !os(Emscripten)
154154
case .stream(let readStream):
155155
try plist = PropertyListSerialization.propertyList(with: readStream, options: [], format: &format)
156156
#endif

0 commit comments

Comments
 (0)