Skip to content

Commit d648396

Browse files
committedMar 8, 2022
[Lobster] file_identifier support
1 parent 777e78d commit d648396

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed
 

‎lobster/flatbuffers.lobster

+16-5
Original file line numberDiff line numberDiff line change
@@ -175,25 +175,30 @@ class builder:
175175
while current_vtable.length <= slotnum: current_vtable.push(0)
176176
current_vtable[slotnum] = head
177177

178-
def __Finish(root_table:offset, size_prefix:int):
178+
def __Finish(root_table:offset, size_prefix:int, file_identifier:string?):
179179
// Finish finalizes a buffer, pointing to the given root_table
180180
assert not finished
181181
assert not nested
182182
var prep_size = sz_32
183+
if file_identifier:
184+
prep_size += sz_32
183185
if size_prefix:
184186
prep_size += sz_32
185187
Prep(minalign, prep_size)
188+
if file_identifier:
189+
assert file_identifier.length == 4
190+
buf, head = buf.write_substring_back(head, file_identifier, false)
186191
PrependUOffsetTRelative(root_table)
187192
if size_prefix:
188193
PrependInt32(head)
189194
finished = true
190195
return Start()
191196

192-
def Finish(root_table:offset):
193-
return __Finish(root_table, false)
197+
def Finish(root_table:offset, file_identifier:string? = nil):
198+
return __Finish(root_table, false, file_identifier)
194199

195-
def FinishSizePrefixed(root_table:offset):
196-
return __Finish(root_table, true)
200+
def FinishSizePrefixed(root_table:offset, file_identifier:string? = nil):
201+
return __Finish(root_table, true, file_identifier)
197202

198203
def PrependBool(x):
199204
buf, head = buf.write_int8_le_back(head, x)
@@ -299,3 +304,9 @@ class builder:
299304
// elsewhere.
300305
assert x.o == head
301306
Slot(v)
307+
308+
def has_identifier(buf:string, file_identifier:string):
309+
assert file_identifier.length == 4
310+
return buf.length >= 8 and buf.substring(4, 4) == file_identifier
311+
312+

‎samples/sample_binary.lobster

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ let orc = MyGame_Sample_MonsterBuilder { b }
5454
.end()
5555

5656
// Finish the buffer!
57-
b.Finish(orc)
57+
b.Finish(orc, "MONS")
5858

5959
// We now have a FlatBuffer that we could store on disk or send over a network.
6060

‎tests/lobstertest.lobster

+8-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import monster_test_generated
1717
import optional_scalars_generated
1818

1919
def check_read_buffer(buf):
20-
// CheckReadBuffer checks that the given buffer is evaluated correctly as the example Monster.
20+
// Check that the given buffer is evaluated correctly as the example Monster.
21+
assert flatbuffers_has_identifier(buf, "MONS")
22+
2123
let monster = MyGame_Example_GetRootAsMonster(buf)
2224

2325
assert monster.hp == 80
@@ -105,7 +107,7 @@ def make_monster_from_generated_code():
105107
.add_vector_of_doubles(vector_of_doubles)
106108
.end()
107109

108-
b.Finish(mon)
110+
b.Finish(mon, "MONS")
109111

110112
return b.SizedCopy()
111113

@@ -126,8 +128,10 @@ def test_optional_scalars():
126128
ss.add_just_enum(optional_scalars_OptionalByte_Two)
127129
ss.add_maybe_enum(optional_scalars_OptionalByte_Two)
128130
ss.add_default_enum(optional_scalars_OptionalByte_Two)
129-
b.Finish(ss.end())
130-
return optional_scalars_GetRootAsScalarStuff(b.SizedCopy())
131+
b.Finish(ss.end(), "NULL")
132+
let buf = b.SizedCopy()
133+
assert flatbuffers_has_identifier(buf, "NULL")
134+
return optional_scalars_GetRootAsScalarStuff(buf)
131135

132136
var root = build(true)
133137

0 commit comments

Comments
 (0)
Please sign in to comment.