mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-24 13:36:09 +00:00
Adds full supposed for Wasm in the swift lib (#7328)
Adds tests for wasm module & github action
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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``
|
||||
///
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -14,7 +14,11 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#if !os(WASI)
|
||||
import Foundation
|
||||
#else
|
||||
import SwiftOverlayShims
|
||||
#endif
|
||||
|
||||
extension Int {
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -14,7 +14,11 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#if !os(WASI)
|
||||
import Foundation
|
||||
#else
|
||||
import SwiftOverlayShims
|
||||
#endif
|
||||
|
||||
extension String: Verifiable {
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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`
|
||||
|
||||
Reference in New Issue
Block a user