This commit is contained in:
Wouter van Oortmerssen
2016-03-07 15:04:18 -08:00
8 changed files with 173 additions and 42 deletions

View File

@@ -189,7 +189,7 @@ static std::string GenGetter(const Type &type, const std::string &arguments) {
}
}
static std::string GenDefaultValue(const Value &value) {
static std::string GenDefaultValue(const Value &value, const std::string &context) {
if (value.type.enum_def) {
if (auto val = value.type.enum_def->ReverseLookup(
atoi(value.constant.c_str()), false)) {
@@ -205,13 +205,11 @@ static std::string GenDefaultValue(const Value &value) {
return "null";
case BASE_TYPE_LONG:
case BASE_TYPE_ULONG:
if (value.constant != "0") {
int64_t constant = StringToInt(value.constant.c_str());
return "new flatbuffers.Long(" + NumToString((int32_t)constant) +
", " + NumToString((int32_t)(constant >> 32)) + ")";
}
return "flatbuffers.Long.ZERO";
case BASE_TYPE_ULONG: {
int64_t constant = StringToInt(value.constant.c_str());
return context + ".createLong(" + NumToString((int32_t)constant) +
", " + NumToString((int32_t)(constant >> 32)) + ")";
}
default:
return value.constant;
@@ -417,7 +415,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
index += ", optionalEncoding";
}
code += offset_prefix + GenGetter(field.value.type,
"(" + index + ")") + " : " + GenDefaultValue(field.value);
"(" + index + ")") + " : " + GenDefaultValue(field.value, "this.bb");
code += ";\n";
}
}
@@ -485,7 +483,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
code += "false";
} else if (field.value.type.element == BASE_TYPE_LONG ||
field.value.type.element == BASE_TYPE_ULONG) {
code += "flatbuffers.Long.ZERO";
code += "this.bb.createLong(0, 0)";
} else if (IsScalar(field.value.type.element)) {
code += "0";
} else {
@@ -511,12 +509,24 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
}
code += "};\n\n";
// Emit a length helper
// Emit vector helpers
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
// Emit a length helper
GenDocComment(code_ptr, "@returns {number}");
code += object_name + ".prototype." + MakeCamel(field.name, false);
code += "Length = function() {\n" + offset_prefix;
code += "this.bb.__vector_len(this.bb_pos + offset) : 0;\n};\n\n";
// For scalar types, emit a typed array helper
auto vectorType = field.value.type.VectorType();
if (IsScalar(vectorType.base_type)) {
GenDocComment(code_ptr, "@returns {" + GenType(vectorType) + "Array}");
code += object_name + ".prototype." + MakeCamel(field.name, false);
code += "Array = function() {\n" + offset_prefix;
code += "new " + GenType(vectorType) + "Array(this.bb.bytes().buffer, "
"this.bb.__vector(this.bb_pos + offset), "
"this.bb.__vector_len(this.bb_pos + offset)) : null;\n};\n\n";
}
}
}
@@ -570,7 +580,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
if (field.value.type.base_type == BASE_TYPE_BOOL) {
code += "+";
}
code += GenDefaultValue(field.value);
code += GenDefaultValue(field.value, "builder");
}
code += ");\n};\n\n";

View File

@@ -1557,8 +1557,12 @@ CheckedError Parser::SkipJsonObject() {
for (;;) {
if ((!opts.strict_json || !fieldn) && Is('}')) break;
if (!Is(kTokenStringConstant))
if (!Is(kTokenStringConstant)) {
EXPECT(opts.strict_json ? kTokenStringConstant : kTokenIdentifier);
}
else {
NEXT();
}
EXPECT(':');
ECHECK(SkipAnyJsonValue());