Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
typed arrays: make DataView throw on non-ArrayBuffer
Browse files Browse the repository at this point in the history
Make the DataView constructor throw an exception when the first
argument is not an ArrayBuffer. Follows the spec and the browsers.
  • Loading branch information
bnoordhuis committed Feb 10, 2013
1 parent 234551a commit fe10335
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/v8_typed_array.cc
Expand Up @@ -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 =
Expand Down
8 changes: 6 additions & 2 deletions test/simple/test-typed-arrays.js
Expand Up @@ -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]');
});
Expand Down Expand Up @@ -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);
})();
Expand Down Expand Up @@ -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));
});

0 comments on commit fe10335

Please sign in to comment.