Index: src/runtime.cc |
diff --git a/src/runtime.cc b/src/runtime.cc |
index 7288f38171eb75f6c4e15106817249b4b0b950fe..f7887fb03ec8163b1569b2ddb294f4eaf07424b9 100644 |
--- a/src/runtime.cc |
+++ b/src/runtime.cc |
@@ -638,14 +638,14 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_Fix) { |
} |
-static size_t ArrayBufferAllocatedLength(Isolate* isolate, |
- JSArrayBuffer* buffer) { |
+static size_t NumberToSize(Isolate* isolate, |
rossberg
2013/04/15 13:11:03
This should probably move to v8conversions.
Dmitry Lomov (no reviews)
2013/04/15 15:40:58
Done.
|
+ Object* number) { |
NoHandleAllocation hc(isolate); |
- Object* byte_length = buffer->byte_length(); |
- if (byte_length->IsSmi()) { |
- return Smi::cast(byte_length)->value(); |
+ if (number->IsSmi()) { |
+ return Smi::cast(number)->value(); |
} else { |
- double value = HeapNumber::cast(byte_length)->value(); |
+ ASSERT(number->IsHeapNumber()); |
+ double value = HeapNumber::cast(number)->value(); |
return static_cast<size_t>(value); |
} |
} |
@@ -658,8 +658,8 @@ static void ArrayBufferWeakCallback(v8::Isolate* external_isolate, |
HandleScope scope(isolate); |
Handle<Object> internal_object = Utils::OpenHandle(*object); |
- size_t allocated_length = ArrayBufferAllocatedLength( |
- isolate, JSArrayBuffer::cast(*internal_object)); |
+ size_t allocated_length = NumberToSize( |
+ isolate, JSArrayBuffer::cast(*internal_object)->byte_length()); |
isolate->heap()->AdjustAmountOfExternalAllocatedMemory( |
-static_cast<intptr_t>(allocated_length)); |
if (data != NULL) |
@@ -744,12 +744,12 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferSliceImpl) { |
CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, target, 1); |
CONVERT_DOUBLE_ARG_CHECKED(first, 2); |
size_t start = static_cast<size_t>(first); |
- size_t target_length = ArrayBufferAllocatedLength(isolate, *target); |
+ size_t target_length = NumberToSize(isolate, target->byte_length()); |
if (target_length == 0) |
return isolate->heap()->undefined_value(); |
- ASSERT(ArrayBufferAllocatedLength(isolate, *source) - target_length >= start); |
+ ASSERT(NumberToSize(isolate, source->byte_length()) - target_length >= start); |
uint8_t* source_data = reinterpret_cast<uint8_t*>(source->backing_store()); |
uint8_t* target_data = reinterpret_cast<uint8_t*>(target->backing_store()); |
CopyBytes(target_data, source_data + start, target_length); |
@@ -757,6 +757,101 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferSliceImpl) { |
} |
+RUNTIME_FUNCTION(MaybeObject*, Runtime_TypedArrayInitialize) { |
+ HandleScope scope(isolate); |
+ ASSERT(args.length() == 6); |
+ CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, holder, 0); |
+ CONVERT_SMI_ARG_CHECKED(arrayId, 1); |
+ CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, buffer, 2); |
+ CONVERT_ARG_HANDLE_CHECKED(Object, byte_offset_object, 3); |
+ CONVERT_ARG_HANDLE_CHECKED(Object, length_object, 4); |
+ CONVERT_ARG_HANDLE_CHECKED(Object, byte_length_object, 5); |
+ |
+ ExternalArrayType arrayType; |
+ ElementsKind elementsKind; |
+ size_t elementSize; |
+ // arrayIds below should be synchromized with typedarray.js natives. |
+ switch (arrayId) { |
+ case 1: |
rossberg
2013/04/15 13:11:03
Can we define an enum for that?
Dmitry Lomov (no reviews)
2013/04/15 15:40:58
Done.
|
+ elementsKind = EXTERNAL_UNSIGNED_BYTE_ELEMENTS; |
+ arrayType = kExternalUnsignedByteArray; |
+ elementSize = 1; |
+ break; |
+ case 2: |
+ elementsKind = EXTERNAL_BYTE_ELEMENTS; |
+ arrayType = kExternalByteArray; |
+ elementSize = 1; |
+ break; |
+ case 3: |
+ elementsKind = EXTERNAL_UNSIGNED_SHORT_ELEMENTS; |
+ arrayType = kExternalUnsignedShortArray; |
+ elementSize = 2; |
+ break; |
+ case 4: |
+ elementsKind = EXTERNAL_SHORT_ELEMENTS; |
+ arrayType = kExternalShortArray; |
+ elementSize = 2; |
+ break; |
+ case 5: |
+ elementsKind = EXTERNAL_UNSIGNED_INT_ELEMENTS; |
+ arrayType = kExternalUnsignedIntArray; |
+ elementSize = 4; |
+ break; |
+ case 6: |
+ elementsKind = EXTERNAL_INT_ELEMENTS; |
+ arrayType = kExternalIntArray; |
+ elementSize = 4; |
+ break; |
+ case 7: |
+ elementsKind = EXTERNAL_FLOAT_ELEMENTS; |
+ arrayType = kExternalFloatArray; |
+ elementSize = 4; |
+ break; |
+ case 8: |
+ elementsKind = EXTERNAL_DOUBLE_ELEMENTS; |
+ arrayType = kExternalDoubleArray; |
+ elementSize = 8; |
+ break; |
+ default: |
+ ASSERT(false); |
rossberg
2013/04/15 13:11:03
You can use UNREACHABLE() here. Also, the NULL ret
Dmitry Lomov (no reviews)
2013/04/15 15:40:58
Done.
|
+ return NULL; |
+ } |
+ holder->set_buffer(*buffer); |
+ holder->set_byte_offset(*byte_offset_object); |
+ holder->set_byte_length(*byte_length_object); |
+ holder->set_length(*length_object); |
+ |
+ size_t byte_offset = NumberToSize(isolate, *byte_offset_object); |
+ size_t byte_length = NumberToSize(isolate, *byte_length_object); |
+ size_t length = NumberToSize(isolate, *length_object); |
+ ASSERT(length * elementSize == byte_length); |
rossberg
2013/04/15 13:11:03
Given this invariant, why not drop the byte_length
Dmitry Lomov (no reviews)
2013/04/15 15:40:58
Done. The invariant helped me find some bugs in pr
|
+ Handle<ExternalArray> elements = |
+ isolate->factory()->NewExternalArray( |
+ length, arrayType, |
+ static_cast<uint8_t*>(buffer->backing_store()) + byte_offset); |
+ Handle<Map> map = |
+ isolate->factory()->GetElementsTransitionMap( |
+ holder, elementsKind); |
rossberg
2013/04/15 13:11:03
Nit: should fit on the previous line
Dmitry Lomov (no reviews)
2013/04/15 15:40:58
Done.
|
+ holder->set_map(*map); |
+ holder->set_elements(*elements); |
+ return isolate->heap()->undefined_value(); |
+} |
+ |
rossberg
2013/04/15 13:11:03
Style: add extra newline
Dmitry Lomov (no reviews)
2013/04/15 15:40:58
Done.
|
+#define TYPED_ARRAY_GETTER(getter, accessor) \ |
+ RUNTIME_FUNCTION(MaybeObject*, Runtime_TypedArrayGet##getter) { \ |
+ HandleScope scope(isolate); \ |
+ ASSERT(args.length() == 1); \ |
+ CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, holder, 0); \ |
+ return holder->accessor(); \ |
+ } |
+ |
+TYPED_ARRAY_GETTER(Buffer, buffer) |
+TYPED_ARRAY_GETTER(ByteLength, byte_length) |
+TYPED_ARRAY_GETTER(ByteOffset, byte_offset) |
+TYPED_ARRAY_GETTER(Length, length) |
+ |
+#undef TYPED_ARRAY_GETTER |
+ |
rossberg
2013/04/15 13:11:03
Style: add another newline
Dmitry Lomov (no reviews)
2013/04/15 15:40:58
Done.
|
RUNTIME_FUNCTION(MaybeObject*, Runtime_SetInitialize) { |
HandleScope scope(isolate); |
ASSERT(args.length() == 1); |