Skip to content

Commit

Permalink
Update to support DataView from another realm
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Jul 28, 2021
1 parent 1f5abd9 commit 49e7026
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/is.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export { default as isArrayBuffer } from "lodash-es/isArrayBuffer";
* @returns {boolean}
*/
export function isDataView(view) {
return view instanceof DataView;
return ArrayBuffer.isView(view) && Object.prototype.toString.call(view) === "[object DataView]";
}

/**
Expand Down
30 changes: 30 additions & 0 deletions test/dataView.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ function clear() {
new Uint16Array(buffer)[0] = 0;
}

let realmDataView;

if (typeof require !== "undefined") {
realmDataView = require("vm").runInNewContext(`
const buffer = new ArrayBuffer(2);
new DataView(buffer);
`);
} else {
const iframe = document.createElement("iframe");
iframe.height = iframe.width = 0;
document.body.appendChild(iframe);

const iframeDocument = iframe.contentDocument;
iframeDocument.write(`<script>
const buffer = new ArrayBuffer(2);
window.realmDataView = new DataView(buffer);
</script>`);
realmDataView = iframe.contentWindow.realmDataView;

iframeDocument.close();
}

describe("additional DataView methods", () => {

describe("getFloat16()", () => {
Expand Down Expand Up @@ -43,6 +65,10 @@ describe("additional DataView methods", () => {
assert( getFloat16(dataView, 0, true) === 0.0007572174072265625 );
});

it("work with DataView from anothor realm", () => {
assert.doesNotThrow(() => getFloat16(realmDataView, 0));
});

});

describe("setFloat16()", () => {
Expand Down Expand Up @@ -76,6 +102,10 @@ describe("additional DataView methods", () => {
assert( dataView.getUint16(0, true) === 0x1234 );
});

it("work with DataView from anothor realm", () => {
assert.doesNotThrow(() => setFloat16(realmDataView, 0, 0));
});

});

});
Expand Down

0 comments on commit 49e7026

Please sign in to comment.