" --escape-proto-ids Disable appending '_' in namespaces names.\n"
" --gen-object-api Generate an additional object-based API.\n"
" --cpp-ptr-type T Set object API pointer type (default std::unique_ptr)\n"
+ " --no-js-exports Removes Node.js style export lines in JS.\n"
+ " --goog-js-export Uses goog.exports* for closure compiler exporting in JS.\n"
" --raw-binary Allow binaries without file_indentifier to be read.\n"
" This may crash flatc given a mismatched schema.\n"
" --proto Input is a .proto, translate to .fbs.\n"
opts.allow_non_utf8 = true;
} else if(arg == "--no-js-exports") {
opts.skip_js_exports = true;
+ } else if(arg == "--goog-js-export") {
+ opts.use_goog_js_export_format = true;
} else if(arg == "--defaults-json") {
opts.output_default_scalars_in_json = true;
} else if (arg == "--unknown-json") {
code += "/**\n * @const\n * @namespace\n */\n";
if (it->find('.') == std::string::npos) {
code += "var ";
- exports += "this." + *it + " = " + *it + ";\n";
+ if(parser_.opts.use_goog_js_export_format) {
+ exports += "goog.exportSymbol('" + *it + "', " + *it + ");\n";
+ } else {
+ exports += "this." + *it + " = " + *it + ";\n";
+ }
}
code += *it + " = " + *it + " || {};\n\n";
}
GenDocComment(enum_def.doc_comment, code_ptr, "@enum");
if (enum_def.defined_namespace->components.empty()) {
code += "var ";
- exports += "this." + enum_def.name + " = " + enum_def.name + ";\n";
+ if(parser_.opts.use_goog_js_export_format) {
+ exports += "goog.exportSymbol('" + enum_def.name + "', " + enum_def.name +
+ ");\n";
+ } else {
+ exports += "this." + enum_def.name + " = " + enum_def.name + ";\n";
+ }
}
code += WrapInNameSpace(enum_def) + " = {\n";
for (auto it = enum_def.vals.vec.begin();
if (auto val = value.type.enum_def->ReverseLookup(
atoi(value.constant.c_str()), false)) {
return WrapInNameSpace(*value.type.enum_def) + "." + val->name;
+ } else {
+ return "/** @type {" + WrapInNameSpace(*value.type.enum_def) + "} */ ("
+ + value.constant + ")";
}
}
std::string object_name = WrapInNameSpace(struct_def);
GenDocComment(struct_def.doc_comment, code_ptr, "@constructor");
if (isStatement) {
- exports += "this." + struct_def.name + " = " + struct_def.name + ";\n";
+ if(parser_.opts.use_goog_js_export_format) {
+ exports += "goog.exportSymbol('" + struct_def.name + "', " +
+ struct_def.name + ");\n";
+ } else {
+ exports += "this." + struct_def.name + " = " + struct_def.name + ";\n";
+ }
code += "function " + object_name;
} else {
code += object_name + " = function";
field.value.type.element == BASE_TYPE_ULONG) {
code += "this.bb.createLong(0, 0)";
} else if (IsScalar(field.value.type.element)) {
- code += "0";
+ if (field.value.type.enum_def) {
+ code += "/** @type {" +
+ WrapInNameSpace(*field.value.type.enum_def) + "} */ (" +
+ field.value.constant + ")";
+ } else {
+ code += "0";
+ }
} else {
code += "null";
}
}
code += "};\n\n";
+ if(parser_.opts.use_goog_js_export_format) {
+ exports += "goog.exportProperty(" + object_name + ".prototype, '" +
+ MakeCamel(field.name, false) + "', " + object_name + ".prototype." +
+ MakeCamel(field.name, false) + ");\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";
code += " this.bb.write" + MakeCamel(GenType(field.value.type)) + "(this.bb_pos + offset, value);\n";
code += " return true;\n";
code += "};\n\n";
+
+ if(parser_.opts.use_goog_js_export_format) {
+ exports += "goog.exportProperty(" + object_name +
+ ".prototype, 'mutate_" + field.name + "', " + object_name +
+ ".prototype.mutate_" + field.name + ");\n";
+ }
}
// Emit vector helpers
code += "Length = function() {\n" + offset_prefix;
code += "this.bb.__vector_len(this.bb_pos + offset) : 0;\n};\n\n";
+ if(parser_.opts.use_goog_js_export_format) {
+ exports += "goog.exportProperty(" + object_name + ".prototype, '" +
+ MakeCamel(field.name, false) + "Length', " + object_name +
+ ".prototype." + MakeCamel(field.name, false) + "Length);\n";
+ }
+
// For scalar types, emit a typed array helper
auto vectorType = field.value.type.VectorType();
if (IsScalar(vectorType.base_type) && !IsLong(vectorType.base_type)) {
code += "new " + GenType(vectorType) + "Array(this.bb.bytes().buffer, "
"this.bb.bytes().byteOffset + this.bb.__vector(this.bb_pos + offset), "
"this.bb.__vector_len(this.bb_pos + offset)) : null;\n};\n\n";
+
+ if(parser_.opts.use_goog_js_export_format) {
+ exports += "goog.exportProperty(" + object_name + ".prototype, '" +
+ MakeCamel(field.name, false) + "Array', " + object_name +
+ ".prototype." + MakeCamel(field.name, false) + "Array);\n";
+ }
}
}
}