Skip to content

Commit e9b0b82

Browse files
committed
Enable String, URL and XML-related types for WASI
1 parent 08f2025 commit e9b0b82

11 files changed

+125
-22
lines changed

Sources/Foundation/NSSet.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ open class NSSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCodi
118118
// The encoding of a NSSet is identical to the encoding of an NSArray of its contents
119119
self.allObjects._nsObject.encode(with: aCoder)
120120
}
121-
121+
122122
open override func copy() -> Any {
123123
return copy(with: nil)
124124
}
@@ -398,11 +398,11 @@ open class NSMutableSet : NSSet {
398398
public required init(capacity numItems: Int) {
399399
super.init(objects: [], count: 0)
400400
}
401-
401+
402402
public required convenience init?(coder aDecoder: NSCoder) {
403403
self.init(array: NSSet._objects(from: aDecoder))
404404
}
405-
405+
406406
open func addObjects(from array: [Any]) {
407407
if type(of: self) === NSMutableSet.self {
408408
for case let obj in array {
@@ -546,7 +546,7 @@ open class NSCountedSet : NSMutableSet {
546546
}
547547
}
548548
}
549-
549+
550550
open override func encode(with coder: NSCoder) {
551551
func fail(_ message: String) {
552552
coder.failWithError(NSError(domain: NSCocoaErrorDomain, code: NSCoderReadCorruptError, userInfo: [NSLocalizedDescriptionKey: message]))

Sources/Foundation/NSString.swift

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extension unichar {
1818
}
1919
}
2020

21+
#if !os(WASI)
2122
/// Returns a localized string, using the main bundle if one is not specified.
2223
public
2324
func NSLocalizedString(_ key: String,
@@ -27,6 +28,7 @@ func NSLocalizedString(_ key: String,
2728
comment: String) -> String {
2829
return bundle.localizedString(forKey: key, value: value, table: tableName)
2930
}
31+
#endif
3032

3133
internal let kCFStringEncodingMacRoman = CFStringBuiltInEncodings.macRoman.rawValue
3234
internal let kCFStringEncodingWindowsLatin1 = CFStringBuiltInEncodings.windowsLatin1.rawValue
@@ -237,7 +239,7 @@ open class NSString : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSC
237239
internal init(_ string: String) {
238240
_storage = string
239241
}
240-
242+
241243
public convenience required init?(coder aDecoder: NSCoder) {
242244
guard aDecoder.allowsKeyedCoding else {
243245
preconditionFailure("Unkeyed coding is unsupported.")
@@ -292,11 +294,11 @@ open class NSString : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSC
292294
characters.deallocate()
293295
return result
294296
}
295-
297+
296298
public static var supportsSecureCoding: Bool {
297299
return true
298300
}
299-
301+
300302
open func encode(with aCoder: NSCoder) {
301303
if let aKeyedCoder = aCoder as? NSKeyedArchiver {
302304
aKeyedCoder._encodePropertyList(self, forKey: "NS.string")
@@ -1268,6 +1270,7 @@ extension NSString {
12681270
data = mData
12691271
}
12701272

1273+
#if !os(WASI)
12711274
internal func _writeTo(_ url: URL, _ useAuxiliaryFile: Bool, _ enc: UInt) throws {
12721275
var data = Data()
12731276
try _getExternalRepresentation(&data, url, enc)
@@ -1281,6 +1284,7 @@ extension NSString {
12811284
open func write(toFile path: String, atomically useAuxiliaryFile: Bool, encoding enc: UInt) throws {
12821285
try _writeTo(URL(fileURLWithPath: path), useAuxiliaryFile, enc)
12831286
}
1287+
#endif
12841288

12851289
public convenience init(charactersNoCopy characters: UnsafeMutablePointer<unichar>, length: Int, freeWhenDone freeBuffer: Bool) /* "NoCopy" is a hint */ {
12861290
// ignore the no-copy-ness
@@ -1365,6 +1369,7 @@ extension NSString {
13651369
}
13661370
}
13671371

1372+
#if !os(WASI)
13681373
public convenience init(contentsOf url: URL, encoding enc: UInt) throws {
13691374
let readResult = try NSData(contentsOf: url, options: [])
13701375

@@ -1452,6 +1457,7 @@ extension NSString {
14521457
public convenience init(contentsOfFile path: String, usedEncoding enc: UnsafeMutablePointer<UInt>?) throws {
14531458
try self.init(contentsOf: URL(fileURLWithPath: path), usedEncoding: enc)
14541459
}
1460+
#endif
14551461
}
14561462

14571463
extension NSString : ExpressibleByStringLiteral { }

Sources/Foundation/NSSwiftRuntime.swift

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
@_exported import Darwin
1717
#elseif os(Linux) || os(Android) || CYGWIN || os(OpenBSD)
1818
@_exported import Glibc
19+
#elseif os(WASI)
20+
@_exported import WASILibc
1921
#elseif os(Windows)
2022
@_exported import CRT
2123
#endif
2224

25+
#if !os(WASI)
2326
@_exported import Dispatch
27+
#endif
2428

2529
#if os(Windows)
2630
import WinSDK
@@ -178,7 +182,10 @@ internal func __CFInitializeSwift() {
178182
_CFRuntimeBridgeTypeToClass(CFLocaleGetTypeID(), unsafeBitCast(NSLocale.self, to: UnsafeRawPointer.self))
179183
_CFRuntimeBridgeTypeToClass(CFTimeZoneGetTypeID(), unsafeBitCast(NSTimeZone.self, to: UnsafeRawPointer.self))
180184
_CFRuntimeBridgeTypeToClass(CFCharacterSetGetTypeID(), unsafeBitCast(_NSCFCharacterSet.self, to: UnsafeRawPointer.self))
185+
186+
#if !os(WASI)
181187
_CFRuntimeBridgeTypeToClass(_CFKeyedArchiverUIDGetTypeID(), unsafeBitCast(_NSKeyedArchiverUID.self, to: UnsafeRawPointer.self))
188+
#endif
182189

183190
// _CFRuntimeBridgeTypeToClass(CFErrorGetTypeID(), unsafeBitCast(NSError.self, UnsafeRawPointer.self))
184191
_CFRuntimeBridgeTypeToClass(CFAttributedStringGetTypeID(), unsafeBitCast(NSMutableAttributedString.self, to: UnsafeRawPointer.self))
@@ -257,8 +264,10 @@ internal func __CFInitializeSwift() {
257264
__CFSwiftBridge.NSMutableString.appendString = _CFSwiftStringAppend
258265
__CFSwiftBridge.NSMutableString.appendCharacters = _CFSwiftStringAppendCharacters
259266
__CFSwiftBridge.NSMutableString._cfAppendCString = _CFSwiftStringAppendCString
260-
267+
268+
#if !os(WASI)
261269
__CFSwiftBridge.NSRunLoop._new = _NSRunLoopNew
270+
#endif
262271

263272
__CFSwiftBridge.NSCharacterSet._expandedCFCharacterSet = _CFSwiftCharacterSetExpandedCFCharacterSet
264273
__CFSwiftBridge.NSCharacterSet._retainedBitmapRepresentation = _CFSwiftCharacterSetRetainedBitmapRepresentation
@@ -304,6 +313,7 @@ internal func __CFInitializeSwift() {
304313

305314
// __CFDefaultEightBitStringEncoding = UInt32(kCFStringEncodingUTF8)
306315

316+
#if !os(WASI)
307317
__CFSwiftBridge.NSURL.copyResourcePropertyForKey = _CFSwiftURLCopyResourcePropertyForKey
308318
__CFSwiftBridge.NSURL.copyResourcePropertiesForKeys = _CFSwiftURLCopyResourcePropertiesForKeys
309319
__CFSwiftBridge.NSURL.setResourcePropertyForKey = _CFSwiftURLSetResourcePropertyForKey
@@ -312,6 +322,7 @@ internal func __CFInitializeSwift() {
312322
__CFSwiftBridge.NSURL.clearResourcePropertyCache = _CFSwiftURLClearResourcePropertyCache
313323
__CFSwiftBridge.NSURL.setTemporaryResourceValueForKey = _CFSwiftSetTemporaryResourceValueForKey
314324
__CFSwiftBridge.NSURL.resourceIsReachable = _CFSwiftURLResourceIsReachable
325+
#endif
315326
}
316327

317328
public func === (lhs: AnyClass, rhs: AnyClass) -> Bool {

Sources/Foundation/NSTextCheckingResult.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ open class NSTextCheckingResult: NSObject, NSCopying, NSSecureCoding {
5353
open class var supportsSecureCoding: Bool {
5454
NSRequiresConcreteImplementation()
5555
}
56-
56+
5757
open override func copy() -> Any {
5858
return copy(with: nil)
5959
}
@@ -116,6 +116,7 @@ internal class NSRegularExpressionCheckingResult: NSTextCheckingResult {
116116
super.init()
117117
}
118118

119+
#if !os(WASI)
119120
public convenience required init?(coder aDecoder: NSCoder) {
120121
guard aDecoder.allowsKeyedCoding else {
121122
fatalError("Decoding this class requires keyed coding")
@@ -141,6 +142,7 @@ internal class NSRegularExpressionCheckingResult: NSTextCheckingResult {
141142
}
142143

143144
override class var supportsSecureCoding: Bool { return true }
145+
#endif
144146

145147
override var resultType: NSTextCheckingResult.CheckingType { return .regularExpression }
146148

Sources/Foundation/NSTimeZone.swift

+7-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ open class NSTimeZone : NSObject, NSCopying, NSSecureCoding, NSCoding {
3939
return nil
4040
}
4141
}
42-
42+
4343
public convenience required init?(coder aDecoder: NSCoder) {
4444
guard aDecoder.allowsKeyedCoding else {
4545
preconditionFailure("Unkeyed coding is unsupported.")
@@ -53,7 +53,7 @@ open class NSTimeZone : NSObject, NSCopying, NSSecureCoding, NSCoding {
5353

5454
self.init(name: String._unconditionallyBridgeFromObjectiveC(name), data: data?._swiftObject)
5555
}
56-
56+
5757
open override var hash: Int {
5858
return Int(bitPattern: CFHash(_cfObject))
5959
}
@@ -112,7 +112,7 @@ open class NSTimeZone : NSObject, NSCopying, NSSecureCoding, NSCoding {
112112
// Darwin versions of this method can and will encode mutable data, however it is not required for compatibility
113113
aCoder.encode(self.data._bridgeToObjectiveC(), forKey:"NS.data")
114114
}
115-
115+
116116
public static var supportsSecureCoding: Bool {
117117
return true
118118
}
@@ -286,17 +286,19 @@ extension NSTimeZone {
286286

287287
}
288288

289+
#if !os(WASI)
289290
extension NSNotification.Name {
290291
public static let NSSystemTimeZoneDidChange = NSNotification.Name(rawValue: kCFTimeZoneSystemTimeZoneDidChangeNotification._swiftObject)
291292
}
293+
#endif
292294

293295
internal class __NSLocalTimeZone: NSTimeZone {
294296
static var shared = __NSLocalTimeZone()
295297

296298
private init() {
297299
super.init(_name: "GMT+0000")
298300
}
299-
301+
300302
public convenience required init?(coder aDecoder: NSCoder) {
301303
// We do not encode details of the local time zone, merely the placeholder object.
302304
self.init()
@@ -305,7 +307,7 @@ internal class __NSLocalTimeZone: NSTimeZone {
305307
override func encode(with aCoder: NSCoder) {
306308
// We do not encode details of the local time zone, merely the placeholder object.
307309
}
308-
310+
309311
private var system: NSTimeZone {
310312
return NSTimeZone.system._nsObject
311313
}

0 commit comments

Comments
 (0)