Fixes for JS generator in VS

parent 9c9fce96
...@@ -271,6 +271,7 @@ ...@@ -271,6 +271,7 @@
<ClCompile Include="..\..\src\idl_gen_go.cpp"> <ClCompile Include="..\..\src\idl_gen_go.cpp">
<WarningLevel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Level4</WarningLevel> <WarningLevel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Level4</WarningLevel>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\idl_gen_js.cpp" />
<ClCompile Include="..\..\src\idl_gen_python.cpp" /> <ClCompile Include="..\..\src\idl_gen_python.cpp" />
<ClCompile Include="..\..\src\idl_parser.cpp" /> <ClCompile Include="..\..\src\idl_parser.cpp" />
<ClCompile Include="..\..\src\idl_gen_cpp.cpp" /> <ClCompile Include="..\..\src\idl_gen_cpp.cpp" />
......
...@@ -90,8 +90,17 @@ inline std::string IntToStringHex(int i, int xdigits) { ...@@ -90,8 +90,17 @@ inline std::string IntToStringHex(int i, int xdigits) {
return ss.str(); return ss.str();
} }
// Portable implementation of strtoull(). // Portable implementation of strtoll().
inline int64_t StringToInt(const char *str, int base = 10) { inline int64_t StringToInt(const char *str, int base = 10) {
#ifdef _MSC_VER
return _strtoi64(str, nullptr, base);
#else
return strtoll(str, nullptr, base);
#endif
}
// Portable implementation of strtoull().
inline int64_t StringToUInt(const char *str, int base = 10) {
#ifdef _MSC_VER #ifdef _MSC_VER
return _strtoui64(str, nullptr, base); return _strtoui64(str, nullptr, base);
#else #else
......
...@@ -101,7 +101,7 @@ static void GenDocComment(const std::vector<std::string> &dc, ...@@ -101,7 +101,7 @@ static void GenDocComment(const std::vector<std::string> &dc,
} }
if (indent) code += indent; if (indent) code += indent;
std::string::size_type start = 0; std::string::size_type start = 0;
while (true) { for (;;) {
auto end = extra_lines.find('\n', start); auto end = extra_lines.find('\n', start);
if (end != std::string::npos) { if (end != std::string::npos) {
code += " * " + extra_lines.substr(start, end - start) + "\n"; code += " * " + extra_lines.substr(start, end - start) + "\n";
...@@ -207,7 +207,7 @@ static std::string GenDefaultValue(const Value &value) { ...@@ -207,7 +207,7 @@ static std::string GenDefaultValue(const Value &value) {
case BASE_TYPE_LONG: case BASE_TYPE_LONG:
case BASE_TYPE_ULONG: case BASE_TYPE_ULONG:
if (value.constant != "0") { if (value.constant != "0") {
int64_t constant = std::atoll(value.constant.c_str()); int64_t constant = StringToInt(value.constant.c_str());
return "new flatbuffers.Long(" + NumToString((int32_t)constant) + return "new flatbuffers.Long(" + NumToString((int32_t)constant) +
", " + NumToString((int32_t)(constant >> 32)) + ")"; ", " + NumToString((int32_t)(constant >> 32)) + ")";
} }
...@@ -218,13 +218,8 @@ static std::string GenDefaultValue(const Value &value) { ...@@ -218,13 +218,8 @@ static std::string GenDefaultValue(const Value &value) {
} }
} }
enum struct InOut { static std::string GenTypeName(const Type &type, bool input) {
IN, if (!input) {
OUT,
};
static std::string GenTypeName(const Type &type, InOut inOut) {
if (inOut == InOut::OUT) {
if (type.base_type == BASE_TYPE_STRING) { if (type.base_type == BASE_TYPE_STRING) {
return "string|Uint8Array"; return "string|Uint8Array";
} }
...@@ -289,7 +284,7 @@ static void GenStructArgs(const StructDef &struct_def, ...@@ -289,7 +284,7 @@ static void GenStructArgs(const StructDef &struct_def,
GenStructArgs(*field.value.type.struct_def, annotations, arguments, GenStructArgs(*field.value.type.struct_def, annotations, arguments,
nameprefix + field.name + "_"); nameprefix + field.name + "_");
} else { } else {
*annotations += "@param {" + GenTypeName(field.value.type, InOut::IN); *annotations += "@param {" + GenTypeName(field.value.type, true);
*annotations += "} " + nameprefix + field.name + "\n"; *annotations += "} " + nameprefix + field.name + "\n";
*arguments += ", " + nameprefix + field.name; *arguments += ", " + nameprefix + field.name;
} }
...@@ -406,7 +401,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, ...@@ -406,7 +401,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
GenDocComment(field.doc_comment, code_ptr, GenDocComment(field.doc_comment, code_ptr,
std::string(field.value.type.base_type == BASE_TYPE_STRING ? std::string(field.value.type.base_type == BASE_TYPE_STRING ?
"@param {flatbuffers.Encoding=} optionalEncoding\n" : "") + "@param {flatbuffers.Encoding=} optionalEncoding\n" : "") +
"@returns {" + GenTypeName(field.value.type, InOut::OUT) + "}"); "@returns {" + GenTypeName(field.value.type, false) + "}");
code += object_name + ".prototype." + MakeCamel(field.name, false); code += object_name + ".prototype." + MakeCamel(field.name, false);
code += " = function("; code += " = function(";
if (field.value.type.base_type == BASE_TYPE_STRING) { if (field.value.type.base_type == BASE_TYPE_STRING) {
...@@ -452,7 +447,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, ...@@ -452,7 +447,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
case BASE_TYPE_VECTOR: { case BASE_TYPE_VECTOR: {
auto vectortype = field.value.type.VectorType(); auto vectortype = field.value.type.VectorType();
auto vectortypename = GenTypeName(vectortype, InOut::OUT); auto vectortypename = GenTypeName(vectortype, false);
auto inline_size = InlineSize(vectortype); auto inline_size = InlineSize(vectortype);
auto index = "this.bb.__vector(this.bb_pos + offset) + index" + auto index = "this.bb.__vector(this.bb_pos + offset) + index" +
MaybeScale(inline_size); MaybeScale(inline_size);
...@@ -559,7 +554,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, ...@@ -559,7 +554,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
// Generate the field insertion method // Generate the field insertion method
GenDocComment(code_ptr, GenDocComment(code_ptr,
"@param {flatbuffers.Builder} builder\n" "@param {flatbuffers.Builder} builder\n"
"@param {" + GenTypeName(field.value.type, InOut::IN) + "} " + "@param {" + GenTypeName(field.value.type, true) + "} " +
argname); argname);
code += object_name + ".add" + MakeCamel(field.name); code += object_name + ".add" + MakeCamel(field.name);
code += " = function(builder, " + argname + ") {\n"; code += " = function(builder, " + argname + ") {\n";
...@@ -588,7 +583,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, ...@@ -588,7 +583,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
if (!IsStruct(vector_type)) { if (!IsStruct(vector_type)) {
GenDocComment(code_ptr, GenDocComment(code_ptr,
"@param {flatbuffers.Builder} builder\n" "@param {flatbuffers.Builder} builder\n"
"@param {Array.<" + GenTypeName(vector_type, InOut::IN) + "@param {Array.<" + GenTypeName(vector_type, true) +
">} data\n" ">} data\n"
"@returns {flatbuffers.Offset}"); "@returns {flatbuffers.Offset}");
code += object_name + ".create" + MakeCamel(field.name); code += object_name + ".create" + MakeCamel(field.name);
......
...@@ -159,7 +159,7 @@ int64_t Parser::ParseHexNum(int nibbles) { ...@@ -159,7 +159,7 @@ int64_t Parser::ParseHexNum(int nibbles) {
Error("escape code must be followed by " + NumToString(nibbles) + Error("escape code must be followed by " + NumToString(nibbles) +
" hex digits"); " hex digits");
std::string target(cursor_, cursor_ + nibbles); std::string target(cursor_, cursor_ + nibbles);
auto val = StringToInt(target.c_str(), 16); auto val = StringToUInt(target.c_str(), 16);
cursor_ += nibbles; cursor_ += nibbles;
return val; return val;
} }
...@@ -288,7 +288,7 @@ void Parser::Next() { ...@@ -288,7 +288,7 @@ void Parser::Next() {
cursor_++; cursor_++;
while (isxdigit(static_cast<unsigned char>(*cursor_))) cursor_++; while (isxdigit(static_cast<unsigned char>(*cursor_))) cursor_++;
attribute_.append(start + 2, cursor_); attribute_.append(start + 2, cursor_);
attribute_ = NumToString(StringToInt(attribute_.c_str(), 16)); attribute_ = NumToString(StringToUInt(attribute_.c_str(), 16));
token_ = kTokenIntegerConstant; token_ = kTokenIntegerConstant;
return; return;
} }
......
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