1212
1313#if DEPLOYMENT_RUNTIME_SWIFT
1414
15+ #if canImport(Glibc)
16+ @usableFromInline let calloc = Glibc . calloc
17+ @usableFromInline let malloc = Glibc . malloc
18+ @usableFromInline let free = Glibc . free
19+ @usableFromInline let memset = Glibc . memset
20+ @usableFromInline let memcpy = Glibc . memcpy
21+ @usableFromInline let memcmp = Glibc . memcmp
22+ #elseif canImport(WASILibc)
23+ @usableFromInline let calloc = WASILibc . calloc
24+ @usableFromInline let malloc = WASILibc . malloc
25+ @usableFromInline let free = WASILibc . free
26+ @usableFromInline let memset = WASILibc . memset
27+ @usableFromInline let memcpy = WASILibc . memcpy
28+ @usableFromInline let memcmp = WASILibc . memcmp
29+ #endif
30+
1531#if !canImport(Darwin)
1632@inlinable // This is @inlinable as trivially computable.
1733internal func malloc_good_size( _ size: Int ) -> Int {
@@ -23,6 +39,8 @@ internal func malloc_good_size(_ size: Int) -> Int {
2339
2440#if canImport(Glibc)
2541import Glibc
42+ #elseif canImport(WASILibc)
43+ import WASILibc
2644#endif
2745
2846internal func __NSDataInvokeDeallocatorUnmap( _ mem: UnsafeMutableRawPointer , _ length: Int ) {
@@ -562,30 +580,30 @@ internal class __NSSwiftData : NSData {
562580 override var classForCoder : AnyClass {
563581 return NSData . self
564582 }
565-
583+
566584 override init ( ) {
567585 fatalError ( )
568586 }
569-
587+
570588 private init ( _correctly: Void ) {
571589 super. init ( )
572590 }
573-
591+
574592 convenience init ( backing: __DataStorage , range: Range < Data . Index > ) {
575593 self . init ( _correctly: ( ) )
576594 _backing = backing
577595 _range = range
578596 }
579-
597+
580598 public required init ? ( coder aDecoder: NSCoder ) {
581599 fatalError ( " This should have been encoded as NSData. " )
582600 }
583-
601+
584602 override func encode( with aCoder: NSCoder ) {
585603 // This should encode this object just like NSData does, and .classForCoder should do the rest.
586604 super. encode ( with: aCoder)
587605 }
588-
606+
589607 override var length : Int {
590608 return _range. upperBound - _range. lowerBound
591609 }
@@ -608,7 +626,7 @@ internal class __NSSwiftData : NSData {
608626 override func copy( with zone: NSZone ? = nil ) -> Any {
609627 return self
610628 }
611-
629+
612630 override func mutableCopy( with zone: NSZone ? = nil ) -> Any {
613631 return NSMutableData ( bytes: bytes, length: length)
614632 }
@@ -654,7 +672,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
654672 @usableFromInline typealias Buffer = ( UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 ,
655673 UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 ) //len //enum
656674 @usableFromInline var bytes : Buffer
657- #elseif arch(i386) || arch(arm)
675+ #elseif arch(i386) || arch(arm) || arch(wasm32)
658676 @usableFromInline typealias Buffer = ( UInt8 , UInt8 , UInt8 , UInt8 ,
659677 UInt8 , UInt8 ) //len //enum
660678 @usableFromInline var bytes : Buffer
@@ -683,7 +701,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
683701 assert ( count <= MemoryLayout< Buffer> . size)
684702#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le)
685703 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 ) )
686- #elseif arch(i386) || arch(arm)
704+ #elseif arch(i386) || arch(arm) || arch(wasm32)
687705 bytes = ( UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) )
688706#else
689707 #error("This architecture isn't known. Add it to the 32-bit or 64-bit line.")
@@ -866,7 +884,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
866884
867885#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le)
868886 @usableFromInline internal typealias HalfInt = Int32
869- #elseif arch(i386) || arch(arm)
887+ #elseif arch(i386) || arch(arm) || arch(wasm32)
870888 @usableFromInline internal typealias HalfInt = Int16
871889#else
872890 #error("This architecture isn't known. Add it to the 32-bit or 64-bit line.")
@@ -2012,6 +2030,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
20122030 }
20132031 }
20142032
2033+ #if !os(WASI)
20152034 /// Initialize a `Data` with the contents of a `URL`.
20162035 ///
20172036 /// - parameter url: The `URL` to read.
@@ -2024,6 +2043,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
20242043 return Data ( bytes: d. bytes, count: d. length)
20252044 }
20262045 }
2046+ #endif
20272047
20282048 /// Initialize a `Data` from a Base-64 encoded String using the given options.
20292049 ///
@@ -2287,6 +2307,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
22872307 }
22882308#endif
22892309
2310+ #if !os(WASI)
22902311 /// Write the contents of the `Data` to a location.
22912312 ///
22922313 /// - parameter url: The location to write the data into.
@@ -2307,6 +2328,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
23072328#endif
23082329 }
23092330 }
2331+ #endif
23102332
23112333 // MARK: -
23122334
0 commit comments