From fe103357aeed7f7acdc687a759f9d443c6c25373 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 9 Feb 2013 09:09:13 +0100 Subject: [PATCH] typed arrays: make DataView throw on non-ArrayBuffer Make the DataView constructor throw an exception when the first argument is not an ArrayBuffer. Follows the spec and the browsers. --- src/v8_typed_array.cc | 2 +- test/simple/test-typed-arrays.js | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/v8_typed_array.cc b/src/v8_typed_array.cc index a74d295f37f..9105086b4d3 100644 --- a/src/v8_typed_array.cc +++ b/src/v8_typed_array.cc @@ -624,7 +624,7 @@ class DataView { return ThrowError("Object must be an ArrayBuffer."); v8::Handle buffer = v8::Handle::Cast(args[0]); - if (!buffer->HasIndexedPropertiesInExternalArrayData()) + if (!ArrayBuffer::HasInstance(buffer)) return ThrowError("Object must be an ArrayBuffer."); unsigned int byte_length = diff --git a/test/simple/test-typed-arrays.js b/test/simple/test-typed-arrays.js index ba454f8a60e..45547c33669 100644 --- a/test/simple/test-typed-arrays.js +++ b/test/simple/test-typed-arrays.js @@ -47,7 +47,7 @@ var assert = require('assert'); assert.equal(obj.toString(), expected); assert.equal(Object.prototype.toString.call(obj), expected); - obj = new DataView(obj); + obj = new DataView(obj.buffer || obj); assert.equal(obj.toString(), '[object DataView]'); assert.equal(Object.prototype.toString.call(obj), '[object DataView]'); }); @@ -197,7 +197,7 @@ assert.throws(function() { // see https://github.com/joyent/node/issues/4626 (function() { var buf = new Uint8Array(2); - var view = new DataView(buf); + var view = new DataView(buf.buffer); view.setUint16(0, 1); assert.equal(view.getUint16(0), 1); })(); @@ -239,3 +239,7 @@ assert.throws(function() { assert.equal(b[0], 1); assert.equal(a.buffer, b.buffer); })(); + +assert.throws(function() { + new DataView(new Int8Array(1)); +});