Skip to content

Commit

Permalink
[Lobster] file_identifier support
Browse files Browse the repository at this point in the history
  • Loading branch information
aardappel committed Mar 8, 2022
1 parent 777e78d commit d648396
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
21 changes: 16 additions & 5 deletions lobster/flatbuffers.lobster
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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


2 changes: 1 addition & 1 deletion samples/sample_binary.lobster
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
12 changes: 8 additions & 4 deletions tests/lobstertest.lobster
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

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

Expand Down

0 comments on commit d648396

Please sign in to comment.