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.
pull/24504/head
Ben Noordhuis 2013-02-09 09:09:13 +01:00
parent 234551a22a
commit fe103357ae
2 changed files with 7 additions and 3 deletions

View File

@ -624,7 +624,7 @@ class DataView {
return ThrowError("Object must be an ArrayBuffer.");
v8::Handle<v8::Object> buffer = v8::Handle<v8::Object>::Cast(args[0]);
if (!buffer->HasIndexedPropertiesInExternalArrayData())
if (!ArrayBuffer::HasInstance(buffer))
return ThrowError("Object must be an ArrayBuffer.");
unsigned int byte_length =

View File

@ -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));
});