OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/dart_api_message.h" | 5 #include "vm/dart_api_message.h" |
6 #include "vm/object.h" | 6 #include "vm/object.h" |
7 #include "vm/snapshot_ids.h" | 7 #include "vm/snapshot_ids.h" |
8 #include "vm/symbols.h" | 8 #include "vm/symbols.h" |
9 #include "vm/unicode.h" | 9 #include "vm/unicode.h" |
10 | 10 |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 Dart_CObject* value = | 118 Dart_CObject* value = |
119 reinterpret_cast<Dart_CObject*>( | 119 reinterpret_cast<Dart_CObject*>( |
120 alloc_(NULL, 0, sizeof(Dart_CObject) + length + 1)); | 120 alloc_(NULL, 0, sizeof(Dart_CObject) + length + 1)); |
121 ASSERT(value != NULL); | 121 ASSERT(value != NULL); |
122 value->value.as_string = reinterpret_cast<char*>(value) + sizeof(*value); | 122 value->value.as_string = reinterpret_cast<char*>(value) + sizeof(*value); |
123 value->type = Dart_CObject::kString; | 123 value->type = Dart_CObject::kString; |
124 return value; | 124 return value; |
125 } | 125 } |
126 | 126 |
127 | 127 |
128 static int SizeInBytes(Dart_CObject::TypedDataType type) { | 128 static int GetTypedDataSizeInBytes(Dart_CObject::TypedDataType type) { |
129 switch (type) { | 129 switch (type) { |
130 case Dart_CObject::kInt8Array: | 130 case Dart_CObject::kInt8Array: |
131 case Dart_CObject::kUint8Array: | 131 case Dart_CObject::kUint8Array: |
| 132 case Dart_CObject::kUint8ClampedArray: |
132 return 1; | 133 return 1; |
133 case Dart_CObject::kInt16Array: | 134 case Dart_CObject::kInt16Array: |
134 case Dart_CObject::kUint16Array: | 135 case Dart_CObject::kUint16Array: |
135 return 2; | 136 return 2; |
| 137 case Dart_CObject::kInt32Array: |
| 138 case Dart_CObject::kUint32Array: |
| 139 case Dart_CObject::kFloat32Array: |
| 140 return 4; |
| 141 case Dart_CObject::kInt64Array: |
| 142 case Dart_CObject::kUint64Array: |
| 143 case Dart_CObject::kFloat64Array: |
| 144 return 8; |
136 default: | 145 default: |
137 break; | 146 break; |
138 } | 147 } |
139 UNREACHABLE(); | 148 UNREACHABLE(); |
140 return -1; | 149 return -1; |
141 } | 150 } |
142 | 151 |
143 | 152 |
144 Dart_CObject* ApiMessageReader::AllocateDartCObjectTypedData( | 153 Dart_CObject* ApiMessageReader::AllocateDartCObjectTypedData( |
145 Dart_CObject::TypedDataType type, intptr_t length) { | 154 Dart_CObject::TypedDataType type, intptr_t length) { |
146 // Allocate a Dart_CObject structure followed by an array of bytes | 155 // Allocate a Dart_CObject structure followed by an array of bytes |
147 // for the byte array content. The pointer to the byte array content | 156 // for the byte array content. The pointer to the byte array content |
148 // is set up to this area. | 157 // is set up to this area. |
149 intptr_t length_in_bytes = SizeInBytes(type) * length; | 158 intptr_t length_in_bytes = GetTypedDataSizeInBytes(type) * length; |
150 Dart_CObject* value = | 159 Dart_CObject* value = |
151 reinterpret_cast<Dart_CObject*>( | 160 reinterpret_cast<Dart_CObject*>( |
152 alloc_(NULL, 0, sizeof(Dart_CObject) + length_in_bytes)); | 161 alloc_(NULL, 0, sizeof(Dart_CObject) + length_in_bytes)); |
153 ASSERT(value != NULL); | 162 ASSERT(value != NULL); |
154 value->type = Dart_CObject::kTypedData; | 163 value->type = Dart_CObject::kTypedData; |
155 value->value.as_typed_data.type = type; | 164 value->value.as_typed_data.type = type; |
156 value->value.as_typed_data.length = length_in_bytes; | 165 value->value.as_typed_data.length = length_in_bytes; |
157 if (length > 0) { | 166 if (length > 0) { |
158 value->value.as_typed_data.values = | 167 value->value.as_typed_data.values = |
159 reinterpret_cast<uint8_t*>(value) + sizeof(*value); | 168 reinterpret_cast<uint8_t*>(value) + sizeof(*value); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 | 220 |
212 | 221 |
213 static Dart_CObject::TypedDataType GetTypedDataTypeFromView( | 222 static Dart_CObject::TypedDataType GetTypedDataTypeFromView( |
214 Dart_CObject_Internal* object) { | 223 Dart_CObject_Internal* object) { |
215 struct { | 224 struct { |
216 const char* name; | 225 const char* name; |
217 Dart_CObject::TypedDataType type; | 226 Dart_CObject::TypedDataType type; |
218 } view_class_names[] = { | 227 } view_class_names[] = { |
219 { "_Int8ArrayView", Dart_CObject::kInt8Array }, | 228 { "_Int8ArrayView", Dart_CObject::kInt8Array }, |
220 { "_Uint8ArrayView", Dart_CObject::kUint8Array }, | 229 { "_Uint8ArrayView", Dart_CObject::kUint8Array }, |
| 230 { "_Uint8ClampedArrayView", Dart_CObject::kUint8ClampedArray }, |
221 { "_Int16ArrayView", Dart_CObject::kInt16Array }, | 231 { "_Int16ArrayView", Dart_CObject::kInt16Array }, |
222 { "_Uint16ArrayView", Dart_CObject::kUint16Array }, | 232 { "_Uint16ArrayView", Dart_CObject::kUint16Array }, |
| 233 { "_Int32ArrayView", Dart_CObject::kInt32Array }, |
| 234 { "_Uint32ArrayView", Dart_CObject::kUint32Array }, |
| 235 { "_Int64ArrayView", Dart_CObject::kInt64Array }, |
| 236 { "_Uint64ArrayView", Dart_CObject::kUint64Array }, |
| 237 { "_ByteDataView", Dart_CObject::kUint8Array }, |
| 238 { "_Float32ArrayView", Dart_CObject::kFloat32Array }, |
| 239 { "_Float64ArrayView", Dart_CObject::kFloat64Array }, |
223 { NULL, Dart_CObject::kNumberOfTypedDataTypes }, | 240 { NULL, Dart_CObject::kNumberOfTypedDataTypes }, |
224 }; | 241 }; |
225 | 242 |
226 char* library_url = | 243 char* library_url = |
227 object->cls->internal.as_class.library_url->value.as_string; | 244 object->cls->internal.as_class.library_url->value.as_string; |
228 char* class_name = | 245 char* class_name = |
229 object->cls->internal.as_class.class_name->value.as_string; | 246 object->cls->internal.as_class.class_name->value.as_string; |
230 if (strcmp("dart:typeddata", library_url) != 0) { | 247 if (strcmp("dart:typeddata", library_url) != 0) { |
231 return Dart_CObject::kNumberOfTypedDataTypes; | 248 return Dart_CObject::kNumberOfTypedDataTypes; |
232 } | 249 } |
233 int i = 0; | 250 int i = 0; |
234 while (view_class_names[i].name != NULL) { | 251 while (view_class_names[i].name != NULL) { |
235 if (strncmp(view_class_names[i].name, | 252 if (strncmp(view_class_names[i].name, |
236 class_name, | 253 class_name, |
237 strlen(view_class_names[i].name)) == 0) { | 254 strlen(view_class_names[i].name)) == 0) { |
238 return view_class_names[i].type; | 255 return view_class_names[i].type; |
239 } | 256 } |
240 i++; | 257 i++; |
241 } | 258 } |
242 return Dart_CObject::kNumberOfTypedDataTypes; | 259 return Dart_CObject::kNumberOfTypedDataTypes; |
243 } | 260 } |
244 | 261 |
245 | 262 |
246 static int GetTypedDataTypeElementSize(Dart_CObject::TypedDataType type) { | |
247 switch (type) { | |
248 case Dart_CObject::kInt8Array: | |
249 case Dart_CObject::kUint8Array: | |
250 return 1; | |
251 case Dart_CObject::kInt16Array: | |
252 case Dart_CObject::kUint16Array: | |
253 return 2; | |
254 default: | |
255 break; | |
256 } | |
257 UNREACHABLE(); | |
258 return -1; | |
259 } | |
260 | |
261 | |
262 Dart_CObject* ApiMessageReader::ReadInlinedObject(intptr_t object_id) { | 263 Dart_CObject* ApiMessageReader::ReadInlinedObject(intptr_t object_id) { |
263 // Read the class header information and lookup the class. | 264 // Read the class header information and lookup the class. |
264 intptr_t class_header = ReadIntptrValue(); | 265 intptr_t class_header = ReadIntptrValue(); |
265 intptr_t tags = ReadIntptrValue(); | 266 intptr_t tags = ReadIntptrValue(); |
266 USE(tags); | 267 USE(tags); |
267 intptr_t class_id; | 268 intptr_t class_id; |
268 | 269 |
269 // There is limited support for reading regular dart instances. Only | 270 // There is limited support for reading regular dart instances. Only |
270 // typed data views are currently handled. | 271 // typed data views are currently handled. |
271 if (SerializedHeaderData::decode(class_header) == kInstanceObjectId) { | 272 if (SerializedHeaderData::decode(class_header) == kInstanceObjectId) { |
(...skipping 26 matching lines...) Expand all Loading... |
298 // The buffer is fully read now as typed data objects are | 299 // The buffer is fully read now as typed data objects are |
299 // serialized in-line. | 300 // serialized in-line. |
300 Dart_CObject* buffer = object->internal.as_view.buffer; | 301 Dart_CObject* buffer = object->internal.as_view.buffer; |
301 ASSERT(buffer->type == Dart_CObject::kTypedData); | 302 ASSERT(buffer->type == Dart_CObject::kTypedData); |
302 | 303 |
303 // Now turn the view into a byte array. | 304 // Now turn the view into a byte array. |
304 object->type = Dart_CObject::kTypedData; | 305 object->type = Dart_CObject::kTypedData; |
305 object->value.as_typed_data.type = type; | 306 object->value.as_typed_data.type = type; |
306 object->value.as_typed_data.length = | 307 object->value.as_typed_data.length = |
307 object->internal.as_view.length * | 308 object->internal.as_view.length * |
308 GetTypedDataTypeElementSize(type); | 309 GetTypedDataSizeInBytes(type); |
309 object->value.as_typed_data.values = | 310 object->value.as_typed_data.values = |
310 buffer->value.as_typed_data.values + | 311 buffer->value.as_typed_data.values + |
311 object->internal.as_view.offset_in_bytes; | 312 object->internal.as_view.offset_in_bytes; |
312 } else { | 313 } else { |
313 // TODO(sgjesse): Handle other instances. Currently this will | 314 // TODO(sgjesse): Handle other instances. Currently this will |
314 // skew the reading as the fields of the instance is not read. | 315 // skew the reading as the fields of the instance is not read. |
315 } | 316 } |
316 return object; | 317 return object; |
317 } | 318 } |
318 | 319 |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
594 } \ | 595 } \ |
595 | 596 |
596 case kTypedDataInt8ArrayCid: | 597 case kTypedDataInt8ArrayCid: |
597 case kExternalTypedDataInt8ArrayCid: | 598 case kExternalTypedDataInt8ArrayCid: |
598 READ_TYPED_DATA(Int8, int8_t); | 599 READ_TYPED_DATA(Int8, int8_t); |
599 | 600 |
600 case kTypedDataUint8ArrayCid: | 601 case kTypedDataUint8ArrayCid: |
601 case kExternalTypedDataUint8ArrayCid: | 602 case kExternalTypedDataUint8ArrayCid: |
602 READ_TYPED_DATA(Uint8, uint8_t); | 603 READ_TYPED_DATA(Uint8, uint8_t); |
603 | 604 |
| 605 case kTypedDataUint8ClampedArrayCid: |
| 606 case kExternalTypedDataUint8ClampedArrayCid: |
| 607 READ_TYPED_DATA(Uint8Clamped, uint8_t); |
| 608 |
604 case kTypedDataInt16ArrayCid: | 609 case kTypedDataInt16ArrayCid: |
605 case kExternalTypedDataInt16ArrayCid: | 610 case kExternalTypedDataInt16ArrayCid: |
606 READ_TYPED_DATA(Int16, int16_t); | 611 READ_TYPED_DATA(Int16, int16_t); |
607 | 612 |
608 case kTypedDataUint16ArrayCid: | 613 case kTypedDataUint16ArrayCid: |
609 case kExternalTypedDataUint16ArrayCid: | 614 case kExternalTypedDataUint16ArrayCid: |
610 READ_TYPED_DATA(Uint16, uint16_t); | 615 READ_TYPED_DATA(Uint16, uint16_t); |
611 | 616 |
| 617 case kTypedDataInt32ArrayCid: |
| 618 case kExternalTypedDataInt32ArrayCid: |
| 619 READ_TYPED_DATA(Int32, int32_t); |
| 620 |
| 621 case kTypedDataUint32ArrayCid: |
| 622 case kExternalTypedDataUint32ArrayCid: |
| 623 READ_TYPED_DATA(Uint32, uint32_t); |
| 624 |
| 625 case kTypedDataInt64ArrayCid: |
| 626 case kExternalTypedDataInt64ArrayCid: |
| 627 READ_TYPED_DATA(Int64, int64_t); |
| 628 |
| 629 case kTypedDataUint64ArrayCid: |
| 630 case kExternalTypedDataUint64ArrayCid: |
| 631 READ_TYPED_DATA(Uint64, uint64_t); |
| 632 |
| 633 case kTypedDataFloat32ArrayCid: |
| 634 case kExternalTypedDataFloat32ArrayCid: |
| 635 READ_TYPED_DATA(Float32, float); |
| 636 |
| 637 case kTypedDataFloat64ArrayCid: |
| 638 case kExternalTypedDataFloat64ArrayCid: |
| 639 READ_TYPED_DATA(Float64, double); |
| 640 |
612 case kGrowableObjectArrayCid: { | 641 case kGrowableObjectArrayCid: { |
613 // A GrowableObjectArray is serialized as its length followed by | 642 // A GrowableObjectArray is serialized as its length followed by |
614 // its backing store. The backing store is an array with a | 643 // its backing store. The backing store is an array with a |
615 // length which might be longer than the length of the | 644 // length which might be longer than the length of the |
616 // GrowableObjectArray. | 645 // GrowableObjectArray. |
617 intptr_t len = ReadSmiValue(); | 646 intptr_t len = ReadSmiValue(); |
618 | 647 |
619 Dart_CObject* value = GetBackRef(object_id); | 648 Dart_CObject* value = GetBackRef(object_id); |
620 ASSERT(value == NULL); | 649 ASSERT(value == NULL); |
621 // Allocate an empty array for the GrowableObjectArray which | 650 // Allocate an empty array for the GrowableObjectArray which |
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1083 if (!success) { | 1112 if (!success) { |
1084 UnmarkAllCObjects(object); | 1113 UnmarkAllCObjects(object); |
1085 return false; | 1114 return false; |
1086 } | 1115 } |
1087 } | 1116 } |
1088 UnmarkAllCObjects(object); | 1117 UnmarkAllCObjects(object); |
1089 return true; | 1118 return true; |
1090 } | 1119 } |
1091 | 1120 |
1092 } // namespace dart | 1121 } // namespace dart |
OLD | NEW |