From d6483965159697172b7ffe6914968a0553726c34 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Tue, 8 Mar 2022 15:39:12 -0800 Subject: [PATCH] [Lobster] file_identifier support --- lobster/flatbuffers.lobster | 21 ++++++++++++++++----- samples/sample_binary.lobster | 2 +- tests/lobstertest.lobster | 12 ++++++++---- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/lobster/flatbuffers.lobster b/lobster/flatbuffers.lobster index 8b04c87b3..1199e50f6 100644 --- a/lobster/flatbuffers.lobster +++ b/lobster/flatbuffers.lobster @@ -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 + + diff --git a/samples/sample_binary.lobster b/samples/sample_binary.lobster index cd7adab2a..3c4851d6d 100644 --- a/samples/sample_binary.lobster +++ b/samples/sample_binary.lobster @@ -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. diff --git a/tests/lobstertest.lobster b/tests/lobstertest.lobster index a0f81ce6c..454c2b894 100644 --- a/tests/lobstertest.lobster +++ b/tests/lobstertest.lobster @@ -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)