| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 | 273 |
| 274 private: | 274 private: |
| 275 ZigZag(); | 275 ZigZag(); |
| 276 }; | 276 }; |
| 277 | 277 |
| 278 // Writer is responsible for serializing primitive types and storing | 278 // Writer is responsible for serializing primitive types and storing |
| 279 // information used to reconstruct composite types. | 279 // information used to reconstruct composite types. |
| 280 class Writer { | 280 class Writer { |
| 281 WTF_MAKE_NONCOPYABLE(Writer); | 281 WTF_MAKE_NONCOPYABLE(Writer); |
| 282 public: | 282 public: |
| 283 Writer(v8::Isolate* isolate) | 283 explicit Writer(v8::Isolate* isolate) |
| 284 : m_position(0) | 284 : m_position(0) |
| 285 , m_isolate(isolate) | 285 , m_isolate(isolate) |
| 286 { | 286 { |
| 287 } | 287 } |
| 288 | 288 |
| 289 // Write functions for primitive types. | 289 // Write functions for primitive types. |
| 290 | 290 |
| 291 void writeUndefined() { append(UndefinedTag); } | 291 void writeUndefined() { append(UndefinedTag); } |
| 292 | 292 |
| 293 void writeNull() { append(NullTag); } | 293 void writeNull() { append(NullTag); } |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 525 doWriteUint32(length); | 525 doWriteUint32(length); |
| 526 } | 526 } |
| 527 | 527 |
| 528 void writeDenseArray(uint32_t numProperties, uint32_t length) | 528 void writeDenseArray(uint32_t numProperties, uint32_t length) |
| 529 { | 529 { |
| 530 append(DenseArrayTag); | 530 append(DenseArrayTag); |
| 531 doWriteUint32(numProperties); | 531 doWriteUint32(numProperties); |
| 532 doWriteUint32(length); | 532 doWriteUint32(length); |
| 533 } | 533 } |
| 534 | 534 |
| 535 Vector<BufferValueType>& data() | 535 StringBuffer<BufferValueType>& data() |
| 536 { | 536 { |
| 537 fillHole(); | 537 fillHole(); |
| 538 return m_buffer; | 538 return m_buffer; |
| 539 } | 539 } |
| 540 | 540 |
| 541 void writeReferenceCount(uint32_t numberOfReferences) | 541 void writeReferenceCount(uint32_t numberOfReferences) |
| 542 { | 542 { |
| 543 append(ReferenceCountTag); | 543 append(ReferenceCountTag); |
| 544 doWriteUint32(numberOfReferences); | 544 doWriteUint32(numberOfReferences); |
| 545 } | 545 } |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 639 void append(const uint8_t* data, int length) | 639 void append(const uint8_t* data, int length) |
| 640 { | 640 { |
| 641 ensureSpace(length); | 641 ensureSpace(length); |
| 642 memcpy(byteAt(m_position), data, length); | 642 memcpy(byteAt(m_position), data, length); |
| 643 m_position += length; | 643 m_position += length; |
| 644 } | 644 } |
| 645 | 645 |
| 646 void ensureSpace(int extra) | 646 void ensureSpace(int extra) |
| 647 { | 647 { |
| 648 COMPILE_ASSERT(sizeof(BufferValueType) == 2, BufferValueTypeIsTwoBytes); | 648 COMPILE_ASSERT(sizeof(BufferValueType) == 2, BufferValueTypeIsTwoBytes); |
| 649 m_buffer.grow((m_position + extra + 1) / 2); // "+ 1" to round up. | 649 m_buffer.resize((m_position + extra + 1) / 2); // "+ 1" to round up. |
| 650 } | 650 } |
| 651 | 651 |
| 652 void fillHole() | 652 void fillHole() |
| 653 { | 653 { |
| 654 COMPILE_ASSERT(sizeof(BufferValueType) == 2, BufferValueTypeIsTwoBytes); | 654 COMPILE_ASSERT(sizeof(BufferValueType) == 2, BufferValueTypeIsTwoBytes); |
| 655 // If the writer is at odd position in the buffer, then one of | 655 // If the writer is at odd position in the buffer, then one of |
| 656 // the bytes in the last UChar is not initialized. | 656 // the bytes in the last UChar is not initialized. |
| 657 if (m_position % 2) | 657 if (m_position % 2) |
| 658 *byteAt(m_position) = static_cast<uint8_t>(PaddingTag); | 658 *byteAt(m_position) = static_cast<uint8_t>(PaddingTag); |
| 659 } | 659 } |
| 660 | 660 |
| 661 uint8_t* byteAt(int position) | 661 uint8_t* byteAt(int position) |
| 662 { | 662 { |
| 663 return reinterpret_cast<uint8_t*>(m_buffer.data()) + position; | 663 return reinterpret_cast<uint8_t*>(m_buffer.characters()) + position; |
| 664 } | 664 } |
| 665 | 665 |
| 666 int v8StringWriteOptions() | 666 int v8StringWriteOptions() |
| 667 { | 667 { |
| 668 return v8::String::NO_NULL_TERMINATION; | 668 return v8::String::NO_NULL_TERMINATION; |
| 669 } | 669 } |
| 670 | 670 |
| 671 Vector<BufferValueType> m_buffer; | 671 StringBuffer<BufferValueType> m_buffer; |
| 672 unsigned m_position; | 672 unsigned m_position; |
| 673 v8::Isolate* m_isolate; | 673 v8::Isolate* m_isolate; |
| 674 }; | 674 }; |
| 675 | 675 |
| 676 static v8::Handle<v8::Object> toV8Object(MessagePort* impl, v8::Isolate* isolate
) | 676 static v8::Handle<v8::Object> toV8Object(MessagePort* impl, v8::Isolate* isolate
) |
| 677 { | 677 { |
| 678 if (!impl) | 678 if (!impl) |
| 679 return v8::Handle<v8::Object>(); | 679 return v8::Handle<v8::Object>(); |
| 680 v8::Handle<v8::Value> wrapper = toV8(impl, v8::Handle<v8::Object>(), isolate
); | 680 v8::Handle<v8::Value> wrapper = toV8(impl, v8::Handle<v8::Object>(), isolate
); |
| 681 ASSERT(wrapper->IsObject()); | 681 ASSERT(wrapper->IsObject()); |
| (...skipping 1580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2262 | 2262 |
| 2263 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create(const String& da
ta) | 2263 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create(const String& da
ta) |
| 2264 { | 2264 { |
| 2265 return create(data, v8::Isolate::GetCurrent()); | 2265 return create(data, v8::Isolate::GetCurrent()); |
| 2266 } | 2266 } |
| 2267 | 2267 |
| 2268 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create(const String& da
ta, v8::Isolate* isolate) | 2268 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create(const String& da
ta, v8::Isolate* isolate) |
| 2269 { | 2269 { |
| 2270 Writer writer(isolate); | 2270 Writer writer(isolate); |
| 2271 writer.writeWebCoreString(data); | 2271 writer.writeWebCoreString(data); |
| 2272 String wireData = StringImpl::adopt(writer.data()); | 2272 String wireData = String::adopt(writer.data()); |
| 2273 return adoptRef(new SerializedScriptValue(wireData)); | 2273 return adoptRef(new SerializedScriptValue(wireData)); |
| 2274 } | 2274 } |
| 2275 | 2275 |
| 2276 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create() | 2276 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create() |
| 2277 { | 2277 { |
| 2278 return adoptRef(new SerializedScriptValue()); | 2278 return adoptRef(new SerializedScriptValue()); |
| 2279 } | 2279 } |
| 2280 | 2280 |
| 2281 PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue() | 2281 PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue() |
| 2282 { | 2282 { |
| 2283 return nullValue(v8::Isolate::GetCurrent()); | 2283 return nullValue(v8::Isolate::GetCurrent()); |
| 2284 } | 2284 } |
| 2285 | 2285 |
| 2286 PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue(v8::Isolate*
isolate) | 2286 PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue(v8::Isolate*
isolate) |
| 2287 { | 2287 { |
| 2288 Writer writer(isolate); | 2288 Writer writer(isolate); |
| 2289 writer.writeNull(); | 2289 writer.writeNull(); |
| 2290 String wireData = StringImpl::adopt(writer.data()); | 2290 String wireData = String::adopt(writer.data()); |
| 2291 return adoptRef(new SerializedScriptValue(wireData)); | 2291 return adoptRef(new SerializedScriptValue(wireData)); |
| 2292 } | 2292 } |
| 2293 | 2293 |
| 2294 PassRefPtr<SerializedScriptValue> SerializedScriptValue::undefinedValue() | 2294 PassRefPtr<SerializedScriptValue> SerializedScriptValue::undefinedValue() |
| 2295 { | 2295 { |
| 2296 return undefinedValue(v8::Isolate::GetCurrent()); | 2296 return undefinedValue(v8::Isolate::GetCurrent()); |
| 2297 } | 2297 } |
| 2298 | 2298 |
| 2299 PassRefPtr<SerializedScriptValue> SerializedScriptValue::undefinedValue(v8::Isol
ate* isolate) | 2299 PassRefPtr<SerializedScriptValue> SerializedScriptValue::undefinedValue(v8::Isol
ate* isolate) |
| 2300 { | 2300 { |
| 2301 Writer writer(isolate); | 2301 Writer writer(isolate); |
| 2302 writer.writeUndefined(); | 2302 writer.writeUndefined(); |
| 2303 String wireData = StringImpl::adopt(writer.data()); | 2303 String wireData = String::adopt(writer.data()); |
| 2304 return adoptRef(new SerializedScriptValue(wireData)); | 2304 return adoptRef(new SerializedScriptValue(wireData)); |
| 2305 } | 2305 } |
| 2306 | 2306 |
| 2307 PassRefPtr<SerializedScriptValue> SerializedScriptValue::booleanValue(bool value
) | 2307 PassRefPtr<SerializedScriptValue> SerializedScriptValue::booleanValue(bool value
) |
| 2308 { | 2308 { |
| 2309 return booleanValue(value, v8::Isolate::GetCurrent()); | 2309 return booleanValue(value, v8::Isolate::GetCurrent()); |
| 2310 } | 2310 } |
| 2311 | 2311 |
| 2312 PassRefPtr<SerializedScriptValue> SerializedScriptValue::booleanValue(bool value
, v8::Isolate* isolate) | 2312 PassRefPtr<SerializedScriptValue> SerializedScriptValue::booleanValue(bool value
, v8::Isolate* isolate) |
| 2313 { | 2313 { |
| 2314 Writer writer(isolate); | 2314 Writer writer(isolate); |
| 2315 if (value) | 2315 if (value) |
| 2316 writer.writeTrue(); | 2316 writer.writeTrue(); |
| 2317 else | 2317 else |
| 2318 writer.writeFalse(); | 2318 writer.writeFalse(); |
| 2319 String wireData = StringImpl::adopt(writer.data()); | 2319 String wireData = String::adopt(writer.data()); |
| 2320 return adoptRef(new SerializedScriptValue(wireData)); | 2320 return adoptRef(new SerializedScriptValue(wireData)); |
| 2321 } | 2321 } |
| 2322 | 2322 |
| 2323 PassRefPtr<SerializedScriptValue> SerializedScriptValue::numberValue(double valu
e) | 2323 PassRefPtr<SerializedScriptValue> SerializedScriptValue::numberValue(double valu
e) |
| 2324 { | 2324 { |
| 2325 return numberValue(value, v8::Isolate::GetCurrent()); | 2325 return numberValue(value, v8::Isolate::GetCurrent()); |
| 2326 } | 2326 } |
| 2327 | 2327 |
| 2328 PassRefPtr<SerializedScriptValue> SerializedScriptValue::numberValue(double valu
e, v8::Isolate* isolate) | 2328 PassRefPtr<SerializedScriptValue> SerializedScriptValue::numberValue(double valu
e, v8::Isolate* isolate) |
| 2329 { | 2329 { |
| 2330 Writer writer(isolate); | 2330 Writer writer(isolate); |
| 2331 writer.writeNumber(value); | 2331 writer.writeNumber(value); |
| 2332 String wireData = StringImpl::adopt(writer.data()); | 2332 String wireData = String::adopt(writer.data()); |
| 2333 return adoptRef(new SerializedScriptValue(wireData)); | 2333 return adoptRef(new SerializedScriptValue(wireData)); |
| 2334 } | 2334 } |
| 2335 | 2335 |
| 2336 // Convert serialized string to big endian wire data. | 2336 // Convert serialized string to big endian wire data. |
| 2337 void SerializedScriptValue::toWireBytes(Vector<char>& result) const | 2337 void SerializedScriptValue::toWireBytes(Vector<char>& result) const |
| 2338 { | 2338 { |
| 2339 ASSERT(result.isEmpty()); | 2339 ASSERT(result.isEmpty()); |
| 2340 size_t length = m_data.length(); | 2340 size_t length = m_data.length(); |
| 2341 result.resize(length * sizeof(UChar)); | 2341 result.resize(length * sizeof(UChar)); |
| 2342 UChar* dst = reinterpret_cast<UChar*>(result.data()); | 2342 UChar* dst = reinterpret_cast<UChar*>(result.data()); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2454 if (policy == ThrowExceptions) | 2454 if (policy == ThrowExceptions) |
| 2455 setDOMException(InvalidStateError, isolate); | 2455 setDOMException(InvalidStateError, isolate); |
| 2456 return; | 2456 return; |
| 2457 case Serializer::JSFailure: | 2457 case Serializer::JSFailure: |
| 2458 // If there was a JS failure (but no exception), there's not | 2458 // If there was a JS failure (but no exception), there's not |
| 2459 // much we can do except for unwinding the C++ stack by | 2459 // much we can do except for unwinding the C++ stack by |
| 2460 // pretending there was a JS exception. | 2460 // pretending there was a JS exception. |
| 2461 didThrow = true; | 2461 didThrow = true; |
| 2462 return; | 2462 return; |
| 2463 case Serializer::Success: | 2463 case Serializer::Success: |
| 2464 m_data = String(StringImpl::adopt(writer.data())).isolatedCopy(); | 2464 // FIXME: This call to isolatedCopy should be redundant. |
| 2465 m_data = String(String::adopt(writer.data())).isolatedCopy(); |
| 2465 if (arrayBuffers && arrayBuffers->size()) | 2466 if (arrayBuffers && arrayBuffers->size()) |
| 2466 m_arrayBufferContentsArray = transferArrayBuffers(*arrayBuffers, did
Throw, isolate); | 2467 m_arrayBufferContentsArray = transferArrayBuffers(*arrayBuffers, did
Throw, isolate); |
| 2467 return; | 2468 return; |
| 2468 case Serializer::JSException: | 2469 case Serializer::JSException: |
| 2469 // We should never get here because this case was handled above. | 2470 // We should never get here because this case was handled above. |
| 2470 break; | 2471 break; |
| 2471 } | 2472 } |
| 2472 ASSERT_NOT_REACHED(); | 2473 ASSERT_NOT_REACHED(); |
| 2473 } | 2474 } |
| 2474 | 2475 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2528 v8::V8::AdjustAmountOfExternalAllocatedMemory(-m_externallyAllocatedMemo
ry); | 2529 v8::V8::AdjustAmountOfExternalAllocatedMemory(-m_externallyAllocatedMemo
ry); |
| 2529 } | 2530 } |
| 2530 } | 2531 } |
| 2531 | 2532 |
| 2532 uint32_t SerializedScriptValue::wireFormatVersion() | 2533 uint32_t SerializedScriptValue::wireFormatVersion() |
| 2533 { | 2534 { |
| 2534 return WebCore::wireFormatVersion; | 2535 return WebCore::wireFormatVersion; |
| 2535 } | 2536 } |
| 2536 | 2537 |
| 2537 } // namespace WebCore | 2538 } // namespace WebCore |
| OLD | NEW |