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

Side by Side Diff: src/d8.cc

Issue 9429015: Added no argument constructor support to external arrays. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 8 years, 10 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 | test/mjsunit/external-array.js » ('j') | 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 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 ExternalArrayType type, 323 ExternalArrayType type,
324 size_t element_size) { 324 size_t element_size) {
325 TryCatch try_catch; 325 TryCatch try_catch;
326 bool is_array_buffer_construct = element_size == 0; 326 bool is_array_buffer_construct = element_size == 0;
327 if (is_array_buffer_construct) { 327 if (is_array_buffer_construct) {
328 type = v8::kExternalByteArray; 328 type = v8::kExternalByteArray;
329 element_size = 1; 329 element_size = 1;
330 } 330 }
331 ASSERT(element_size == 1 || element_size == 2 || element_size == 4 || 331 ASSERT(element_size == 1 || element_size == 2 || element_size == 4 ||
332 element_size == 8); 332 element_size == 8);
333 if (args.Length() == 0) { 333
334 return ThrowException(
335 String::New("Array constructor must have at least one "
336 "parameter."));
337 }
338 bool first_arg_is_array_buffer = 334 bool first_arg_is_array_buffer =
335 args.Length() > 0 &&
339 args[0]->IsObject() && 336 args[0]->IsObject() &&
340 args[0]->ToObject()->Get( 337 args[0]->ToObject()->Get(
341 String::New(kArrayBufferMarkerPropName))->IsTrue(); 338 String::New(kArrayBufferMarkerPropName))->IsTrue();
342 // Currently, only the following constructors are supported: 339 // Currently, only the following constructors are supported:
340 // TypedArray()
343 // TypedArray(unsigned long length) 341 // TypedArray(unsigned long length)
344 // TypedArray(ArrayBuffer buffer, 342 // TypedArray(ArrayBuffer buffer,
345 // optional unsigned long byteOffset, 343 // optional unsigned long byteOffset,
346 // optional unsigned long length) 344 // optional unsigned long length)
347 if (args.Length() > 3) { 345 if (args.Length() > 3) {
348 return ThrowException( 346 return ThrowException(
349 String::New("Array constructor from ArrayBuffer must " 347 String::New("Array constructor from ArrayBuffer must "
350 "have 1-3 parameters.")); 348 "have 0-3 parameters."));
351 } 349 }
352 350
353 Local<Value> length_value = (args.Length() < 3) 351 size_t length;
354 ? (first_arg_is_array_buffer 352 if (args.Length() > 0) {
355 ? args[0]->ToObject()->Get(String::New("length")) 353 Local<Value> length_value = (args.Length() < 3)
356 : args[0]) 354 ? (first_arg_is_array_buffer
357 : args[2]; 355 ? args[0]->ToObject()->Get(String::New("length"))
358 size_t length = convertToUint(length_value, &try_catch); 356 : args[0])
359 if (try_catch.HasCaught()) return try_catch.Exception(); 357 : args[2];
358 length = convertToUint(length_value, &try_catch);
359 if (try_catch.HasCaught()) return try_catch.Exception();
360 } else {
361 length = 0;
362 }
360 363
361 void* data = NULL; 364 void* data = NULL;
362 size_t offset = 0; 365 size_t offset = 0;
363 366
364 Handle<Object> array = Object::New(); 367 Handle<Object> array = Object::New();
365 if (first_arg_is_array_buffer) { 368 if (first_arg_is_array_buffer) {
366 Handle<Object> derived_from = args[0]->ToObject(); 369 Handle<Object> derived_from = args[0]->ToObject();
367 data = derived_from->GetIndexedPropertiesExternalArrayData(); 370 data = derived_from->GetIndexedPropertiesExternalArrayData();
368 371
369 size_t array_buffer_length = convertToUint( 372 size_t array_buffer_length = convertToUint(
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 array->Set(String::New(kArrayBufferReferencePropName), args[0], ReadOnly); 424 array->Set(String::New(kArrayBufferReferencePropName), args[0], ReadOnly);
422 } 425 }
423 426
424 if (is_array_buffer_construct) { 427 if (is_array_buffer_construct) {
425 array->Set(String::New(kArrayBufferMarkerPropName), True(), ReadOnly); 428 array->Set(String::New(kArrayBufferMarkerPropName), True(), ReadOnly);
426 } 429 }
427 430
428 Persistent<Object> persistent_array = Persistent<Object>::New(array); 431 Persistent<Object> persistent_array = Persistent<Object>::New(array);
429 persistent_array.MakeWeak(data, ExternalArrayWeakCallback); 432 persistent_array.MakeWeak(data, ExternalArrayWeakCallback);
430 persistent_array.MarkIndependent(); 433 persistent_array.MarkIndependent();
431 if (data == NULL && length != 0) { 434 if (data == NULL) {
432 data = calloc(length, element_size); 435 data = calloc(length, element_size);
433 if (data == NULL) { 436 if (data == NULL) {
434 return ThrowException(String::New("Memory allocation failed.")); 437 return ThrowException(String::New("Memory allocation failed."));
435 } 438 }
436 } 439 }
437 440
438 array->SetIndexedPropertiesToExternalArrayData( 441 array->SetIndexedPropertiesToExternalArrayData(
439 reinterpret_cast<uint8_t*>(data) + offset, type, 442 reinterpret_cast<uint8_t*>(data) + offset, type,
440 static_cast<int>(length)); 443 static_cast<int>(length));
441 array->Set(String::New("length"), 444 array->Set(String::New("length"),
(...skipping 1089 matching lines...) Expand 10 before | Expand all | Expand 10 after
1531 } 1534 }
1532 1535
1533 } // namespace v8 1536 } // namespace v8
1534 1537
1535 1538
1536 #ifndef GOOGLE3 1539 #ifndef GOOGLE3
1537 int main(int argc, char* argv[]) { 1540 int main(int argc, char* argv[]) {
1538 return v8::Shell::Main(argc, argv); 1541 return v8::Shell::Main(argc, argv);
1539 } 1542 }
1540 #endif 1543 #endif
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/external-array.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698