Commit f6017d36 authored by rossberg@chromium.org's avatar rossberg@chromium.org

Clean up d8 ArrayBuffer implementation and fix bug in readbuffer:

- Separate CreateExternalArrayBuffer function.
- Properly create buffers for arrays constructed with size argument only.
- Finalization of data array is tied to buffer object exclusively.
- Get rid of hidden buffer reference in array objects and size header in data.
- Use 'new' instead of 'malloc' in readbuffer.
- Test cases for additional array and buffer properties.

R=mstarzinger@chromium.org
BUG=
TEST=

Review URL: https://chromiumcodereview.appspot.com/10459047

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@11698 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 5c51a368
This diff is collapsed.
...@@ -383,9 +383,11 @@ class Shell : public i::AllStatic { ...@@ -383,9 +383,11 @@ class Shell : public i::AllStatic {
static void RunShell(); static void RunShell();
static bool SetOptions(int argc, char* argv[]); static bool SetOptions(int argc, char* argv[]);
static Handle<ObjectTemplate> CreateGlobalTemplate(); static Handle<ObjectTemplate> CreateGlobalTemplate();
static Handle<Value> CreateExternalArrayBuffer(int32_t size);
static Handle<Value> CreateExternalArrayBuffer(const Arguments& args);
static Handle<Value> CreateExternalArray(const Arguments& args, static Handle<Value> CreateExternalArray(const Arguments& args,
ExternalArrayType type, ExternalArrayType type,
size_t element_size); int32_t element_size);
static void ExternalArrayWeakCallback(Persistent<Value> object, void* data); static void ExternalArrayWeakCallback(Persistent<Value> object, void* data);
}; };
......
...@@ -52,13 +52,53 @@ assertThrows(abfunc1); ...@@ -52,13 +52,53 @@ assertThrows(abfunc1);
// Test derivation from an ArrayBuffer // Test derivation from an ArrayBuffer
var ab = new ArrayBuffer(12); var ab = new ArrayBuffer(12);
var derived_uint8 = new Uint8Array(ab); var derived_uint8 = new Uint8Array(ab);
assertSame(ab, derived_uint8.buffer);
assertEquals(12, derived_uint8.length); assertEquals(12, derived_uint8.length);
assertEquals(12, derived_uint8.byteLength);
assertEquals(0, derived_uint8.byteOffset);
assertEquals(1, derived_uint8.BYTES_PER_ELEMENT);
var derived_uint8_2 = new Uint8Array(ab,7);
assertSame(ab, derived_uint8_2.buffer);
assertEquals(5, derived_uint8_2.length);
assertEquals(5, derived_uint8_2.byteLength);
assertEquals(7, derived_uint8_2.byteOffset);
assertEquals(1, derived_uint8_2.BYTES_PER_ELEMENT);
var derived_int16 = new Int16Array(ab);
assertSame(ab, derived_int16.buffer);
assertEquals(6, derived_int16.length);
assertEquals(12, derived_int16.byteLength);
assertEquals(0, derived_int16.byteOffset);
assertEquals(2, derived_int16.BYTES_PER_ELEMENT);
var derived_int16_2 = new Int16Array(ab,6);
assertSame(ab, derived_int16_2.buffer);
assertEquals(3, derived_int16_2.length);
assertEquals(6, derived_int16_2.byteLength);
assertEquals(6, derived_int16_2.byteOffset);
assertEquals(2, derived_int16_2.BYTES_PER_ELEMENT);
var derived_uint32 = new Uint32Array(ab); var derived_uint32 = new Uint32Array(ab);
assertSame(ab, derived_uint32.buffer);
assertEquals(3, derived_uint32.length); assertEquals(3, derived_uint32.length);
assertEquals(12, derived_uint32.byteLength);
assertEquals(0, derived_uint32.byteOffset);
assertEquals(4, derived_uint32.BYTES_PER_ELEMENT);
var derived_uint32_2 = new Uint32Array(ab,4); var derived_uint32_2 = new Uint32Array(ab,4);
assertSame(ab, derived_uint32_2.buffer);
assertEquals(2, derived_uint32_2.length); assertEquals(2, derived_uint32_2.length);
assertEquals(8, derived_uint32_2.byteLength);
assertEquals(4, derived_uint32_2.byteOffset);
assertEquals(4, derived_uint32_2.BYTES_PER_ELEMENT);
var derived_uint32_3 = new Uint32Array(ab,4,1); var derived_uint32_3 = new Uint32Array(ab,4,1);
assertSame(ab, derived_uint32_3.buffer);
assertEquals(1, derived_uint32_3.length); assertEquals(1, derived_uint32_3.length);
assertEquals(4, derived_uint32_3.byteLength);
assertEquals(4, derived_uint32_3.byteOffset);
assertEquals(4, derived_uint32_3.BYTES_PER_ELEMENT);
var derived_float64 = new Float64Array(ab,0,1);
assertSame(ab, derived_float64.buffer);
assertEquals(1, derived_float64.length);
assertEquals(8, derived_float64.byteLength);
assertEquals(0, derived_float64.byteOffset);
assertEquals(8, derived_float64.BYTES_PER_ELEMENT);
// If a given byteOffset and length references an area beyond the end of the // If a given byteOffset and length references an area beyond the end of the
// ArrayBuffer an exception is raised. // ArrayBuffer an exception is raised.
...@@ -87,6 +127,24 @@ function abfunc6() { ...@@ -87,6 +127,24 @@ function abfunc6() {
} }
assertThrows(abfunc6); assertThrows(abfunc6);
// Test that an array constructed without an array buffer creates one properly.
a = new Uint8Array(31);
assertEquals(a.byteLength, a.buffer.byteLength);
assertEquals(a.length, a.buffer.byteLength);
assertEquals(a.length * a.BYTES_PER_ELEMENT, a.buffer.byteLength);
a = new Int16Array(5);
assertEquals(a.byteLength, a.buffer.byteLength);
assertEquals(a.length * a.BYTES_PER_ELEMENT, a.buffer.byteLength);
a = new Float64Array(7);
assertEquals(a.byteLength, a.buffer.byteLength);
assertEquals(a.length * a.BYTES_PER_ELEMENT, a.buffer.byteLength);
// Test that an implicitly created buffer is a valid buffer.
a = new Float64Array(7);
assertSame(a.buffer, (new Uint16Array(a.buffer)).buffer);
assertSame(a.buffer, (new Float32Array(a.buffer,4)).buffer);
assertSame(a.buffer, (new Int8Array(a.buffer,3,51)).buffer);
// Test the correct behavior of the |BYTES_PER_ELEMENT| property (which is // Test the correct behavior of the |BYTES_PER_ELEMENT| property (which is
// "constant", but not read-only). // "constant", but not read-only).
a = new Int32Array(2); a = new Int32Array(2);
...@@ -351,3 +409,25 @@ assertTrue(isNaN(float64_array[0])); ...@@ -351,3 +409,25 @@ assertTrue(isNaN(float64_array[0]));
%OptimizeFunctionOnNextCall(store_float64_undefined); %OptimizeFunctionOnNextCall(store_float64_undefined);
store_float64_undefined(float64_array); store_float64_undefined(float64_array);
assertTrue(isNaN(float64_array[0])); assertTrue(isNaN(float64_array[0]));
// Check handling of 0-sized buffers and arrays.
ab = new ArrayBuffer(0);
assertEquals(0, ab.byteLength);
a = new Int8Array(ab);
assertEquals(0, a.byteLength);
assertEquals(0, a.length);
a[0] = 1;
assertEquals(undefined, a[0])
ab = new ArrayBuffer(16);
a = new Float32Array(ab,4,0);
assertEquals(0, a.byteLength);
assertEquals(0, a.length);
a[0] = 1;
assertEquals(undefined, a[0])
a = new Uint16Array(0);
assertEquals(0, a.byteLength);
assertEquals(0, a.length);
a[0] = 1;
assertEquals(undefined, a[0])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment