Skip to content

Disable file/dispatch-related code in Foundation for WASI #3050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Foundation/CGFloat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@frozen
public struct CGFloat {
#if arch(i386) || arch(arm)
#if arch(i386) || arch(arm) || arch(wasm32)
/// The native type used to store the CGFloat, which is Float on
/// 32-bit architectures and Double on 64-bit architectures.
public typealias NativeType = Float
Expand Down Expand Up @@ -185,7 +185,7 @@ extension CGFloat : BinaryFloatingPoint {

@_transparent
public init(bitPattern: UInt) {
#if arch(i386) || arch(arm)
#if arch(i386) || arch(arm) || arch(wasm32)
native = NativeType(bitPattern: UInt32(bitPattern))
#elseif arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le)
native = NativeType(bitPattern: UInt64(bitPattern))
Expand Down
2 changes: 2 additions & 0 deletions Sources/Foundation/CharacterSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
_wrapped = _SwiftNSCharacterSet(immutableObject: NSCharacterSet(bitmapRepresentation: data))
}

#if !os(WASI)
/// Initialize with the contents of a file.
///
/// Returns `nil` if there was an error reading the file.
Expand All @@ -168,6 +169,7 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
return nil
}
}
#endif

public func hash(into hasher: inout Hasher) {
hasher.combine(_mapUnmanaged { $0 })
Expand Down
28 changes: 25 additions & 3 deletions Sources/Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@

#if DEPLOYMENT_RUNTIME_SWIFT

#if canImport(Glibc)
@usableFromInline let calloc = Glibc.calloc
@usableFromInline let malloc = Glibc.malloc
@usableFromInline let free = Glibc.free
@usableFromInline let memset = Glibc.memset
@usableFromInline let memcpy = Glibc.memcpy
@usableFromInline let memcmp = Glibc.memcmp
#elseif canImport(WASILibc)
@usableFromInline let calloc = WASILibc.calloc
@usableFromInline let malloc = WASILibc.malloc
@usableFromInline let free = WASILibc.free
@usableFromInline let memset = WASILibc.memset
@usableFromInline let memcpy = WASILibc.memcpy
@usableFromInline let memcmp = WASILibc.memcmp
#endif

#if !canImport(Darwin)
@inlinable // This is @inlinable as trivially computable.
internal func malloc_good_size(_ size: Int) -> Int {
Expand All @@ -23,6 +39,8 @@ internal func malloc_good_size(_ size: Int) -> Int {

#if canImport(Glibc)
import Glibc
#elseif canImport(WASILibc)
import WASILibc
#endif

internal func __NSDataInvokeDeallocatorUnmap(_ mem: UnsafeMutableRawPointer, _ length: Int) {
Expand Down Expand Up @@ -654,7 +672,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
@usableFromInline typealias Buffer = (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) //len //enum
@usableFromInline var bytes: Buffer
#elseif arch(i386) || arch(arm)
#elseif arch(i386) || arch(arm) || arch(wasm32)
@usableFromInline typealias Buffer = (UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8) //len //enum
@usableFromInline var bytes: Buffer
Expand Down Expand Up @@ -683,7 +701,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
assert(count <= MemoryLayout<Buffer>.size)
#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le)
bytes = (UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0))
#elseif arch(i386) || arch(arm)
#elseif arch(i386) || arch(arm) || arch(wasm32)
bytes = (UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0))
#else
#error("This architecture isn't known. Add it to the 32-bit or 64-bit line.")
Expand Down Expand Up @@ -866,7 +884,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl

#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le)
@usableFromInline internal typealias HalfInt = Int32
#elseif arch(i386) || arch(arm)
#elseif arch(i386) || arch(arm) || arch(wasm32)
@usableFromInline internal typealias HalfInt = Int16
#else
#error("This architecture isn't known. Add it to the 32-bit or 64-bit line.")
Expand Down Expand Up @@ -2012,6 +2030,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
}
}

#if !os(WASI)
/// Initialize a `Data` with the contents of a `URL`.
///
/// - parameter url: The `URL` to read.
Expand All @@ -2024,6 +2043,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
return Data(bytes: d.bytes, count: d.length)
}
}
#endif

/// Initialize a `Data` from a Base-64 encoded String using the given options.
///
Expand Down Expand Up @@ -2287,6 +2307,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
}
#endif

#if !os(WASI)
/// Write the contents of the `Data` to a location.
///
/// - parameter url: The location to write the data into.
Expand All @@ -2307,6 +2328,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
#endif
}
}
#endif

// MARK: -

Expand Down
2 changes: 2 additions & 0 deletions Sources/Foundation/JSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ open class JSONSerialization : NSObject {

}

#if !os(WASI)
/* 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.
*/
open class func writeJSONObject(_ obj: Any, toStream stream: OutputStream, options opt: WritingOptions) throws -> Int {
Expand Down Expand Up @@ -298,6 +299,7 @@ open class JSONSerialization : NSObject {
} while stream.hasBytesAvailable
return try jsonObject(with: data, options: opt)
}
#endif
}

//MARK: - Encoding Detection
Expand Down
4 changes: 4 additions & 0 deletions Sources/Foundation/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
return objects
}

#if !os(WASI)
open func write(to url: URL) throws {
let pListData = try PropertyListSerialization.data(fromPropertyList: self, format: .xml, options: 0)
try pListData.write(to: url, options: .atomic)
Expand All @@ -463,6 +464,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
return false
}
}
#endif

open func objects(at indexes: IndexSet) -> [Any] {
var objs = [Any]()
Expand Down Expand Up @@ -653,6 +655,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
return lastEqual ? result + 1 : result
}

#if !os(WASI)
public convenience init(contentsOf url: URL, error: ()) throws {
let plistDoc = try Data(contentsOf: url)
guard let plistArray = try PropertyListSerialization.propertyList(from: plistDoc, options: [], format: nil) as? Array<Any>
Expand All @@ -679,6 +682,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
return nil
}
}
#endif

open func pathsMatchingExtensions(_ filterTypes: [String]) -> [String] {
guard !filterTypes.isEmpty else {
Expand Down
2 changes: 2 additions & 0 deletions Sources/Foundation/NSCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,7 @@ internal class _NSCopyOnWriteCalendar: NSCalendar {
}
}

#if !os(WASI)
// This notification is posted through [NSNotificationCenter defaultCenter]
// when the system day changes. Register with "nil" as the object of this
// notification. If the computer/device is asleep when the day changed,
Expand All @@ -1391,6 +1392,7 @@ internal class _NSCopyOnWriteCalendar: NSCalendar {
extension NSNotification.Name {
public static let NSCalendarDayChanged = NSNotification.Name(rawValue: "NSCalendarDayChangedNotification")
}
#endif


extension NSCalendar: _SwiftBridgeable {
Expand Down
2 changes: 2 additions & 0 deletions Sources/Foundation/NSCharacterSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ open class NSCharacterSet : NSObject, NSCopying, NSMutableCopying, NSSecureCodin
_CFCharacterSetInitWithBitmapRepresentation(_cfMutableObject, data._cfObject)
}

#if !os(WASI)
public convenience init?(contentsOfFile fName: String) {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: fName))
Expand Down Expand Up @@ -329,6 +330,7 @@ open class NSCharacterSet : NSObject, NSCopying, NSMutableCopying, NSSecureCodin
aCoder.encode(true, forKey: .characterSetIsInvertedKey)
}
}
#endif

open func characterIsMember(_ aCharacter: unichar) -> Bool {
return longCharacterIsMember(UInt32(aCharacter))
Expand Down
12 changes: 12 additions & 0 deletions Sources/Foundation/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
//

@_implementationOnly import CoreFoundation
#if !os(WASI)
import Dispatch
#endif

extension NSData {
public struct ReadingOptions : OptionSet {
Expand Down Expand Up @@ -149,6 +151,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
_init(bytes: bytes, length: length, copy: false, deallocator: deallocator)
}

#if !os(WASI)
/// Initializes a data object with the contents of the file at a given path.
public init(contentsOfFile path: String, options readOptionsMask: ReadingOptions = []) throws {
super.init()
Expand All @@ -171,6 +174,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
return nil
}
}
#endif

/// Initializes a data object with the contents of another data object.
public init(data: Data) {
Expand All @@ -180,6 +184,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
}
}

#if !os(WASI)
/// Initializes a data object with the data from the location specified by a given URL.
public init(contentsOf url: URL, options readOptionsMask: ReadingOptions = []) throws {
super.init()
Expand Down Expand Up @@ -212,6 +217,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
return try _NSNonfileURLContentLoader.current.contentsOf(url: url)
}
}
#endif

