mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-28 07:08:05 +00:00
Adds windows swift support (#8622)
Adding support for windows requires the code generations to add a compiler statement to completely ignore GRPC code generation on windows Cleanup the project to use the main Package.swift to run tests instead of having it separate and includes the imports for GRPC within it. Adds windows swift ci
This commit is contained in:
3
tests/swift/fuzzer/CodeGenerationTests/test_import.fbs
Normal file
3
tests/swift/fuzzer/CodeGenerationTests/test_import.fbs
Normal file
@@ -0,0 +1,3 @@
|
||||
table Message {
|
||||
internal_message: string;
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// swiftlint:disable all
|
||||
// swiftformat:disable all
|
||||
|
||||
@_implementationOnly import FlatBuffers
|
||||
|
||||
internal struct Message: FlatBufferObject, Verifiable, ObjectAPIPacker {
|
||||
|
||||
static func validateVersion() { FlatBuffersVersion_25_2_10() }
|
||||
internal var __buffer: ByteBuffer! { return _accessor.bb }
|
||||
private var _accessor: Table
|
||||
|
||||
private init(_ t: Table) { _accessor = t }
|
||||
internal init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }
|
||||
|
||||
private enum VTOFFSET: VOffset {
|
||||
case internalMessage = 4
|
||||
var v: Int32 { Int32(self.rawValue) }
|
||||
var p: VOffset { self.rawValue }
|
||||
}
|
||||
|
||||
internal var internalMessage: String? { let o = _accessor.offset(VTOFFSET.internalMessage.v); return o == 0 ? nil : _accessor.string(at: o) }
|
||||
internal var internalMessageSegmentArray: [UInt8]? { return _accessor.getVector(at: VTOFFSET.internalMessage.v) }
|
||||
internal static func startMessage(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 1) }
|
||||
internal static func add(internalMessage: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: internalMessage, at: VTOFFSET.internalMessage.p) }
|
||||
internal static func endMessage(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); return end }
|
||||
internal static func createMessage(
|
||||
_ fbb: inout FlatBufferBuilder,
|
||||
internalMessageOffset internalMessage: Offset = Offset()
|
||||
) -> Offset {
|
||||
let __start = Message.startMessage(&fbb)
|
||||
Message.add(internalMessage: internalMessage, &fbb)
|
||||
return Message.endMessage(&fbb, start: __start)
|
||||
}
|
||||
|
||||
|
||||
internal mutating func unpack() -> MessageT {
|
||||
return MessageT(&self)
|
||||
}
|
||||
internal static func pack(_ builder: inout FlatBufferBuilder, obj: inout MessageT?) -> Offset {
|
||||
guard var obj = obj else { return Offset() }
|
||||
return pack(&builder, obj: &obj)
|
||||
}
|
||||
|
||||
internal static func pack(_ builder: inout FlatBufferBuilder, obj: inout MessageT) -> Offset {
|
||||
let __internalMessage: Offset
|
||||
if let s = obj.internalMessage {
|
||||
__internalMessage = builder.create(string: s)
|
||||
} else {
|
||||
__internalMessage = Offset()
|
||||
}
|
||||
|
||||
let __root = Message.startMessage(&builder)
|
||||
Message.add(internalMessage: __internalMessage, &builder)
|
||||
return Message.endMessage(&builder, start: __root)
|
||||
}
|
||||
|
||||
internal static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
|
||||
var _v = try verifier.visitTable(at: position)
|
||||
try _v.visit(field: VTOFFSET.internalMessage.p, fieldName: "internalMessage", required: false, type: ForwardOffset<String>.self)
|
||||
_v.finish()
|
||||
}
|
||||
}
|
||||
|
||||
extension Message: Encodable {
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case internalMessage = "internal_message"
|
||||
}
|
||||
internal func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encodeIfPresent(internalMessage, forKey: .internalMessage)
|
||||
}
|
||||
}
|
||||
|
||||
internal class MessageT: NativeObject {
|
||||
|
||||
internal var internalMessage: String?
|
||||
|
||||
internal init(_ _t: inout Message) {
|
||||
internalMessage = _t.internalMessage
|
||||
}
|
||||
|
||||
internal init() {
|
||||
}
|
||||
|
||||
internal func serialize() -> ByteBuffer { return serialize(type: Message.self) }
|
||||
|
||||
}
|
||||
13
tests/swift/fuzzer/CodeGenerationTests/test_no_include.fbs
Normal file
13
tests/swift/fuzzer/CodeGenerationTests/test_no_include.fbs
Normal file
@@ -0,0 +1,13 @@
|
||||
struct BytesCount {
|
||||
x: int64;
|
||||
}
|
||||
|
||||
table InternalMessage {
|
||||
str: string;
|
||||
}
|
||||
|
||||
table Message {
|
||||
id: int64;
|
||||
position: BytesCount (required);
|
||||
pointer: InternalMessage (required);
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// swiftlint:disable all
|
||||
// swiftformat:disable all
|
||||
|
||||
public struct BytesCount: NativeStruct, Verifiable, FlatbuffersInitializable, NativeObject {
|
||||
|
||||
static func validateVersion() { FlatBuffersVersion_25_2_10() }
|
||||
|
||||
private var _x: Int64
|
||||
|
||||
public init(_ bb: ByteBuffer, o: Int32) {
|
||||
let _accessor = Struct(bb: bb, position: o)
|
||||
_x = _accessor.readBuffer(of: Int64.self, at: 0)
|
||||
}
|
||||
|
||||
public init(x: Int64) {
|
||||
_x = x
|
||||
}
|
||||
|
||||
public init() {
|
||||
_x = 0
|
||||
}
|
||||
|
||||
public init(_ _t: inout BytesCount_Mutable) {
|
||||
_x = _t.x
|
||||
}
|
||||
|
||||
public var x: Int64 { _x }
|
||||
|
||||
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
|
||||
try verifier.inBuffer(position: position, of: BytesCount.self)
|
||||
}
|
||||
}
|
||||
|
||||
extension BytesCount: Encodable {
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case x = "x"
|
||||
}
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
if x != 0 {
|
||||
try container.encodeIfPresent(x, forKey: .x)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct BytesCount_Mutable: FlatBufferObject {
|
||||
|
||||
static func validateVersion() { FlatBuffersVersion_25_2_10() }
|
||||
public var __buffer: ByteBuffer! { return _accessor.bb }
|
||||
private var _accessor: Struct
|
||||
|
||||
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Struct(bb: bb, position: o) }
|
||||
|
||||
public var x: Int64 { return _accessor.readBuffer(of: Int64.self, at: 0) }
|
||||
@discardableResult public func mutate(x: Int64) -> Bool { return _accessor.mutate(x, index: 0) }
|
||||
|
||||
|
||||
public mutating func unpack() -> BytesCount {
|
||||
return BytesCount(&self)
|
||||
}
|
||||
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout BytesCount?) -> Offset {
|
||||
guard var obj = obj else { return Offset() }
|
||||
return pack(&builder, obj: &obj)
|
||||
}
|
||||
|
||||
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout BytesCount) -> Offset {
|
||||
return builder.create(struct: obj)
|
||||
}
|
||||
}
|
||||
|
||||
public struct InternalMessage: FlatBufferObject, Verifiable, ObjectAPIPacker {
|
||||
|
||||
static func validateVersion() { FlatBuffersVersion_25_2_10() }
|
||||
public var __buffer: ByteBuffer! { return _accessor.bb }
|
||||
private var _accessor: Table
|
||||
|
||||
private init(_ t: Table) { _accessor = t }
|
||||
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }
|
||||
|
||||
private enum VTOFFSET: VOffset {
|
||||
case str = 4
|
||||
var v: Int32 { Int32(self.rawValue) }
|
||||
var p: VOffset { self.rawValue }
|
||||
}
|
||||
|
||||
public var str: String? { let o = _accessor.offset(VTOFFSET.str.v); return o == 0 ? nil : _accessor.string(at: o) }
|
||||
public var strSegmentArray: [UInt8]? { return _accessor.getVector(at: VTOFFSET.str.v) }
|
||||
public static func startInternalMessage(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 1) }
|
||||
public static func add(str: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: str, at: VTOFFSET.str.p) }
|
||||
public static func endInternalMessage(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); return end }
|
||||
public static func createInternalMessage(
|
||||
_ fbb: inout FlatBufferBuilder,
|
||||
strOffset str: Offset = Offset()
|
||||
) -> Offset {
|
||||
let __start = InternalMessage.startInternalMessage(&fbb)
|
||||
InternalMessage.add(str: str, &fbb)
|
||||
return InternalMessage.endInternalMessage(&fbb, start: __start)
|
||||
}
|
||||
|
||||
|
||||
public mutating func unpack() -> InternalMessageT {
|
||||
return InternalMessageT(&self)
|
||||
}
|
||||
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout InternalMessageT?) -> Offset {
|
||||
guard var obj = obj else { return Offset() }
|
||||
return pack(&builder, obj: &obj)
|
||||
}
|
||||
|
||||
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout InternalMessageT) -> Offset {
|
||||
let __str: Offset
|
||||
if let s = obj.str {
|
||||
__str = builder.create(string: s)
|
||||
} else {
|
||||
__str = Offset()
|
||||
}
|
||||
|
||||
let __root = InternalMessage.startInternalMessage(&builder)
|
||||
InternalMessage.add(str: __str, &builder)
|
||||
return InternalMessage.endInternalMessage(&builder, start: __root)
|
||||
}
|
||||
|
||||
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
|
||||
var _v = try verifier.visitTable(at: position)
|
||||
try _v.visit(field: VTOFFSET.str.p, fieldName: "str", required: false, type: ForwardOffset<String>.self)
|
||||
_v.finish()
|
||||
}
|
||||
}
|
||||
|
||||
extension InternalMessage: Encodable {
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case str = "str"
|
||||
}
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encodeIfPresent(str, forKey: .str)
|
||||
}
|
||||
}
|
||||
|
||||
public class InternalMessageT: NativeObject {
|
||||
|
||||
public var str: String?
|
||||
|
||||
public init(_ _t: inout InternalMessage) {
|
||||
str = _t.str
|
||||
}
|
||||
|
||||
public init() {
|
||||
}
|
||||
|
||||
public func serialize() -> ByteBuffer { return serialize(type: InternalMessage.self) }
|
||||
|
||||
}
|
||||
public struct Message: FlatBufferObject, Verifiable, ObjectAPIPacker {
|
||||
|
||||
static func validateVersion() { FlatBuffersVersion_25_2_10() }
|
||||
public var __buffer: ByteBuffer! { return _accessor.bb }
|
||||
private var _accessor: Table
|
||||
|
||||
private init(_ t: Table) { _accessor = t }
|
||||
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }
|
||||
|
||||
private enum VTOFFSET: VOffset {
|
||||
case id = 4
|
||||
case position = 6
|
||||
case pointer = 8
|
||||
var v: Int32 { Int32(self.rawValue) }
|
||||
var p: VOffset { self.rawValue }
|
||||
}
|
||||
|
||||
public var id: Int64 { let o = _accessor.offset(VTOFFSET.id.v); return o == 0 ? 0 : _accessor.readBuffer(of: Int64.self, at: o) }
|
||||
@discardableResult public func mutate(id: Int64) -> Bool {let o = _accessor.offset(VTOFFSET.id.v); return _accessor.mutate(id, index: o) }
|
||||
public var position: BytesCount! { let o = _accessor.offset(VTOFFSET.position.v); return _accessor.readBuffer(of: BytesCount.self, at: o) }
|
||||
public var mutablePosition: BytesCount_Mutable! { let o = _accessor.offset(VTOFFSET.position.v); return BytesCount_Mutable(_accessor.bb, o: o + _accessor.position) }
|
||||
public var pointer: InternalMessage! { let o = _accessor.offset(VTOFFSET.pointer.v); return InternalMessage(_accessor.bb, o: _accessor.indirect(o + _accessor.position)) }
|
||||
public static func startMessage(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 3) }
|
||||
public static func add(id: Int64, _ fbb: inout FlatBufferBuilder) { fbb.add(element: id, def: 0, at: VTOFFSET.id.p) }
|
||||
public static func add(position: BytesCount?, _ fbb: inout FlatBufferBuilder) { guard let position = position else { return }; fbb.create(struct: position, position: VTOFFSET.position.p) }
|
||||
public static func add(pointer: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: pointer, at: VTOFFSET.pointer.p) }
|
||||
public static func endMessage(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); fbb.require(table: end, fields: [6, 8]); return end }
|
||||
public static func createMessage(
|
||||
_ fbb: inout FlatBufferBuilder,
|
||||
id: Int64 = 0,
|
||||
position: BytesCount,
|
||||
pointerOffset pointer: Offset
|
||||
) -> Offset {
|
||||
let __start = Message.startMessage(&fbb)
|
||||
Message.add(id: id, &fbb)
|
||||
Message.add(position: position, &fbb)
|
||||
Message.add(pointer: pointer, &fbb)
|
||||
return Message.endMessage(&fbb, start: __start)
|
||||
}
|
||||
|
||||
|
||||
public mutating func unpack() -> MessageT {
|
||||
return MessageT(&self)
|
||||
}
|
||||
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MessageT?) -> Offset {
|
||||
guard var obj = obj else { return Offset() }
|
||||
return pack(&builder, obj: &obj)
|
||||
}
|
||||
|
||||
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MessageT) -> Offset {
|
||||
let __pointer = InternalMessage.pack(&builder, obj: &obj.pointer)
|
||||
let __root = Message.startMessage(&builder)
|
||||
Message.add(id: obj.id, &builder)
|
||||
Message.add(position: obj.position, &builder)
|
||||
Message.add(pointer: __pointer, &builder)
|
||||
return Message.endMessage(&builder, start: __root)
|
||||
}
|
||||
|
||||
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
|
||||
var _v = try verifier.visitTable(at: position)
|
||||
try _v.visit(field: VTOFFSET.id.p, fieldName: "id", required: false, type: Int64.self)
|
||||
try _v.visit(field: VTOFFSET.position.p, fieldName: "position", required: true, type: BytesCount.self)
|
||||
try _v.visit(field: VTOFFSET.pointer.p, fieldName: "pointer", required: true, type: ForwardOffset<InternalMessage>.self)
|
||||
_v.finish()
|
||||
}
|
||||
}
|
||||
|
||||
extension Message: Encodable {
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case id = "id"
|
||||
case position = "position"
|
||||
case pointer = "pointer"
|
||||
}
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
if id != 0 {
|
||||
try container.encodeIfPresent(id, forKey: .id)
|
||||
}
|
||||
try container.encodeIfPresent(position, forKey: .position)
|
||||
try container.encodeIfPresent(pointer, forKey: .pointer)
|
||||
}
|
||||
}
|
||||
|
||||
public class MessageT: NativeObject {
|
||||
|
||||
public var id: Int64
|
||||
public var position: BytesCount
|
||||
public var pointer: InternalMessageT
|
||||
|
||||
public init(_ _t: inout Message) {
|
||||
id = _t.id
|
||||
position = _t.position
|
||||
var __pointer = _t.pointer
|
||||
pointer = __pointer!.unpack()
|
||||
}
|
||||
|
||||
public init() {
|
||||
id = 0
|
||||
position = BytesCount()
|
||||
pointer = InternalMessageT()
|
||||
}
|
||||
|
||||
public func serialize() -> ByteBuffer { return serialize(type: Message.self) }
|
||||
|
||||
}
|
||||
35
tests/swift/fuzzer/Package.swift
Normal file
35
tests/swift/fuzzer/Package.swift
Normal file
@@ -0,0 +1,35 @@
|
||||
// swift-tools-version:5.9
|
||||
/*
|
||||
* Copyright 2020 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "fuzzer",
|
||||
platforms: [
|
||||
.iOS(.v12),
|
||||
.macOS(.v10_14),
|
||||
],
|
||||
dependencies: [
|
||||
.package(path: "../../.."),
|
||||
],
|
||||
targets: [
|
||||
.executableTarget(
|
||||
name: "fuzzer",
|
||||
dependencies: [
|
||||
.product(name: "FlatBuffers", package: "flatbuffers"),
|
||||
]),
|
||||
])
|
||||
32
tests/swift/fuzzer/Sources/fuzzer/fuzzer.fbs
Normal file
32
tests/swift/fuzzer/Sources/fuzzer/fuzzer.fbs
Normal file
@@ -0,0 +1,32 @@
|
||||
enum Color:ubyte (bit_flags) {
|
||||
Red = 0, // color Red = (1u << 0)
|
||||
/// \brief color Green
|
||||
/// Green is bit_flag with value (1u << 1)
|
||||
Green,
|
||||
/// \brief color Blue (1u << 3)
|
||||
Blue = 3,
|
||||
}
|
||||
|
||||
struct Test { a:short; b:byte; }
|
||||
|
||||
struct Vec3 (force_align: 8) {
|
||||
x:float;
|
||||
y:float;
|
||||
z:float;
|
||||
test1:double;
|
||||
test2:Color;
|
||||
test3:Test;
|
||||
}
|
||||
|
||||
/// an example documentation comment: "monster object"
|
||||
table Monster {
|
||||
pos:Vec3 (id: 0);
|
||||
hp:short = 100 (id: 2);
|
||||
mana:short = 150 (id: 1);
|
||||
name:string (id: 3, key);
|
||||
color:Color = Blue (id: 6);
|
||||
inventory:[ubyte] (id: 5);
|
||||
testarrayoftables:[Monster] (id: 4);
|
||||
}
|
||||
|
||||
root_type Monster;
|
||||
337
tests/swift/fuzzer/Sources/fuzzer/fuzzer_generated.swift
Normal file
337
tests/swift/fuzzer/Sources/fuzzer/fuzzer_generated.swift
Normal file
@@ -0,0 +1,337 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// swiftlint:disable all
|
||||
// swiftformat:disable all
|
||||
|
||||
import FlatBuffers
|
||||
|
||||
public enum Color: UInt8, Enum, Verifiable {
|
||||
public typealias T = UInt8
|
||||
public static var byteSize: Int { return MemoryLayout<UInt8>.size }
|
||||
public var value: UInt8 { return self.rawValue }
|
||||
case red = 1
|
||||
/// \brief color Green
|
||||
/// Green is bit_flag with value (1u << 1)
|
||||
case green = 2
|
||||
/// \brief color Blue (1u << 3)
|
||||
case blue = 8
|
||||
|
||||
public static var max: Color { return .blue }
|
||||
public static var min: Color { return .red }
|
||||
}
|
||||
|
||||
extension Color: Encodable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .red: try container.encode("Red")
|
||||
case .green: try container.encode("Green")
|
||||
case .blue: try container.encode("Blue")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct Test: NativeStruct, Verifiable, FlatbuffersInitializable {
|
||||
|
||||
static func validateVersion() { FlatBuffersVersion_25_2_10() }
|
||||
|
||||
private var _a: Int16
|
||||
private var _b: Int8
|
||||
private let padding0__: UInt8 = 0
|
||||
|
||||
public init(_ bb: ByteBuffer, o: Int32) {
|
||||
let _accessor = Struct(bb: bb, position: o)
|
||||
_a = _accessor.readBuffer(of: Int16.self, at: 0)
|
||||
_b = _accessor.readBuffer(of: Int8.self, at: 2)
|
||||
}
|
||||
|
||||
public init(a: Int16, b: Int8) {
|
||||
_a = a
|
||||
_b = b
|
||||
}
|
||||
|
||||
public init() {
|
||||
_a = 0
|
||||
_b = 0
|
||||
}
|
||||
|
||||
public var a: Int16 { _a }
|
||||
public var b: Int8 { _b }
|
||||
|
||||
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
|
||||
try verifier.inBuffer(position: position, of: Test.self)
|
||||
}
|
||||
}
|
||||
|
||||
extension Test: Encodable {
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case a = "a"
|
||||
case b = "b"
|
||||
}
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
if a != 0 {
|
||||
try container.encodeIfPresent(a, forKey: .a)
|
||||
}
|
||||
if b != 0 {
|
||||
try container.encodeIfPresent(b, forKey: .b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct Test_Mutable: FlatBufferObject {
|
||||
|
||||
static func validateVersion() { FlatBuffersVersion_25_2_10() }
|
||||
public var __buffer: ByteBuffer! { return _accessor.bb }
|
||||
private var _accessor: Struct
|
||||
|
||||
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Struct(bb: bb, position: o) }
|
||||
|
||||
public var a: Int16 { return _accessor.readBuffer(of: Int16.self, at: 0) }
|
||||
public var b: Int8 { return _accessor.readBuffer(of: Int8.self, at: 2) }
|
||||
}
|
||||
|
||||
public struct Vec3: NativeStruct, Verifiable, FlatbuffersInitializable {
|
||||
|
||||
static func validateVersion() { FlatBuffersVersion_25_2_10() }
|
||||
|
||||
private var _x: Float32
|
||||
private var _y: Float32
|
||||
private var _z: Float32
|
||||
private let padding0__: UInt32 = 0
|
||||
private var _test1: Double
|
||||
private var _test2: UInt8
|
||||
private let padding1__: UInt8 = 0
|
||||
private var _test3: Test
|
||||
private let padding2__: UInt16 = 0
|
||||
|
||||
public init(_ bb: ByteBuffer, o: Int32) {
|
||||
let _accessor = Struct(bb: bb, position: o)
|
||||
_x = _accessor.readBuffer(of: Float32.self, at: 0)
|
||||
_y = _accessor.readBuffer(of: Float32.self, at: 4)
|
||||
_z = _accessor.readBuffer(of: Float32.self, at: 8)
|
||||
_test1 = _accessor.readBuffer(of: Double.self, at: 16)
|
||||
_test2 = _accessor.readBuffer(of: UInt8.self, at: 24)
|
||||
_test3 = Test(_accessor.bb, o: _accessor.position + 26)
|
||||
}
|
||||
|
||||
public init(x: Float32, y: Float32, z: Float32, test1: Double, test2: Color, test3: Test) {
|
||||
_x = x
|
||||
_y = y
|
||||
_z = z
|
||||
_test1 = test1
|
||||
_test2 = test2.value
|
||||
_test3 = test3
|
||||
}
|
||||
|
||||
public init() {
|
||||
_x = 0.0
|
||||
_y = 0.0
|
||||
_z = 0.0
|
||||
_test1 = 0.0
|
||||
_test2 = 0
|
||||
_test3 = Test()
|
||||
}
|
||||
|
||||
public var x: Float32 { _x }
|
||||
public var y: Float32 { _y }
|
||||
public var z: Float32 { _z }
|
||||
public var test1: Double { _test1 }
|
||||
public var test2: Color { Color(rawValue: _test2)! }
|
||||
public var test3: Test { _test3 }
|
||||
|
||||
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
|
||||
try verifier.inBuffer(position: position, of: Vec3.self)
|
||||
}
|
||||
}
|
||||
|
||||
extension Vec3: Encodable {
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case x = "x"
|
||||
case y = "y"
|
||||
case z = "z"
|
||||
case test1 = "test1"
|
||||
case test2 = "test2"
|
||||
case test3 = "test3"
|
||||
}
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
if x != 0.0 {
|
||||
try container.encodeIfPresent(x, forKey: .x)
|
||||
}
|
||||
if y != 0.0 {
|
||||
try container.encodeIfPresent(y, forKey: .y)
|
||||
}
|
||||
if z != 0.0 {
|
||||
try container.encodeIfPresent(z, forKey: .z)
|
||||
}
|
||||
if test1 != 0.0 {
|
||||
try container.encodeIfPresent(test1, forKey: .test1)
|
||||
}
|
||||
if test2 != .red {
|
||||
try container.encodeIfPresent(test2, forKey: .test2)
|
||||
}
|
||||
try container.encodeIfPresent(test3, forKey: .test3)
|
||||
}
|
||||
}
|
||||
|
||||
public struct Vec3_Mutable: FlatBufferObject {
|
||||
|
||||
static func validateVersion() { FlatBuffersVersion_25_2_10() }
|
||||
public var __buffer: ByteBuffer! { return _accessor.bb }
|
||||
private var _accessor: Struct
|
||||
|
||||
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Struct(bb: bb, position: o) }
|
||||
|
||||
public var x: Float32 { return _accessor.readBuffer(of: Float32.self, at: 0) }
|
||||
public var y: Float32 { return _accessor.readBuffer(of: Float32.self, at: 4) }
|
||||
public var z: Float32 { return _accessor.readBuffer(of: Float32.self, at: 8) }
|
||||
public var test1: Double { return _accessor.readBuffer(of: Double.self, at: 16) }
|
||||
public var test2: Color { return Color(rawValue: _accessor.readBuffer(of: UInt8.self, at: 24)) ?? .red }
|
||||
public var test3: Test_Mutable { return Test_Mutable(_accessor.bb, o: _accessor.position + 26) }
|
||||
}
|
||||
|
||||
/// an example documentation comment: "monster object"
|
||||
public struct Monster: FlatBufferObject, Verifiable {
|
||||
|
||||
static func validateVersion() { FlatBuffersVersion_25_2_10() }
|
||||
public var __buffer: ByteBuffer! { return _accessor.bb }
|
||||
private var _accessor: Table
|
||||
|
||||
private init(_ t: Table) { _accessor = t }
|
||||
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }
|
||||
|
||||
private enum VTOFFSET: VOffset {
|
||||
case pos = 4
|
||||
case mana = 6
|
||||
case hp = 8
|
||||
case name = 10
|
||||
case testarrayoftables = 12
|
||||
case inventory = 14
|
||||
case color = 16
|
||||
var v: Int32 { Int32(self.rawValue) }
|
||||
var p: VOffset { self.rawValue }
|
||||
}
|
||||
|
||||
public var pos: Vec3? { let o = _accessor.offset(VTOFFSET.pos.v); return o == 0 ? nil : _accessor.readBuffer(of: Vec3.self, at: o) }
|
||||
public var mutablePos: Vec3_Mutable? { let o = _accessor.offset(VTOFFSET.pos.v); return o == 0 ? nil : Vec3_Mutable(_accessor.bb, o: o + _accessor.position) }
|
||||
public var mana: Int16 { let o = _accessor.offset(VTOFFSET.mana.v); return o == 0 ? 150 : _accessor.readBuffer(of: Int16.self, at: o) }
|
||||
public var hp: Int16 { let o = _accessor.offset(VTOFFSET.hp.v); return o == 0 ? 100 : _accessor.readBuffer(of: Int16.self, at: o) }
|
||||
public var name: String! { let o = _accessor.offset(VTOFFSET.name.v); return _accessor.string(at: o) }
|
||||
public var nameSegmentArray: [UInt8]! { return _accessor.getVector(at: VTOFFSET.name.v) }
|
||||
public var hasTestarrayoftables: Bool { let o = _accessor.offset(VTOFFSET.testarrayoftables.v); return o == 0 ? false : true }
|
||||
public var testarrayoftablesCount: Int32 { let o = _accessor.offset(VTOFFSET.testarrayoftables.v); return o == 0 ? 0 : _accessor.vector(count: o) }
|
||||
public func testarrayoftables(at index: Int32) -> Monster? { let o = _accessor.offset(VTOFFSET.testarrayoftables.v); return o == 0 ? nil : Monster(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) }
|
||||
public func testarrayoftablesBy(key: String) -> Monster? { let o = _accessor.offset(VTOFFSET.testarrayoftables.v); return o == 0 ? nil : Monster.lookupByKey(vector: _accessor.vector(at: o), key: key, fbb: _accessor.bb) }
|
||||
public var hasInventory: Bool { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? false : true }
|
||||
public var inventoryCount: Int32 { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? 0 : _accessor.vector(count: o) }
|
||||
public func inventory(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) }
|
||||
public var inventory: [UInt8] { return _accessor.getVector(at: VTOFFSET.inventory.v) ?? [] }
|
||||
public func withUnsafePointerToInventory<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.inventory.v, body: body) }
|
||||
public var color: Color { let o = _accessor.offset(VTOFFSET.color.v); return o == 0 ? .blue : Color(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .blue }
|
||||
public static func startMonster(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 7) }
|
||||
public static func add(pos: Vec3?, _ fbb: inout FlatBufferBuilder) { guard let pos = pos else { return }; fbb.create(struct: pos, position: VTOFFSET.pos.p) }
|
||||
public static func add(mana: Int16, _ fbb: inout FlatBufferBuilder) { fbb.add(element: mana, def: 150, at: VTOFFSET.mana.p) }
|
||||
public static func add(hp: Int16, _ fbb: inout FlatBufferBuilder) { fbb.add(element: hp, def: 100, at: VTOFFSET.hp.p) }
|
||||
public static func add(name: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: name, at: VTOFFSET.name.p) }
|
||||
public static func addVectorOf(testarrayoftables: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: testarrayoftables, at: VTOFFSET.testarrayoftables.p) }
|
||||
public static func addVectorOf(inventory: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: inventory, at: VTOFFSET.inventory.p) }
|
||||
public static func add(color: Color, _ fbb: inout FlatBufferBuilder) { fbb.add(element: color.rawValue, def: 8, at: VTOFFSET.color.p) }
|
||||
public static func endMonster(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); fbb.require(table: end, fields: [10]); return end }
|
||||
public static func createMonster(
|
||||
_ fbb: inout FlatBufferBuilder,
|
||||
pos: Vec3? = nil,
|
||||
mana: Int16 = 150,
|
||||
hp: Int16 = 100,
|
||||
nameOffset name: Offset,
|
||||
testarrayoftablesVectorOffset testarrayoftables: Offset = Offset(),
|
||||
inventoryVectorOffset inventory: Offset = Offset(),
|
||||
color: Color = .blue
|
||||
) -> Offset {
|
||||
let __start = Monster.startMonster(&fbb)
|
||||
Monster.add(pos: pos, &fbb)
|
||||
Monster.add(mana: mana, &fbb)
|
||||
Monster.add(hp: hp, &fbb)
|
||||
Monster.add(name: name, &fbb)
|
||||
Monster.addVectorOf(testarrayoftables: testarrayoftables, &fbb)
|
||||
Monster.addVectorOf(inventory: inventory, &fbb)
|
||||
Monster.add(color: color, &fbb)
|
||||
return Monster.endMonster(&fbb, start: __start)
|
||||
}
|
||||
public static func sortVectorOfMonster(offsets:[Offset], _ fbb: inout FlatBufferBuilder) -> Offset {
|
||||
var off = offsets
|
||||
off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: 10, fbb: &fbb), Table.offset(Int32($0.o), vOffset: 10, fbb: &fbb), fbb: &fbb) < 0 }
|
||||
return fbb.createVector(ofOffsets: off)
|
||||
}
|
||||
fileprivate static func lookupByKey(vector: Int32, key: String, fbb: ByteBuffer) -> Monster? {
|
||||
let key = key.utf8.map { $0 }
|
||||
var span = fbb.read(def: Int32.self, position: Int(vector - 4))
|
||||
var start: Int32 = 0
|
||||
while span != 0 {
|
||||
var middle = span / 2
|
||||
let tableOffset = Table.indirect(vector + 4 * (start + middle), fbb)
|
||||
let comp = Table.compare(Table.offset(Int32(fbb.capacity) - tableOffset, vOffset: 10, fbb: fbb), key, fbb: fbb)
|
||||
if comp > 0 {
|
||||
span = middle
|
||||
} else if comp < 0 {
|
||||
middle += 1
|
||||
start += middle
|
||||
span -= middle
|
||||
} else {
|
||||
return Monster(fbb, o: tableOffset)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
|
||||
var _v = try verifier.visitTable(at: position)
|
||||
try _v.visit(field: VTOFFSET.pos.p, fieldName: "pos", required: false, type: Vec3.self)
|
||||
try _v.visit(field: VTOFFSET.mana.p, fieldName: "mana", required: false, type: Int16.self)
|
||||
try _v.visit(field: VTOFFSET.hp.p, fieldName: "hp", required: false, type: Int16.self)
|
||||
try _v.visit(field: VTOFFSET.name.p, fieldName: "name", required: true, type: ForwardOffset<String>.self)
|
||||
try _v.visit(field: VTOFFSET.testarrayoftables.p, fieldName: "testarrayoftables", required: false, type: ForwardOffset<Vector<ForwardOffset<Monster>, Monster>>.self)
|
||||
try _v.visit(field: VTOFFSET.inventory.p, fieldName: "inventory", required: false, type: ForwardOffset<Vector<UInt8, UInt8>>.self)
|
||||
try _v.visit(field: VTOFFSET.color.p, fieldName: "color", required: false, type: Color.self)
|
||||
_v.finish()
|
||||
}
|
||||
}
|
||||
|
||||
extension Monster: Encodable {
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case pos = "pos"
|
||||
case mana = "mana"
|
||||
case hp = "hp"
|
||||
case name = "name"
|
||||
case testarrayoftables = "testarrayoftables"
|
||||
case inventory = "inventory"
|
||||
case color = "color"
|
||||
}
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encodeIfPresent(pos, forKey: .pos)
|
||||
if mana != 150 {
|
||||
try container.encodeIfPresent(mana, forKey: .mana)
|
||||
}
|
||||
if hp != 100 {
|
||||
try container.encodeIfPresent(hp, forKey: .hp)
|
||||
}
|
||||
try container.encodeIfPresent(name, forKey: .name)
|
||||
if testarrayoftablesCount > 0 {
|
||||
var contentEncoder = container.nestedUnkeyedContainer(forKey: .testarrayoftables)
|
||||
for index in 0..<testarrayoftablesCount {
|
||||
guard let type = testarrayoftables(at: index) else { continue }
|
||||
try contentEncoder.encode(type)
|
||||
}
|
||||
}
|
||||
if inventoryCount > 0 {
|
||||
try container.encodeIfPresent(inventory, forKey: .inventory)
|
||||
}
|
||||
if color != .blue {
|
||||
try container.encodeIfPresent(color, forKey: .color)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
29
tests/swift/fuzzer/Sources/fuzzer/main.swift
Normal file
29
tests/swift/fuzzer/Sources/fuzzer/main.swift
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2024 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import FlatBuffers
|
||||
|
||||
@_cdecl("LLVMFuzzerTestOneInput")
|
||||
public func FuzzFlatbuffers(_ start: UnsafeRawPointer, _ count: Int) -> CInt {
|
||||
let bytes = UnsafeRawBufferPointer(start: start, count: count)
|
||||
do {
|
||||
var buffer = ByteBuffer(contiguousBytes: bytes, count: count)
|
||||
let _: Monster = try getCheckedRoot(byteBuffer: &buffer)
|
||||
} catch {
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
Reference in New Issue
Block a user