diff --git a/FlatBuffers.podspec b/FlatBuffers.podspec index 2aa69e2a027..83c86e16a05 100644 --- a/FlatBuffers.podspec +++ b/FlatBuffers.podspec @@ -10,12 +10,15 @@ Pod::Spec.new do |s| s.homepage = 'https://github.com/google/flatbuffers' s.license = { :type => 'Apache2.0', :file => 'LICENSE' } - s.author = { 'mustii' => 'mustii@mmk.one' } - s.source = { :git => 'https://github.com/google/flatbuffers.git', :tag => s.version.to_s, :submodules => true } + s.author = { 'mustii' => 'me@mustiikhalil.se' } + s.source = { :git => 'https://github.com/google/flatbuffers.git', :tag => "v" + s.version.to_s, :submodules => true } s.ios.deployment_target = '11.0' s.osx.deployment_target = '10.14' s.swift_version = '5.0' s.source_files = 'swift/Sources/Flatbuffers/*.swift' + s.pod_target_xcconfig = { + 'BUILD_LIBRARY_FOR_DISTRIBUTION' => 'YES' + } end diff --git a/grpc/flatbuffers-java-grpc/pom.xml b/grpc/flatbuffers-java-grpc/pom.xml index f7d41763747..dee931344d4 100644 --- a/grpc/flatbuffers-java-grpc/pom.xml +++ b/grpc/flatbuffers-java-grpc/pom.xml @@ -24,7 +24,7 @@ </developer> </developers> <properties> - <gRPC.version>1.36.0</gRPC.version> + <gRPC.version>1.67.1</gRPC.version> </properties> <dependencies> <dependency> diff --git a/samples/monster_generated.swift b/samples/monster_generated.swift index b536d1f4f5e..39ed03e10c6 100644 --- a/samples/monster_generated.swift +++ b/samples/monster_generated.swift @@ -111,7 +111,7 @@ public struct MyGame_Sample_Monster: FlatBufferObject, Verifiable { } public var pos: MyGame_Sample_Vec3? { let o = _accessor.offset(VTOFFSET.pos.v); return o == 0 ? nil : _accessor.readBuffer(of: MyGame_Sample_Vec3.self, at: o) } - public var mutablePos: MyGame_Sample_Vec3_Mutable? { let o = _accessor.offset(VTOFFSET.pos.v); return o == 0 ? nil : MyGame_Sample_Vec3_Mutable(_accessor.bb, o: o + _accessor.postion) } + public var mutablePos: MyGame_Sample_Vec3_Mutable? { let o = _accessor.offset(VTOFFSET.pos.v); return o == 0 ? nil : MyGame_Sample_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) } @discardableResult public func mutate(mana: Int16) -> Bool {let o = _accessor.offset(VTOFFSET.mana.v); return _accessor.mutate(mana, index: o) } public var hp: Int16 { let o = _accessor.offset(VTOFFSET.hp.v); return o == 0 ? 100 : _accessor.readBuffer(of: Int16.self, at: o) } diff --git a/src/idl_gen_swift.cpp b/src/idl_gen_swift.cpp index 6ac487c5573..a6a6668dcb0 100644 --- a/src/idl_gen_swift.cpp +++ b/src/idl_gen_swift.cpp @@ -328,7 +328,7 @@ class SwiftGenerator : public BaseGenerator { } else { code_ += "_{{FIELDVAR}} = {{VALUETYPE}}({{ACCESS}}.bb, o: " - "{{ACCESS}}.postion + {{OFFSET}})"; + "{{ACCESS}}.position + {{OFFSET}})"; } } Outdent(); @@ -361,7 +361,7 @@ class SwiftGenerator : public BaseGenerator { } else if (IsStruct(field.value.type)) { code_.SetValue("VALUETYPE", GenType(field.value.type) + Mutable()); code_ += GenReaderMainBody() + "return " + - GenConstructor("{{ACCESS}}.postion + {{OFFSET}}"); + GenConstructor("{{ACCESS}}.position + {{OFFSET}}"); } if (parser_.opts.mutable_buffer && !IsStruct(field.value.type)) code_ += GenMutate("{{OFFSET}}", "", IsEnum(field.value.type)); @@ -754,7 +754,7 @@ class SwiftGenerator : public BaseGenerator { code_.SetValue("VALUETYPE", GenType(field.value.type) + Mutable()); code_.SetValue("CONSTANT", "nil"); code_ += GenReaderMainBody(is_required) + GenOffset() + required_reader + - GenConstructor("o + {{ACCESS}}.postion"); + GenConstructor("o + {{ACCESS}}.position"); return; } switch (field.value.type.base_type) { @@ -763,7 +763,7 @@ class SwiftGenerator : public BaseGenerator { code_.SetValue("CONSTANT", "nil"); code_ += GenReaderMainBody(is_required) + GenOffset() + required_reader + - GenConstructor(GenIndirect("o + {{ACCESS}}.postion")); + GenConstructor(GenIndirect("o + {{ACCESS}}.position")); break; case BASE_TYPE_STRING: { diff --git a/swift/Sources/FlatBuffers/FlatBufferBuilder.swift b/swift/Sources/FlatBuffers/FlatBufferBuilder.swift index 57fecc6c911..e31b10cca4e 100644 --- a/swift/Sources/FlatBuffers/FlatBufferBuilder.swift +++ b/swift/Sources/FlatBuffers/FlatBufferBuilder.swift @@ -811,7 +811,7 @@ public struct FlatBufferBuilder { /// *NOTE: Never call this manually* /// /// - Parameter element: Element to insert - /// - returns: Postion of the Element + /// - returns: position of the Element @inline(__always) @discardableResult mutating public func push<T: Scalar>(element: T) -> UOffset { diff --git a/swift/Sources/FlatBuffers/FlatbuffersErrors.swift b/swift/Sources/FlatBuffers/FlatbuffersErrors.swift index c054d166194..13207b53a99 100644 --- a/swift/Sources/FlatBuffers/FlatbuffersErrors.swift +++ b/swift/Sources/FlatBuffers/FlatbuffersErrors.swift @@ -19,6 +19,8 @@ import Foundation /// Collection of thrown from the Flatbuffer verifier public enum FlatbuffersErrors: Error, Equatable { + /// Thrown when trying to verify a buffer that doesnt have the length of an ID + case bufferDoesntContainID /// Thrown when verifying a file id that doesnt match buffer id case bufferIdDidntMatchPassedId /// Prefixed size doesnt match the current (readable) buffer size diff --git a/swift/Sources/FlatBuffers/Mutable.swift b/swift/Sources/FlatBuffers/Mutable.swift index f6e4688b10c..307e9a927c3 100644 --- a/swift/Sources/FlatBuffers/Mutable.swift +++ b/swift/Sources/FlatBuffers/Mutable.swift @@ -21,7 +21,7 @@ public protocol Mutable { /// makes Flatbuffer accessed within the Protocol var bb: ByteBuffer { get } /// makes position of the ``Table``/``Struct`` accessed within the Protocol - var postion: Int32 { get } + var position: Int32 { get } } extension Mutable { @@ -45,7 +45,7 @@ extension Mutable where Self == Table { /// - index: index of the Element public func mutate<T: Scalar>(_ value: T, index: Int32) -> Bool { guard index != 0 else { return false } - return mutate(value: value, o: index + postion) + return mutate(value: value, o: index + position) } /// Directly mutates the element by calling mutate @@ -66,7 +66,7 @@ extension Mutable where Self == Struct { /// - value: New value to be inserted to the buffer /// - index: index of the Element public func mutate<T: Scalar>(_ value: T, index: Int32) -> Bool { - mutate(value: value, o: index + postion) + mutate(value: value, o: index + position) } /// Directly mutates the element by calling mutate diff --git a/swift/Sources/FlatBuffers/Struct.swift b/swift/Sources/FlatBuffers/Struct.swift index 5124b2af997..bbce8f978c7 100644 --- a/swift/Sources/FlatBuffers/Struct.swift +++ b/swift/Sources/FlatBuffers/Struct.swift @@ -24,7 +24,7 @@ public struct Struct { /// Hosting Bytebuffer public private(set) var bb: ByteBuffer /// Current position of the struct - public private(set) var postion: Int32 + public private(set) var position: Int32 /// Initializer for a mutable flatbuffers struct /// - Parameters: @@ -32,7 +32,7 @@ public struct Struct { /// - position: Current position for the struct in the ByteBuffer public init(bb: ByteBuffer, position: Int32 = 0) { self.bb = bb - postion = position + self.position = position } /// Reads data from the buffer directly at offset O @@ -41,7 +41,7 @@ public struct Struct { /// - o: Current offset of the data /// - Returns: Data of Type T that conforms to type Scalar public func readBuffer<T: Scalar>(of type: T.Type, at o: Int32) -> T { - let r = bb.read(def: T.self, position: Int(o + postion)) + let r = bb.read(def: T.self, position: Int(o + position)) return r } } diff --git a/swift/Sources/FlatBuffers/Table.swift b/swift/Sources/FlatBuffers/Table.swift index 7ad0ae0cc0c..02a2e6f2cdd 100644 --- a/swift/Sources/FlatBuffers/Table.swift +++ b/swift/Sources/FlatBuffers/Table.swift @@ -24,7 +24,7 @@ public struct Table { /// Hosting Bytebuffer public private(set) var bb: ByteBuffer /// Current position of the table within the buffer - public private(set) var postion: Int32 + public private(set) var position: Int32 /// Initializer for the table interface to allow generated code to read /// data from memory @@ -38,7 +38,7 @@ public struct Table { "Reading/Writing a buffer in big endian machine is not supported on swift") } self.bb = bb - postion = position + self.position = position } /// Gets the offset of the current field within the buffer by reading @@ -46,7 +46,7 @@ public struct Table { /// - Parameter o: current offset /// - Returns: offset of field within buffer public func offset(_ o: Int32) -> Int32 { - let vtable = postion - bb.read(def: Int32.self, position: Int(postion)) + let vtable = position - bb.read(def: Int32.self, position: Int(position)) return o < bb .read(def: VOffset.self, position: Int(vtable)) ? Int32(bb.read( def: Int16.self, @@ -64,7 +64,7 @@ public struct Table { /// String reads from the buffer with respect to position of the current table. /// - Parameter offset: Offset of the string public func string(at offset: Int32) -> String? { - directString(at: offset + postion) + directString(at: offset + position) } /// Direct string reads from the buffer disregarding the position of the table. @@ -84,7 +84,7 @@ public struct Table { /// - type: Type of Element that needs to be read from the buffer /// - o: Offset of the Element public func readBuffer<T>(of type: T.Type, at o: Int32) -> T { - directRead(of: T.self, offset: o + postion) + directRead(of: T.self, offset: o + position) } /// Reads from the buffer disregarding the position of the table. @@ -110,7 +110,7 @@ public struct Table { /// - Parameter o: offset /// - Returns: A flatbuffers object public func union<T: FlatbuffersInitializable>(_ o: Int32) -> T { - let o = o + postion + let o = o + position return directUnion(o) } @@ -136,7 +136,7 @@ public struct Table { /// - returns: Count of elements public func vector(count o: Int32) -> Int32 { var o = o - o += postion + o += position o += bb.read(def: Int32.self, position: Int(o)) return bb.read(def: Int32.self, position: Int(o)) } @@ -146,7 +146,7 @@ public struct Table { /// - returns: the start index of the vector public func vector(at o: Int32) -> Int32 { var o = o - o += postion + o += position return o + bb.read(def: Int32.self, position: Int(o)) + 4 } diff --git a/swift/Sources/FlatBuffers/Verifier.swift b/swift/Sources/FlatBuffers/Verifier.swift index 121f4349896..7b3170426f2 100644 --- a/swift/Sources/FlatBuffers/Verifier.swift +++ b/swift/Sources/FlatBuffers/Verifier.swift @@ -89,7 +89,7 @@ public struct Verifier { /// Checks if the value of Size "X" is within the range of the buffer /// - Parameters: - /// - position: Current postion to be read + /// - position: Current position to be read /// - size: `Byte` Size of readable object within the buffer /// - Throws: `outOfBounds` if the value is out of the bounds of the buffer /// and `apparentSizeTooLarge` if the apparent size is bigger than the one specified @@ -201,8 +201,12 @@ public struct Verifier { _depth -= 1 } + @inline(__always) mutating func verify(id: String) throws { let size = MemoryLayout<Int32>.size + guard _capacity >= (size * 2) else { + throw FlatbuffersErrors.bufferDoesntContainID + } let str = _buffer.readString(at: size, count: size) if id == str { return diff --git a/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/monster_test_generated.swift b/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/monster_test_generated.swift index c678282c9b0..2657318c21b 100644 --- a/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/monster_test_generated.swift +++ b/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/monster_test_generated.swift @@ -337,7 +337,7 @@ public struct MyGame_Example_Vec3: NativeStruct, Verifiable, FlatbuffersInitiali _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 = MyGame_Example_Test(_accessor.bb, o: _accessor.postion + 26) + _test3 = MyGame_Example_Test(_accessor.bb, o: _accessor.position + 26) } public init(x: Float32, y: Float32, z: Float32, test1: Double, test2: MyGame_Example_Color, test3: MyGame_Example_Test) { @@ -429,7 +429,7 @@ public struct MyGame_Example_Vec3_Mutable: FlatBufferObject { @discardableResult public func mutate(test1: Double) -> Bool { return _accessor.mutate(test1, index: 16) } public var test2: MyGame_Example_Color { return MyGame_Example_Color(rawValue: _accessor.readBuffer(of: UInt8.self, at: 24)) ?? .red } @discardableResult public func mutate(test2: MyGame_Example_Color) -> Bool { return _accessor.mutate(test2.rawValue, index: 24) } - public var test3: MyGame_Example_Test_Mutable { return MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.postion + 26) } + public var test3: MyGame_Example_Test_Mutable { return MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.position + 26) } public mutating func unpack() -> MyGame_Example_Vec3 { @@ -535,9 +535,9 @@ public struct MyGame_Example_StructOfStructs: NativeStruct, Verifiable, Flatbuff public init(_ bb: ByteBuffer, o: Int32) { let _accessor = Struct(bb: bb, position: o) - _a = MyGame_Example_Ability(_accessor.bb, o: _accessor.postion + 0) - _b = MyGame_Example_Test(_accessor.bb, o: _accessor.postion + 8) - _c = MyGame_Example_Ability(_accessor.bb, o: _accessor.postion + 12) + _a = MyGame_Example_Ability(_accessor.bb, o: _accessor.position + 0) + _b = MyGame_Example_Test(_accessor.bb, o: _accessor.position + 8) + _c = MyGame_Example_Ability(_accessor.bb, o: _accessor.position + 12) } public init(a: MyGame_Example_Ability, b: MyGame_Example_Test, c: MyGame_Example_Ability) { @@ -593,9 +593,9 @@ public struct MyGame_Example_StructOfStructs_Mutable: FlatBufferObject { public init(_ bb: ByteBuffer, o: Int32) { _accessor = Struct(bb: bb, position: o) } - public var a: MyGame_Example_Ability_Mutable { return MyGame_Example_Ability_Mutable(_accessor.bb, o: _accessor.postion + 0) } - public var b: MyGame_Example_Test_Mutable { return MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.postion + 8) } - public var c: MyGame_Example_Ability_Mutable { return MyGame_Example_Ability_Mutable(_accessor.bb, o: _accessor.postion + 12) } + public var a: MyGame_Example_Ability_Mutable { return MyGame_Example_Ability_Mutable(_accessor.bb, o: _accessor.position + 0) } + public var b: MyGame_Example_Test_Mutable { return MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.position + 8) } + public var c: MyGame_Example_Ability_Mutable { return MyGame_Example_Ability_Mutable(_accessor.bb, o: _accessor.position + 12) } public mutating func unpack() -> MyGame_Example_StructOfStructs { @@ -619,7 +619,7 @@ public struct MyGame_Example_StructOfStructsOfStructs: NativeStruct, Verifiable, public init(_ bb: ByteBuffer, o: Int32) { let _accessor = Struct(bb: bb, position: o) - _a = MyGame_Example_StructOfStructs(_accessor.bb, o: _accessor.postion + 0) + _a = MyGame_Example_StructOfStructs(_accessor.bb, o: _accessor.position + 0) } public init(a: MyGame_Example_StructOfStructs) { @@ -661,7 +661,7 @@ public struct MyGame_Example_StructOfStructsOfStructs_Mutable: FlatBufferObject public init(_ bb: ByteBuffer, o: Int32) { _accessor = Struct(bb: bb, position: o) } - public var a: MyGame_Example_StructOfStructs_Mutable { return MyGame_Example_StructOfStructs_Mutable(_accessor.bb, o: _accessor.postion + 0) } + public var a: MyGame_Example_StructOfStructs_Mutable { return MyGame_Example_StructOfStructs_Mutable(_accessor.bb, o: _accessor.position + 0) } public mutating func unpack() -> MyGame_Example_StructOfStructsOfStructs { @@ -1185,7 +1185,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac } public var pos: MyGame_Example_Vec3? { let o = _accessor.offset(VTOFFSET.pos.v); return o == 0 ? nil : _accessor.readBuffer(of: MyGame_Example_Vec3.self, at: o) } - public var mutablePos: MyGame_Example_Vec3_Mutable? { let o = _accessor.offset(VTOFFSET.pos.v); return o == 0 ? nil : MyGame_Example_Vec3_Mutable(_accessor.bb, o: o + _accessor.postion) } + public var mutablePos: MyGame_Example_Vec3_Mutable? { let o = _accessor.offset(VTOFFSET.pos.v); return o == 0 ? nil : MyGame_Example_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) } @discardableResult public func mutate(mana: Int16) -> Bool {let o = _accessor.offset(VTOFFSET.mana.v); return _accessor.mutate(mana, index: o) } public var hp: Int16 { let o = _accessor.offset(VTOFFSET.hp.v); return o == 0 ? 100 : _accessor.readBuffer(of: Int16.self, at: o) } @@ -1214,13 +1214,13 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac 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) -> MyGame_Example_Monster? { let o = _accessor.offset(VTOFFSET.testarrayoftables.v); return o == 0 ? nil : MyGame_Example_Monster(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) } public func testarrayoftablesBy(key: String) -> MyGame_Example_Monster? { let o = _accessor.offset(VTOFFSET.testarrayoftables.v); return o == 0 ? nil : MyGame_Example_Monster.lookupByKey(vector: _accessor.vector(at: o), key: key, fbb: _accessor.bb) } - public var enemy: MyGame_Example_Monster? { let o = _accessor.offset(VTOFFSET.enemy.v); return o == 0 ? nil : MyGame_Example_Monster(_accessor.bb, o: _accessor.indirect(o + _accessor.postion)) } + public var enemy: MyGame_Example_Monster? { let o = _accessor.offset(VTOFFSET.enemy.v); return o == 0 ? nil : MyGame_Example_Monster(_accessor.bb, o: _accessor.indirect(o + _accessor.position)) } public var hasTestnestedflatbuffer: Bool { let o = _accessor.offset(VTOFFSET.testnestedflatbuffer.v); return o == 0 ? false : true } public var testnestedflatbufferCount: Int32 { let o = _accessor.offset(VTOFFSET.testnestedflatbuffer.v); return o == 0 ? 0 : _accessor.vector(count: o) } public func testnestedflatbuffer(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.testnestedflatbuffer.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) } public var testnestedflatbuffer: [UInt8] { return _accessor.getVector(at: VTOFFSET.testnestedflatbuffer.v) ?? [] } public func mutate(testnestedflatbuffer: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testnestedflatbuffer.v); return _accessor.directMutate(testnestedflatbuffer, index: _accessor.vector(at: o) + index * 1) } - public var testempty: MyGame_Example_Stat? { let o = _accessor.offset(VTOFFSET.testempty.v); return o == 0 ? nil : MyGame_Example_Stat(_accessor.bb, o: _accessor.indirect(o + _accessor.postion)) } + public var testempty: MyGame_Example_Stat? { let o = _accessor.offset(VTOFFSET.testempty.v); return o == 0 ? nil : MyGame_Example_Stat(_accessor.bb, o: _accessor.indirect(o + _accessor.position)) } public var testbool: Bool { let o = _accessor.offset(VTOFFSET.testbool.v); return o == 0 ? false : _accessor.readBuffer(of: Bool.self, at: o) } @discardableResult public func mutate(testbool: Bool) -> Bool {let o = _accessor.offset(VTOFFSET.testbool.v); return _accessor.mutate(testbool, index: o) } public var testhashs32Fnv1: Int32 { let o = _accessor.offset(VTOFFSET.testhashs32Fnv1.v); return o == 0 ? 0 : _accessor.readBuffer(of: Int32.self, at: o) } @@ -1276,7 +1276,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac public func vectorOfDoubles(at index: Int32) -> Double { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return o == 0 ? 0 : _accessor.directRead(of: Double.self, offset: _accessor.vector(at: o) + index * 8) } public var vectorOfDoubles: [Double] { return _accessor.getVector(at: VTOFFSET.vectorOfDoubles.v) ?? [] } public func mutate(vectorOfDoubles: Double, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return _accessor.directMutate(vectorOfDoubles, index: _accessor.vector(at: o) + index * 8) } - public var parentNamespaceTest: MyGame_InParentNamespace? { let o = _accessor.offset(VTOFFSET.parentNamespaceTest.v); return o == 0 ? nil : MyGame_InParentNamespace(_accessor.bb, o: _accessor.indirect(o + _accessor.postion)) } + public var parentNamespaceTest: MyGame_InParentNamespace? { let o = _accessor.offset(VTOFFSET.parentNamespaceTest.v); return o == 0 ? nil : MyGame_InParentNamespace(_accessor.bb, o: _accessor.indirect(o + _accessor.position)) } public var hasVectorOfReferrables: Bool { let o = _accessor.offset(VTOFFSET.vectorOfReferrables.v); return o == 0 ? false : true } public var vectorOfReferrablesCount: Int32 { let o = _accessor.offset(VTOFFSET.vectorOfReferrables.v); return o == 0 ? 0 : _accessor.vector(count: o) } public func vectorOfReferrables(at index: Int32) -> MyGame_Example_Referrable? { let o = _accessor.offset(VTOFFSET.vectorOfReferrables.v); return o == 0 ? nil : MyGame_Example_Referrable(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) } @@ -1325,7 +1325,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac public func scalarKeySortedTables(at index: Int32) -> MyGame_Example_Stat? { let o = _accessor.offset(VTOFFSET.scalarKeySortedTables.v); return o == 0 ? nil : MyGame_Example_Stat(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) } public func scalarKeySortedTablesBy(key: UInt16) -> MyGame_Example_Stat? { let o = _accessor.offset(VTOFFSET.scalarKeySortedTables.v); return o == 0 ? nil : MyGame_Example_Stat.lookupByKey(vector: _accessor.vector(at: o), key: key, fbb: _accessor.bb) } public var nativeInline: MyGame_Example_Test? { let o = _accessor.offset(VTOFFSET.nativeInline.v); return o == 0 ? nil : _accessor.readBuffer(of: MyGame_Example_Test.self, at: o) } - public var mutableNativeInline: MyGame_Example_Test_Mutable? { let o = _accessor.offset(VTOFFSET.nativeInline.v); return o == 0 ? nil : MyGame_Example_Test_Mutable(_accessor.bb, o: o + _accessor.postion) } + public var mutableNativeInline: MyGame_Example_Test_Mutable? { let o = _accessor.offset(VTOFFSET.nativeInline.v); return o == 0 ? nil : MyGame_Example_Test_Mutable(_accessor.bb, o: o + _accessor.position) } public var longEnumNonEnumDefault: MyGame_Example_LongEnum { let o = _accessor.offset(VTOFFSET.longEnumNonEnumDefault.v); return o == 0 ? .longone : MyGame_Example_LongEnum(rawValue: _accessor.readBuffer(of: UInt64.self, at: o)) ?? .longone } @discardableResult public func mutate(longEnumNonEnumDefault: MyGame_Example_LongEnum) -> Bool {let o = _accessor.offset(VTOFFSET.longEnumNonEnumDefault.v); return _accessor.mutate(longEnumNonEnumDefault.rawValue, index: o) } public var longEnumNormalDefault: MyGame_Example_LongEnum { let o = _accessor.offset(VTOFFSET.longEnumNormalDefault.v); return o == 0 ? .longone : MyGame_Example_LongEnum(rawValue: _accessor.readBuffer(of: UInt64.self, at: o)) ?? .longone } diff --git a/tests/swift/tests/CodeGenerationTests/test_no_include_generated.swift b/tests/swift/tests/CodeGenerationTests/test_no_include_generated.swift index be8e8db9c39..5dc5eb7abc7 100644 --- a/tests/swift/tests/CodeGenerationTests/test_no_include_generated.swift +++ b/tests/swift/tests/CodeGenerationTests/test_no_include_generated.swift @@ -173,8 +173,8 @@ public struct Message: FlatBufferObject, Verifiable, ObjectAPIPacker { 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.postion) } - public var pointer: InternalMessage! { let o = _accessor.offset(VTOFFSET.pointer.v); return InternalMessage(_accessor.bb, o: _accessor.indirect(o + _accessor.postion)) } + 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) } diff --git a/tests/swift/tests/Sources/SwiftFlatBuffers/fuzzer_generated.swift b/tests/swift/tests/Sources/SwiftFlatBuffers/fuzzer_generated.swift index 765415672ca..08cd3cb2342 100644 --- a/tests/swift/tests/Sources/SwiftFlatBuffers/fuzzer_generated.swift +++ b/tests/swift/tests/Sources/SwiftFlatBuffers/fuzzer_generated.swift @@ -112,7 +112,7 @@ public struct Vec3: NativeStruct, Verifiable, FlatbuffersInitializable { _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.postion + 26) + _test3 = Test(_accessor.bb, o: _accessor.position + 26) } public init(x: Float32, y: Float32, z: Float32, test1: Double, test2: Color, test3: Test) { @@ -189,7 +189,7 @@ public struct Vec3_Mutable: FlatBufferObject { 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.postion + 26) } + public var test3: Test_Mutable { return Test_Mutable(_accessor.bb, o: _accessor.position + 26) } } /// an example documentation comment: "monster object" @@ -199,8 +199,6 @@ public struct Monster: FlatBufferObject, Verifiable { public var __buffer: ByteBuffer! { return _accessor.bb } private var _accessor: Table - public static func getRootAsMonster(bb: ByteBuffer) -> Monster { return Monster(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: bb.reader)) + Int32(bb.reader))) } - private init(_ t: Table) { _accessor = t } public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) } @@ -217,7 +215,7 @@ public struct Monster: FlatBufferObject, Verifiable { } 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.postion) } + 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) } diff --git a/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/FlatbuffersVerifierTests.swift b/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/FlatbuffersVerifierTests.swift index 00b02b76018..71a00789073 100644 --- a/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/FlatbuffersVerifierTests.swift +++ b/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/FlatbuffersVerifierTests.swift @@ -63,6 +63,17 @@ final class FlatbuffersVerifierTests: XCTestCase { XCTAssertThrowsError(try Verifier(buffer: &buffer)) } + func testFailingID() { + let dutData : [UInt8] = [1,2,3,4,5,6,7] + var buff = ByteBuffer(bytes: dutData) + do { + let _: Monster = try getCheckedRoot(byteBuffer: &buff, fileId: "ABCD") + XCTFail("This should always fail") + } catch { + XCTAssertEqual(error as? FlatbuffersErrors, .bufferDoesntContainID) + } + } + func testVerifierCheckAlignment() { var verifier = try! Verifier(buffer: &buffer) do { diff --git a/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/MutatingBool_generated.swift b/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/MutatingBool_generated.swift index 15f096df7d1..09c785cf3a9 100644 --- a/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/MutatingBool_generated.swift +++ b/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/MutatingBool_generated.swift @@ -88,7 +88,7 @@ public struct TestMutatingBool: FlatBufferObject, Verifiable, ObjectAPIPacker { } public var b: Property? { let o = _accessor.offset(VTOFFSET.b.v); return o == 0 ? nil : _accessor.readBuffer(of: Property.self, at: o) } - public var mutableB: Property_Mutable? { let o = _accessor.offset(VTOFFSET.b.v); return o == 0 ? nil : Property_Mutable(_accessor.bb, o: o + _accessor.postion) } + public var mutableB: Property_Mutable? { let o = _accessor.offset(VTOFFSET.b.v); return o == 0 ? nil : Property_Mutable(_accessor.bb, o: o + _accessor.position) } public static func startTestMutatingBool(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 1) } public static func add(b: Property?, _ fbb: inout FlatBufferBuilder) { guard let b = b else { return }; fbb.create(struct: b, position: VTOFFSET.b.p) } public static func endTestMutatingBool(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); return end } diff --git a/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/monster_test_generated.swift b/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/monster_test_generated.swift index c678282c9b0..2657318c21b 100644 --- a/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/monster_test_generated.swift +++ b/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/monster_test_generated.swift @@ -337,7 +337,7 @@ public struct MyGame_Example_Vec3: NativeStruct, Verifiable, FlatbuffersInitiali _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 = MyGame_Example_Test(_accessor.bb, o: _accessor.postion + 26) + _test3 = MyGame_Example_Test(_accessor.bb, o: _accessor.position + 26) } public init(x: Float32, y: Float32, z: Float32, test1: Double, test2: MyGame_Example_Color, test3: MyGame_Example_Test) { @@ -429,7 +429,7 @@ public struct MyGame_Example_Vec3_Mutable: FlatBufferObject { @discardableResult public func mutate(test1: Double) -> Bool { return _accessor.mutate(test1, index: 16) } public var test2: MyGame_Example_Color { return MyGame_Example_Color(rawValue: _accessor.readBuffer(of: UInt8.self, at: 24)) ?? .red } @discardableResult public func mutate(test2: MyGame_Example_Color) -> Bool { return _accessor.mutate(test2.rawValue, index: 24) } - public var test3: MyGame_Example_Test_Mutable { return MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.postion + 26) } + public var test3: MyGame_Example_Test_Mutable { return MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.position + 26) } public mutating func unpack() -> MyGame_Example_Vec3 { @@ -535,9 +535,9 @@ public struct MyGame_Example_StructOfStructs: NativeStruct, Verifiable, Flatbuff public init(_ bb: ByteBuffer, o: Int32) { let _accessor = Struct(bb: bb, position: o) - _a = MyGame_Example_Ability(_accessor.bb, o: _accessor.postion + 0) - _b = MyGame_Example_Test(_accessor.bb, o: _accessor.postion + 8) - _c = MyGame_Example_Ability(_accessor.bb, o: _accessor.postion + 12) + _a = MyGame_Example_Ability(_accessor.bb, o: _accessor.position + 0) + _b = MyGame_Example_Test(_accessor.bb, o: _accessor.position + 8) + _c = MyGame_Example_Ability(_accessor.bb, o: _accessor.position + 12) } public init(a: MyGame_Example_Ability, b: MyGame_Example_Test, c: MyGame_Example_Ability) { @@ -593,9 +593,9 @@ public struct MyGame_Example_StructOfStructs_Mutable: FlatBufferObject { public init(_ bb: ByteBuffer, o: Int32) { _accessor = Struct(bb: bb, position: o) } - public var a: MyGame_Example_Ability_Mutable { return MyGame_Example_Ability_Mutable(_accessor.bb, o: _accessor.postion + 0) } - public var b: MyGame_Example_Test_Mutable { return MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.postion + 8) } - public var c: MyGame_Example_Ability_Mutable { return MyGame_Example_Ability_Mutable(_accessor.bb, o: _accessor.postion + 12) } + public var a: MyGame_Example_Ability_Mutable { return MyGame_Example_Ability_Mutable(_accessor.bb, o: _accessor.position + 0) } + public var b: MyGame_Example_Test_Mutable { return MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.position + 8) } + public var c: MyGame_Example_Ability_Mutable { return MyGame_Example_Ability_Mutable(_accessor.bb, o: _accessor.position + 12) } public mutating func unpack() -> MyGame_Example_StructOfStructs { @@ -619,7 +619,7 @@ public struct MyGame_Example_StructOfStructsOfStructs: NativeStruct, Verifiable, public init(_ bb: ByteBuffer, o: Int32) { let _accessor = Struct(bb: bb, position: o) - _a = MyGame_Example_StructOfStructs(_accessor.bb, o: _accessor.postion + 0) + _a = MyGame_Example_StructOfStructs(_accessor.bb, o: _accessor.position + 0) } public init(a: MyGame_Example_StructOfStructs) { @@ -661,7 +661,7 @@ public struct MyGame_Example_StructOfStructsOfStructs_Mutable: FlatBufferObject public init(_ bb: ByteBuffer, o: Int32) { _accessor = Struct(bb: bb, position: o) } - public var a: MyGame_Example_StructOfStructs_Mutable { return MyGame_Example_StructOfStructs_Mutable(_accessor.bb, o: _accessor.postion + 0) } + public var a: MyGame_Example_StructOfStructs_Mutable { return MyGame_Example_StructOfStructs_Mutable(_accessor.bb, o: _accessor.position + 0) } public mutating func unpack() -> MyGame_Example_StructOfStructsOfStructs { @@ -1185,7 +1185,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac } public var pos: MyGame_Example_Vec3? { let o = _accessor.offset(VTOFFSET.pos.v); return o == 0 ? nil : _accessor.readBuffer(of: MyGame_Example_Vec3.self, at: o) } - public var mutablePos: MyGame_Example_Vec3_Mutable? { let o = _accessor.offset(VTOFFSET.pos.v); return o == 0 ? nil : MyGame_Example_Vec3_Mutable(_accessor.bb, o: o + _accessor.postion) } + public var mutablePos: MyGame_Example_Vec3_Mutable? { let o = _accessor.offset(VTOFFSET.pos.v); return o == 0 ? nil : MyGame_Example_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) } @discardableResult public func mutate(mana: Int16) -> Bool {let o = _accessor.offset(VTOFFSET.mana.v); return _accessor.mutate(mana, index: o) } public var hp: Int16 { let o = _accessor.offset(VTOFFSET.hp.v); return o == 0 ? 100 : _accessor.readBuffer(of: Int16.self, at: o) } @@ -1214,13 +1214,13 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac 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) -> MyGame_Example_Monster? { let o = _accessor.offset(VTOFFSET.testarrayoftables.v); return o == 0 ? nil : MyGame_Example_Monster(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) } public func testarrayoftablesBy(key: String) -> MyGame_Example_Monster? { let o = _accessor.offset(VTOFFSET.testarrayoftables.v); return o == 0 ? nil : MyGame_Example_Monster.lookupByKey(vector: _accessor.vector(at: o), key: key, fbb: _accessor.bb) } - public var enemy: MyGame_Example_Monster? { let o = _accessor.offset(VTOFFSET.enemy.v); return o == 0 ? nil : MyGame_Example_Monster(_accessor.bb, o: _accessor.indirect(o + _accessor.postion)) } + public var enemy: MyGame_Example_Monster? { let o = _accessor.offset(VTOFFSET.enemy.v); return o == 0 ? nil : MyGame_Example_Monster(_accessor.bb, o: _accessor.indirect(o + _accessor.position)) } public var hasTestnestedflatbuffer: Bool { let o = _accessor.offset(VTOFFSET.testnestedflatbuffer.v); return o == 0 ? false : true } public var testnestedflatbufferCount: Int32 { let o = _accessor.offset(VTOFFSET.testnestedflatbuffer.v); return o == 0 ? 0 : _accessor.vector(count: o) } public func testnestedflatbuffer(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.testnestedflatbuffer.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) } public var testnestedflatbuffer: [UInt8] { return _accessor.getVector(at: VTOFFSET.testnestedflatbuffer.v) ?? [] } public func mutate(testnestedflatbuffer: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testnestedflatbuffer.v); return _accessor.directMutate(testnestedflatbuffer, index: _accessor.vector(at: o) + index * 1) } - public var testempty: MyGame_Example_Stat? { let o = _accessor.offset(VTOFFSET.testempty.v); return o == 0 ? nil : MyGame_Example_Stat(_accessor.bb, o: _accessor.indirect(o + _accessor.postion)) } + public var testempty: MyGame_Example_Stat? { let o = _accessor.offset(VTOFFSET.testempty.v); return o == 0 ? nil : MyGame_Example_Stat(_accessor.bb, o: _accessor.indirect(o + _accessor.position)) } public var testbool: Bool { let o = _accessor.offset(VTOFFSET.testbool.v); return o == 0 ? false : _accessor.readBuffer(of: Bool.self, at: o) } @discardableResult public func mutate(testbool: Bool) -> Bool {let o = _accessor.offset(VTOFFSET.testbool.v); return _accessor.mutate(testbool, index: o) } public var testhashs32Fnv1: Int32 { let o = _accessor.offset(VTOFFSET.testhashs32Fnv1.v); return o == 0 ? 0 : _accessor.readBuffer(of: Int32.self, at: o) } @@ -1276,7 +1276,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac public func vectorOfDoubles(at index: Int32) -> Double { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return o == 0 ? 0 : _accessor.directRead(of: Double.self, offset: _accessor.vector(at: o) + index * 8) } public var vectorOfDoubles: [Double] { return _accessor.getVector(at: VTOFFSET.vectorOfDoubles.v) ?? [] } public func mutate(vectorOfDoubles: Double, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return _accessor.directMutate(vectorOfDoubles, index: _accessor.vector(at: o) + index * 8) } - public var parentNamespaceTest: MyGame_InParentNamespace? { let o = _accessor.offset(VTOFFSET.parentNamespaceTest.v); return o == 0 ? nil : MyGame_InParentNamespace(_accessor.bb, o: _accessor.indirect(o + _accessor.postion)) } + public var parentNamespaceTest: MyGame_InParentNamespace? { let o = _accessor.offset(VTOFFSET.parentNamespaceTest.v); return o == 0 ? nil : MyGame_InParentNamespace(_accessor.bb, o: _accessor.indirect(o + _accessor.position)) } public var hasVectorOfReferrables: Bool { let o = _accessor.offset(VTOFFSET.vectorOfReferrables.v); return o == 0 ? false : true } public var vectorOfReferrablesCount: Int32 { let o = _accessor.offset(VTOFFSET.vectorOfReferrables.v); return o == 0 ? 0 : _accessor.vector(count: o) } public func vectorOfReferrables(at index: Int32) -> MyGame_Example_Referrable? { let o = _accessor.offset(VTOFFSET.vectorOfReferrables.v); return o == 0 ? nil : MyGame_Example_Referrable(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) } @@ -1325,7 +1325,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac public func scalarKeySortedTables(at index: Int32) -> MyGame_Example_Stat? { let o = _accessor.offset(VTOFFSET.scalarKeySortedTables.v); return o == 0 ? nil : MyGame_Example_Stat(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) } public func scalarKeySortedTablesBy(key: UInt16) -> MyGame_Example_Stat? { let o = _accessor.offset(VTOFFSET.scalarKeySortedTables.v); return o == 0 ? nil : MyGame_Example_Stat.lookupByKey(vector: _accessor.vector(at: o), key: key, fbb: _accessor.bb) } public var nativeInline: MyGame_Example_Test? { let o = _accessor.offset(VTOFFSET.nativeInline.v); return o == 0 ? nil : _accessor.readBuffer(of: MyGame_Example_Test.self, at: o) } - public var mutableNativeInline: MyGame_Example_Test_Mutable? { let o = _accessor.offset(VTOFFSET.nativeInline.v); return o == 0 ? nil : MyGame_Example_Test_Mutable(_accessor.bb, o: o + _accessor.postion) } + public var mutableNativeInline: MyGame_Example_Test_Mutable? { let o = _accessor.offset(VTOFFSET.nativeInline.v); return o == 0 ? nil : MyGame_Example_Test_Mutable(_accessor.bb, o: o + _accessor.position) } public var longEnumNonEnumDefault: MyGame_Example_LongEnum { let o = _accessor.offset(VTOFFSET.longEnumNonEnumDefault.v); return o == 0 ? .longone : MyGame_Example_LongEnum(rawValue: _accessor.readBuffer(of: UInt64.self, at: o)) ?? .longone } @discardableResult public func mutate(longEnumNonEnumDefault: MyGame_Example_LongEnum) -> Bool {let o = _accessor.offset(VTOFFSET.longEnumNonEnumDefault.v); return _accessor.mutate(longEnumNonEnumDefault.rawValue, index: o) } public var longEnumNormalDefault: MyGame_Example_LongEnum { let o = _accessor.offset(VTOFFSET.longEnumNormalDefault.v); return o == 0 ? .longone : MyGame_Example_LongEnum(rawValue: _accessor.readBuffer(of: UInt64.self, at: o)) ?? .longone }