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