OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 // Defined when linking against shared lib on Windows. | 6 // Defined when linking against shared lib on Windows. |
7 #if defined(USING_V8_SHARED) && !defined(V8_SHARED) | 7 #if defined(USING_V8_SHARED) && !defined(V8_SHARED) |
8 #define V8_SHARED | 8 #define V8_SHARED |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 1515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1526 if (thread_ == NULL) return; | 1526 if (thread_ == NULL) return; |
1527 if (Shell::options.last_run) { | 1527 if (Shell::options.last_run) { |
1528 thread_->Join(); | 1528 thread_->Join(); |
1529 } else { | 1529 } else { |
1530 done_semaphore_.Wait(); | 1530 done_semaphore_.Wait(); |
1531 } | 1531 } |
1532 } | 1532 } |
1533 | 1533 |
1534 | 1534 |
1535 SerializationData::~SerializationData() { | 1535 SerializationData::~SerializationData() { |
1536 // Any ArrayBuffer::Contents are owned by this SerializationData object if | 1536 // Any ArrayBuffer::Contents are owned by this SerializationData object. |
1537 // ownership hasn't been transferred out via ReadArrayBufferContents. | 1537 // SharedArrayBuffer::Contents may be used by other threads, so must be |
1538 // SharedArrayBuffer::Contents may be used by multiple threads, so must be | |
1539 // cleaned up by the main thread in Shell::CleanupWorkers(). | 1538 // cleaned up by the main thread in Shell::CleanupWorkers(). |
1540 for (int i = 0; i < array_buffer_contents_.length(); ++i) { | 1539 for (int i = 0; i < array_buffer_contents.length(); ++i) { |
1541 ArrayBuffer::Contents& contents = array_buffer_contents_[i]; | 1540 ArrayBuffer::Contents& contents = array_buffer_contents[i]; |
1542 if (contents.Data()) { | 1541 Shell::array_buffer_allocator->Free(contents.Data(), contents.ByteLength()); |
1543 Shell::array_buffer_allocator->Free(contents.Data(), | |
1544 contents.ByteLength()); | |
1545 } | |
1546 } | 1542 } |
1547 } | 1543 } |
1548 | 1544 |
1549 | 1545 |
1550 void SerializationData::WriteTag(SerializationTag tag) { data_.Add(tag); } | 1546 void SerializationData::WriteTag(SerializationTag tag) { data.Add(tag); } |
1551 | 1547 |
1552 | 1548 |
1553 void SerializationData::WriteMemory(const void* p, int length) { | 1549 void SerializationData::WriteMemory(const void* p, int length) { |
1554 if (length > 0) { | 1550 if (length > 0) { |
1555 i::Vector<uint8_t> block = data_.AddBlock(0, length); | 1551 i::Vector<uint8_t> block = data.AddBlock(0, length); |
1556 memcpy(&block[0], p, length); | 1552 memcpy(&block[0], p, length); |
1557 } | 1553 } |
1558 } | 1554 } |
1559 | 1555 |
1560 | 1556 |
1561 void SerializationData::WriteArrayBufferContents( | 1557 void SerializationData::WriteArrayBufferContents( |
1562 const ArrayBuffer::Contents& contents) { | 1558 const ArrayBuffer::Contents& contents) { |
1563 array_buffer_contents_.Add(contents); | 1559 array_buffer_contents.Add(contents); |
1564 WriteTag(kSerializationTagTransferredArrayBuffer); | 1560 WriteTag(kSerializationTagTransferredArrayBuffer); |
1565 int index = array_buffer_contents_.length() - 1; | 1561 int index = array_buffer_contents.length() - 1; |
1566 Write(index); | 1562 Write(index); |
1567 } | 1563 } |
1568 | 1564 |
1569 | 1565 |
1570 void SerializationData::WriteSharedArrayBufferContents( | 1566 void SerializationData::WriteSharedArrayBufferContents( |
1571 const SharedArrayBuffer::Contents& contents) { | 1567 const SharedArrayBuffer::Contents& contents) { |
1572 shared_array_buffer_contents_.Add(contents); | 1568 shared_array_buffer_contents.Add(contents); |
1573 WriteTag(kSerializationTagTransferredSharedArrayBuffer); | 1569 WriteTag(kSerializationTagTransferredSharedArrayBuffer); |
1574 int index = shared_array_buffer_contents_.length() - 1; | 1570 int index = shared_array_buffer_contents.length() - 1; |
1575 Write(index); | 1571 Write(index); |
1576 } | 1572 } |
1577 | 1573 |
1578 | 1574 |
1579 SerializationTag SerializationData::ReadTag(int* offset) const { | 1575 SerializationTag SerializationData::ReadTag(int* offset) const { |
1580 return static_cast<SerializationTag>(Read<uint8_t>(offset)); | 1576 return static_cast<SerializationTag>(Read<uint8_t>(offset)); |
1581 } | 1577 } |
1582 | 1578 |
1583 | 1579 |
1584 void SerializationData::ReadMemory(void* p, int length, int* offset) const { | 1580 void SerializationData::ReadMemory(void* p, int length, int* offset) const { |
1585 if (length > 0) { | 1581 if (length > 0) { |
1586 memcpy(p, &data_[*offset], length); | 1582 memcpy(p, &data[*offset], length); |
1587 (*offset) += length; | 1583 (*offset) += length; |
1588 } | 1584 } |
1589 } | 1585 } |
1590 | 1586 |
1591 | 1587 |
1592 void SerializationData::ReadArrayBufferContents(ArrayBuffer::Contents* contents, | 1588 void SerializationData::ReadArrayBufferContents(ArrayBuffer::Contents* contents, |
1593 int* offset) const { | 1589 int* offset) const { |
1594 int index = Read<int>(offset); | 1590 int index = Read<int>(offset); |
1595 DCHECK(index < array_buffer_contents_.length()); | 1591 DCHECK(index < array_buffer_contents.length()); |
1596 *contents = array_buffer_contents_[index]; | 1592 *contents = array_buffer_contents[index]; |
1597 // Ownership of this ArrayBuffer::Contents is passed to the caller. Neuter | |
1598 // our copy so it won't be double-free'd when this SerializationData is | |
1599 // destroyed. | |
1600 array_buffer_contents_[index] = ArrayBuffer::Contents(); | |
1601 } | 1593 } |
1602 | 1594 |
1603 | 1595 |
1604 void SerializationData::ReadSharedArrayBufferContents( | 1596 void SerializationData::ReadSharedArrayBufferContents( |
1605 SharedArrayBuffer::Contents* contents, int* offset) const { | 1597 SharedArrayBuffer::Contents* contents, int* offset) const { |
1606 int index = Read<int>(offset); | 1598 int index = Read<int>(offset); |
1607 DCHECK(index < shared_array_buffer_contents_.length()); | 1599 DCHECK(index < shared_array_buffer_contents.length()); |
1608 *contents = shared_array_buffer_contents_[index]; | 1600 *contents = shared_array_buffer_contents[index]; |
1609 } | 1601 } |
1610 | 1602 |
1611 | 1603 |
1612 void SerializationDataQueue::Enqueue(SerializationData* data) { | 1604 void SerializationDataQueue::Enqueue(SerializationData* data) { |
1613 base::LockGuard<base::Mutex> lock_guard(&mutex_); | 1605 base::LockGuard<base::Mutex> lock_guard(&mutex_); |
1614 data_.Add(data); | 1606 data_.Add(data); |
1615 } | 1607 } |
1616 | 1608 |
1617 | 1609 |
1618 bool SerializationDataQueue::Dequeue(SerializationData** data) { | 1610 bool SerializationDataQueue::Dequeue(SerializationData** data) { |
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2041 return false; | 2033 return false; |
2042 } | 2034 } |
2043 seen_objects->Add(array_buffer); | 2035 seen_objects->Add(array_buffer); |
2044 if (FindInObjectList(array_buffer, to_transfer)) { | 2036 if (FindInObjectList(array_buffer, to_transfer)) { |
2045 // Transfer ArrayBuffer | 2037 // Transfer ArrayBuffer |
2046 if (!array_buffer->IsNeuterable()) { | 2038 if (!array_buffer->IsNeuterable()) { |
2047 Throw(isolate, "Attempting to transfer an un-neuterable ArrayBuffer"); | 2039 Throw(isolate, "Attempting to transfer an un-neuterable ArrayBuffer"); |
2048 return false; | 2040 return false; |
2049 } | 2041 } |
2050 | 2042 |
2051 ArrayBuffer::Contents contents = array_buffer->IsExternal() | 2043 ArrayBuffer::Contents contents = array_buffer->Externalize(); |
2052 ? array_buffer->GetContents() | |
2053 : array_buffer->Externalize(); | |
2054 array_buffer->Neuter(); | 2044 array_buffer->Neuter(); |
2055 out_data->WriteArrayBufferContents(contents); | 2045 out_data->WriteArrayBufferContents(contents); |
2056 } else { | 2046 } else { |
2057 ArrayBuffer::Contents contents = array_buffer->GetContents(); | 2047 ArrayBuffer::Contents contents = array_buffer->GetContents(); |
2058 // Clone ArrayBuffer | 2048 // Clone ArrayBuffer |
2059 if (contents.ByteLength() > i::kMaxUInt32) { | 2049 if (contents.ByteLength() > i::kMaxUInt32) { |
2060 Throw(isolate, "ArrayBuffer is too big to clone"); | 2050 Throw(isolate, "ArrayBuffer is too big to clone"); |
2061 return false; | 2051 return false; |
2062 } | 2052 } |
2063 | 2053 |
2064 int byte_length = static_cast<int>(contents.ByteLength()); | 2054 int byte_length = static_cast<int>(contents.ByteLength()); |
2065 out_data->WriteTag(kSerializationTagArrayBuffer); | 2055 out_data->WriteTag(kSerializationTagArrayBuffer); |
2066 out_data->Write(byte_length); | 2056 out_data->Write(byte_length); |
2067 out_data->WriteMemory(contents.Data(), | 2057 out_data->WriteMemory(contents.Data(), |
2068 static_cast<int>(contents.ByteLength())); | 2058 static_cast<int>(contents.ByteLength())); |
2069 } | 2059 } |
2070 } else if (value->IsSharedArrayBuffer()) { | 2060 } else if (value->IsSharedArrayBuffer()) { |
2071 Handle<SharedArrayBuffer> sab = Handle<SharedArrayBuffer>::Cast(value); | 2061 Handle<SharedArrayBuffer> sab = Handle<SharedArrayBuffer>::Cast(value); |
2072 if (FindInObjectList(sab, *seen_objects)) { | 2062 if (FindInObjectList(sab, *seen_objects)) { |
2073 Throw(isolate, "Duplicated shared array buffers not supported"); | 2063 Throw(isolate, "Duplicated shared array buffers not supported"); |
2074 return false; | 2064 return false; |
2075 } | 2065 } |
2076 seen_objects->Add(sab); | 2066 seen_objects->Add(sab); |
2077 if (!FindInObjectList(sab, to_transfer)) { | 2067 if (!FindInObjectList(sab, to_transfer)) { |
2078 Throw(isolate, "SharedArrayBuffer must be transferred"); | 2068 Throw(isolate, "SharedArrayBuffer must be transferred"); |
2079 return false; | 2069 return false; |
2080 } | 2070 } |
2081 | 2071 |
2082 SharedArrayBuffer::Contents contents; | 2072 SharedArrayBuffer::Contents contents = sab->Externalize(); |
2083 if (sab->IsExternal()) { | |
2084 contents = sab->GetContents(); | |
2085 } else { | |
2086 contents = sab->Externalize(); | |
2087 externalized_shared_contents_.Add(contents); | |
2088 } | |
2089 out_data->WriteSharedArrayBufferContents(contents); | 2073 out_data->WriteSharedArrayBufferContents(contents); |
| 2074 externalized_shared_contents_.Add(contents); |
2090 } else if (value->IsObject()) { | 2075 } else if (value->IsObject()) { |
2091 Handle<Object> object = Handle<Object>::Cast(value); | 2076 Handle<Object> object = Handle<Object>::Cast(value); |
2092 if (FindInObjectList(object, *seen_objects)) { | 2077 if (FindInObjectList(object, *seen_objects)) { |
2093 Throw(isolate, "Duplicated objects not supported"); | 2078 Throw(isolate, "Duplicated objects not supported"); |
2094 return false; | 2079 return false; |
2095 } | 2080 } |
2096 seen_objects->Add(object); | 2081 seen_objects->Add(object); |
2097 Local<Array> property_names; | 2082 Local<Array> property_names; |
2098 if (!object->GetOwnPropertyNames(context).ToLocal(&property_names)) { | 2083 if (!object->GetOwnPropertyNames(context).ToLocal(&property_names)) { |
2099 Throw(isolate, "Unable to get property names"); | 2084 Throw(isolate, "Unable to get property names"); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2195 Handle<ArrayBuffer> array_buffer = ArrayBuffer::New(isolate, byte_length); | 2180 Handle<ArrayBuffer> array_buffer = ArrayBuffer::New(isolate, byte_length); |
2196 ArrayBuffer::Contents contents = array_buffer->GetContents(); | 2181 ArrayBuffer::Contents contents = array_buffer->GetContents(); |
2197 DCHECK(static_cast<size_t>(byte_length) == contents.ByteLength()); | 2182 DCHECK(static_cast<size_t>(byte_length) == contents.ByteLength()); |
2198 data.ReadMemory(contents.Data(), byte_length, offset); | 2183 data.ReadMemory(contents.Data(), byte_length, offset); |
2199 result = array_buffer; | 2184 result = array_buffer; |
2200 break; | 2185 break; |
2201 } | 2186 } |
2202 case kSerializationTagTransferredArrayBuffer: { | 2187 case kSerializationTagTransferredArrayBuffer: { |
2203 ArrayBuffer::Contents contents; | 2188 ArrayBuffer::Contents contents; |
2204 data.ReadArrayBufferContents(&contents, offset); | 2189 data.ReadArrayBufferContents(&contents, offset); |
2205 result = ArrayBuffer::New(isolate, contents.Data(), contents.ByteLength(), | 2190 result = |
2206 ArrayBufferCreationMode::kInternalized); | 2191 ArrayBuffer::New(isolate, contents.Data(), contents.ByteLength()); |
2207 break; | 2192 break; |
2208 } | 2193 } |
2209 case kSerializationTagTransferredSharedArrayBuffer: { | 2194 case kSerializationTagTransferredSharedArrayBuffer: { |
2210 SharedArrayBuffer::Contents contents; | 2195 SharedArrayBuffer::Contents contents; |
2211 data.ReadSharedArrayBufferContents(&contents, offset); | 2196 data.ReadSharedArrayBufferContents(&contents, offset); |
2212 result = SharedArrayBuffer::New(isolate, contents.Data(), | 2197 result = SharedArrayBuffer::New(isolate, contents.Data(), |
2213 contents.ByteLength()); | 2198 contents.ByteLength()); |
2214 break; | 2199 break; |
2215 } | 2200 } |
2216 default: | 2201 default: |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2445 } | 2430 } |
2446 | 2431 |
2447 } // namespace v8 | 2432 } // namespace v8 |
2448 | 2433 |
2449 | 2434 |
2450 #ifndef GOOGLE3 | 2435 #ifndef GOOGLE3 |
2451 int main(int argc, char* argv[]) { | 2436 int main(int argc, char* argv[]) { |
2452 return v8::Shell::Main(argc, argv); | 2437 return v8::Shell::Main(argc, argv); |
2453 } | 2438 } |
2454 #endif | 2439 #endif |
OLD | NEW |