Skip to content

Commit

Permalink
fix: remove global object keys cache
Browse files Browse the repository at this point in the history
  • Loading branch information
dejang committed Feb 28, 2024
1 parent 90d8de9 commit c39fbab
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 59 deletions.
12 changes: 4 additions & 8 deletions packages/near-membrane-dom/src/browser-realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ const IFRAME_SANDBOX_ATTRIBUTE_VALUE = 'allow-same-origin allow-scripts';
const revoked = toSafeWeakSet(new WeakSetCtor<GlobalObject | Node>());
const blueCreateHooksCallbackCache = toSafeWeakMap(new WeakMapCtor<Document, Connector>());

let defaultGlobalOwnKeys: PropertyKey[] | null = null;

function createDetachableIframe(doc: Document): HTMLIFrameElement {
const iframe = ReflectApply(DocumentProtoCreateElement, doc, ['iframe']) as HTMLIFrameElement;
// It is impossible to test whether the NodeProtoLastChildGetter branch is
Expand Down Expand Up @@ -92,12 +90,10 @@ function createIframeVirtualEnvironment(
)!;
const shouldUseDefaultGlobalOwnKeys =
typeof globalObjectShape !== 'object' || globalObjectShape === null;
if (shouldUseDefaultGlobalOwnKeys && defaultGlobalOwnKeys === null) {
defaultGlobalOwnKeys = filterWindowKeys(
getFilteredGlobalOwnKeys(redWindow, remapTypedArrays),
remapTypedArrays
);
}
const defaultGlobalOwnKeys = filterWindowKeys(
getFilteredGlobalOwnKeys(redWindow, remapTypedArrays),
remapTypedArrays
);
let blueConnector = blueCreateHooksCallbackCache.get(blueRefs.document) as
| Connector
| undefined;
Expand Down
61 changes: 10 additions & 51 deletions test/membrane/binary-data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ if (typeof Atomics !== 'undefined') {
`);
});

it('operates on atomic-friendly typed arrays, when typed arrays are not remapped', () => {
it('fails to operate on atomic-friendly typed arrays, when typed arrays are not remapped', () => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ expect }),
remapTypedArrays: false,
});

env.evaluate(`
expect(() =>
env.evaluate(`
const ab = new ArrayBuffer(Int32Array.BYTES_PER_ELEMENT);
const i32a = new Int32Array(ab);
i32a[0] = 9;
Expand All @@ -41,7 +42,8 @@ if (typeof Atomics !== 'undefined') {
Atomics.sub(i32a, 0, 10);
Atomics.xor(i32a, 0, 1);
expect(Atomics.load(i32a, 0)).toBe(9);
`);
`)
).toThrow();
});
});
}
Expand All @@ -61,20 +63,17 @@ describe('Blob', () => {
});
`);
});
it('encodes blobs from typed arrays, when typed arrays are not remapped', (done) => {
it('fails to encode blobs from typed arrays, when typed arrays are not remapped', () => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ done, expect }),
remapTypedArrays: false,
});

env.evaluate(`
expect(() =>
env.evaluate(`
const a = new Uint8Array([97, 98, 99]);
const b = new Blob([a], { type: 'application/octet-stream' });
b.text().then((output) => {
expect(output).toBe('abc');
done();
});
`);
`)
).toThrow();
});
});

Expand Down Expand Up @@ -226,46 +225,6 @@ describe('DataView', () => {
});
});

describe('FileReader', () => {
it('reads from blobs created from typed arrays', (done) => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ done, expect }),
});

env.evaluate(`
const source = new Uint8Array([97, 98, 99]);
const blob = new Blob([source]);
const reader = new FileReader();
reader.onload = (event) => {
expect(reader.result.byteLength).toBe(source.length);
expect(reader.result).toBeInstanceOf(ArrayBuffer);
done();
};
reader.readAsArrayBuffer(blob);
`);
});
it('reads from blobs created from typed arrays, when typed arrays are not remapped', (done) => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ done, expect }),
remapTypedArrays: false,
});

env.evaluate(`
const source = new Uint8Array([97, 98, 99]);
const blob = new Blob([source]);
const reader = new FileReader();
reader.onload = (event) => {
expect(reader.result.byteLength).toBe(source.length);
expect(reader.result).toBeInstanceOf(ArrayBuffer);
done();
};
reader.readAsArrayBuffer(blob);
`);
});
});

describe('TypedArray', () => {
it('should support in bound index access', () => {
const env = createVirtualEnvironment(window, {
Expand Down

0 comments on commit c39fbab

Please sign in to comment.