danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_JSON_PARSER_H_ |
| 29 | #define V8_JSON_PARSER_H_ |
| 30 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 31 | #include "v8.h" |
| 32 | |
| 33 | #include "char-predicates-inl.h" |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 34 | #include "v8conversions.h" |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 35 | #include "messages.h" |
| 36 | #include "spaces-inl.h" |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 37 | #include "token.h" |
| 38 | |
| 39 | namespace v8 { |
| 40 | namespace internal { |
| 41 | |
| 42 | // A simple json parser. |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 43 | template <bool seq_ascii> |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 44 | class JsonParser BASE_EMBEDDED { |
| 45 | public: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 46 | static Handle<Object> Parse(Handle<String> source, Zone* zone) { |
| 47 | return JsonParser().ParseJson(source, zone); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static const int kEndOfString = -1; |
| 51 | |
| 52 | private: |
| 53 | // Parse a string containing a single JSON value. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 54 | Handle<Object> ParseJson(Handle<String> source, Zone* zone); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 55 | |
| 56 | inline void Advance() { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 57 | position_++; |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 58 | if (position_ >= source_length_) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 59 | c0_ = kEndOfString; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 60 | } else if (seq_ascii) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 61 | c0_ = seq_source_->SeqOneByteStringGet(position_); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 62 | } else { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 63 | c0_ = source_->Get(position_); |
| 64 | } |
| 65 | } |
| 66 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 67 | // The JSON lexical grammar is specified in the ECMAScript 5 standard, |
| 68 | // section 15.12.1.1. The only allowed whitespace characters between tokens |
| 69 | // are tab, carriage-return, newline and space. |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 70 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 71 | inline void AdvanceSkipWhitespace() { |
| 72 | do { |
| 73 | Advance(); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 74 | } while (c0_ == ' ' || c0_ == '\t' || c0_ == '\n' || c0_ == '\r'); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 75 | } |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 76 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 77 | inline void SkipWhitespace() { |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 78 | while (c0_ == ' ' || c0_ == '\t' || c0_ == '\n' || c0_ == '\r') { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 79 | Advance(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | inline uc32 AdvanceGetChar() { |
| 84 | Advance(); |
| 85 | return c0_; |
| 86 | } |
| 87 | |
| 88 | // Checks that current charater is c. |
| 89 | // If so, then consume c and skip whitespace. |
| 90 | inline bool MatchSkipWhiteSpace(uc32 c) { |
| 91 | if (c0_ == c) { |
| 92 | AdvanceSkipWhitespace(); |
| 93 | return true; |
| 94 | } |
| 95 | return false; |
| 96 | } |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 97 | |
| 98 | // A JSON string (production JSONString) is subset of valid JavaScript string |
| 99 | // literals. The string must only be double-quoted (not single-quoted), and |
| 100 | // the only allowed backslash-escapes are ", /, \, b, f, n, r, t and |
| 101 | // four-digit hex escapes (uXXXX). Any other use of backslashes is invalid. |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 102 | Handle<String> ParseJsonString() { |
| 103 | return ScanJsonString<false>(); |
| 104 | } |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 105 | Handle<String> ParseJsonInternalizedString() { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 106 | return ScanJsonString<true>(); |
| 107 | } |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 108 | template <bool is_internalized> |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 109 | Handle<String> ScanJsonString(); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 110 | // Creates a new string and copies prefix[start..end] into the beginning |
| 111 | // of it. Then scans the rest of the string, adding characters after the |
| 112 | // prefix. Called by ScanJsonString when reaching a '\' or non-ASCII char. |
| 113 | template <typename StringType, typename SinkChar> |
| 114 | Handle<String> SlowScanJsonString(Handle<String> prefix, int start, int end); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 115 | |
| 116 | // A JSON number (production JSONNumber) is a subset of the valid JavaScript |
| 117 | // decimal number literals. |
| 118 | // It includes an optional minus sign, must have at least one |
| 119 | // digit before and after a decimal point, may not have prefixed zeros (unless |
| 120 | // the integer part is zero), and may include an exponent part (e.g., "e-10"). |
| 121 | // Hexadecimal and octal numbers are not allowed. |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 122 | Handle<Object> ParseJsonNumber(); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 123 | |
| 124 | // Parse a single JSON value from input (grammar production JSONValue). |
| 125 | // A JSON value is either a (double-quoted) string literal, a number literal, |
| 126 | // one of "true", "false", or "null", or an object or array literal. |
| 127 | Handle<Object> ParseJsonValue(); |
| 128 | |
| 129 | // Parse a JSON object literal (grammar production JSONObject). |
| 130 | // An object literal is a squiggly-braced and comma separated sequence |
| 131 | // (possibly empty) of key/value pairs, where the key is a JSON string |
| 132 | // literal, the value is a JSON value, and the two are separated by a colon. |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 133 | // A JSON array doesn't allow numbers and identifiers as keys, like a |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 134 | // JavaScript array. |
| 135 | Handle<Object> ParseJsonObject(); |
| 136 | |
| 137 | // Parses a JSON array literal (grammar production JSONArray). An array |
| 138 | // literal is a square-bracketed and comma separated sequence (possibly empty) |
| 139 | // of JSON values. |
| 140 | // A JSON array doesn't allow leaving out values from the sequence, nor does |
| 141 | // it allow a terminal comma, like a JavaScript array does. |
| 142 | Handle<Object> ParseJsonArray(); |
| 143 | |
| 144 | |
| 145 | // Mark that a parsing error has happened at the current token, and |
| 146 | // return a null handle. Primarily for readability. |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 147 | inline Handle<Object> ReportUnexpectedCharacter() { |
| 148 | return Handle<Object>::null(); |
| 149 | } |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 150 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 151 | inline Isolate* isolate() { return isolate_; } |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 152 | inline Factory* factory() { return factory_; } |
| 153 | inline Handle<JSFunction> object_constructor() { return object_constructor_; } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 154 | inline Zone* zone() const { return zone_; } |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 155 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 156 | static const int kInitialSpecialStringLength = 1024; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 157 | static const int kPretenureTreshold = 100 * 1024; |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 158 | |
| 159 | |
| 160 | private: |
| 161 | Handle<String> source_; |
| 162 | int source_length_; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 163 | Handle<SeqOneByteString> seq_source_; |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 164 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 165 | PretenureFlag pretenure_; |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 166 | Isolate* isolate_; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 167 | Factory* factory_; |
| 168 | Handle<JSFunction> object_constructor_; |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 169 | uc32 c0_; |
| 170 | int position_; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 171 | Zone* zone_; |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 174 | template <bool seq_ascii> |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 175 | Handle<Object> JsonParser<seq_ascii>::ParseJson(Handle<String> source, |
| 176 | Zone* zone) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 177 | isolate_ = source->map()->GetHeap()->isolate(); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 178 | factory_ = isolate_->factory(); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 179 | object_constructor_ = Handle<JSFunction>( |
| 180 | isolate()->native_context()->object_function(), isolate()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 181 | zone_ = zone; |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 182 | FlattenString(source); |
| 183 | source_ = source; |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 184 | source_length_ = source_->length(); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 185 | pretenure_ = (source_length_ >= kPretenureTreshold) ? TENURED : NOT_TENURED; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 186 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 187 | // Optimized fast case where we only have ASCII characters. |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 188 | if (seq_ascii) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 189 | seq_source_ = Handle<SeqOneByteString>::cast(source_); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // Set initial position right before the string. |
| 193 | position_ = -1; |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 194 | // Advance to the first character (possibly EOS) |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 195 | AdvanceSkipWhitespace(); |
| 196 | Handle<Object> result = ParseJsonValue(); |
| 197 | if (result.is_null() || c0_ != kEndOfString) { |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 198 | // Some exception (for example stack overflow) is already pending. |
| 199 | if (isolate_->has_pending_exception()) return Handle<Object>::null(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 200 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 201 | // Parse failed. Current character is the unexpected token. |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 202 | const char* message; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 203 | Factory* factory = this->factory(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 204 | Handle<JSArray> array; |
| 205 | |
| 206 | switch (c0_) { |
| 207 | case kEndOfString: |
| 208 | message = "unexpected_eos"; |
| 209 | array = factory->NewJSArray(0); |
| 210 | break; |
| 211 | case '-': |
| 212 | case '0': |
| 213 | case '1': |
| 214 | case '2': |
| 215 | case '3': |
| 216 | case '4': |
| 217 | case '5': |
| 218 | case '6': |
| 219 | case '7': |
| 220 | case '8': |
| 221 | case '9': |
| 222 | message = "unexpected_token_number"; |
| 223 | array = factory->NewJSArray(0); |
| 224 | break; |
| 225 | case '"': |
| 226 | message = "unexpected_token_string"; |
| 227 | array = factory->NewJSArray(0); |
| 228 | break; |
| 229 | default: |
| 230 | message = "unexpected_token"; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 231 | Handle<Object> name = |
| 232 | LookupSingleCharacterStringFromCode(isolate_, c0_); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 233 | Handle<FixedArray> element = factory->NewFixedArray(1); |
| 234 | element->set(0, *name); |
| 235 | array = factory->NewJSArrayWithElements(element); |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | MessageLocation location(factory->NewScript(source), |
| 240 | position_, |
| 241 | position_ + 1); |
| 242 | Handle<Object> result = factory->NewSyntaxError(message, array); |
| 243 | isolate()->Throw(*result, &location); |
| 244 | return Handle<Object>::null(); |
| 245 | } |
| 246 | return result; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | // Parse any JSON value. |
| 251 | template <bool seq_ascii> |
| 252 | Handle<Object> JsonParser<seq_ascii>::ParseJsonValue() { |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 253 | StackLimitCheck stack_check(isolate_); |
| 254 | if (stack_check.HasOverflowed()) { |
| 255 | isolate_->StackOverflow(); |
| 256 | return Handle<Object>::null(); |
| 257 | } |
| 258 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 259 | if (c0_ == '"') return ParseJsonString(); |
| 260 | if ((c0_ >= '0' && c0_ <= '9') || c0_ == '-') return ParseJsonNumber(); |
| 261 | if (c0_ == '{') return ParseJsonObject(); |
| 262 | if (c0_ == '[') return ParseJsonArray(); |
| 263 | if (c0_ == 'f') { |
| 264 | if (AdvanceGetChar() == 'a' && AdvanceGetChar() == 'l' && |
| 265 | AdvanceGetChar() == 's' && AdvanceGetChar() == 'e') { |
| 266 | AdvanceSkipWhitespace(); |
| 267 | return factory()->false_value(); |
| 268 | } |
| 269 | return ReportUnexpectedCharacter(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 270 | } |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 271 | if (c0_ == 't') { |
| 272 | if (AdvanceGetChar() == 'r' && AdvanceGetChar() == 'u' && |
| 273 | AdvanceGetChar() == 'e') { |
| 274 | AdvanceSkipWhitespace(); |
| 275 | return factory()->true_value(); |
| 276 | } |
| 277 | return ReportUnexpectedCharacter(); |
| 278 | } |
| 279 | if (c0_ == 'n') { |
| 280 | if (AdvanceGetChar() == 'u' && AdvanceGetChar() == 'l' && |
| 281 | AdvanceGetChar() == 'l') { |
| 282 | AdvanceSkipWhitespace(); |
| 283 | return factory()->null_value(); |
| 284 | } |
| 285 | return ReportUnexpectedCharacter(); |
| 286 | } |
| 287 | return ReportUnexpectedCharacter(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | |
| 291 | // Parse a JSON object. Position must be right at '{'. |
| 292 | template <bool seq_ascii> |
| 293 | Handle<Object> JsonParser<seq_ascii>::ParseJsonObject() { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 294 | Handle<JSObject> json_object = |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 295 | factory()->NewJSObject(object_constructor(), pretenure_); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 296 | ASSERT_EQ(c0_, '{'); |
| 297 | |
| 298 | AdvanceSkipWhitespace(); |
| 299 | if (c0_ != '}') { |
| 300 | do { |
| 301 | if (c0_ != '"') return ReportUnexpectedCharacter(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 302 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 303 | int start_position = position_; |
| 304 | Advance(); |
| 305 | |
| 306 | uint32_t index = 0; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 307 | if (c0_ >= '0' && c0_ <= '9') { |
| 308 | // Maybe an array index, try to parse it. |
| 309 | if (c0_ == '0') { |
| 310 | // With a leading zero, the string has to be "0" only to be an index. |
| 311 | Advance(); |
| 312 | } else { |
| 313 | do { |
| 314 | int d = c0_ - '0'; |
| 315 | if (index > 429496729U - ((d > 5) ? 1 : 0)) break; |
| 316 | index = (index * 10) + d; |
| 317 | Advance(); |
| 318 | } while (c0_ >= '0' && c0_ <= '9'); |
| 319 | } |
| 320 | |
| 321 | if (c0_ == '"') { |
| 322 | // Successfully parsed index, parse and store element. |
| 323 | AdvanceSkipWhitespace(); |
| 324 | |
| 325 | if (c0_ != ':') return ReportUnexpectedCharacter(); |
| 326 | AdvanceSkipWhitespace(); |
| 327 | Handle<Object> value = ParseJsonValue(); |
| 328 | if (value.is_null()) return ReportUnexpectedCharacter(); |
| 329 | |
| 330 | JSObject::SetOwnElement(json_object, index, value, kNonStrictMode); |
| 331 | continue; |
| 332 | } |
| 333 | // Not an index, fallback to the slow path. |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 334 | } |
| 335 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 336 | position_ = start_position; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 337 | #ifdef DEBUG |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 338 | c0_ = '"'; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 339 | #endif |
| 340 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 341 | Handle<String> key = ParseJsonInternalizedString(); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 342 | if (key.is_null() || c0_ != ':') return ReportUnexpectedCharacter(); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 343 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 344 | AdvanceSkipWhitespace(); |
| 345 | Handle<Object> value = ParseJsonValue(); |
| 346 | if (value.is_null()) return ReportUnexpectedCharacter(); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 347 | |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 348 | if (JSObject::TryTransitionToField(json_object, key)) { |
| 349 | int index = json_object->LastAddedFieldIndex(); |
| 350 | json_object->FastPropertyAtPut(index, *value); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 351 | } else { |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 352 | JSObject::SetLocalPropertyIgnoreAttributes( |
| 353 | json_object, key, value, NONE); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 354 | } |
| 355 | } while (MatchSkipWhiteSpace(',')); |
| 356 | if (c0_ != '}') { |
| 357 | return ReportUnexpectedCharacter(); |
| 358 | } |
| 359 | } |
| 360 | AdvanceSkipWhitespace(); |
| 361 | return json_object; |
| 362 | } |
| 363 | |
| 364 | // Parse a JSON array. Position must be right at '['. |
| 365 | template <bool seq_ascii> |
| 366 | Handle<Object> JsonParser<seq_ascii>::ParseJsonArray() { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 367 | ZoneScope zone_scope(zone(), DELETE_ON_EXIT); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 368 | ZoneList<Handle<Object> > elements(4, zone()); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 369 | ASSERT_EQ(c0_, '['); |
| 370 | |
| 371 | AdvanceSkipWhitespace(); |
| 372 | if (c0_ != ']') { |
| 373 | do { |
| 374 | Handle<Object> element = ParseJsonValue(); |
| 375 | if (element.is_null()) return ReportUnexpectedCharacter(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 376 | elements.Add(element, zone()); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 377 | } while (MatchSkipWhiteSpace(',')); |
| 378 | if (c0_ != ']') { |
| 379 | return ReportUnexpectedCharacter(); |
| 380 | } |
| 381 | } |
| 382 | AdvanceSkipWhitespace(); |
| 383 | // Allocate a fixed array with all the elements. |
| 384 | Handle<FixedArray> fast_elements = |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 385 | factory()->NewFixedArray(elements.length(), pretenure_); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 386 | for (int i = 0, n = elements.length(); i < n; i++) { |
| 387 | fast_elements->set(i, *elements[i]); |
| 388 | } |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 389 | return factory()->NewJSArrayWithElements( |
| 390 | fast_elements, FAST_ELEMENTS, pretenure_); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | |
| 394 | template <bool seq_ascii> |
| 395 | Handle<Object> JsonParser<seq_ascii>::ParseJsonNumber() { |
| 396 | bool negative = false; |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 397 | int beg_pos = position_; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 398 | if (c0_ == '-') { |
| 399 | Advance(); |
| 400 | negative = true; |
| 401 | } |
| 402 | if (c0_ == '0') { |
| 403 | Advance(); |
| 404 | // Prefix zero is only allowed if it's the only digit before |
| 405 | // a decimal point or exponent. |
| 406 | if ('0' <= c0_ && c0_ <= '9') return ReportUnexpectedCharacter(); |
| 407 | } else { |
| 408 | int i = 0; |
| 409 | int digits = 0; |
| 410 | if (c0_ < '1' || c0_ > '9') return ReportUnexpectedCharacter(); |
| 411 | do { |
| 412 | i = i * 10 + c0_ - '0'; |
| 413 | digits++; |
| 414 | Advance(); |
| 415 | } while (c0_ >= '0' && c0_ <= '9'); |
| 416 | if (c0_ != '.' && c0_ != 'e' && c0_ != 'E' && digits < 10) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 417 | SkipWhitespace(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 418 | return Handle<Smi>(Smi::FromInt((negative ? -i : i)), isolate()); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | if (c0_ == '.') { |
| 422 | Advance(); |
| 423 | if (c0_ < '0' || c0_ > '9') return ReportUnexpectedCharacter(); |
| 424 | do { |
| 425 | Advance(); |
| 426 | } while (c0_ >= '0' && c0_ <= '9'); |
| 427 | } |
| 428 | if (AsciiAlphaToLower(c0_) == 'e') { |
| 429 | Advance(); |
| 430 | if (c0_ == '-' || c0_ == '+') Advance(); |
| 431 | if (c0_ < '0' || c0_ > '9') return ReportUnexpectedCharacter(); |
| 432 | do { |
| 433 | Advance(); |
| 434 | } while (c0_ >= '0' && c0_ <= '9'); |
| 435 | } |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 436 | int length = position_ - beg_pos; |
| 437 | double number; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 438 | if (seq_ascii) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 439 | Vector<const uint8_t> chars(seq_source_->GetChars() + beg_pos, length); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 440 | number = StringToDouble(isolate()->unicode_cache(), |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 441 | Vector<const char>::cast(chars), |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 442 | NO_FLAGS, // Hex, octal or trailing junk. |
| 443 | OS::nan_value()); |
| 444 | } else { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 445 | Vector<uint8_t> buffer = Vector<uint8_t>::New(length); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 446 | String::WriteToFlat(*source_, buffer.start(), beg_pos, position_); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 447 | Vector<const uint8_t> result = |
| 448 | Vector<const uint8_t>(buffer.start(), length); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 449 | number = StringToDouble(isolate()->unicode_cache(), |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 450 | // TODO(dcarney): Convert StringToDouble to uint_t. |
| 451 | Vector<const char>::cast(result), |
| 452 | NO_FLAGS, // Hex, octal or trailing junk. |
| 453 | 0.0); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 454 | buffer.Dispose(); |
| 455 | } |
| 456 | SkipWhitespace(); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 457 | return factory()->NewNumber(number, pretenure_); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 458 | } |
| 459 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 460 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 461 | template <typename StringType> |
| 462 | inline void SeqStringSet(Handle<StringType> seq_str, int i, uc32 c); |
| 463 | |
| 464 | template <> |
| 465 | inline void SeqStringSet(Handle<SeqTwoByteString> seq_str, int i, uc32 c) { |
| 466 | seq_str->SeqTwoByteStringSet(i, c); |
| 467 | } |
| 468 | |
| 469 | template <> |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 470 | inline void SeqStringSet(Handle<SeqOneByteString> seq_str, int i, uc32 c) { |
| 471 | seq_str->SeqOneByteStringSet(i, c); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | template <typename StringType> |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 475 | inline Handle<StringType> NewRawString(Factory* factory, |
| 476 | int length, |
| 477 | PretenureFlag pretenure); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 478 | |
| 479 | template <> |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 480 | inline Handle<SeqTwoByteString> NewRawString(Factory* factory, |
| 481 | int length, |
| 482 | PretenureFlag pretenure) { |
| 483 | return factory->NewRawTwoByteString(length, pretenure); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | template <> |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 487 | inline Handle<SeqOneByteString> NewRawString(Factory* factory, |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 488 | int length, |
| 489 | PretenureFlag pretenure) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 490 | return factory->NewRawOneByteString(length, pretenure); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | |
| 494 | // Scans the rest of a JSON string starting from position_ and writes |
| 495 | // prefix[start..end] along with the scanned characters into a |
| 496 | // sequential string of type StringType. |
| 497 | template <bool seq_ascii> |
| 498 | template <typename StringType, typename SinkChar> |
| 499 | Handle<String> JsonParser<seq_ascii>::SlowScanJsonString( |
| 500 | Handle<String> prefix, int start, int end) { |
| 501 | int count = end - start; |
| 502 | int max_length = count + source_length_ - position_; |
| 503 | int length = Min(max_length, Max(kInitialSpecialStringLength, 2 * count)); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 504 | Handle<StringType> seq_str = |
| 505 | NewRawString<StringType>(factory(), length, pretenure_); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 506 | // Copy prefix into seq_str. |
| 507 | SinkChar* dest = seq_str->GetChars(); |
| 508 | String::WriteToFlat(*prefix, dest, start, end); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 509 | |
| 510 | while (c0_ != '"') { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 511 | // Check for control character (0x00-0x1f) or unterminated string (<0). |
| 512 | if (c0_ < 0x20) return Handle<String>::null(); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 513 | if (count >= length) { |
| 514 | // We need to create a longer sequential string for the result. |
| 515 | return SlowScanJsonString<StringType, SinkChar>(seq_str, 0, count); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 516 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 517 | if (c0_ != '\\') { |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 518 | // If the sink can contain UC16 characters, or source_ contains only |
| 519 | // ASCII characters, there's no need to test whether we can store the |
| 520 | // character. Otherwise check whether the UC16 source character can fit |
| 521 | // in the ASCII sink. |
| 522 | if (sizeof(SinkChar) == kUC16Size || |
| 523 | seq_ascii || |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 524 | c0_ <= String::kMaxOneByteCharCode) { |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 525 | SeqStringSet(seq_str, count++, c0_); |
| 526 | Advance(); |
| 527 | } else { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 528 | // StringType is SeqOneByteString and we just read a non-ASCII char. |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 529 | return SlowScanJsonString<SeqTwoByteString, uc16>(seq_str, 0, count); |
| 530 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 531 | } else { |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 532 | Advance(); // Advance past the \. |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 533 | switch (c0_) { |
| 534 | case '"': |
| 535 | case '\\': |
| 536 | case '/': |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 537 | SeqStringSet(seq_str, count++, c0_); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 538 | break; |
| 539 | case 'b': |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 540 | SeqStringSet(seq_str, count++, '\x08'); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 541 | break; |
| 542 | case 'f': |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 543 | SeqStringSet(seq_str, count++, '\x0c'); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 544 | break; |
| 545 | case 'n': |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 546 | SeqStringSet(seq_str, count++, '\x0a'); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 547 | break; |
| 548 | case 'r': |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 549 | SeqStringSet(seq_str, count++, '\x0d'); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 550 | break; |
| 551 | case 't': |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 552 | SeqStringSet(seq_str, count++, '\x09'); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 553 | break; |
| 554 | case 'u': { |
| 555 | uc32 value = 0; |
| 556 | for (int i = 0; i < 4; i++) { |
| 557 | Advance(); |
| 558 | int digit = HexValue(c0_); |
| 559 | if (digit < 0) { |
| 560 | return Handle<String>::null(); |
| 561 | } |
| 562 | value = value * 16 + digit; |
| 563 | } |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 564 | if (sizeof(SinkChar) == kUC16Size || |
| 565 | value <= String::kMaxOneByteCharCode) { |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 566 | SeqStringSet(seq_str, count++, value); |
| 567 | break; |
| 568 | } else { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 569 | // StringType is SeqOneByteString and we just read a non-ASCII char. |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 570 | position_ -= 6; // Rewind position_ to \ in \uxxxx. |
| 571 | Advance(); |
| 572 | return SlowScanJsonString<SeqTwoByteString, uc16>(seq_str, |
| 573 | 0, |
| 574 | count); |
| 575 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 576 | } |
| 577 | default: |
| 578 | return Handle<String>::null(); |
| 579 | } |
| 580 | Advance(); |
| 581 | } |
| 582 | } |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 583 | // Shrink seq_string length to count. |
| 584 | if (isolate()->heap()->InNewSpace(*seq_str)) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 585 | isolate()->heap()->new_space()-> |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 586 | template ShrinkStringAtAllocationBoundary<StringType>( |
| 587 | *seq_str, count); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 588 | } else { |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 589 | int string_size = StringType::SizeFor(count); |
| 590 | int allocated_string_size = StringType::SizeFor(length); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 591 | int delta = allocated_string_size - string_size; |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 592 | Address start_filler_object = seq_str->address() + string_size; |
| 593 | seq_str->set_length(count); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 594 | isolate()->heap()->CreateFillerObjectAt(start_filler_object, delta); |
| 595 | } |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 596 | ASSERT_EQ('"', c0_); |
| 597 | // Advance past the last '"'. |
| 598 | AdvanceSkipWhitespace(); |
| 599 | return seq_str; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 600 | } |
| 601 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 602 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 603 | template <bool seq_ascii> |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 604 | template <bool is_internalized> |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 605 | Handle<String> JsonParser<seq_ascii>::ScanJsonString() { |
| 606 | ASSERT_EQ('"', c0_); |
| 607 | Advance(); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 608 | if (c0_ == '"') { |
| 609 | AdvanceSkipWhitespace(); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 610 | return factory()->empty_string(); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 611 | } |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 612 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 613 | if (seq_ascii && is_internalized) { |
| 614 | // Fast path for existing internalized strings. If the the string being |
| 615 | // parsed is not a known internalized string, contains backslashes or |
| 616 | // unexpectedly reaches the end of string, return with an empty handle. |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 617 | uint32_t running_hash = isolate()->heap()->HashSeed(); |
| 618 | int position = position_; |
| 619 | uc32 c0 = c0_; |
| 620 | do { |
| 621 | if (c0 == '\\') { |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 622 | c0_ = c0; |
| 623 | int beg_pos = position_; |
| 624 | position_ = position; |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 625 | return SlowScanJsonString<SeqOneByteString, uint8_t>(source_, |
| 626 | beg_pos, |
| 627 | position_); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 628 | } |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 629 | if (c0 < 0x20) return Handle<String>::null(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 630 | if (static_cast<uint32_t>(c0) > |
| 631 | unibrow::Utf16::kMaxNonSurrogateCharCode) { |
| 632 | running_hash = |
| 633 | StringHasher::AddCharacterCore(running_hash, |
| 634 | unibrow::Utf16::LeadSurrogate(c0)); |
| 635 | running_hash = |
| 636 | StringHasher::AddCharacterCore(running_hash, |
| 637 | unibrow::Utf16::TrailSurrogate(c0)); |
| 638 | } else { |
| 639 | running_hash = StringHasher::AddCharacterCore(running_hash, c0); |
| 640 | } |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 641 | position++; |
| 642 | if (position >= source_length_) return Handle<String>::null(); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 643 | c0 = seq_source_->SeqOneByteStringGet(position); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 644 | } while (c0 != '"'); |
| 645 | int length = position - position_; |
| 646 | uint32_t hash = (length <= String::kMaxHashCalcLength) |
| 647 | ? StringHasher::GetHashCore(running_hash) : length; |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 648 | Vector<const uint8_t> string_vector( |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 649 | seq_source_->GetChars() + position_, length); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 650 | StringTable* string_table = isolate()->heap()->string_table(); |
| 651 | uint32_t capacity = string_table->Capacity(); |
| 652 | uint32_t entry = StringTable::FirstProbe(hash, capacity); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 653 | uint32_t count = 1; |
| 654 | while (true) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 655 | Object* element = string_table->KeyAt(entry); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 656 | if (element == isolate()->heap()->undefined_value()) { |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 657 | // Lookup failure. |
| 658 | break; |
| 659 | } |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 660 | if (element != isolate()->heap()->the_hole_value() && |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 661 | String::cast(element)->IsOneByteEqualTo(string_vector)) { |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 662 | // Lookup success, update the current position. |
| 663 | position_ = position; |
| 664 | // Advance past the last '"'. |
| 665 | AdvanceSkipWhitespace(); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 666 | return Handle<String>(String::cast(element), isolate()); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 667 | } |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 668 | entry = StringTable::NextProbe(entry, count++, capacity); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 669 | } |
| 670 | } |
| 671 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 672 | int beg_pos = position_; |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 673 | // Fast case for ASCII only without escape characters. |
| 674 | do { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 675 | // Check for control character (0x00-0x1f) or unterminated string (<0). |
| 676 | if (c0_ < 0x20) return Handle<String>::null(); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 677 | if (c0_ != '\\') { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 678 | if (seq_ascii || c0_ <= String::kMaxOneByteCharCode) { |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 679 | Advance(); |
| 680 | } else { |
| 681 | return SlowScanJsonString<SeqTwoByteString, uc16>(source_, |
| 682 | beg_pos, |
| 683 | position_); |
| 684 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 685 | } else { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 686 | return SlowScanJsonString<SeqOneByteString, uint8_t>(source_, |
| 687 | beg_pos, |
| 688 | position_); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 689 | } |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 690 | } while (c0_ != '"'); |
| 691 | int length = position_ - beg_pos; |
| 692 | Handle<String> result; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 693 | if (seq_ascii && is_internalized) { |
| 694 | result = factory()->InternalizeOneByteString(seq_source_, beg_pos, length); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 695 | } else { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 696 | result = factory()->NewRawOneByteString(length, pretenure_); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 697 | uint8_t* dest = SeqOneByteString::cast(*result)->GetChars(); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 698 | String::WriteToFlat(*source_, dest, beg_pos, position_); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 699 | } |
| 700 | ASSERT_EQ('"', c0_); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 701 | // Advance past the last '"'. |
| 702 | AdvanceSkipWhitespace(); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 703 | return result; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 704 | } |
| 705 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 706 | } } // namespace v8::internal |
| 707 | |
| 708 | #endif // V8_JSON_PARSER_H_ |