/// Initializes a data object with the given Base64 encoded string.
public init?(base64Encoded base64String: String, options: Base64DecodingOptions = []) {
Expand Down Expand Up @@ -291,6 +297,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
return isEqual(to: data._swiftObject)
}

#if !os(WASI)
if let data = value as? DispatchData {
if data.count != length {
return false
Expand All @@ -300,6 +307,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
return memcmp(bytes1, bytes2, length) == 0
}
}
#endif

return false
}
Expand Down Expand Up @@ -425,6 +433,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
}
}

#if !os(WASI)
internal static func readBytesFromFileWithExtendedAttributes(_ path: String, options: ReadingOptions) throws -> NSDataReadResult {
guard let handle = FileHandle(path: path, flags: O_RDONLY, createMode: 0) else {
throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno), userInfo: nil)
Expand Down Expand Up @@ -528,6 +537,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
}
try write(toFile: url.path, options: writeOptionsMask)
}
#endif

// MARK: - Bytes
/// Copies a number of bytes from the start of the data object into a given buffer.
Expand Down Expand Up @@ -994,6 +1004,7 @@ open class NSMutableData : NSData {
super.init(data: data)
}

#if !os(WASI)
public override init?(contentsOfFile path: String) {
super.init(contentsOfFile: path)
}
Expand All @@ -1009,6 +1020,7 @@ open class NSMutableData : NSData {
public override init(contentsOf url: URL, options: NSData.ReadingOptions = []) throws {
try super.init(contentsOf: url, options: options)
}
#endif

public override init?(base64Encoded base64Data: Data, options: NSData.Base64DecodingOptions = []) {
super.init(base64Encoded: base64Data, options: options)
Expand Down
13 changes: 13 additions & 0 deletions Sources/Foundation/NSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@


@_implementationOnly import CoreFoundation

#if !os(WASI)
import Dispatch
#endif

open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCoding, ExpressibleByDictionaryLiteral {
private let _cfinfo = _CFInfo(typeID: CFDictionaryGetTypeID())
Expand Down Expand Up @@ -48,6 +51,7 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
return NSGeneratorEnumerator(_storage.keys.map { __SwiftValue.fetch(nonOptional: $0) }.makeIterator())
}

#if !os(WASI)
@available(*, deprecated)
public convenience init?(contentsOfFile path: String) {
self.init(contentsOf: URL(fileURLWithPath: path))
Expand All @@ -64,6 +68,7 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
return nil
}
}
#endif

public override convenience init() {
self.init(objects: [], forKeys: [], count: 0)
Expand Down Expand Up @@ -494,6 +499,7 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
return objects
}

#if !os(WASI)
open func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool {
return write(to: URL(fileURLWithPath: path), atomically: useAuxiliaryFile)
}
Expand All @@ -508,6 +514,7 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
return false
}
}
#endif

open func enumerateKeysAndObjects(_ block: (Any, Any, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
enumerateKeysAndObjects(options: [], using: block)
Expand Down Expand Up @@ -538,13 +545,19 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
}
}

#if !os(WASI)
if opts.contains(.concurrent) {
DispatchQueue.concurrentPerform(iterations: count, execute: iteration)
} else {
for idx in 0..<count {
iteration(idx)
}
}
#else
for idx in 0..<count {
iteration(idx)
}
#endif
}
}

Expand Down
8 changes: 8 additions & 0 deletions Sources/Foundation/NSIndexSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//

#if !os(WASI)
import Dispatch
#endif

/* Class for managing set of indexes. The set of valid indexes are 0 .. NSNotFound - 1; trying to use indexes outside this range is an error. NSIndexSet uses NSNotFound as a return value in cases where the queried index doesn't exist in the set; for instance, when you ask firstIndex and there are no indexes; or when you ask for indexGreaterThanIndex: on the last index, and so on.

Expand Down Expand Up @@ -504,13 +506,19 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
}
}
}
#if !os(WASI)
if opts.contains(.concurrent) {
DispatchQueue.concurrentPerform(iterations: Int(rangeSequence.count), execute: iteration)
} else {
for idx in 0..<Int(rangeSequence.count) {
iteration(idx)
}
}
#else
for idx in 0..<Int(rangeSequence.count) {
iteration(idx)
}
#endif
}

return result
Expand Down
Loading