OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 if (seq_ascii) { | 185 if (seq_ascii) { |
186 seq_source_ = Handle<SeqAsciiString>::cast(source_); | 186 seq_source_ = Handle<SeqAsciiString>::cast(source_); |
187 } | 187 } |
188 | 188 |
189 // Set initial position right before the string. | 189 // Set initial position right before the string. |
190 position_ = -1; | 190 position_ = -1; |
191 // Advance to the first character (possibly EOS) | 191 // Advance to the first character (possibly EOS) |
192 AdvanceSkipWhitespace(); | 192 AdvanceSkipWhitespace(); |
193 Handle<Object> result = ParseJsonValue(); | 193 Handle<Object> result = ParseJsonValue(); |
194 if (result.is_null() || c0_ != kEndOfString) { | 194 if (result.is_null() || c0_ != kEndOfString) { |
| 195 // Some exception (for example stack overflow) is already pending. |
| 196 if (isolate_->has_pending_exception()) return Handle<Object>::null(); |
| 197 |
195 // Parse failed. Current character is the unexpected token. | 198 // Parse failed. Current character is the unexpected token. |
196 | |
197 const char* message; | 199 const char* message; |
198 Factory* factory = this->factory(); | 200 Factory* factory = this->factory(); |
199 Handle<JSArray> array; | 201 Handle<JSArray> array; |
200 | 202 |
201 switch (c0_) { | 203 switch (c0_) { |
202 case kEndOfString: | 204 case kEndOfString: |
203 message = "unexpected_eos"; | 205 message = "unexpected_eos"; |
204 array = factory->NewJSArray(0); | 206 array = factory->NewJSArray(0); |
205 break; | 207 break; |
206 case '-': | 208 case '-': |
(...skipping 30 matching lines...) Expand all Loading... |
237 isolate()->Throw(*result, &location); | 239 isolate()->Throw(*result, &location); |
238 return Handle<Object>::null(); | 240 return Handle<Object>::null(); |
239 } | 241 } |
240 return result; | 242 return result; |
241 } | 243 } |
242 | 244 |
243 | 245 |
244 // Parse any JSON value. | 246 // Parse any JSON value. |
245 template <bool seq_ascii> | 247 template <bool seq_ascii> |
246 Handle<Object> JsonParser<seq_ascii>::ParseJsonValue() { | 248 Handle<Object> JsonParser<seq_ascii>::ParseJsonValue() { |
| 249 StackLimitCheck stack_check(isolate_); |
| 250 if (stack_check.HasOverflowed()) { |
| 251 isolate_->StackOverflow(); |
| 252 return Handle<Object>::null(); |
| 253 } |
| 254 |
247 if (c0_ == '"') return ParseJsonString(); | 255 if (c0_ == '"') return ParseJsonString(); |
248 if ((c0_ >= '0' && c0_ <= '9') || c0_ == '-') return ParseJsonNumber(); | 256 if ((c0_ >= '0' && c0_ <= '9') || c0_ == '-') return ParseJsonNumber(); |
249 if (c0_ == '{') return ParseJsonObject(); | 257 if (c0_ == '{') return ParseJsonObject(); |
250 if (c0_ == '[') return ParseJsonArray(); | 258 if (c0_ == '[') return ParseJsonArray(); |
251 if (c0_ == 'f') { | 259 if (c0_ == 'f') { |
252 if (AdvanceGetChar() == 'a' && AdvanceGetChar() == 'l' && | 260 if (AdvanceGetChar() == 'a' && AdvanceGetChar() == 'l' && |
253 AdvanceGetChar() == 's' && AdvanceGetChar() == 'e') { | 261 AdvanceGetChar() == 's' && AdvanceGetChar() == 'e') { |
254 AdvanceSkipWhitespace(); | 262 AdvanceSkipWhitespace(); |
255 return factory()->false_value(); | 263 return factory()->false_value(); |
256 } | 264 } |
(...skipping 29 matching lines...) Expand all Loading... |
286 | 294 |
287 AdvanceSkipWhitespace(); | 295 AdvanceSkipWhitespace(); |
288 if (c0_ != '}') { | 296 if (c0_ != '}') { |
289 do { | 297 do { |
290 if (c0_ != '"') return ReportUnexpectedCharacter(); | 298 if (c0_ != '"') return ReportUnexpectedCharacter(); |
291 | 299 |
292 int start_position = position_; | 300 int start_position = position_; |
293 Advance(); | 301 Advance(); |
294 | 302 |
295 uint32_t index = 0; | 303 uint32_t index = 0; |
296 while (c0_ >= '0' && c0_ <= '9') { | 304 if (c0_ >= '0' && c0_ <= '9') { |
297 int d = c0_ - '0'; | 305 // Maybe an array index, try to parse it. |
298 if (index > 429496729U - ((d > 5) ? 1 : 0)) break; | 306 if (c0_ == '0') { |
299 index = (index * 10) + d; | 307 // With a leading zero, the string has to be "0" only to be an index. |
300 Advance(); | 308 Advance(); |
| 309 } else { |
| 310 do { |
| 311 int d = c0_ - '0'; |
| 312 if (index > 429496729U - ((d > 5) ? 1 : 0)) break; |
| 313 index = (index * 10) + d; |
| 314 Advance(); |
| 315 } while (c0_ >= '0' && c0_ <= '9'); |
| 316 } |
| 317 |
| 318 if (c0_ == '"') { |
| 319 // Successfully parsed index, parse and store element. |
| 320 AdvanceSkipWhitespace(); |
| 321 |
| 322 if (c0_ != ':') return ReportUnexpectedCharacter(); |
| 323 AdvanceSkipWhitespace(); |
| 324 Handle<Object> value = ParseJsonValue(); |
| 325 if (value.is_null()) return ReportUnexpectedCharacter(); |
| 326 |
| 327 JSObject::SetOwnElement(json_object, index, value, kNonStrictMode); |
| 328 continue; |
| 329 } |
| 330 // Not an index, fallback to the slow path. |
301 } | 331 } |
302 | 332 |
303 if (position_ != start_position + 1 && c0_ == '"') { | 333 position_ = start_position; |
304 AdvanceSkipWhitespace(); | |
305 | |
306 if (c0_ != ':') return ReportUnexpectedCharacter(); | |
307 AdvanceSkipWhitespace(); | |
308 Handle<Object> value = ParseJsonValue(); | |
309 if (value.is_null()) return ReportUnexpectedCharacter(); | |
310 | |
311 JSObject::SetOwnElement(json_object, index, value, kNonStrictMode); | |
312 } else { | |
313 position_ = start_position; | |
314 #ifdef DEBUG | 334 #ifdef DEBUG |
315 c0_ = '"'; | 335 c0_ = '"'; |
316 #endif | 336 #endif |
317 | 337 |
318 Handle<String> key = ParseJsonSymbol(); | 338 Handle<String> key = ParseJsonSymbol(); |
319 if (key.is_null() || c0_ != ':') return ReportUnexpectedCharacter(); | 339 if (key.is_null() || c0_ != ':') return ReportUnexpectedCharacter(); |
320 | 340 |
321 AdvanceSkipWhitespace(); | 341 AdvanceSkipWhitespace(); |
322 Handle<Object> value = ParseJsonValue(); | 342 Handle<Object> value = ParseJsonValue(); |
323 if (value.is_null()) return ReportUnexpectedCharacter(); | 343 if (value.is_null()) return ReportUnexpectedCharacter(); |
324 | 344 |
325 if (key->Equals(isolate()->heap()->Proto_symbol())) { | 345 if (key->Equals(isolate()->heap()->Proto_symbol())) { |
326 prototype = value; | 346 prototype = value; |
| 347 } else { |
| 348 if (JSObject::TryTransitionToField(json_object, key)) { |
| 349 int index = json_object->LastAddedFieldIndex(); |
| 350 json_object->FastPropertyAtPut(index, *value); |
327 } else { | 351 } else { |
328 if (JSObject::TryTransitionToField(json_object, key)) { | 352 JSObject::SetLocalPropertyIgnoreAttributes( |
329 int index = json_object->LastAddedFieldIndex(); | 353 json_object, key, value, NONE); |
330 json_object->FastPropertyAtPut(index, *value); | |
331 } else { | |
332 JSObject::SetLocalPropertyIgnoreAttributes( | |
333 json_object, key, value, NONE); | |
334 } | |
335 } | 354 } |
336 } | 355 } |
337 } while (MatchSkipWhiteSpace(',')); | 356 } while (MatchSkipWhiteSpace(',')); |
338 if (c0_ != '}') { | 357 if (c0_ != '}') { |
339 return ReportUnexpectedCharacter(); | 358 return ReportUnexpectedCharacter(); |
340 } | 359 } |
341 if (!prototype.is_null()) SetPrototype(json_object, prototype); | 360 if (!prototype.is_null()) SetPrototype(json_object, prototype); |
342 } | 361 } |
343 AdvanceSkipWhitespace(); | 362 AdvanceSkipWhitespace(); |
344 return json_object; | 363 return json_object; |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 } | 684 } |
666 ASSERT_EQ('"', c0_); | 685 ASSERT_EQ('"', c0_); |
667 // Advance past the last '"'. | 686 // Advance past the last '"'. |
668 AdvanceSkipWhitespace(); | 687 AdvanceSkipWhitespace(); |
669 return result; | 688 return result; |
670 } | 689 } |
671 | 690 |
672 } } // namespace v8::internal | 691 } } // namespace v8::internal |
673 | 692 |
674 #endif // V8_JSON_PARSER_H_ | 693 #endif // V8_JSON_PARSER_H_ |
OLD | NEW |