Skip to content

Commit

Permalink
Merge branch 'master' into go-object-api
Browse files Browse the repository at this point in the history
  • Loading branch information
Razvan Smadu committed Oct 7, 2024
2 parents c467f42 + 2f59a03 commit 0444c4e
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 62 deletions.
7 changes: 5 additions & 2 deletions FlatBuffers.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]' }
s.source = { :git => 'https://github.com/google/flatbuffers.git', :tag => s.version.to_s, :submodules => true }
s.author = { 'mustii' => '[email protected]' }
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
2 changes: 1 addition & 1 deletion grpc/flatbuffers-java-grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</developer>
</developers>
<properties>
<gRPC.version>1.36.0</gRPC.version>
<gRPC.version>1.67.1</gRPC.version>
</properties>
<dependencies>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion samples/monster_generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
8 changes: 4 additions & 4 deletions src/idl_gen_swift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class SwiftGenerator : public BaseGenerator {
} else {
code_ +=
"_{{FIELDVAR}} = {{VALUETYPE}}({{ACCESS}}.bb, o: "
"{{ACCESS}}.postion + {{OFFSET}})";
"{{ACCESS}}.position + {{OFFSET}})";
}
}
Outdent();
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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) {
Expand All @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion swift/Sources/FlatBuffers/FlatBufferBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions swift/Sources/FlatBuffers/FlatbuffersErrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions swift/Sources/FlatBuffers/Mutable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions swift/Sources/FlatBuffers/Struct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ 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:
/// - bb: Current hosting Bytebuffer
/// - 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
Expand All @@ -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
}
}
16 changes: 8 additions & 8 deletions swift/Sources/FlatBuffers/Table.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,15 +38,15 @@ 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
/// the vtable
/// - 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,
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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)
}

Expand All @@ -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))
}
Expand All @@ -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
}

Expand Down
6 changes: 5 additions & 1 deletion swift/Sources/FlatBuffers/Verifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 0444c4e

Please sign in to comment.