Commit 0230a717 authored by Michael Paulson's avatar Michael Paulson

feat(mutable-js): The mutable Scalar generation.

This is just the initial commit to start the conversation on adding mutation to javascript.
parent 1bba4fd9
...@@ -79,7 +79,7 @@ class JsGenerator : public BaseGenerator { ...@@ -79,7 +79,7 @@ class JsGenerator : public BaseGenerator {
for (auto it = parser_.structs_.vec.begin(); for (auto it = parser_.structs_.vec.begin();
it != parser_.structs_.vec.end(); ++it) { it != parser_.structs_.vec.end(); ++it) {
auto &struct_def = **it; auto &struct_def = **it;
GenStruct(struct_def, decl_code_ptr, exports_code_ptr); GenStruct(parser_, struct_def, decl_code_ptr, exports_code_ptr);
} }
} }
void GenNamespaces(std::string *code_ptr, std::string *exports_ptr) { void GenNamespaces(std::string *code_ptr, std::string *exports_ptr) {
...@@ -361,7 +361,7 @@ static void GenStructBody(const StructDef &struct_def, ...@@ -361,7 +361,7 @@ static void GenStructBody(const StructDef &struct_def,
} }
// Generate an accessor struct with constructor for a flatbuffers struct. // Generate an accessor struct with constructor for a flatbuffers struct.
void GenStruct(StructDef &struct_def, std::string *code_ptr, std::string *exports_ptr) { void GenStruct(const Parser &parser, StructDef &struct_def, std::string *code_ptr, std::string *exports_ptr) {
if (struct_def.generated) return; if (struct_def.generated) return;
std::string &code = *code_ptr; std::string &code = *code_ptr;
std::string &exports = *exports_ptr; std::string &exports = *exports_ptr;
...@@ -550,6 +550,22 @@ void GenStruct(StructDef &struct_def, std::string *code_ptr, std::string *export ...@@ -550,6 +550,22 @@ void GenStruct(StructDef &struct_def, std::string *code_ptr, std::string *export
} }
code += "};\n\n"; code += "};\n\n";
// Adds the mutable scalar value to the output
if (IsScalar(field.value.type.base_type) && parser.opts.mutable_buffer) {
std::string annotations = "@param {" + GenTypeName(field.value.type, true) + "} value\n";
GenDocComment(code_ptr, annotations +
"@returns {boolean}");
code += object_name + ".prototype.mutate_" + field.name + " = function(value) {\n";
code += " var offset = this.bb.__offset(this.bb_pos, " + NumToString(field.value.offset) + ")\n\n";
code += " if (offset === 0) {\n";
code += " return false;\n";
code += " }\n\n";
code += " this.bb.write" + MakeCamel(GenType(field.value.type)) + "(this.bb_pos + offset, value);\n";
code += " return true;\n";
code += "}\n\n";
}
// Emit vector helpers // Emit vector helpers
if (field.value.type.base_type == BASE_TYPE_VECTOR) { if (field.value.type.base_type == BASE_TYPE_VECTOR) {
// Emit a length helper // Emit a length helper
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment