Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update near-membrane-dom to use ShadowRealm #243

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions karma.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,28 @@
const globby = require('globby');
const istanbul = require('rollup-plugin-istanbul');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const replaceRollupPlugin = require('@rollup/plugin-replace');
const path = require('node:path');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');

process.env.CHROME_BIN = require('puppeteer').executablePath();

let testFilesPattern = './test/**/*.spec.js';

const basePath = path.resolve(__dirname, './');
const matchArg = process.argv.indexOf('--match');
const argv = yargs(hideBin(process.argv))
.options({
coverage: { type: 'boolean' },
match: { type: 'string' },
'use-shadow-realm': { type: 'boolean' },
})
.hide('help')
.hide('version').argv;
const { coverage, match, useShadowRealm } = argv;

if (matchArg > -1) {
testFilesPattern = process.argv[matchArg + 1] || '';
if (match) {
testFilesPattern = match;
}

if (globby.sync(testFilesPattern).length) {
Expand All @@ -27,8 +38,6 @@ if (globby.sync(testFilesPattern).length) {
process.exit(0);
}

const coverage = process.argv.includes('--coverage');

const customLaunchers = {
ChromeHeadlessNoSandbox: {
base: 'ChromeHeadless',
Expand Down Expand Up @@ -74,6 +83,13 @@ module.exports = function (config) {
nodeResolve({
preferBuiltins: true,
}),
replaceRollupPlugin({
include: ['./test/__bootstrap__/create-virtual-environment.js'],
preventAssignment: true,
values: {
'process.env.USE_SHADOW_REALM': JSON.stringify(useShadowRealm),
},
}),
],
},
};
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@commitlint/config-conventional": "17.1.0",
"@rollup/plugin-babel": "5.3.1",
"@rollup/plugin-node-resolve": "13.0.5",
"@rollup/plugin-replace": "4.0.0",
"@rollup/plugin-replace": "^5.0.2",
"@rollup/plugin-typescript": "8.4.0",
"@types/eslint": "8.4.6",
"@types/jest": "29.0.0",
Expand Down Expand Up @@ -111,5 +111,7 @@
"node": "16.13.1",
"yarn": "1.22.11"
},
"dependencies": {}
"dependencies": {
"yargs": "^17.6.2"
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import {
assignFilteredGlobalDescriptorsFromPropertyDescriptorMap,
createBlueConnector,
Expand Down
106 changes: 77 additions & 29 deletions packages/near-membrane-base/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,37 @@ import {
import type { ProxyTarget } from '@locker/near-membrane-shared/types';
import type {
CallableDefineProperties,
CallableDescriptorCallback,
CallableEvaluate,
CallableGetPropertyValuePointer,
CallableInstallDateProtoToJSON,
CallableInstallJSONStringify,
CallableInstallLazyPropertyDescriptors,
CallableIsTargetLive,
CallableIsTargetRevoked,
CallableLinkPointers,
CallableSerializeTarget,
CallableSetPrototypeOf,
CallableTrackAsFastTarget,
GetSelectedTarget,
GetTransferableValue,
HooksCallback,
Pointer,
VirtualEnvironmentOptions,
CallableTrackAsFastTarget,
CallableDescriptorCallback,
} from './types';

const LOCKER_NEAR_MEMBRANE_UNDEFINED_VALUE_SYMBOL = Symbol.for(
'@@lockerNearMembraneUndefinedValue'
);

const { prototype: DateProto } = Date;
const { toJSON: DateProtoToJSON } = DateProto;
const WindowJSON = JSON;

const installableDateToJSON = function toJSON(this: Date) {
return ReflectApply(DateProtoToJSON, this, []);
};

export class VirtualEnvironment {
private readonly blueCallableGetPropertyValuePointer: CallableGetPropertyValuePointer;

Expand All @@ -39,18 +52,29 @@ export class VirtualEnvironment {

private readonly blueGlobalThisPointer: Pointer;

private readonly redCallableDefineProperties: CallableDefineProperties;

private readonly redCallableEvaluate: CallableEvaluate;

private readonly redCallableGetPropertyValuePointer: CallableGetPropertyValuePointer;

private readonly redCallableInstallDateProtoToJSON: CallableInstallDateProtoToJSON;

private readonly redCallableInstallJSONStringify: CallableInstallJSONStringify;

private readonly redCallableInstallLazyPropertyDescriptors: CallableInstallLazyPropertyDescriptors;

private readonly redCallableLinkPointers: CallableLinkPointers;

private readonly redCallableSetPrototypeOf: CallableSetPrototypeOf;

<<<<<<< HEAD
=======
private readonly redCallableDefineProperties: CallableDefineProperties;

private readonly redCallableInstallLazyPropertyDescriptors: CallableInstallLazyPropertyDescriptors;

>>>>>>> 3cede24 (test: add minimum test for ShadowRealm virtual environment, with instructions)
private readonly redCallableTrackAsFastTarget: CallableTrackAsFastTarget;

private readonly redGlobalThisPointer: Pointer;
Expand Down Expand Up @@ -105,22 +129,24 @@ export class VirtualEnvironment {
18: blueCallablePreventExtensions,
19: blueCallableSet,
20: blueCallableSetPrototypeOf,
21: blueCallableDebugInfo,
// 21: blueCallableDebugInfo,
// 22: blueCallableDefineProperties,
23: blueCallableGetLazyPropertyDescriptorStateByTarget,
24: blueCallableGetPropertyValue,
25: blueCallableGetTargetIntegrityTraits,
26: blueCallableGetToStringTagOfTarget,
27: blueCallableInstallErrorPrepareStackTrace,
// 28: blueCallableInstallLazyPropertyDescriptors,
29: blueCallableIsTargetLive,
30: blueCallableIsTargetRevoked,
31: blueCallableSerializeTarget,
32: blueCallableSetLazyPropertyDescriptorStateByTarget,
// 33: blueTrackAsFastTarget,
34: blueCallableBatchGetPrototypeOfAndGetOwnPropertyDescriptors,
35: blueCallableBatchGetPrototypeOfWhenHasNoOwnProperty,
36: blueCallableBatchGetPrototypeOfWhenHasNoOwnPropertyDescriptor,
// 27: blueCallableInstallDateProtoToJSON,
28: blueCallableInstallErrorPrepareStackTrace,
// 29: blueCallableInstallJSONStringify,
// 30: blueCallableInstallLazyPropertyDescriptors,
31: blueCallableIsTargetLive,
// 32: blueCallableIsTargetRevoked,
// 33: blueCallableSerializeTarget,
34: blueCallableSetLazyPropertyDescriptorStateByTarget,
// 35: blueTrackAsFastTarget,
36: blueCallableBatchGetPrototypeOfAndGetOwnPropertyDescriptors,
37: blueCallableBatchGetPrototypeOfWhenHasNoOwnProperty,
38: blueCallableBatchGetPrototypeOfWhenHasNoOwnPropertyDescriptor,
} = blueHooks!;
let redHooks: Parameters<HooksCallback>;
const redConnect = redConnector('red', (...hooks: Parameters<HooksCallback>) => {
Expand Down Expand Up @@ -151,19 +177,21 @@ export class VirtualEnvironment {
21: redCallableDebugInfo,
22: redCallableDefineProperties,
23: redCallableGetLazyPropertyDescriptorStateByTarget,
24: redCallableGetPropertyValue,
// 24: redCallableGetPropertyValue,
25: redCallableGetTargetIntegrityTraits,
26: redCallableGetToStringTagOfTarget,
27: redCallableInstallErrorPrepareStackTrace,
28: redCallableInstallLazyPropertyDescriptors,
29: redCallableIsTargetLive,
30: redCallableIsTargetRevoked,
31: redCallableSerializeTarget,
32: redCallableSetLazyPropertyDescriptorStateByTarget,
33: redCallableTrackAsFastTarget,
34: redCallableBatchGetPrototypeOfAndGetOwnPropertyDescriptors,
35: redCallableBatchGetPrototypeOfWhenHasNoOwnProperty,
36: redCallableBatchGetPrototypeOfWhenHasNoOwnPropertyDescriptor,
27: redCallableInstallDateProtoToJSON,
28: redCallableInstallErrorPrepareStackTrace,
29: redCallableInstallJSONStringify,
30: redCallableInstallLazyPropertyDescriptors,
// 31: redCallableIsTargetLive,
32: redCallableIsTargetRevoked,
33: redCallableSerializeTarget,
34: redCallableSetLazyPropertyDescriptorStateByTarget,
35: redCallableTrackAsFastTarget,
36: redCallableBatchGetPrototypeOfAndGetOwnPropertyDescriptors,
37: redCallableBatchGetPrototypeOfWhenHasNoOwnProperty,
38: redCallableBatchGetPrototypeOfWhenHasNoOwnPropertyDescriptor,
} = redHooks!;
blueConnect(
noop, // redGlobalThisPointer,
Expand All @@ -190,12 +218,14 @@ export class VirtualEnvironment {
redCallableDebugInfo,
noop, // redCallableDefineProperties,
redCallableGetLazyPropertyDescriptorStateByTarget,
redCallableGetPropertyValue,
noop, // redCallableGetPropertyValue,
redCallableGetTargetIntegrityTraits,
redCallableGetToStringTagOfTarget,
noop, // redCallableInstallDateProtoToJSON,
redCallableInstallErrorPrepareStackTrace,
noop, // redCallableInstallJSONStringify
noop, // redCallableInstallLazyPropertyDescriptors,
redCallableIsTargetLive,
noop as unknown as CallableIsTargetLive, // redCallableIsTargetLive,
redCallableIsTargetRevoked,
redCallableSerializeTarget,
redCallableSetLazyPropertyDescriptorStateByTarget,
Expand Down Expand Up @@ -226,17 +256,19 @@ export class VirtualEnvironment {
blueCallablePreventExtensions,
blueCallableSet,
blueCallableSetPrototypeOf,
blueCallableDebugInfo,
noop, // blueCallableDebugInfo
noop, // blueCallableDefineProperties,
blueCallableGetLazyPropertyDescriptorStateByTarget,
blueCallableGetPropertyValue,
blueCallableGetTargetIntegrityTraits,
blueCallableGetToStringTagOfTarget,
blueCallableInstallErrorPrepareStackTrace,
noop, // blueCallableInstallDateProtoToJSON
noop, // blueCallableInstallJSONStringify
noop, // blueCallableInstallLazyPropertyDescriptors,
blueCallableIsTargetLive,
blueCallableIsTargetRevoked,
blueCallableSerializeTarget,
noop as unknown as CallableIsTargetRevoked, // blueCallableIsTargetRevoked,
noop as CallableSerializeTarget, // blueCallableSerializeTarget,,
blueCallableSetLazyPropertyDescriptorStateByTarget,
noop, // blueCallableTrackAsFastTarget,
blueCallableBatchGetPrototypeOfAndGetOwnPropertyDescriptors,
Expand Down Expand Up @@ -274,6 +306,8 @@ export class VirtualEnvironment {
}
ReflectApply(redCallableDefineProperties, undefined, args);
};
this.redCallableInstallJSONStringify = (WindowJSONPointer: Pointer) =>
redCallableInstallJSONStringify(WindowJSONPointer);
this.redCallableInstallLazyPropertyDescriptors = (
targetPointer: Pointer,
...ownKeysAndUnforgeableGlobalThisKeys: PropertyKey[]
Expand All @@ -286,6 +320,10 @@ export class VirtualEnvironment {
}
ReflectApply(redCallableInstallLazyPropertyDescriptors, undefined, args);
};
this.redCallableInstallDateProtoToJSON = (
DateProtoPointer: Pointer,
DataProtoToJSONPointer: Pointer
) => redCallableInstallDateProtoToJSON(DateProtoPointer, DataProtoToJSONPointer);
this.redCallableTrackAsFastTarget = (targetPointer: Pointer) =>
redCallableTrackAsFastTarget(targetPointer);
}
Expand All @@ -303,6 +341,16 @@ export class VirtualEnvironment {
}
}

installRemapOverrides() {
const transferableWindowJSON = this.blueGetTransferableValue(WindowJSON) as Pointer;
this.redCallableTrackAsFastTarget(transferableWindowJSON);
this.redCallableInstallDateProtoToJSON(
this.blueGetTransferableValue(DateProto) as Pointer,
this.blueGetTransferableValue(installableDateToJSON) as Pointer
);
this.redCallableInstallJSONStringify(transferableWindowJSON);
}

lazyRemapProperties(
target: ProxyTarget,
ownKeys: PropertyKey[],
Expand Down
Loading