Adds full supposed for Wasm in the swift lib (#7328)

Adds tests for wasm module & github action
This commit is contained in:
mustiikhalil
2022-05-29 20:56:33 +02:00
committed by GitHub
parent 9aa08a429e
commit 967df08b1d
26 changed files with 3092 additions and 12 deletions

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
/// `ByteBuffer` is the interface that stores the data for a `Flatbuffers` object
/// it allows users to write and read data directly from memory thus the use of its
@@ -125,6 +129,7 @@ public struct ByteBuffer {
}
}
#if !os(WASI)
/// Constructor that creates a Flatbuffer from the Swift Data type object
/// - Parameter data: Swift data Object
public init(data: Data) {
@@ -135,6 +140,7 @@ public struct ByteBuffer {
self._storage.copy(from: bufferPointer.baseAddress!, count: data.count)
}
}
#endif
/// Constructor that creates a Flatbuffer instance with a size
/// - Parameter size: Length of the buffer
@@ -144,7 +150,7 @@ public struct ByteBuffer {
_storage.initialize(for: size)
}
#if swift(>=5.0)
#if swift(>=5.0) && !os(WASI)
/// Constructor that creates a Flatbuffer object from a ContiguousBytes
/// - Parameters:
/// - contiguousBytes: Binary stripe to use as the buffer
@@ -367,6 +373,7 @@ public struct ByteBuffer {
return Array(array)
}
#if !os(WASI)
/// Reads a string from the buffer and encodes it to a swift string
/// - Parameters:
/// - index: index of the string in the buffer
@@ -386,6 +393,26 @@ public struct ByteBuffer {
let bufprt = UnsafeBufferPointer(start: start, count: count)
return String(bytes: Array(bufprt), encoding: type)
}
#else
/// Reads a string from the buffer and encodes it to a swift string
/// - Parameters:
/// - index: index of the string in the buffer
/// - count: length of the string
/// - type: Encoding of the string
@inline(__always)
public func readString(
at index: Int,
count: Int) -> String?
{
assert(
index + count <= _storage.capacity,
"Reading out of bounds is illegal")
let start = _storage.memory.advanced(by: index)
.assumingMemoryBound(to: UInt8.self)
let bufprt = UnsafeBufferPointer(start: start, count: count)
return String(cString: bufprt.baseAddress!)
}
#endif
/// Creates a new Flatbuffer object that's duplicated from the current one
/// - Parameter removeBytes: the amount of bytes to remove from the current Size

View File

@@ -14,15 +14,21 @@
* limitations under the License.
*/
#if os(Linux)
import CoreFoundation
#if !os(WASI)
#if os(Linux)
import CoreFoundation
#else
import Foundation
#endif
#else
import Foundation
import SwiftOverlayShims
#endif
/// A boolean to see if the system is littleEndian
let isLitteEndian = CFByteOrderGetCurrent() ==
Int(CFByteOrderLittleEndian.rawValue)
let isLitteEndian: Bool = {
let number: UInt32 = 0x12345678
return number == number.littleEndian
}()
/// Constant for the file id length
let FileIdLength = 4
/// Type aliases

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
/// Enum is a protocol that all flatbuffers enums should conform to
/// Since it allows us to get the actual `ByteSize` and `Value` from

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
/// ``FlatBufferBuilder`` builds a `FlatBuffer` through manipulating its internal state.
///
@@ -55,6 +59,7 @@ public struct FlatBufferBuilder {
/// Gives a read access to the buffer's size
public var size: UOffset { _bb.size }
#if !os(WASI)
/// Data representation of the buffer
///
/// Should only be used after ``finish(offset:addPrefix:)`` is called
@@ -64,6 +69,7 @@ public struct FlatBufferBuilder {
bytes: _bb.memory.advanced(by: _bb.writerIndex),
count: _bb.capacity &- _bb.writerIndex)
}
#endif
/// Returns the underlying bytes in the ``ByteBuffer``
///

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
/// NativeStruct is a protocol that indicates if the struct is a native `swift` struct
/// since now we will be serializing native structs into the buffer.

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
/// FlatBuffersUtils hosts some utility functions that might be useful
public enum FlatBuffersUtils {

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
/// Collection of thrown from the Flatbuffer verifier
public enum FlatbuffersErrors: Error, Equatable {
@@ -57,10 +61,17 @@ public enum FlatbuffersErrors: Error, Equatable {
fieldName: String)
case apparentSizeTooLarge
public static func == (
lhs: FlatbuffersErrors,
rhs: FlatbuffersErrors) -> Bool
{
lhs.localizedDescription == rhs.localizedDescription
}
}
#if !os(WASI)
extension FlatbuffersErrors {
public static func == (
lhs: FlatbuffersErrors,
rhs: FlatbuffersErrors) -> Bool
{
lhs.localizedDescription == rhs.localizedDescription
}
}
#endif

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
extension Int {

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
/// FlatBufferGRPCMessage protocol that should allow us to invoke
/// initializers directly from the GRPC generated code

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
/// Mutable is a protocol that allows us to mutate Scalar values within a ``ByteBuffer``
public protocol Mutable {

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
/// NativeObject is a protocol that all of the `Object-API` generated code should be
/// conforming to since it allows developers the ease of use to pack and unpack their

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
/// Offset object for all the Objects that are written into the buffer
public struct Offset {

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
/// Takes in a prefixed sized buffer, where the prefixed size would be skipped.
/// And would verify that the buffer passed is a valid `Flatbuffers` Object.

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
extension String: Verifiable {

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
/// Struct is a representation of a mutable `Flatbuffers` struct
/// since native structs are value types and cant be mutated

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
/// `Table` is a Flatbuffers object that can read,
/// mutate scalar fields within a valid flatbuffers buffer

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
/// `TableVerifier` verifies a table object is within a provided memory.
/// It checks if all the objects for a specific generated table, are within

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
/// `VerifierOptions` is a set of options to verify a flatbuffer
public struct VerifierOptions {

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
/// Verifiable is a protocol all swift flatbuffers object should conform to,
/// since swift is similar to `cpp` and `rust` where the data is read directly

View File

@@ -14,7 +14,11 @@
* limitations under the License.
*/
#if !os(WASI)
import Foundation
#else
import SwiftOverlayShims
#endif
/// Verifier that check if the buffer passed into it is a valid,
/// safe, aligned Flatbuffers object since swift read from `unsafeMemory`