travisstaloch / flatbufferz Goto Github PK
View Code? Open in Web Editor NEWa flatbuffers codegen library in zig
License: MIT License
a flatbuffers codegen library in zig
License: MIT License
The suffix T
is a misnomer.
from #2 (comment) by @clickingbuttons
Hey @travisstaloch , would you accept a PR refactoring src/codegen.zig
?
--no-gen-object-api
still imports object API ...T
types and Unpack
functionsunion Type { Null, Int }
table Field { type: Type }
generates a non-existent call to rcv.Type_Type()
instead of the correct rcv.TypeType()
:
pub fn UnpackTo(rcv: Field, t: *FieldT, __pack_opts: fb.common.PackOptions) !void {
if (rcv.Type_()) |_tab| {
t.type = try TypeT.Unpack(rcv.Type_Type(), _tab, __pack_opts);
}
}
optional_field: ?T
instead of optional_field: ?*T
to prevent unnecessary allocation._
s from variable names. Zig purposefully disallows private struct members. Also, constants like __file_indent
are already namespaced to the source file. Parameters like __builder
can simply be renamed to builder
.self: Self
instead of inconsitently rcv: {type}
and self: {type}
Title
over snake_case
is to prevent parameter name shadowing like:pub fn rcv(rcv: Field) bool {
...
}
This can of course be solved by renaming the parameter to rcv_
or something similar but adds another layer of complexity.
Useful for when Zig breaks codegenned syntax and for codegen devs. Here's one way to do it:
fn format(allocator: Allocator, fname: []const u8, code: [:0]const u8) ![]const u8 {
var ast = try std.zig.Ast.parse(allocator, code, .zig);
defer ast.deinit(allocator);
if (ast.errors.len > 0) {
for (ast.errors) |err| {
var buf = std.ArrayList(u8).init(allocator);
defer buf.deinit();
ast.renderError(err, buf.writer()) catch {};
log.err("formatting {s}: {s}", .{ fname, buf.items });
}
return code;
}
return try ast.render(allocator);
}
For the following FBS:
enum AdvancedFeatures : ulong (bit_flags) {
AdvancedArrayFeatures,
AdvancedUnionFeatures,
OptionalScalars,
DefaultVectorsAndStrings,
}
The following enum is generated:
pub const AdvancedFeatures = enum(u64) {
AdvancedArrayFeatures = 1,
AdvancedUnionFeatures = 2,
OptionalScalars = 4,
DefaultVectorsAndStrings = 8,
pub fn tagName(v: @This()) []const u8 {
return @tagName(v);
}
};
Which causes the @intToEnum
cast to fail if multiple bit flags (or none) are set:
pub fn AdvancedFeatures(rcv: Schema) reflection.AdvancedFeatures {
const o = rcv._tab.offset(16);
if (o != 0) {
return rcv._tab.read(reflection.AdvancedFeatures, o + rcv._tab.pos);
}
return @intToEnum(reflection.AdvancedFeatures, 0);
}
One potential fix is to generate a packed struct instead of an enum and read it like you would any other scalar field. The packed struct looks like this:
pub const AdvancedFeatures = packed struct {
AdvancedArrayFeatures: bool,
AdvancedUnionFeatures: bool,
OptionalScalars: bool,
DefaultVectorsAndStrings: bool,
};
Zig has a style guide, but it's subject to change. Everyone has their own preferences coming from different languages.
Make an identifier string pool per-file to handle naming clashes. Give user fields and enums their function, field, and type identifiers. Then:
_tab: flatbufferz.Table
) their identifiersThis will allow removing _
s from field names (like _tab
) and temporaries. It will also allow consistently naming the first self: Self
parameter.
Arrays are only allowed inside of structs and are laid out like sequential struct fields.
struct Vec4 {
v: [float:4];
}
Gives:
thread 280733 panic: TODO TypeFmt.init() base_ty=reflection.BaseType.fb.BaseType.Array
/home/thesm/.zvm/master/lib/std/debug.zig:291:22: 0x30b555 in panicExtra__anon_19121 (flatc-zig)
std.builtin.panic(msg, trace, ret_addr);
^
/home/thesm/.zvm/master/lib/std/debug.zig:266:15: 0x2e9449 in panic__anon_17084 (flatc-zig)
panicExtra(null, null, format, args);
^
/home/thesm/Downloads/flatbufferz/src/common.zig:19:20: 0x2c9318 in todo__anon_16220 (flatc-zig)
std.debug.panic("TODO " ++ fmt, args);
^
/home/thesm/Downloads/flatbufferz/src/codegen.zig:183:20: 0x2c8f54 in format__anon_16215 (flatc-zig)
} else todo("TypeFmt.init() base_ty={}", .{base_ty});
Instead of TitleCaseFunctionNames
follow https://ziglang.org/documentation/master/#Names and use camelCaseFunctionNames
like the language and standard library.
More convenient. Requires object API.
generate simple tests which do a serialize/deserialize/serialize cycle and compare the resulting bytes
this makes running scripts/test-all.sh useful for catching compile errors in generated code
--no-gen-object-api
still imports object API ...T
types and Unpack
functions taking a ...T
.
Ideally this flag is just removed and always turned on since it allows for nicer Unpack
methods.
Save a dereferencing .?
and a memory allocation per-field. Change to optional_field: ?T
instead of optional_field: ?*T
.
Fbs:
union Type { Null, Int }
table Field { type: Type }
generates a non-existent call to rcv.Type_Type()
instead of the correct rcv.TypeType()
:
pub fn UnpackTo(rcv: Field, t: *FieldT, __pack_opts: fb.common.PackOptions) !void {
if (rcv.Type_()) |_tab| {
t.type = try TypeT.Unpack(rcv.Type_Type(), _tab, __pack_opts);
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.