Skip to content

Commit

Permalink
Renamed maxCompatMode to maxPerfMode and flipped the logic
Browse files Browse the repository at this point in the history
Split the maxPerfModeKeys into intrinsics and browser, ok for now, to be improved
  • Loading branch information
manuel-jasso committed Aug 2, 2024
1 parent efb77ba commit 8ab59e7
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 84 deletions.
66 changes: 30 additions & 36 deletions packages/near-membrane-base/src/intrinsics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { VirtualEnvironment } from './environment';
* problematic, and requires a lot more work to guarantee that objects from both sides
* can be considered equivalents (without identity discontinuity).
*/
function getESGlobalKeys(maxCompatMode = true) {
function getESGlobalKeys(maxPerfMode: boolean) {
const ESGlobalKeys = [
// *** 19.1 Value Properties of the Global Object
'globalThis',
Expand Down Expand Up @@ -92,33 +92,30 @@ function getESGlobalKeys(maxCompatMode = true) {
];

// This set is for maxPerfMode, all of these must be from the same global object
const maxPerfModeKeys = [
'ArrayBuffer',
'Atomics',
'BigInt64Array',
'BigUint64Array',
'Blob',
'crypto',
'Crypto',
'DataView',
'File',
'FileReader',
'Float32Array',
'Float64Array',
'Int16Array',
'Int32Array',
'Int8Array',
'SharedArrayBuffer',
'SubtleCrypto',
'Uint16Array',
'Uint32Array',
'Uint8Array',
'Uint8ClampedArray',
'URL',
];
const maxPerfModeKeys = {
intrinsics: [
'ArrayBuffer',
'Atomics',
'BigInt64Array',
'BigUint64Array',
'DataView',
'Float32Array',
'Float64Array',
'Int16Array',
'Int32Array',
'Int8Array',
'SharedArrayBuffer',
'Uint16Array',
'Uint32Array',
'Uint8Array',
'Uint8ClampedArray',
],
// Ideally these should come from browser-realm, that's a code reorg improvement for later
browser: ['Blob', 'crypto', 'Crypto', 'File', 'FileReader', 'SubtleCrypto', 'URL'],
};

if (maxCompatMode === false) {
ESGlobalKeys.push(...maxPerfModeKeys);
if (maxPerfMode) {
ESGlobalKeys.push(...maxPerfModeKeys.intrinsics, ...maxPerfModeKeys.browser);
}
return ESGlobalKeys;
}
Expand All @@ -142,8 +139,8 @@ const ReflectiveIntrinsicObjectNames = [
'globalThis',
];

function getESGlobalsAndReflectiveIntrinsicObjectNames(maxCompatMode = true) {
const ESGlobalKeys = getESGlobalKeys(maxCompatMode);
function getESGlobalsAndReflectiveIntrinsicObjectNames(maxPerfMode: boolean) {
const ESGlobalKeys = getESGlobalKeys(maxPerfMode);
return toSafeArray([...ESGlobalKeys, ...ReflectiveIntrinsicObjectNames]);
}

Expand All @@ -160,10 +157,10 @@ function getGlobalObjectOwnKeys(source: object): PropertyKey[] {

export function assignFilteredGlobalDescriptorsFromPropertyDescriptorMap<
T extends PropertyDescriptorMap
>(descs: T, source: PropertyDescriptorMap, includeTypedArrays?: boolean): T {
>(descs: T, source: PropertyDescriptorMap, maxPerfMode: boolean): T {
const ownKeys = getGlobalObjectOwnKeys(source);
const ESGlobalsAndReflectiveIntrinsicObjectNames =
getESGlobalsAndReflectiveIntrinsicObjectNames(includeTypedArrays);
getESGlobalsAndReflectiveIntrinsicObjectNames(maxPerfMode);
for (let i = 0, { length } = ownKeys; i < length; i += 1) {
const ownKey = ownKeys[i];
// Avoid overriding ECMAScript global names that correspond to
Expand All @@ -183,15 +180,12 @@ export function assignFilteredGlobalDescriptorsFromPropertyDescriptorMap<
return descs;
}

export function getFilteredGlobalOwnKeys(
source: object,
includeTypedArrays?: boolean
): PropertyKey[] {
export function getFilteredGlobalOwnKeys(source: object, maxPerfMode: boolean): PropertyKey[] {
const result: PropertyKey[] = [];
let resultOffset = 0;
const ownKeys = getGlobalObjectOwnKeys(source);
const ESGlobalsAndReflectiveIntrinsicObjectNames =
getESGlobalsAndReflectiveIntrinsicObjectNames(includeTypedArrays);
getESGlobalsAndReflectiveIntrinsicObjectNames(maxPerfMode);
for (let i = 0, { length } = ownKeys; i < length; i += 1) {
const ownKey = ownKeys[i];
// Avoid overriding ECMAScript global names that correspond to global
Expand Down
10 changes: 4 additions & 6 deletions packages/near-membrane-dom/src/browser-realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function createIframeVirtualEnvironment(
instrumentation,
keepAlive = true,
liveTargetCallback,
maxCompatMode = true,
maxPerfMode = false,
signSourceCallback,
// eslint-disable-next-line prefer-object-spread
} = ObjectAssign({ __proto__: null }, providedOptions) as BrowserEnvironmentOptions;
Expand All @@ -90,9 +90,7 @@ function createIframeVirtualEnvironment(
)!;
const shouldUseDefaultGlobalOwnKeys =
typeof globalObjectShape !== 'object' || globalObjectShape === null;
const defaultGlobalOwnKeys = filterWindowKeys(
getFilteredGlobalOwnKeys(redWindow, maxCompatMode)
);
const defaultGlobalOwnKeys = filterWindowKeys(getFilteredGlobalOwnKeys(redWindow, maxPerfMode));
let blueConnector = blueCreateHooksCallbackCache.get(blueRefs.document) as
| Connector
| undefined;
Expand Down Expand Up @@ -141,7 +139,7 @@ function createIframeVirtualEnvironment(
blueRefs.window,
shouldUseDefaultGlobalOwnKeys
? (defaultGlobalOwnKeys as PropertyKey[])
: filterWindowKeys(getFilteredGlobalOwnKeys(globalObjectShape)),
: filterWindowKeys(getFilteredGlobalOwnKeys(globalObjectShape, maxPerfMode)),
// Chromium based browsers have a bug that nulls the result of `window`
// getters in detached iframes when the property descriptor of `window.window`
// is retrieved.
Expand All @@ -153,7 +151,7 @@ function createIframeVirtualEnvironment(
assignFilteredGlobalDescriptorsFromPropertyDescriptorMap(
filteredEndowments,
endowments,
maxCompatMode
maxPerfMode
);
removeWindowDescriptors(filteredEndowments);
env.remapProperties(blueRefs.window, filteredEndowments);
Expand Down
2 changes: 1 addition & 1 deletion packages/near-membrane-dom/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export interface BrowserEnvironmentOptions {
instrumentation?: Instrumentation;
keepAlive?: boolean;
liveTargetCallback?: LiveTargetCallback;
maxCompatMode?: boolean;
maxPerfMode?: boolean;
signSourceCallback?: SignSourceCallback;
}
8 changes: 4 additions & 4 deletions packages/near-membrane-node/src/node-realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function createVirtualEnvironment(
globalObjectShape,
instrumentation,
liveTargetCallback,
maxCompatMode,
maxPerfMode = false,
signSourceCallback,
} = ObjectAssign({ __proto__: null }, providedOptions) as NodeEnvironmentOptions;
let blueConnector = blueCreateHooksCallbackCache.get(globalObject) as Connector | undefined;
Expand All @@ -60,22 +60,22 @@ export default function createVirtualEnvironment(
const shouldUseDefaultGlobalOwnKeys =
typeof globalObjectShape !== 'object' || globalObjectShape === null;
if (shouldUseDefaultGlobalOwnKeys && defaultGlobalOwnKeys === null) {
defaultGlobalOwnKeys = getFilteredGlobalOwnKeys(redGlobalObject, maxCompatMode);
defaultGlobalOwnKeys = getFilteredGlobalOwnKeys(redGlobalObject, maxPerfMode);
}

env.lazyRemapProperties(
globalObject,
shouldUseDefaultGlobalOwnKeys
? (defaultGlobalOwnKeys as PropertyKey[])
: getFilteredGlobalOwnKeys(globalObjectShape)
: getFilteredGlobalOwnKeys(globalObjectShape, maxPerfMode)
);

if (endowments) {
const filteredEndowments = {};
assignFilteredGlobalDescriptorsFromPropertyDescriptorMap(
filteredEndowments,
endowments,
maxCompatMode
maxPerfMode
);
env.remapProperties(globalObject, filteredEndowments);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/near-membrane-node/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export interface NodeEnvironmentOptions {
globalObjectShape?: object;
instrumentation?: Instrumentation;
liveTargetCallback?: LiveTargetCallback;
maxCompatMode?: boolean;
maxPerfMode?: boolean;
signSourceCallback?: SignSourceCallback;
}
Loading

0 comments on commit 8ab59e7

Please sign in to comment.