Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(377)

Side by Side Diff: src/d8.cc

Issue 10389140: Improve typed arrays support in d8. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 ASSERT(kMaxLength == i::ExternalArray::kMaxLength); 308 ASSERT(kMaxLength == i::ExternalArray::kMaxLength);
309 #endif // V8_SHARED 309 #endif // V8_SHARED
310 if (raw_value > static_cast<int32_t>(kMaxLength)) { 310 if (raw_value > static_cast<int32_t>(kMaxLength)) {
311 ThrowException( 311 ThrowException(
312 String::New("Array length exceeds maximum length.")); 312 String::New("Array length exceeds maximum length."));
313 } 313 }
314 return static_cast<size_t>(raw_value); 314 return static_cast<size_t>(raw_value);
315 } 315 }
316 316
317 317
318 const char kArrayBufferReferencePropName[] = "_is_array_buffer_"; 318 const char kArrayBufferMarkerPropName[] = "_is_array_buffer_";
319 const char kArrayBufferMarkerPropName[] = "_array_buffer_ref_"; 319 const char kArrayBufferReferencePropName[] = "_array_buffer_ref_";
320 320
321 static const int kExternalArrayAllocationHeaderSize = 2; 321 static const int kExternalArrayAllocationHeaderSize = 2;
322 322
323 Handle<Value> Shell::CreateExternalArray(const Arguments& args, 323 Handle<Value> Shell::CreateExternalArray(const Arguments& args,
324 ExternalArrayType type, 324 ExternalArrayType type,
325 size_t element_size) { 325 size_t element_size) {
326 TryCatch try_catch; 326 TryCatch try_catch;
327 bool is_array_buffer_construct = element_size == 0; 327 bool is_array_buffer_construct = element_size == 0;
328 if (is_array_buffer_construct) { 328 if (is_array_buffer_construct) {
329 type = v8::kExternalByteArray; 329 type = v8::kExternalByteArray;
(...skipping 16 matching lines...) Expand all
346 // optional unsigned long byteOffset, 346 // optional unsigned long byteOffset,
347 // optional unsigned long length) 347 // optional unsigned long length)
348 if (args.Length() > 3) { 348 if (args.Length() > 3) {
349 return ThrowException( 349 return ThrowException(
350 String::New("Array constructor from ArrayBuffer must " 350 String::New("Array constructor from ArrayBuffer must "
351 "have 1-3 parameters.")); 351 "have 1-3 parameters."));
352 } 352 }
353 353
354 Local<Value> length_value = (args.Length() < 3) 354 Local<Value> length_value = (args.Length() < 3)
355 ? (first_arg_is_array_buffer 355 ? (first_arg_is_array_buffer
356 ? args[0]->ToObject()->Get(String::New("length")) 356 ? args[0]->ToObject()->Get(String::New("byteLength"))
357 : args[0]) 357 : args[0])
358 : args[2]; 358 : args[2];
359 size_t length = convertToUint(length_value, &try_catch); 359 size_t byteLength = convertToUint(length_value, &try_catch);
360 size_t length = byteLength;
360 if (try_catch.HasCaught()) return try_catch.Exception(); 361 if (try_catch.HasCaught()) return try_catch.Exception();
361 362
362 void* data = NULL; 363 void* data = NULL;
363 size_t offset = 0; 364 size_t offset = 0;
364 365
365 Handle<Object> array = Object::New(); 366 Handle<Object> array = Object::New();
366 if (first_arg_is_array_buffer) { 367 if (first_arg_is_array_buffer) {
367 Handle<Object> derived_from = args[0]->ToObject(); 368 Handle<Object> derived_from = args[0]->ToObject();
368 data = derived_from->GetIndexedPropertiesExternalArrayData(); 369 data = derived_from->GetIndexedPropertiesExternalArrayData();
369 370
370 size_t array_buffer_length = convertToUint( 371 size_t array_buffer_length = convertToUint(
371 derived_from->Get(String::New("length")), 372 derived_from->Get(String::New("byteLength")),
372 &try_catch); 373 &try_catch);
373 if (try_catch.HasCaught()) return try_catch.Exception(); 374 if (try_catch.HasCaught()) return try_catch.Exception();
374 375
375 if (data == NULL && array_buffer_length != 0) { 376 if (data == NULL && array_buffer_length != 0) {
376 return ThrowException( 377 return ThrowException(
377 String::New("ArrayBuffer doesn't have data")); 378 String::New("ArrayBuffer doesn't have data"));
378 } 379 }
379 380
380 if (args.Length() > 1) { 381 if (args.Length() > 1) {
381 offset = convertToUint(args[1], &try_catch); 382 offset = convertToUint(args[1], &try_catch);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 data = reinterpret_cast<size_t*>(data) + kExternalArrayAllocationHeaderSize; 445 data = reinterpret_cast<size_t*>(data) + kExternalArrayAllocationHeaderSize;
445 memset(data, 0, length * element_size); 446 memset(data, 0, length * element_size);
446 V8::AdjustAmountOfExternalAllocatedMemory(total_size); 447 V8::AdjustAmountOfExternalAllocatedMemory(total_size);
447 } 448 }
448 persistent_array.MakeWeak(data, ExternalArrayWeakCallback); 449 persistent_array.MakeWeak(data, ExternalArrayWeakCallback);
449 persistent_array.MarkIndependent(); 450 persistent_array.MarkIndependent();
450 451
451 array->SetIndexedPropertiesToExternalArrayData( 452 array->SetIndexedPropertiesToExternalArrayData(
452 reinterpret_cast<uint8_t*>(data) + offset, type, 453 reinterpret_cast<uint8_t*>(data) + offset, type,
453 static_cast<int>(length)); 454 static_cast<int>(length));
454 array->Set(String::New("length"), 455 array->Set(String::New("byteLength"),
455 Int32::New(static_cast<int32_t>(length)), ReadOnly); 456 Int32::New(static_cast<int32_t>(byteLength)), ReadOnly);
456 array->Set(String::New("BYTES_PER_ELEMENT"), 457 if (!is_array_buffer_construct) {
457 Int32::New(static_cast<int32_t>(element_size))); 458 array->Set(String::New("length"),
459 Int32::New(static_cast<int32_t>(length)), ReadOnly);
460 array->Set(String::New("byteOffset"),
461 Int32::New(static_cast<int32_t>(offset)), ReadOnly);
462 array->Set(String::New("BYTES_PER_ELEMENT"),
463 Int32::New(static_cast<int32_t>(element_size)));
464 // We currently support 'buffer' property only if constructed from a buffer.
465 if (first_arg_is_array_buffer) {
466 array->Set(String::New("buffer"), args[0], ReadOnly);
467 }
468 }
458 return array; 469 return array;
459 } 470 }
460 471
461 472
462 void Shell::ExternalArrayWeakCallback(Persistent<Value> object, void* data) { 473 void Shell::ExternalArrayWeakCallback(Persistent<Value> object, void* data) {
463 HandleScope scope; 474 HandleScope scope;
464 Handle<String> prop_name = String::New(kArrayBufferReferencePropName); 475 Handle<String> prop_name = String::New(kArrayBufferReferencePropName);
465 Handle<Object> converted_object = object->ToObject(); 476 Handle<Object> converted_object = object->ToObject();
466 Local<Value> prop_value = converted_object->Get(prop_name); 477 Local<Value> prop_value = converted_object->Get(prop_name);
467 if (data != NULL && !prop_value->IsObject()) { 478 if (data != NULL && !prop_value->IsObject()) {
(...skipping 1087 matching lines...) Expand 10 before | Expand all | Expand 10 after
1555 } 1566 }
1556 1567
1557 } // namespace v8 1568 } // namespace v8
1558 1569
1559 1570
1560 #ifndef GOOGLE3 1571 #ifndef GOOGLE3
1561 int main(int argc, char* argv[]) { 1572 int main(int argc, char* argv[]) {
1562 return v8::Shell::Main(argc, argv); 1573 return v8::Shell::Main(argc, argv);
1563 } 1574 }
1564 #endif 1575 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698