| 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 25 matching lines...) Expand all Loading... |
| 36 #include "spaces-inl.h" | 36 #include "spaces-inl.h" |
| 37 #include "token.h" | 37 #include "token.h" |
| 38 | 38 |
| 39 namespace v8 { | 39 namespace v8 { |
| 40 namespace internal { | 40 namespace internal { |
| 41 | 41 |
| 42 // A simple json parser. | 42 // A simple json parser. |
| 43 template <bool seq_ascii> | 43 template <bool seq_ascii> |
| 44 class JsonParser BASE_EMBEDDED { | 44 class JsonParser BASE_EMBEDDED { |
| 45 public: | 45 public: |
| 46 static Handle<Object> Parse(Handle<String> source) { | 46 static Handle<Object> Parse(Handle<String> source, Zone* zone) { |
| 47 return JsonParser().ParseJson(source); | 47 return JsonParser().ParseJson(source, zone); |
| 48 } | 48 } |
| 49 | 49 |
| 50 static const int kEndOfString = -1; | 50 static const int kEndOfString = -1; |
| 51 | 51 |
| 52 private: | 52 private: |
| 53 // Parse a string containing a single JSON value. | 53 // Parse a string containing a single JSON value. |
| 54 Handle<Object> ParseJson(Handle<String> source); | 54 Handle<Object> ParseJson(Handle<String> source, Zone* zone); |
| 55 | 55 |
| 56 inline void Advance() { | 56 inline void Advance() { |
| 57 position_++; | 57 position_++; |
| 58 if (position_ >= source_length_) { | 58 if (position_ >= source_length_) { |
| 59 c0_ = kEndOfString; | 59 c0_ = kEndOfString; |
| 60 } else if (seq_ascii) { | 60 } else if (seq_ascii) { |
| 61 c0_ = seq_source_->SeqAsciiStringGet(position_); | 61 c0_ = seq_source_->SeqAsciiStringGet(position_); |
| 62 } else { | 62 } else { |
| 63 c0_ = source_->Get(position_); | 63 c0_ = source_->Get(position_); |
| 64 } | 64 } |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 Handle<Object> ParseJsonArray(); | 142 Handle<Object> ParseJsonArray(); |
| 143 | 143 |
| 144 | 144 |
| 145 // Mark that a parsing error has happened at the current token, and | 145 // Mark that a parsing error has happened at the current token, and |
| 146 // return a null handle. Primarily for readability. | 146 // return a null handle. Primarily for readability. |
| 147 inline Handle<Object> ReportUnexpectedCharacter() { | 147 inline Handle<Object> ReportUnexpectedCharacter() { |
| 148 return Handle<Object>::null(); | 148 return Handle<Object>::null(); |
| 149 } | 149 } |
| 150 | 150 |
| 151 inline Isolate* isolate() { return isolate_; } | 151 inline Isolate* isolate() { return isolate_; } |
| 152 inline Zone* zone() const { return zone_; } |
| 152 | 153 |
| 153 static const int kInitialSpecialStringLength = 1024; | 154 static const int kInitialSpecialStringLength = 1024; |
| 154 | 155 |
| 155 | 156 |
| 156 private: | 157 private: |
| 157 Handle<String> source_; | 158 Handle<String> source_; |
| 158 int source_length_; | 159 int source_length_; |
| 159 Handle<SeqAsciiString> seq_source_; | 160 Handle<SeqAsciiString> seq_source_; |
| 160 | 161 |
| 161 Isolate* isolate_; | 162 Isolate* isolate_; |
| 162 uc32 c0_; | 163 uc32 c0_; |
| 163 int position_; | 164 int position_; |
| 165 Zone* zone_; |
| 164 }; | 166 }; |
| 165 | 167 |
| 166 template <bool seq_ascii> | 168 template <bool seq_ascii> |
| 167 Handle<Object> JsonParser<seq_ascii>::ParseJson(Handle<String> source) { | 169 Handle<Object> JsonParser<seq_ascii>::ParseJson(Handle<String> source, |
| 170 Zone* zone) { |
| 168 isolate_ = source->map()->GetHeap()->isolate(); | 171 isolate_ = source->map()->GetHeap()->isolate(); |
| 172 zone_ = zone; |
| 169 FlattenString(source); | 173 FlattenString(source); |
| 170 source_ = source; | 174 source_ = source; |
| 171 source_length_ = source_->length(); | 175 source_length_ = source_->length(); |
| 172 | 176 |
| 173 // Optimized fast case where we only have ASCII characters. | 177 // Optimized fast case where we only have ASCII characters. |
| 174 if (seq_ascii) { | 178 if (seq_ascii) { |
| 175 seq_source_ = Handle<SeqAsciiString>::cast(source_); | 179 seq_source_ = Handle<SeqAsciiString>::cast(source_); |
| 176 } | 180 } |
| 177 | 181 |
| 178 // Set initial position right before the string. | 182 // Set initial position right before the string. |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 } | 320 } |
| 317 } | 321 } |
| 318 AdvanceSkipWhitespace(); | 322 AdvanceSkipWhitespace(); |
| 319 return json_object; | 323 return json_object; |
| 320 } | 324 } |
| 321 | 325 |
| 322 // Parse a JSON array. Position must be right at '['. | 326 // Parse a JSON array. Position must be right at '['. |
| 323 template <bool seq_ascii> | 327 template <bool seq_ascii> |
| 324 Handle<Object> JsonParser<seq_ascii>::ParseJsonArray() { | 328 Handle<Object> JsonParser<seq_ascii>::ParseJsonArray() { |
| 325 ZoneScope zone_scope(isolate(), DELETE_ON_EXIT); | 329 ZoneScope zone_scope(isolate(), DELETE_ON_EXIT); |
| 326 ZoneList<Handle<Object> > elements(4); | 330 ZoneList<Handle<Object> > elements(4, zone()); |
| 327 ASSERT_EQ(c0_, '['); | 331 ASSERT_EQ(c0_, '['); |
| 328 | 332 |
| 329 AdvanceSkipWhitespace(); | 333 AdvanceSkipWhitespace(); |
| 330 if (c0_ != ']') { | 334 if (c0_ != ']') { |
| 331 do { | 335 do { |
| 332 Handle<Object> element = ParseJsonValue(); | 336 Handle<Object> element = ParseJsonValue(); |
| 333 if (element.is_null()) return ReportUnexpectedCharacter(); | 337 if (element.is_null()) return ReportUnexpectedCharacter(); |
| 334 elements.Add(element); | 338 elements.Add(element, zone()); |
| 335 } while (MatchSkipWhiteSpace(',')); | 339 } while (MatchSkipWhiteSpace(',')); |
| 336 if (c0_ != ']') { | 340 if (c0_ != ']') { |
| 337 return ReportUnexpectedCharacter(); | 341 return ReportUnexpectedCharacter(); |
| 338 } | 342 } |
| 339 } | 343 } |
| 340 AdvanceSkipWhitespace(); | 344 AdvanceSkipWhitespace(); |
| 341 // Allocate a fixed array with all the elements. | 345 // Allocate a fixed array with all the elements. |
| 342 Handle<FixedArray> fast_elements = | 346 Handle<FixedArray> fast_elements = |
| 343 isolate()->factory()->NewFixedArray(elements.length()); | 347 isolate()->factory()->NewFixedArray(elements.length()); |
| 344 for (int i = 0, n = elements.length(); i < n; i++) { | 348 for (int i = 0, n = elements.length(); i < n; i++) { |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 591 } | 595 } |
| 592 ASSERT_EQ('"', c0_); | 596 ASSERT_EQ('"', c0_); |
| 593 // Advance past the last '"'. | 597 // Advance past the last '"'. |
| 594 AdvanceSkipWhitespace(); | 598 AdvanceSkipWhitespace(); |
| 595 return result; | 599 return result; |
| 596 } | 600 } |
| 597 | 601 |
| 598 } } // namespace v8::internal | 602 } } // namespace v8::internal |
| 599 | 603 |
| 600 #endif // V8_JSON_PARSER_H_ | 604 #endif // V8_JSON_PARSER_H_ |
| OLD | NEW |