[Lobster] file_identifier support

This commit is contained in:
Wouter van Oortmerssen 2022-03-08 15:39:12 -08:00
parent 777e78d8dd
commit d648396515
3 changed files with 25 additions and 10 deletions

View File

@ -175,25 +175,30 @@ class builder:
while current_vtable.length <= slotnum: current_vtable.push(0)
current_vtable[slotnum] = head
def __Finish(root_table:offset, size_prefix:int):
def __Finish(root_table:offset, size_prefix:int, file_identifier:string?):
// Finish finalizes a buffer, pointing to the given root_table
assert not finished
assert not nested
var prep_size = sz_32
if file_identifier:
prep_size += sz_32
if size_prefix:
prep_size += sz_32
Prep(minalign, prep_size)
if file_identifier:
assert file_identifier.length == 4
buf, head = buf.write_substring_back(head, file_identifier, false)
PrependUOffsetTRelative(root_table)
if size_prefix:
PrependInt32(head)
finished = true
return Start()
def Finish(root_table:offset):
return __Finish(root_table, false)
def Finish(root_table:offset, file_identifier:string? = nil):
return __Finish(root_table, false, file_identifier)
def FinishSizePrefixed(root_table:offset):
return __Finish(root_table, true)
def FinishSizePrefixed(root_table:offset, file_identifier:string? = nil):
return __Finish(root_table, true, file_identifier)
def PrependBool(x):
buf, head = buf.write_int8_le_back(head, x)
@ -299,3 +304,9 @@ class builder:
// elsewhere.
assert x.o == head
Slot(v)
def has_identifier(buf:string, file_identifier:string):
assert file_identifier.length == 4
return buf.length >= 8 and buf.substring(4, 4) == file_identifier

View File

@ -54,7 +54,7 @@ let orc = MyGame_Sample_MonsterBuilder { b }
.end()
// Finish the buffer!
b.Finish(orc)
b.Finish(orc, "MONS")
// We now have a FlatBuffer that we could store on disk or send over a network.

View File

@ -17,7 +17,9 @@ import monster_test_generated
import optional_scalars_generated
def check_read_buffer(buf):
// CheckReadBuffer checks that the given buffer is evaluated correctly as the example Monster.
// Check that the given buffer is evaluated correctly as the example Monster.
assert flatbuffers_has_identifier(buf, "MONS")
let monster = MyGame_Example_GetRootAsMonster(buf)
assert monster.hp == 80
@ -105,7 +107,7 @@ def make_monster_from_generated_code():
.add_vector_of_doubles(vector_of_doubles)
.end()
b.Finish(mon)
b.Finish(mon, "MONS")
return b.SizedCopy()
@ -126,8 +128,10 @@ def test_optional_scalars():
ss.add_just_enum(optional_scalars_OptionalByte_Two)
ss.add_maybe_enum(optional_scalars_OptionalByte_Two)
ss.add_default_enum(optional_scalars_OptionalByte_Two)
b.Finish(ss.end())
return optional_scalars_GetRootAsScalarStuff(b.SizedCopy())
b.Finish(ss.end(), "NULL")
let buf = b.SizedCopy()
assert flatbuffers_has_identifier(buf, "NULL")
return optional_scalars_GetRootAsScalarStuff(buf)
var root = build(true)