forked from BigfootDev/flatbuffers
[Lobster] file_identifier support
This commit is contained in:
@@ -175,25 +175,30 @@ class builder:
|
|||||||
while current_vtable.length <= slotnum: current_vtable.push(0)
|
while current_vtable.length <= slotnum: current_vtable.push(0)
|
||||||
current_vtable[slotnum] = head
|
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
|
// Finish finalizes a buffer, pointing to the given root_table
|
||||||
assert not finished
|
assert not finished
|
||||||
assert not nested
|
assert not nested
|
||||||
var prep_size = sz_32
|
var prep_size = sz_32
|
||||||
|
if file_identifier:
|
||||||
|
prep_size += sz_32
|
||||||
if size_prefix:
|
if size_prefix:
|
||||||
prep_size += sz_32
|
prep_size += sz_32
|
||||||
Prep(minalign, prep_size)
|
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)
|
PrependUOffsetTRelative(root_table)
|
||||||
if size_prefix:
|
if size_prefix:
|
||||||
PrependInt32(head)
|
PrependInt32(head)
|
||||||
finished = true
|
finished = true
|
||||||
return Start()
|
return Start()
|
||||||
|
|
||||||
def Finish(root_table:offset):
|
def Finish(root_table:offset, file_identifier:string? = nil):
|
||||||
return __Finish(root_table, false)
|
return __Finish(root_table, false, file_identifier)
|
||||||
|
|
||||||
def FinishSizePrefixed(root_table:offset):
|
def FinishSizePrefixed(root_table:offset, file_identifier:string? = nil):
|
||||||
return __Finish(root_table, true)
|
return __Finish(root_table, true, file_identifier)
|
||||||
|
|
||||||
def PrependBool(x):
|
def PrependBool(x):
|
||||||
buf, head = buf.write_int8_le_back(head, x)
|
buf, head = buf.write_int8_le_back(head, x)
|
||||||
@@ -299,3 +304,9 @@ class builder:
|
|||||||
// elsewhere.
|
// elsewhere.
|
||||||
assert x.o == head
|
assert x.o == head
|
||||||
Slot(v)
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ let orc = MyGame_Sample_MonsterBuilder { b }
|
|||||||
.end()
|
.end()
|
||||||
|
|
||||||
// Finish the buffer!
|
// 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.
|
// We now have a FlatBuffer that we could store on disk or send over a network.
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,9 @@ import monster_test_generated
|
|||||||
import optional_scalars_generated
|
import optional_scalars_generated
|
||||||
|
|
||||||
def check_read_buffer(buf):
|
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)
|
let monster = MyGame_Example_GetRootAsMonster(buf)
|
||||||
|
|
||||||
assert monster.hp == 80
|
assert monster.hp == 80
|
||||||
@@ -105,7 +107,7 @@ def make_monster_from_generated_code():
|
|||||||
.add_vector_of_doubles(vector_of_doubles)
|
.add_vector_of_doubles(vector_of_doubles)
|
||||||
.end()
|
.end()
|
||||||
|
|
||||||
b.Finish(mon)
|
b.Finish(mon, "MONS")
|
||||||
|
|
||||||
return b.SizedCopy()
|
return b.SizedCopy()
|
||||||
|
|
||||||
@@ -126,8 +128,10 @@ def test_optional_scalars():
|
|||||||
ss.add_just_enum(optional_scalars_OptionalByte_Two)
|
ss.add_just_enum(optional_scalars_OptionalByte_Two)
|
||||||
ss.add_maybe_enum(optional_scalars_OptionalByte_Two)
|
ss.add_maybe_enum(optional_scalars_OptionalByte_Two)
|
||||||
ss.add_default_enum(optional_scalars_OptionalByte_Two)
|
ss.add_default_enum(optional_scalars_OptionalByte_Two)
|
||||||
b.Finish(ss.end())
|
b.Finish(ss.end(), "NULL")
|
||||||
return optional_scalars_GetRootAsScalarStuff(b.SizedCopy())
|
let buf = b.SizedCopy()
|
||||||
|
assert flatbuffers_has_identifier(buf, "NULL")
|
||||||
|
return optional_scalars_GetRootAsScalarStuff(buf)
|
||||||
|
|
||||||
var root = build(true)
|
var root = build(true)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user