danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 1 | // Copyright 2012 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_STRINGIFIER_H_ |
| 29 | #define V8_JSON_STRINGIFIER_H_ |
| 30 | |
| 31 | #include "v8.h" |
| 32 | #include "v8utils.h" |
| 33 | #include "v8conversions.h" |
| 34 | |
| 35 | namespace v8 { |
| 36 | namespace internal { |
| 37 | |
| 38 | class BasicJsonStringifier BASE_EMBEDDED { |
| 39 | public: |
| 40 | explicit BasicJsonStringifier(Isolate* isolate); |
| 41 | |
| 42 | MaybeObject* Stringify(Handle<Object> object); |
| 43 | |
| 44 | private: |
| 45 | static const int kInitialPartLength = 32; |
| 46 | static const int kMaxPartLength = 16 * 1024; |
| 47 | static const int kPartLengthGrowthFactor = 2; |
| 48 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 49 | enum Result { UNCHANGED, SUCCESS, EXCEPTION, CIRCULAR, STACK_OVERFLOW }; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 50 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 51 | void Extend(); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 52 | |
| 53 | void ChangeEncoding(); |
| 54 | |
| 55 | void ShrinkCurrentPart(); |
| 56 | |
| 57 | template <bool is_ascii, typename Char> |
| 58 | INLINE(void Append_(Char c)); |
| 59 | |
| 60 | template <bool is_ascii, typename Char> |
| 61 | INLINE(void Append_(const Char* chars)); |
| 62 | |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 63 | INLINE(void Append(uint8_t c)) { |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 64 | if (is_ascii_) { |
| 65 | Append_<true>(c); |
| 66 | } else { |
| 67 | Append_<false>(c); |
| 68 | } |
| 69 | } |
| 70 | |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 71 | INLINE(void AppendAscii(const char* chars)) { |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 72 | if (is_ascii_) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 73 | Append_<true>(reinterpret_cast<const uint8_t*>(chars)); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 74 | } else { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 75 | Append_<false>(reinterpret_cast<const uint8_t*>(chars)); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 79 | Handle<Object> ApplyToJsonFunction(Handle<Object> object, |
| 80 | Handle<Object> key); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 81 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 82 | Result SerializeGeneric(Handle<Object> object, |
| 83 | Handle<Object> key, |
| 84 | bool deferred_comma, |
| 85 | bool deferred_key); |
| 86 | |
| 87 | // Entry point to serialize the object. |
| 88 | INLINE(Result SerializeObject(Handle<Object> obj)) { |
| 89 | return Serialize_<false>(obj, false, factory_->empty_string()); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 90 | } |
| 91 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 92 | // Serialize an array element. |
| 93 | // The index may serve as argument for the toJSON function. |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 94 | INLINE(Result SerializeElement(Isolate* isolate, |
| 95 | Handle<Object> object, |
| 96 | int i)) { |
| 97 | return Serialize_<false>(object, |
| 98 | false, |
| 99 | Handle<Object>(Smi::FromInt(i), isolate)); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Serialize a object property. |
| 103 | // The key may or may not be serialized depending on the property. |
| 104 | // The key may also serve as argument for the toJSON function. |
| 105 | INLINE(Result SerializeProperty(Handle<Object> object, |
| 106 | bool deferred_comma, |
| 107 | Handle<String> deferred_key)) { |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 108 | ASSERT(!deferred_key.is_null()); |
| 109 | return Serialize_<true>(object, deferred_comma, deferred_key); |
| 110 | } |
| 111 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 112 | template <bool deferred_string_key> |
| 113 | Result Serialize_(Handle<Object> object, bool comma, Handle<Object> key); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 114 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 115 | void SerializeDeferredKey(bool deferred_comma, Handle<Object> deferred_key) { |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 116 | if (deferred_comma) Append(','); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 117 | SerializeString(Handle<String>::cast(deferred_key)); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 118 | Append(':'); |
| 119 | } |
| 120 | |
| 121 | Result SerializeSmi(Smi* object); |
| 122 | |
| 123 | Result SerializeDouble(double number); |
| 124 | INLINE(Result SerializeHeapNumber(Handle<HeapNumber> object)) { |
| 125 | return SerializeDouble(object->value()); |
| 126 | } |
| 127 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 128 | Result SerializeJSValue(Handle<JSValue> object); |
| 129 | |
| 130 | INLINE(Result SerializeJSArray(Handle<JSArray> object)); |
| 131 | INLINE(Result SerializeJSObject(Handle<JSObject> object)); |
| 132 | |
| 133 | Result SerializeJSArraySlow(Handle<JSArray> object, int length); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 134 | |
| 135 | void SerializeString(Handle<String> object); |
| 136 | |
| 137 | template <typename SrcChar, typename DestChar> |
| 138 | INLINE(void SerializeStringUnchecked_(const SrcChar* src, |
| 139 | DestChar* dest, |
| 140 | int length)); |
| 141 | |
| 142 | template <bool is_ascii, typename Char> |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 143 | INLINE(void SerializeString_(Handle<String> string)); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 144 | |
| 145 | template <typename Char> |
| 146 | INLINE(bool DoNotEscape(Char c)); |
| 147 | |
| 148 | template <typename Char> |
| 149 | INLINE(Vector<const Char> GetCharVector(Handle<String> string)); |
| 150 | |
| 151 | Result StackPush(Handle<Object> object); |
| 152 | void StackPop(); |
| 153 | |
| 154 | INLINE(Handle<String> accumulator()) { |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 155 | return Handle<String>(String::cast(accumulator_store_->value()), isolate_); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | INLINE(void set_accumulator(Handle<String> string)) { |
| 159 | return accumulator_store_->set_value(*string); |
| 160 | } |
| 161 | |
| 162 | Isolate* isolate_; |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 163 | Factory* factory_; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 164 | // We use a value wrapper for the string accumulator to keep the |
| 165 | // (indirect) handle to it in the outermost handle scope. |
| 166 | Handle<JSValue> accumulator_store_; |
| 167 | Handle<String> current_part_; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 168 | Handle<String> tojson_string_; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 169 | Handle<JSArray> stack_; |
| 170 | int current_index_; |
| 171 | int part_length_; |
| 172 | bool is_ascii_; |
| 173 | |
| 174 | static const int kJsonEscapeTableEntrySize = 8; |
| 175 | static const char* const JsonEscapeTable; |
| 176 | }; |
| 177 | |
| 178 | |
| 179 | // Translation table to escape ASCII characters. |
| 180 | // Table entries start at a multiple of 8 and are null-terminated. |
| 181 | const char* const BasicJsonStringifier::JsonEscapeTable = |
| 182 | "\\u0000\0 \\u0001\0 \\u0002\0 \\u0003\0 " |
| 183 | "\\u0004\0 \\u0005\0 \\u0006\0 \\u0007\0 " |
| 184 | "\\b\0 \\t\0 \\n\0 \\u000b\0 " |
| 185 | "\\f\0 \\r\0 \\u000e\0 \\u000f\0 " |
| 186 | "\\u0010\0 \\u0011\0 \\u0012\0 \\u0013\0 " |
| 187 | "\\u0014\0 \\u0015\0 \\u0016\0 \\u0017\0 " |
| 188 | "\\u0018\0 \\u0019\0 \\u001a\0 \\u001b\0 " |
| 189 | "\\u001c\0 \\u001d\0 \\u001e\0 \\u001f\0 " |
| 190 | " \0 !\0 \\\"\0 #\0 " |
| 191 | "$\0 %\0 &\0 '\0 " |
| 192 | "(\0 )\0 *\0 +\0 " |
| 193 | ",\0 -\0 .\0 /\0 " |
| 194 | "0\0 1\0 2\0 3\0 " |
| 195 | "4\0 5\0 6\0 7\0 " |
| 196 | "8\0 9\0 :\0 ;\0 " |
| 197 | "<\0 =\0 >\0 ?\0 " |
| 198 | "@\0 A\0 B\0 C\0 " |
| 199 | "D\0 E\0 F\0 G\0 " |
| 200 | "H\0 I\0 J\0 K\0 " |
| 201 | "L\0 M\0 N\0 O\0 " |
| 202 | "P\0 Q\0 R\0 S\0 " |
| 203 | "T\0 U\0 V\0 W\0 " |
| 204 | "X\0 Y\0 Z\0 [\0 " |
| 205 | "\\\\\0 ]\0 ^\0 _\0 " |
| 206 | "`\0 a\0 b\0 c\0 " |
| 207 | "d\0 e\0 f\0 g\0 " |
| 208 | "h\0 i\0 j\0 k\0 " |
| 209 | "l\0 m\0 n\0 o\0 " |
| 210 | "p\0 q\0 r\0 s\0 " |
| 211 | "t\0 u\0 v\0 w\0 " |
| 212 | "x\0 y\0 z\0 {\0 " |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 213 | "|\0 }\0 ~\0 \177\0 " |
| 214 | "\200\0 \201\0 \202\0 \203\0 " |
| 215 | "\204\0 \205\0 \206\0 \207\0 " |
| 216 | "\210\0 \211\0 \212\0 \213\0 " |
| 217 | "\214\0 \215\0 \216\0 \217\0 " |
| 218 | "\220\0 \221\0 \222\0 \223\0 " |
| 219 | "\224\0 \225\0 \226\0 \227\0 " |
| 220 | "\230\0 \231\0 \232\0 \233\0 " |
| 221 | "\234\0 \235\0 \236\0 \237\0 " |
| 222 | "\240\0 \241\0 \242\0 \243\0 " |
| 223 | "\244\0 \245\0 \246\0 \247\0 " |
| 224 | "\250\0 \251\0 \252\0 \253\0 " |
| 225 | "\254\0 \255\0 \256\0 \257\0 " |
| 226 | "\260\0 \261\0 \262\0 \263\0 " |
| 227 | "\264\0 \265\0 \266\0 \267\0 " |
| 228 | "\270\0 \271\0 \272\0 \273\0 " |
| 229 | "\274\0 \275\0 \276\0 \277\0 " |
| 230 | "\300\0 \301\0 \302\0 \303\0 " |
| 231 | "\304\0 \305\0 \306\0 \307\0 " |
| 232 | "\310\0 \311\0 \312\0 \313\0 " |
| 233 | "\314\0 \315\0 \316\0 \317\0 " |
| 234 | "\320\0 \321\0 \322\0 \323\0 " |
| 235 | "\324\0 \325\0 \326\0 \327\0 " |
| 236 | "\330\0 \331\0 \332\0 \333\0 " |
| 237 | "\334\0 \335\0 \336\0 \337\0 " |
| 238 | "\340\0 \341\0 \342\0 \343\0 " |
| 239 | "\344\0 \345\0 \346\0 \347\0 " |
| 240 | "\350\0 \351\0 \352\0 \353\0 " |
| 241 | "\354\0 \355\0 \356\0 \357\0 " |
| 242 | "\360\0 \361\0 \362\0 \363\0 " |
| 243 | "\364\0 \365\0 \366\0 \367\0 " |
| 244 | "\370\0 \371\0 \372\0 \373\0 " |
| 245 | "\374\0 \375\0 \376\0 \377\0 "; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 246 | |
| 247 | BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate) |
| 248 | : isolate_(isolate), current_index_(0), is_ascii_(true) { |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 249 | factory_ = isolate_->factory(); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 250 | accumulator_store_ = Handle<JSValue>::cast( |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 251 | factory_->ToObject(factory_->empty_string())); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 252 | part_length_ = kInitialPartLength; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 253 | current_part_ = factory_->NewRawOneByteString(kInitialPartLength); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 254 | tojson_string_ = |
| 255 | factory_->InternalizeOneByteString(STATIC_ASCII_VECTOR("toJSON")); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 256 | stack_ = factory_->NewJSArray(8); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | |
| 260 | MaybeObject* BasicJsonStringifier::Stringify(Handle<Object> object) { |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 261 | switch (SerializeObject(object)) { |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 262 | case UNCHANGED: |
| 263 | return isolate_->heap()->undefined_value(); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 264 | case SUCCESS: |
| 265 | ShrinkCurrentPart(); |
| 266 | return *factory_->NewConsString(accumulator(), current_part_); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 267 | case CIRCULAR: |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 268 | return isolate_->Throw(*factory_->NewTypeError( |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 269 | "circular_structure", HandleVector<Object>(NULL, 0))); |
| 270 | case STACK_OVERFLOW: |
| 271 | return isolate_->StackOverflow(); |
| 272 | default: |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 273 | return Failure::Exception(); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | |
| 278 | template <bool is_ascii, typename Char> |
| 279 | void BasicJsonStringifier::Append_(Char c) { |
| 280 | if (is_ascii) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 281 | SeqOneByteString::cast(*current_part_)->SeqOneByteStringSet( |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 282 | current_index_++, c); |
| 283 | } else { |
| 284 | SeqTwoByteString::cast(*current_part_)->SeqTwoByteStringSet( |
| 285 | current_index_++, c); |
| 286 | } |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 287 | if (current_index_ == part_length_) Extend(); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | |
| 291 | template <bool is_ascii, typename Char> |
| 292 | void BasicJsonStringifier::Append_(const Char* chars) { |
| 293 | for ( ; *chars != '\0'; chars++) Append_<is_ascii, Char>(*chars); |
| 294 | } |
| 295 | |
| 296 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 297 | Handle<Object> BasicJsonStringifier::ApplyToJsonFunction( |
| 298 | Handle<Object> object, Handle<Object> key) { |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 299 | LookupResult lookup(isolate_); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 300 | JSObject::cast(*object)->LookupRealNamedProperty(*tojson_string_, &lookup); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 301 | if (!lookup.IsProperty()) return object; |
| 302 | PropertyAttributes attr; |
| 303 | Handle<Object> fun = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 304 | Object::GetProperty(object, object, &lookup, tojson_string_, &attr); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 305 | if (!fun->IsJSFunction()) return object; |
| 306 | |
| 307 | // Call toJSON function. |
| 308 | if (key->IsSmi()) key = factory_->NumberToString(key); |
| 309 | Handle<Object> argv[] = { key }; |
| 310 | bool has_exception = false; |
| 311 | HandleScope scope(isolate_); |
| 312 | object = Execution::Call(fun, object, 1, argv, &has_exception); |
| 313 | // Return empty handle to signal an exception. |
| 314 | if (has_exception) return Handle<Object>::null(); |
| 315 | return scope.CloseAndEscape(object); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | |
| 319 | BasicJsonStringifier::Result BasicJsonStringifier::StackPush( |
| 320 | Handle<Object> object) { |
| 321 | StackLimitCheck check(isolate_); |
| 322 | if (check.HasOverflowed()) return STACK_OVERFLOW; |
| 323 | |
| 324 | int length = Smi::cast(stack_->length())->value(); |
| 325 | FixedArray* elements = FixedArray::cast(stack_->elements()); |
| 326 | for (int i = 0; i < length; i++) { |
| 327 | if (elements->get(i) == *object) { |
| 328 | return CIRCULAR; |
| 329 | } |
| 330 | } |
| 331 | stack_->EnsureSize(length + 1); |
| 332 | FixedArray::cast(stack_->elements())->set(length, *object); |
| 333 | stack_->set_length(Smi::FromInt(length + 1)); |
| 334 | return SUCCESS; |
| 335 | } |
| 336 | |
| 337 | |
| 338 | void BasicJsonStringifier::StackPop() { |
| 339 | int length = Smi::cast(stack_->length())->value(); |
| 340 | stack_->set_length(Smi::FromInt(length - 1)); |
| 341 | } |
| 342 | |
| 343 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 344 | template <bool deferred_string_key> |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 345 | BasicJsonStringifier::Result BasicJsonStringifier::Serialize_( |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 346 | Handle<Object> object, bool comma, Handle<Object> key) { |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 347 | if (object->IsJSObject()) { |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 348 | object = ApplyToJsonFunction(object, key); |
| 349 | if (object.is_null()) return EXCEPTION; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 350 | } |
| 351 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 352 | if (object->IsSmi()) { |
| 353 | if (deferred_string_key) SerializeDeferredKey(comma, key); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 354 | return SerializeSmi(Smi::cast(*object)); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 355 | } |
| 356 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 357 | switch (HeapObject::cast(*object)->map()->instance_type()) { |
| 358 | case HEAP_NUMBER_TYPE: |
| 359 | if (deferred_string_key) SerializeDeferredKey(comma, key); |
| 360 | return SerializeHeapNumber(Handle<HeapNumber>::cast(object)); |
| 361 | case ODDBALL_TYPE: |
| 362 | switch (Oddball::cast(*object)->kind()) { |
| 363 | case Oddball::kFalse: |
| 364 | if (deferred_string_key) SerializeDeferredKey(comma, key); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 365 | AppendAscii("false"); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 366 | return SUCCESS; |
| 367 | case Oddball::kTrue: |
| 368 | if (deferred_string_key) SerializeDeferredKey(comma, key); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 369 | AppendAscii("true"); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 370 | return SUCCESS; |
| 371 | case Oddball::kNull: |
| 372 | if (deferred_string_key) SerializeDeferredKey(comma, key); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 373 | AppendAscii("null"); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 374 | return SUCCESS; |
| 375 | default: |
| 376 | return UNCHANGED; |
| 377 | } |
| 378 | case JS_ARRAY_TYPE: |
| 379 | if (deferred_string_key) SerializeDeferredKey(comma, key); |
| 380 | return SerializeJSArray(Handle<JSArray>::cast(object)); |
| 381 | case JS_VALUE_TYPE: |
| 382 | if (deferred_string_key) SerializeDeferredKey(comma, key); |
| 383 | return SerializeJSValue(Handle<JSValue>::cast(object)); |
| 384 | case JS_FUNCTION_TYPE: |
| 385 | return UNCHANGED; |
| 386 | default: |
| 387 | if (object->IsString()) { |
| 388 | if (deferred_string_key) SerializeDeferredKey(comma, key); |
| 389 | SerializeString(Handle<String>::cast(object)); |
| 390 | return SUCCESS; |
| 391 | } else if (object->IsJSObject()) { |
| 392 | if (deferred_string_key) SerializeDeferredKey(comma, key); |
| 393 | return SerializeJSObject(Handle<JSObject>::cast(object)); |
| 394 | } else { |
| 395 | return SerializeGeneric(object, key, comma, deferred_string_key); |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | |
| 401 | BasicJsonStringifier::Result BasicJsonStringifier::SerializeGeneric( |
| 402 | Handle<Object> object, |
| 403 | Handle<Object> key, |
| 404 | bool deferred_comma, |
| 405 | bool deferred_key) { |
| 406 | Handle<JSObject> builtins(isolate_->native_context()->builtins()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 407 | Handle<JSFunction> builtin = |
| 408 | Handle<JSFunction>::cast(GetProperty(builtins, "JSONSerializeAdapter")); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 409 | |
| 410 | Handle<Object> argv[] = { key, object }; |
| 411 | bool has_exception = false; |
| 412 | Handle<Object> result = |
| 413 | Execution::Call(builtin, object, 2, argv, &has_exception); |
| 414 | if (has_exception) return EXCEPTION; |
| 415 | if (result->IsUndefined()) return UNCHANGED; |
| 416 | if (deferred_key) { |
| 417 | if (key->IsSmi()) key = factory_->NumberToString(key); |
| 418 | SerializeDeferredKey(deferred_comma, key); |
| 419 | } |
| 420 | |
| 421 | Handle<String> result_string = Handle<String>::cast(result); |
| 422 | // Shrink current part, attach it to the accumulator, also attach the result |
| 423 | // string to the accumulator, and allocate a new part. |
| 424 | ShrinkCurrentPart(); // Shrink. |
| 425 | part_length_ = kInitialPartLength; // Allocate conservatively. |
| 426 | Extend(); // Attach current part and allocate new part. |
| 427 | // Attach result string to the accumulator. |
| 428 | set_accumulator(factory_->NewConsString(accumulator(), result_string)); |
| 429 | return SUCCESS; |
| 430 | } |
| 431 | |
| 432 | |
| 433 | BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSValue( |
| 434 | Handle<JSValue> object) { |
| 435 | bool has_exception = false; |
| 436 | String* class_name = object->class_name(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 437 | if (class_name == isolate_->heap()->String_string()) { |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 438 | Handle<Object> value = Execution::ToString(object, &has_exception); |
| 439 | if (has_exception) return EXCEPTION; |
| 440 | SerializeString(Handle<String>::cast(value)); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 441 | } else if (class_name == isolate_->heap()->Number_string()) { |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 442 | Handle<Object> value = Execution::ToNumber(object, &has_exception); |
| 443 | if (has_exception) return EXCEPTION; |
| 444 | if (value->IsSmi()) return SerializeSmi(Smi::cast(*value)); |
| 445 | SerializeHeapNumber(Handle<HeapNumber>::cast(value)); |
| 446 | } else { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 447 | ASSERT(class_name == isolate_->heap()->Boolean_string()); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 448 | Object* value = JSValue::cast(*object)->value(); |
| 449 | ASSERT(value->IsBoolean()); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 450 | AppendAscii(value->IsTrue() ? "true" : "false"); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 451 | } |
| 452 | return SUCCESS; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | |
| 456 | BasicJsonStringifier::Result BasicJsonStringifier::SerializeSmi(Smi* object) { |
| 457 | static const int kBufferSize = 100; |
| 458 | char chars[kBufferSize]; |
| 459 | Vector<char> buffer(chars, kBufferSize); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 460 | AppendAscii(IntToCString(object->value(), buffer)); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 461 | return SUCCESS; |
| 462 | } |
| 463 | |
| 464 | |
| 465 | BasicJsonStringifier::Result BasicJsonStringifier::SerializeDouble( |
| 466 | double number) { |
| 467 | if (isinf(number) || isnan(number)) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 468 | AppendAscii("null"); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 469 | return SUCCESS; |
| 470 | } |
| 471 | static const int kBufferSize = 100; |
| 472 | char chars[kBufferSize]; |
| 473 | Vector<char> buffer(chars, kBufferSize); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 474 | AppendAscii(DoubleToCString(number, buffer)); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 475 | return SUCCESS; |
| 476 | } |
| 477 | |
| 478 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 479 | BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArray( |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 480 | Handle<JSArray> object) { |
| 481 | HandleScope handle_scope(isolate_); |
| 482 | Result stack_push = StackPush(object); |
| 483 | if (stack_push != SUCCESS) return stack_push; |
| 484 | int length = Smi::cast(object->length())->value(); |
| 485 | Append('['); |
| 486 | switch (object->GetElementsKind()) { |
| 487 | case FAST_SMI_ELEMENTS: { |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 488 | Handle<FixedArray> elements( |
| 489 | FixedArray::cast(object->elements()), isolate_); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 490 | for (int i = 0; i < length; i++) { |
| 491 | if (i > 0) Append(','); |
| 492 | SerializeSmi(Smi::cast(elements->get(i))); |
| 493 | } |
| 494 | break; |
| 495 | } |
| 496 | case FAST_DOUBLE_ELEMENTS: { |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 497 | Handle<FixedDoubleArray> elements( |
| 498 | FixedDoubleArray::cast(object->elements()), isolate_); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 499 | for (int i = 0; i < length; i++) { |
| 500 | if (i > 0) Append(','); |
| 501 | SerializeDouble(elements->get_scalar(i)); |
| 502 | } |
| 503 | break; |
| 504 | } |
| 505 | case FAST_ELEMENTS: { |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 506 | Handle<FixedArray> elements( |
| 507 | FixedArray::cast(object->elements()), isolate_); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 508 | for (int i = 0; i < length; i++) { |
| 509 | if (i > 0) Append(','); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 510 | Result result = |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 511 | SerializeElement(isolate_, |
| 512 | Handle<Object>(elements->get(i), isolate_), |
| 513 | i); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 514 | if (result == SUCCESS) continue; |
| 515 | if (result == UNCHANGED) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 516 | AppendAscii("null"); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 517 | } else { |
| 518 | return result; |
| 519 | } |
| 520 | } |
| 521 | break; |
| 522 | } |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 523 | // TODO(yangguo): The FAST_HOLEY_* cases could be handled in a faster way. |
| 524 | // They resemble the non-holey cases except that a prototype chain lookup |
| 525 | // is necessary for holes. |
| 526 | default: { |
| 527 | Result result = SerializeJSArraySlow(object, length); |
| 528 | if (result != SUCCESS) return result; |
| 529 | break; |
| 530 | } |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 531 | } |
| 532 | Append(']'); |
| 533 | StackPop(); |
| 534 | current_part_ = handle_scope.CloseAndEscape(current_part_); |
| 535 | return SUCCESS; |
| 536 | } |
| 537 | |
| 538 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 539 | BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArraySlow( |
| 540 | Handle<JSArray> object, int length) { |
| 541 | for (int i = 0; i < length; i++) { |
| 542 | if (i > 0) Append(','); |
| 543 | Handle<Object> element = Object::GetElement(object, i); |
| 544 | if (element->IsUndefined()) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 545 | AppendAscii("null"); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 546 | } else { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 547 | Result result = SerializeElement(object->GetIsolate(), element, i); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 548 | if (result == SUCCESS) continue; |
| 549 | if (result == UNCHANGED) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 550 | AppendAscii("null"); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 551 | } else { |
| 552 | return result; |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | return SUCCESS; |
| 557 | } |
| 558 | |
| 559 | |
| 560 | BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSObject( |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 561 | Handle<JSObject> object) { |
| 562 | HandleScope handle_scope(isolate_); |
| 563 | Result stack_push = StackPush(object); |
| 564 | if (stack_push != SUCCESS) return stack_push; |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 565 | if (object->IsJSGlobalProxy()) { |
| 566 | object = Handle<JSObject>( |
| 567 | JSObject::cast(object->GetPrototype()), isolate_); |
| 568 | ASSERT(object->IsGlobalObject()); |
| 569 | } |
| 570 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 571 | Append('{'); |
| 572 | bool comma = false; |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 573 | |
| 574 | if (object->HasFastProperties() && |
| 575 | !object->HasIndexedInterceptor() && |
| 576 | !object->HasNamedInterceptor() && |
| 577 | object->elements()->length() == 0) { |
| 578 | Handle<Map> map(object->map()); |
| 579 | for (int i = 0; i < map->NumberOfOwnDescriptors(); i++) { |
| 580 | Handle<String> key(map->instance_descriptors()->GetKey(i), isolate_); |
| 581 | PropertyDetails details = map->instance_descriptors()->GetDetails(i); |
| 582 | if (details.IsDontEnum() || details.IsDeleted()) continue; |
| 583 | Handle<Object> property; |
| 584 | if (details.type() == FIELD && *map == object->map()) { |
| 585 | property = Handle<Object>( |
| 586 | object->FastPropertyAt( |
| 587 | map->instance_descriptors()->GetFieldIndex(i)), |
| 588 | isolate_); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 589 | } else { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 590 | property = GetProperty(isolate_, object, key); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 591 | if (property.is_null()) return EXCEPTION; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 592 | } |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 593 | Result result = SerializeProperty(property, comma, key); |
| 594 | if (!comma && result == SUCCESS) comma = true; |
| 595 | if (result >= EXCEPTION) return result; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 596 | } |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 597 | } else { |
| 598 | bool has_exception = false; |
| 599 | Handle<FixedArray> contents = |
| 600 | GetKeysInFixedArrayFor(object, LOCAL_ONLY, &has_exception); |
| 601 | if (has_exception) return EXCEPTION; |
| 602 | |
| 603 | for (int i = 0; i < contents->length(); i++) { |
| 604 | Object* key = contents->get(i); |
| 605 | Handle<String> key_handle; |
| 606 | Handle<Object> property; |
| 607 | if (key->IsString()) { |
| 608 | key_handle = Handle<String>(String::cast(key), isolate_); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 609 | property = GetProperty(isolate_, object, key_handle); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 610 | } else { |
| 611 | ASSERT(key->IsNumber()); |
| 612 | key_handle = factory_->NumberToString(Handle<Object>(key, isolate_)); |
| 613 | uint32_t index; |
| 614 | if (key->IsSmi()) { |
| 615 | property = Object::GetElement(object, Smi::cast(key)->value()); |
| 616 | } else if (key_handle->AsArrayIndex(&index)) { |
| 617 | property = Object::GetElement(object, index); |
| 618 | } else { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 619 | property = GetProperty(isolate_, object, key_handle); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | if (property.is_null()) return EXCEPTION; |
| 623 | Result result = SerializeProperty(property, comma, key_handle); |
| 624 | if (!comma && result == SUCCESS) comma = true; |
| 625 | if (result >= EXCEPTION) return result; |
| 626 | } |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 627 | } |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 628 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 629 | Append('}'); |
| 630 | StackPop(); |
| 631 | current_part_ = handle_scope.CloseAndEscape(current_part_); |
| 632 | return SUCCESS; |
| 633 | } |
| 634 | |
| 635 | |
| 636 | void BasicJsonStringifier::ShrinkCurrentPart() { |
| 637 | ASSERT(current_index_ < part_length_); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 638 | current_part_ = Handle<String>( |
| 639 | SeqString::cast(*current_part_)->Truncate(current_index_), isolate_); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 643 | void BasicJsonStringifier::Extend() { |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 644 | set_accumulator(factory_->NewConsString(accumulator(), current_part_)); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 645 | if (part_length_ <= kMaxPartLength / kPartLengthGrowthFactor) { |
| 646 | part_length_ *= kPartLengthGrowthFactor; |
| 647 | } |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 648 | if (is_ascii_) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 649 | current_part_ = factory_->NewRawOneByteString(part_length_); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 650 | } else { |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 651 | current_part_ = factory_->NewRawTwoByteString(part_length_); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 652 | } |
| 653 | current_index_ = 0; |
| 654 | } |
| 655 | |
| 656 | |
| 657 | void BasicJsonStringifier::ChangeEncoding() { |
| 658 | ShrinkCurrentPart(); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 659 | set_accumulator(factory_->NewConsString(accumulator(), current_part_)); |
| 660 | current_part_ = factory_->NewRawTwoByteString(part_length_); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 661 | current_index_ = 0; |
| 662 | is_ascii_ = false; |
| 663 | } |
| 664 | |
| 665 | |
| 666 | template <typename SrcChar, typename DestChar> |
| 667 | void BasicJsonStringifier::SerializeStringUnchecked_(const SrcChar* src, |
| 668 | DestChar* dest, |
| 669 | int length) { |
| 670 | dest += current_index_; |
| 671 | DestChar* dest_start = dest; |
| 672 | |
| 673 | // Assert that uc16 character is not truncated down to 8 bit. |
| 674 | // The <uc16, char> version of this method must not be called. |
| 675 | ASSERT(sizeof(*dest) >= sizeof(*src)); |
| 676 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 677 | for (int i = 0; i < length; i++) { |
| 678 | SrcChar c = src[i]; |
| 679 | if (DoNotEscape(c)) { |
| 680 | *(dest++) = static_cast<DestChar>(c); |
| 681 | } else { |
| 682 | const char* chars = &JsonEscapeTable[c * kJsonEscapeTableEntrySize]; |
| 683 | while (*chars != '\0') *(dest++) = *(chars++); |
| 684 | } |
| 685 | } |
| 686 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 687 | current_index_ += static_cast<int>(dest - dest_start); |
| 688 | } |
| 689 | |
| 690 | |
| 691 | template <bool is_ascii, typename Char> |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 692 | void BasicJsonStringifier::SerializeString_(Handle<String> string) { |
| 693 | int length = string->length(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 694 | Append_<is_ascii, char>('"'); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 695 | // We make a rough estimate to find out if the current string can be |
| 696 | // serialized without allocating a new string part. The worst case length of |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 697 | // an escaped character is 6. Shifting the remainin string length right by 3 |
| 698 | // is a more pessimistic estimate, but faster to calculate. |
| 699 | |
| 700 | if (((part_length_ - current_index_) >> 3) > length) { |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 701 | AssertNoAllocation no_allocation; |
| 702 | Vector<const Char> vector = GetCharVector<Char>(string); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 703 | if (is_ascii) { |
| 704 | SerializeStringUnchecked_( |
| 705 | vector.start(), |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 706 | SeqOneByteString::cast(*current_part_)->GetChars(), |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 707 | length); |
| 708 | } else { |
| 709 | SerializeStringUnchecked_( |
| 710 | vector.start(), |
| 711 | SeqTwoByteString::cast(*current_part_)->GetChars(), |
| 712 | length); |
| 713 | } |
| 714 | } else { |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 715 | String* string_location = *string; |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 716 | Vector<const Char> vector = GetCharVector<Char>(string); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 717 | for (int i = 0; i < length; i++) { |
| 718 | Char c = vector[i]; |
| 719 | if (DoNotEscape(c)) { |
| 720 | Append_<is_ascii, Char>(c); |
| 721 | } else { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 722 | Append_<is_ascii, uint8_t>( |
| 723 | reinterpret_cast<const uint8_t*>( |
| 724 | &JsonEscapeTable[c * kJsonEscapeTableEntrySize])); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 725 | } |
| 726 | // If GC moved the string, we need to refresh the vector. |
| 727 | if (*string != string_location) { |
| 728 | vector = GetCharVector<Char>(string); |
| 729 | string_location = *string; |
| 730 | } |
| 731 | } |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 732 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 733 | |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 734 | Append_<is_ascii, uint8_t>('"'); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 738 | template <> |
| 739 | bool BasicJsonStringifier::DoNotEscape(uint8_t c) { |
| 740 | return c >= '#' && c <= '~' && c != '\\'; |
| 741 | } |
| 742 | |
| 743 | |
| 744 | template <> |
| 745 | bool BasicJsonStringifier::DoNotEscape(uint16_t c) { |
| 746 | return c >= '#' && c != '\\' && c != 0x7f; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | |
| 750 | template <> |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 751 | Vector<const uint8_t> BasicJsonStringifier::GetCharVector( |
| 752 | Handle<String> string) { |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 753 | String::FlatContent flat = string->GetFlatContent(); |
| 754 | ASSERT(flat.IsAscii()); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 755 | return flat.ToOneByteVector(); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | |
| 759 | template <> |
| 760 | Vector<const uc16> BasicJsonStringifier::GetCharVector(Handle<String> string) { |
| 761 | String::FlatContent flat = string->GetFlatContent(); |
| 762 | ASSERT(flat.IsTwoByte()); |
| 763 | return flat.ToUC16Vector(); |
| 764 | } |
| 765 | |
| 766 | |
| 767 | void BasicJsonStringifier::SerializeString(Handle<String> object) { |
| 768 | FlattenString(object); |
| 769 | String::FlatContent flat = object->GetFlatContent(); |
| 770 | if (is_ascii_) { |
| 771 | if (flat.IsAscii()) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 772 | SerializeString_<true, uint8_t>(object); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 773 | } else { |
| 774 | ChangeEncoding(); |
| 775 | SerializeString(object); |
| 776 | } |
| 777 | } else { |
| 778 | if (flat.IsAscii()) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 779 | SerializeString_<false, uint8_t>(object); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 780 | } else { |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 781 | SerializeString_<false, uc16>(object); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | } } // namespace v8::internal |
| 787 | |
| 788 | #endif // V8_JSON_STRINGIFIER_H_ |