diff --git a/BUILD.gn b/BUILD.gn index da8f801680..ae58e53473 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1130,6 +1130,7 @@ source_set("v8_base") { "src/isolate-inl.h", "src/isolate.cc", "src/isolate.h", + "src/json-parser.h", "src/json-stringifier.h", "src/key-accumulator.h", "src/key-accumulator.cc", @@ -1165,7 +1166,6 @@ source_set("v8_base") { "src/parsing/expression-classifier.h", "src/parsing/func-name-inferrer.cc", "src/parsing/func-name-inferrer.h", - "src/parsing/json-parser.h", "src/parsing/parameter-initializer-rewriter.cc", "src/parsing/parameter-initializer-rewriter.h", "src/parsing/parser-base.h", diff --git a/src/api.cc b/src/api.cc index aa6c8a8ba9..b7dcd446c1 100644 --- a/src/api.cc +++ b/src/api.cc @@ -38,8 +38,8 @@ #include "src/global-handles.h" #include "src/icu_util.h" #include "src/isolate-inl.h" +#include "src/json-parser.h" #include "src/messages.h" -#include "src/parsing/json-parser.h" #include "src/parsing/parser.h" #include "src/parsing/scanner-character-streams.h" #include "src/pending-compilation-error-handler.h" diff --git a/src/parsing/json-parser.h b/src/json-parser.h similarity index 97% rename from src/parsing/json-parser.h rename to src/json-parser.h index 096e18801c..fa46cf098e 100644 --- a/src/parsing/json-parser.h +++ b/src/json-parser.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef V8_PARSING_JSON_PARSER_H_ -#define V8_PARSING_JSON_PARSER_H_ +#ifndef V8_JSON_PARSER_H_ +#define V8_JSON_PARSER_H_ #include "src/char-predicates.h" #include "src/conversions.h" @@ -217,11 +217,12 @@ MaybeHandle JsonParser::ParseJson() { // Parse failed. Current character is the unexpected token. Factory* factory = this->factory(); MessageTemplate::Template message; - Handle argument; + Handle arg1 = Handle(Smi::FromInt(position_), isolate()); + Handle arg2; switch (c0_) { case kEndOfString: - message = MessageTemplate::kUnexpectedEOS; + message = MessageTemplate::kJsonParseUnexpectedEOS; break; case '-': case '0': @@ -234,14 +235,15 @@ MaybeHandle JsonParser::ParseJson() { case '7': case '8': case '9': - message = MessageTemplate::kUnexpectedTokenNumber; + message = MessageTemplate::kJsonParseUnexpectedTokenNumber; break; case '"': - message = MessageTemplate::kUnexpectedTokenString; + message = MessageTemplate::kJsonParseUnexpectedTokenString; break; default: - message = MessageTemplate::kUnexpectedToken; - argument = factory->LookupSingleCharacterStringFromCode(c0_); + message = MessageTemplate::kJsonParseUnexpectedToken; + arg2 = arg1; + arg1 = factory->LookupSingleCharacterStringFromCode(c0_); break; } @@ -250,7 +252,7 @@ MaybeHandle JsonParser::ParseJson() { // separated source file. isolate()->debug()->OnCompileError(script); MessageLocation location(script, position_, position_ + 1); - Handle error = factory->NewSyntaxError(message, argument); + Handle error = factory->NewSyntaxError(message, arg1, arg2); return isolate()->template Throw(error, &location); } return result; @@ -839,4 +841,4 @@ Handle JsonParser::ScanJsonString() { } // namespace internal } // namespace v8 -#endif // V8_PARSING_JSON_PARSER_H_ +#endif // V8_JSON_PARSER_H_ diff --git a/src/messages.h b/src/messages.h index c8a072761f..af1538a1aa 100644 --- a/src/messages.h +++ b/src/messages.h @@ -406,6 +406,10 @@ class CallSite { T(InvalidLhsInPrefixOp, \ "Invalid left-hand side expression in prefix operation") \ T(InvalidRegExpFlags, "Invalid flags supplied to RegExp constructor '%'") \ + T(JsonParseUnexpectedEOS, "Unexpected end of JSON input") \ + T(JsonParseUnexpectedToken, "Unexpected token % in JSON at position %") \ + T(JsonParseUnexpectedTokenNumber, "Unexpected number in JSON at position %") \ + T(JsonParseUnexpectedTokenString, "Unexpected string in JSON at position %") \ T(LabelRedeclaration, "Label '%' has already been declared") \ T(MalformedArrowFunParamList, "Malformed arrow function parameter list") \ T(MalformedRegExp, "Invalid regular expression: /%/: %") \ diff --git a/src/runtime/runtime-json.cc b/src/runtime/runtime-json.cc index 45f8183052..07232d59b8 100644 --- a/src/runtime/runtime-json.cc +++ b/src/runtime/runtime-json.cc @@ -7,9 +7,9 @@ #include "src/arguments.h" #include "src/char-predicates-inl.h" #include "src/isolate-inl.h" +#include "src/json-parser.h" #include "src/json-stringifier.h" #include "src/objects-inl.h" -#include "src/parsing/json-parser.h" namespace v8 { namespace internal { diff --git a/test/mjsunit/messages.js b/test/mjsunit/messages.js index 8da7e6bd7b..772670a06c 100644 --- a/test/mjsunit/messages.js +++ b/test/mjsunit/messages.js @@ -345,6 +345,26 @@ test(function() { eval("/a/x.test(\"a\");"); }, "Invalid regular expression flags", SyntaxError); +//kJsonParseUnexpectedEOS +test(function() { + JSON.parse("{") +}, "Unexpected end of JSON input", SyntaxError); + +// kJsonParseUnexpectedTokenAt +test(function() { + JSON.parse("/") +}, "Unexpected token / in JSON at position 0", SyntaxError); + +// kJsonParseUnexpectedTokenNumberAt +test(function() { + JSON.parse("{ 1") +}, "Unexpected number in JSON at position 2", SyntaxError); + +// kJsonParseUnexpectedTokenStringAt +test(function() { + JSON.parse('"""') +}, "Unexpected string in JSON at position 2", SyntaxError); + // kMalformedRegExp test(function() { /(/.test("a"); @@ -355,27 +375,6 @@ test(function() { new Function(")", ""); }, "Function arg string contains parenthesis", SyntaxError); -// kUnexpectedEOS -test(function() { - JSON.parse("{") -}, "Unexpected end of input", SyntaxError); - -// kUnexpectedToken -test(function() { - JSON.parse("/") -}, "Unexpected token /", SyntaxError); - -// kUnexpectedTokenNumber -test(function() { - JSON.parse("{ 1") -}, "Unexpected number", SyntaxError); - -// kUnexpectedTokenString -test(function() { - JSON.parse('"""') -}, "Unexpected string", SyntaxError); - - // === ReferenceError === // kNotDefined diff --git a/tools/gyp/v8.gyp b/tools/gyp/v8.gyp index 5951a0c6a7..5b80827a30 100644 --- a/tools/gyp/v8.gyp +++ b/tools/gyp/v8.gyp @@ -952,6 +952,7 @@ '../../src/isolate-inl.h', '../../src/isolate.cc', '../../src/isolate.h', + '../../src/json-parser.h', '../../src/json-stringifier.h', '../../src/key-accumulator.h', '../../src/key-accumulator.cc', @@ -989,7 +990,6 @@ '../../src/parsing/expression-classifier.h', '../../src/parsing/func-name-inferrer.cc', '../../src/parsing/func-name-inferrer.h', - '../../src/parsing/json-parser.h', '../../src/parsing/parameter-initializer-rewriter.cc', '../../src/parsing/parameter-initializer-rewriter.h', '../../src/parsing/parser-base.h',