OLD | NEW |
---|---|
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
490 | 490 |
491 | 491 |
492 void ExternalArrayWeakCallback(v8::Persistent<v8::Value> object, void* data) { | 492 void ExternalArrayWeakCallback(v8::Persistent<v8::Value> object, void* data) { |
493 free(data); | 493 free(data); |
494 object.Dispose(); | 494 object.Dispose(); |
495 } | 495 } |
496 | 496 |
497 | 497 |
498 v8::Handle<v8::Value> CreateExternalArray(const v8::Arguments& args, | 498 v8::Handle<v8::Value> CreateExternalArray(const v8::Arguments& args, |
499 v8::ExternalArrayType type, | 499 v8::ExternalArrayType type, |
500 int element_size) { | 500 size_t element_size) { |
Lasse Reichstein
2011/06/28 09:28:58
Let's ASSERT that element_size it's a meaningful v
Jakob Kummerow
2011/06/28 14:02:38
Done.
Changed the interface to size_t anyway becau
| |
501 if (args.Length() != 1) { | 501 if (args.Length() != 1) { |
502 return v8::ThrowException( | 502 return v8::ThrowException( |
503 v8::String::New("Array constructor needs one parameter.")); | 503 v8::String::New("Array constructor needs one parameter.")); |
504 } | 504 } |
505 int length = args[0]->Int32Value(); | 505 if (args[0]->Int32Value() < 0) { |
Lasse Reichstein
2011/06/28 09:28:58
You convert args[0] to int32 twice. Just do it onc
Lasse Reichstein
2011/06/28 09:44:12
That is, ofcourse, unless there is a specification
Jakob Kummerow
2011/06/28 14:02:38
Spec says the c'tor argument is an "unsigned long"
Lasse Reichstein
2011/06/29 08:51:05
An unsigned long can take values that are too big
| |
506 void* data = malloc(length * element_size); | 506 return v8::ThrowException( |
507 memset(data, 0, length * element_size); | 507 v8::String::New("Array length must not be negative.")); |
508 } | |
509 size_t length = static_cast<size_t>(args[0]->Int32Value()); | |
510 if (length > static_cast<size_t>(v8::internal::ExternalArray::kMaxLength)) { | |
Lasse Reichstein
2011/06/28 09:28:58
If both values are int32, there's no need to conve
Jakob Kummerow
2011/06/28 14:02:38
True. Removed the cast.
| |
511 return v8::ThrowException( | |
512 v8::String::New("Array length exceeds maximum length.")); | |
513 } | |
514 size_t malloc_size = length * element_size; | |
515 // Check for overflow in the multiplication. | |
516 if (malloc_size / length != element_size) { | |
517 return v8::ThrowException( | |
518 v8::String::New("Array size exceeds memory limit.")); | |
Lasse Reichstein
2011/06/28 09:28:58
Can this happen?
I.e., is kMaxLength * maximal ele
Jakob Kummerow
2011/06/28 14:02:38
It can happen. kMaxLength is 2^30 - 1, and Float64
| |
519 } | |
520 void* data = malloc(malloc_size); | |
Lasse Reichstein
2011/06/28 09:28:58
How about using calloc instead? It seems like just
Jakob Kummerow
2011/06/28 14:02:38
Done, thanks for the hint.
| |
521 if (data == NULL) { | |
522 return v8::ThrowException(v8::String::New("Memory allocation failed.")); | |
523 } | |
524 memset(data, 0, malloc_size); | |
508 v8::Handle<v8::Object> array = v8::Object::New(); | 525 v8::Handle<v8::Object> array = v8::Object::New(); |
509 v8::Persistent<v8::Object> persistent_array = | 526 v8::Persistent<v8::Object> persistent_array = |
510 v8::Persistent<v8::Object>::New(array); | 527 v8::Persistent<v8::Object>::New(array); |
511 persistent_array.MakeWeak(data, ExternalArrayWeakCallback); | 528 persistent_array.MakeWeak(data, ExternalArrayWeakCallback); |
512 persistent_array.MarkIndependent(); | 529 persistent_array.MarkIndependent(); |
513 array->SetIndexedPropertiesToExternalArrayData(data, type, length); | 530 array->SetIndexedPropertiesToExternalArrayData(data, type, length); |
514 array->Set(v8::String::New("length"), v8::Int32::New(length), | 531 array->Set(v8::String::New("length"), v8::Int32::New(length), |
515 v8::ReadOnly); | 532 v8::ReadOnly); |
516 array->Set(v8::String::New("BYTES_PER_ELEMENT"), | 533 array->Set(v8::String::New("BYTES_PER_ELEMENT"), |
517 v8::Int32::New(element_size)); | 534 v8::Int32::New(element_size)); |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
676 printf("^"); | 693 printf("^"); |
677 } | 694 } |
678 printf("\n"); | 695 printf("\n"); |
679 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); | 696 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); |
680 if (stack_trace.length() > 0) { | 697 if (stack_trace.length() > 0) { |
681 const char* stack_trace_string = ToCString(stack_trace); | 698 const char* stack_trace_string = ToCString(stack_trace); |
682 printf("%s\n", stack_trace_string); | 699 printf("%s\n", stack_trace_string); |
683 } | 700 } |
684 } | 701 } |
685 } | 702 } |
OLD | NEW |