mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-07 22:03:40 +00:00
[Python/JS/TS] Codegen SizeOf method for structs (#6136)
* [Python] Codegen SizeOf classmethod for structs This codegens a `SizeOf()` classmethod for all structs since we can't determine the size of a FlatBuffer generated struct from Python otherwise. * [JS/TS] Codegen sizeOf static method for structs This codegens a `sizeOf()` static method for all structs since we can't determine the size of a FlatBuffer generated struct from JavaScript or TypeScript otherwise.
This commit is contained in:
@@ -1795,6 +1795,18 @@ class JsTsGenerator : public BaseGenerator {
|
||||
code += "}\n\n";
|
||||
}
|
||||
|
||||
// Emit the size of the struct.
|
||||
if (struct_def.fixed) {
|
||||
GenDocComment(code_ptr, GenTypeAnnotation(kReturns, "number", "", false));
|
||||
if (lang_.language == IDLOptions::kTs) {
|
||||
code += "static sizeOf():number {\n";
|
||||
} else {
|
||||
code += object_name + ".sizeOf = function() {\n";
|
||||
}
|
||||
code += " return " + NumToString(struct_def.bytesize) + ";\n";
|
||||
code += "}\n\n";
|
||||
}
|
||||
|
||||
// Emit a factory constructor
|
||||
if (struct_def.fixed) {
|
||||
std::string annotations =
|
||||
|
||||
Reference in New Issue
Block a user