Skip to content

Commit 47d8ff4

Browse files
committed
Enable String, URL and XML-related types for WASI
fix
1 parent e2219b9 commit 47d8ff4

8 files changed

+109
-25
lines changed

Sources/Foundation/NSString.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ func NSLocalizedString(_ key: String,
2525
bundle: Bundle = Bundle.main,
2626
value: String = "",
2727
comment: String) -> String {
28+
#if os(WASI)
29+
return key
30+
#else
2831
return bundle.localizedString(forKey: key, value: value, table: tableName)
32+
#endif
2933
}
3034

3135
internal let kCFStringEncodingMacRoman = CFStringBuiltInEncodings.macRoman.rawValue
@@ -237,7 +241,7 @@ open class NSString : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSC
237241
internal init(_ string: String) {
238242
_storage = string
239243
}
240-
244+
241245
public convenience required init?(coder aDecoder: NSCoder) {
242246
guard aDecoder.allowsKeyedCoding else {
243247
preconditionFailure("Unkeyed coding is unsupported.")
@@ -292,11 +296,11 @@ open class NSString : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSC
292296
characters.deallocate()
293297
return result
294298
}
295-
299+
296300
public static var supportsSecureCoding: Bool {
297301
return true
298302
}
299-
303+
300304
open func encode(with aCoder: NSCoder) {
301305
if let aKeyedCoder = aCoder as? NSKeyedArchiver {
302306
aKeyedCoder._encodePropertyList(self, forKey: "NS.string")
@@ -1267,7 +1271,7 @@ extension NSString {
12671271
}
12681272
data = mData
12691273
}
1270-
1274+
12711275
internal func _writeTo(_ url: URL, _ useAuxiliaryFile: Bool, _ enc: UInt) throws {
12721276
var data = Data()
12731277
try _getExternalRepresentation(&data, url, enc)

Sources/Foundation/NSSwiftRuntime.swift

Lines changed: 9 additions & 1 deletion
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
@@ -257,8 +261,10 @@ internal func __CFInitializeSwift() {
257261
__CFSwiftBridge.NSMutableString.appendString = _CFSwiftStringAppend
258262
__CFSwiftBridge.NSMutableString.appendCharacters = _CFSwiftStringAppendCharacters
259263
__CFSwiftBridge.NSMutableString._cfAppendCString = _CFSwiftStringAppendCString
260-
264+
265+
#if !os(WASI)
261266
__CFSwiftBridge.NSRunLoop._new = _NSRunLoopNew
267+
#endif
262268

263269
__CFSwiftBridge.NSCharacterSet._expandedCFCharacterSet = _CFSwiftCharacterSetExpandedCFCharacterSet
264270
__CFSwiftBridge.NSCharacterSet._retainedBitmapRepresentation = _CFSwiftCharacterSetRetainedBitmapRepresentation
@@ -304,6 +310,7 @@ internal func __CFInitializeSwift() {
304310

305311
// __CFDefaultEightBitStringEncoding = UInt32(kCFStringEncodingUTF8)
306312

313+
#if !os(WASI)
307314
__CFSwiftBridge.NSURL.copyResourcePropertyForKey = _CFSwiftURLCopyResourcePropertyForKey
308315
__CFSwiftBridge.NSURL.copyResourcePropertiesForKeys = _CFSwiftURLCopyResourcePropertiesForKeys
309316
__CFSwiftBridge.NSURL.setResourcePropertyForKey = _CFSwiftURLSetResourcePropertyForKey
@@ -312,6 +319,7 @@ internal func __CFInitializeSwift() {
312319
__CFSwiftBridge.NSURL.clearResourcePropertyCache = _CFSwiftURLClearResourcePropertyCache
313320
__CFSwiftBridge.NSURL.setTemporaryResourceValueForKey = _CFSwiftSetTemporaryResourceValueForKey
314321
__CFSwiftBridge.NSURL.resourceIsReachable = _CFSwiftURLResourceIsReachable
322+
#endif
315323
}
316324

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

Sources/Foundation/NSTextCheckingResult.swift

Lines changed: 2 additions & 2 deletions
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
}
@@ -115,7 +115,7 @@ internal class NSRegularExpressionCheckingResult: NSTextCheckingResult {
115115
_rangeArray = nil
116116
super.init()
117117
}
118-
118+
119119
public convenience required init?(coder aDecoder: NSCoder) {
120120
guard aDecoder.allowsKeyedCoding else {
121121
fatalError("Decoding this class requires keyed coding")

Sources/Foundation/NSTimeZone.swift

Lines changed: 5 additions & 5 deletions
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
}
@@ -296,7 +296,7 @@ internal class __NSLocalTimeZone: NSTimeZone {
296296
private init() {
297297
super.init(_name: "GMT+0000")
298298
}
299-
299+
300300
public convenience required init?(coder aDecoder: NSCoder) {
301301
// We do not encode details of the local time zone, merely the placeholder object.
302302
self.init()
@@ -305,7 +305,7 @@ internal class __NSLocalTimeZone: NSTimeZone {
305305
override func encode(with aCoder: NSCoder) {
306306
// We do not encode details of the local time zone, merely the placeholder object.
307307
}
308-
308+
309309
private var system: NSTimeZone {
310310
return NSTimeZone.system._nsObject
311311
}

Sources/Foundation/NSURL.swift

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,13 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
153153
open func copy(with zone: NSZone? = nil) -> Any {
154154
if isFileURL {
155155
let newURL = CFURLCreateWithString(kCFAllocatorSystemDefault, relativeString._cfObject, self.baseURL?._cfObject)!
156+
157+
#if !os(WASI)
156158
if let storage = _resourceStorageIfPresent {
157159
let newStorage = URLResourceValuesStorage(copying: storage)
158160
_CFURLSetResourceInfo(newURL, newStorage)
159161
}
162+
#endif
160163
return newURL._nsObject
161164
} else {
162165
return self
@@ -186,7 +189,7 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
186189
aCoder.encode(self.baseURL?._nsObject, forKey:"NS.base")
187190
aCoder.encode(self.relativeString._bridgeToObjectiveC(), forKey:"NS.relative")
188191
}
189-
192+
190193
public init(fileURLWithPath path: String, isDirectory isDir: Bool, relativeTo baseURL: URL?) {
191194
super.init()
192195

@@ -198,14 +201,15 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
198201
_CFURLInitWithFileSystemPathRelativeToBase(_cfObject, baseURL.path._cfObject, kCFURLPlatformPathStyle, baseURL.hasDirectoryPath, nil)
199202
}
200203
}
201-
204+
202205
public convenience init(fileURLWithPath path: String, relativeTo baseURL: URL?) {
203206
let thePath = _standardizedPath(path)
204207

205208
var isDir: ObjCBool = false
206209
if validPathSeps.contains(where: { thePath.hasSuffix(String($0)) }) {
207210
isDir = true
208211
} else {
212+
#if !os(WASI)
209213
let absolutePath: String
210214
if let absPath = baseURL?.appendingPathComponent(path).path {
211215
absolutePath = absPath
@@ -214,6 +218,7 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
214218
}
215219

216220
let _ = FileManager.default.fileExists(atPath: absolutePath, isDirectory: &isDir)
221+
#endif
217222
}
218223

219224
self.init(fileURLWithPath: thePath, isDirectory: isDir.boolValue, relativeTo: baseURL)
@@ -230,9 +235,11 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
230235
if validPathSeps.contains(where: { thePath.hasSuffix(String($0)) }) {
231236
isDir = true
232237
} else {
238+
#if !os(WASI)
233239
if !FileManager.default.fileExists(atPath: path, isDirectory: &isDir) {
234240
isDir = false
235241
}
242+
#endif
236243
}
237244
super.init()
238245
_CFURLInitWithFileSystemPathRelativeToBase(_cfObject, thePath._cfObject, kCFURLPlatformPathStyle, isDir.boolValue, nil)
@@ -243,7 +250,7 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
243250
let pathString = String(cString: path)
244251
self.init(fileURLWithPath: pathString, isDirectory: isDir, relativeTo: baseURL)
245252
}
246-
253+
247254
public convenience init?(string URLString: String) {
248255
self.init(string: URLString, relativeTo:nil)
249256
}
@@ -542,6 +549,9 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
542549
// TODO: should be `checkResourceIsReachableAndReturnError` with autoreleased error parameter.
543550
// Currently Autoreleased pointers is not supported on Linux.
544551
open func checkResourceIsReachable() throws -> Bool {
552+
#if os(WASI)
553+
return false
554+
#else
545555
guard isFileURL,
546556
let path = path else {
547557
throw NSError(domain: NSCocoaErrorDomain,
@@ -557,6 +567,7 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
557567
}
558568

559569
return true
570+
#endif
560571
}
561572

562573
/* Returns a file path URL that refers to the same resource as a specified URL. File path URLs use a file system style path. An error will occur if the url parameter is not a file URL. A file reference URL's resource must exist and be reachable to be converted to a file path URL. Symbol is present in iOS 4, but performs no operation.
@@ -760,7 +771,7 @@ extension NSString {
760771
}
761772

762773
extension NSURL {
763-
774+
764775
/* The following methods work on the path portion of a URL in the same manner that the NSPathUtilities methods on NSString do.
765776
*/
766777
open class func fileURL(withPathComponents components: [String]) -> URL? {
@@ -841,6 +852,9 @@ extension NSURL {
841852

842853
open func appendingPathComponent(_ pathComponent: String) -> URL? {
843854
var result : URL? = appendingPathComponent(pathComponent, isDirectory: false)
855+
856+
// File URLs can't be handled on WASI without file system access
857+
#if !os(WASI)
844858
// Since we are appending to a URL, path seperators should
845859
// always be '/', even if we're on Windows
846860
if !pathComponent.hasSuffix("/") && isFileURL {
@@ -852,6 +866,7 @@ extension NSURL {
852866
}
853867

854868
}
869+
#endif
855870
return result
856871
}
857872

@@ -870,15 +885,15 @@ extension NSURL {
870885
open var deletingPathExtension: URL? {
871886
return CFURLCreateCopyDeletingPathExtension(kCFAllocatorSystemDefault, _cfObject)?._swiftObject
872887
}
873-
888+
874889
/* The following methods work only on `file:` scheme URLs; for non-`file:` scheme URLs, these methods return the URL unchanged.
875890
*/
876891
open var standardizingPath: URL? {
877892
// Documentation says it should expand initial tilde, but it does't do this on OS X.
878893
// In remaining cases it works just like URLByResolvingSymlinksInPath.
879894
return _resolveSymlinksInPath(excludeSystemDirs: true, preserveDirectoryFlag: true)
880895
}
881-
896+
882897
open var resolvingSymlinksInPath: URL? {
883898
return _resolveSymlinksInPath(excludeSystemDirs: true)
884899
}
@@ -896,8 +911,12 @@ extension NSURL {
896911
if selfPath.isAbsolutePath {
897912
absolutePath = selfPath
898913
} else {
914+
#if os(WASI)
915+
return nil
916+
#else
899917
let workingDir = FileManager.default.currentDirectoryPath
900918
absolutePath = workingDir._bridgeToObjectiveC().appendingPathComponent(selfPath)
919+
#endif
901920
}
902921

903922

@@ -918,15 +937,20 @@ extension NSURL {
918937

919938
default:
920939
resolvedPath = resolvedPath._bridgeToObjectiveC().appendingPathComponent(component)
940+
#if !os(WASI)
921941
if let destination = FileManager.default._tryToResolveTrailingSymlinkInPath(resolvedPath) {
922942
resolvedPath = destination
923943
}
944+
#endif
924945
}
925946
}
926947

927948
// It might be a responsibility of NSURL(fileURLWithPath:). Check it.
928949
var isExistingDirectory: ObjCBool = false
950+
951+
#if !os(WASI)
929952
let _ = FileManager.default.fileExists(atPath: resolvedPath, isDirectory: &isExistingDirectory)
953+
#endif
930954

931955
if excludeSystemDirs {
932956
resolvedPath = resolvedPath._tryToRemovePathPrefix("/private") ?? resolvedPath
@@ -1005,6 +1029,7 @@ extension NSURL : _StructTypeBridgeable {
10051029

10061030
// -----
10071031

1032+
#if !os(WASI)
10081033
internal func _CFSwiftURLCopyResourcePropertyForKey(_ url: CFTypeRef, _ key: CFString, _ valuePointer: UnsafeMutablePointer<Unmanaged<CFTypeRef>?>?, _ errorPointer: UnsafeMutablePointer<Unmanaged<CFError>?>?) -> _DarwinCompatibleBoolean {
10091034
do {
10101035
let key = URLResourceKey(rawValue: key._swiftObject)
@@ -1538,6 +1563,7 @@ fileprivate extension URLResourceValuesStorage {
15381563
}
15391564
}
15401565
}
1566+
#endif
15411567

15421568
// -----
15431569

Sources/Foundation/PropertyListSerialization.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ open class PropertyListSerialization : NSObject {
7474
}
7575
}
7676

77+
#if !os(WASI)
7778
internal class func propertyList(with stream: CFReadStream, options opt: ReadOptions, format: UnsafeMutablePointer <PropertyListFormat>?) throws -> Any {
7879
var fmt = kCFPropertyListBinaryFormat_v1_0
7980
var error: Unmanaged<CFError>? = nil
@@ -93,4 +94,5 @@ open class PropertyListSerialization : NSObject {
9394
open class func propertyList(with stream: InputStream, options opt: ReadOptions = [], format: UnsafeMutablePointer<PropertyListFormat>?) throws -> Any {
9495
return try propertyList(with: stream._stream, options: opt, format: format)
9596
}
97+
#endif
9698
}

Sources/Foundation/URL.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ public struct URL : ReferenceConvertible, Equatable {
504504
guard !string.isEmpty, let inner = NSURL(string: string, relativeTo: url) else { return nil }
505505
_url = URL._converted(from: inner)
506506
}
507-
507+
508508
/// Initializes a newly created file URL referencing the local file or directory at path, relative to a base URL.
509509
///
510510
/// If an empty string is used for the path, then the path is assumed to be ".".
@@ -548,10 +548,12 @@ public struct URL : ReferenceConvertible, Equatable {
548548
}
549549
}
550550

551+
#if !os(WASI)
551552
/// Initializes a newly created URL referencing the local file or directory at the file system representation of the path. File system representation is a null-terminated C string with canonical UTF-8 encoding.
552553
public init(fileURLWithFileSystemRepresentation path: UnsafePointer<Int8>, isDirectory: Bool, relativeTo baseURL: URL?) {
553554
_url = URL._converted(from: NSURL(fileURLWithFileSystemRepresentation: path, isDirectory: isDirectory, relativeTo: baseURL))
554555
}
556+
#endif
555557

556558
public func hash(into hasher: inout Hasher) {
557559
hasher.combine(_url)
@@ -888,6 +890,7 @@ public struct URL : ReferenceConvertible, Equatable {
888890
self = self.standardized
889891
}
890892

893+
#if !os(WASI)
891894
/// Standardizes the path of a file URL.
892895
///
893896
/// If the `isFileURL` is false, this method returns `self`.
@@ -964,6 +967,7 @@ public struct URL : ReferenceConvertible, Equatable {
964967
public func checkResourceIsReachable() throws -> Bool {
965968
return try _url.checkResourceIsReachable()
966969
}
970+
#endif
967971

968972
// MARK: - Bridging Support
969973

@@ -1054,7 +1058,7 @@ extension URL : Codable {
10541058
}
10551059
}
10561060

1057-
1061+
#if !os(WASI)
10581062
//===----------------------------------------------------------------------===//
10591063
// File references, for playgrounds.
10601064
//===----------------------------------------------------------------------===//
@@ -1066,3 +1070,4 @@ extension URL : _ExpressibleByFileReferenceLiteral {
10661070
}
10671071

10681072
public typealias _FileReferenceLiteralType = URL
1073+
#endif

0 commit comments

Comments
 (0)