Skip to content

Commit

Permalink
feat: @W-11752332 add shared and shared-dom packages (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton authored Sep 19, 2022
1 parent c80f082 commit 8c7939f
Show file tree
Hide file tree
Showing 72 changed files with 1,856 additions and 201 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
],
"rules": {
"prettier/prettier": "error",
// Overriden
// Overridden
"no-eval": "off",
"camelcase": ["error", { "properties": "never", "allow": [ "W[0-9]+_"] }],
"import/extensions": "off",
Expand Down
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.11.4",
"private": true,
"scripts": {
"clean": "rimraf */bundle.js",
"clean": "locker-trash */bundle.js",
"build": "rollup --config .rolluprc.cjs"
},
"dependencies": {
Expand Down
10 changes: 7 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ module.exports = {
coverageDirectory: 'jest-coverage/json/',
coverageReporters: ['json'],
moduleNameMapper: {
'^@locker/near-membrane-base$': '<rootDir>/packages/near-membrane-base/src',
'^@locker/near-membrane-node$': '<rootDir>/packages/near-membrane-node/src',
'^@locker/(near-membrane-\\w+)$': '<rootDir>/packages/$1/src',
},
roots: ['<rootDir>/packages/near-membrane-base', '<rootDir>/packages/near-membrane-node'],
roots: [
'<rootDir>/packages/near-membrane-base',
'<rootDir>/packages/near-membrane-node',
'<rootDir>/packages/near-membrane-shared',
'<rootDir>/packages/near-membrane-shared-dom',
],
testEnvironment: 'jsdom',
testEnvironmentOptions: {
url: 'http://localhost/',
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@
"karma-safari-launcher": "1.0.0",
"lerna": "5.5.0",
"lint-staged": "13.0.3",
"meow": "10.1.3",
"merge-options": "3.0.4",
"nyc": "15.1.0",
"prettier": "2.7.1",
"puppeteer": "17.1.0",
"rimraf": "3.0.2",
"rollup": "2.79.0",
"rollup-plugin-istanbul": "3.0.0",
"trash": "7.2.0",
"tslib": "2.4.0",
"typescript": "4.8.2"
},
Expand Down
7 changes: 5 additions & 2 deletions packages/near-membrane-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
},
"license": "MIT",
"author": "Caridy Patiño <[email protected]>",
"description": "JavaScript Near Membrane Library to create a sandboxed environment",
"description": "Base membrane library to create sandboxed environments",
"main": "dist/index.cjs.js",
"module": "dist/index.js",
"sideEffects": false,
"typings": "types/index.d.ts",
"scripts": {
"clean": "rimraf dist types",
"clean": "locker-trash dist types",
"build": "tsc --project tsconfig.types.json && rollup --config .rolluprc.cjs",
"build:dev": "cross-env NODE_ENV=development yarn build"
},
Expand All @@ -24,5 +24,8 @@
],
"publishConfig": {
"access": "public"
},
"dependencies": {
"@locker/near-membrane-shared": "0.11.4"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createBlueConnector, createRedConnector } from '../index';
import { createBlueConnector, createRedConnector } from '../../dist/index';

describe('createBlueConnector()', () => {
it('throws when globalObject is not provided', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getFilteredGlobalOwnKeys,
linkIntrinsics,
VirtualEnvironment,
} from '../index';
} from '../../dist/index';

const ESGlobalKeys = [
// *** 19.1 Value Properties of the Global Object
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
import { createBlueConnector, createRedConnector, VirtualEnvironment } from '../index';
import { createBlueConnector, createRedConnector, VirtualEnvironment } from '../../dist/index';

const { toString: ObjectProtoToString } = Object.prototype;

Expand Down
3 changes: 1 addition & 2 deletions packages/near-membrane-base/src/connector.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { TypeErrorCtor } from '@locker/near-membrane-shared';
import { createMembraneMarshall } from './membrane';
import type { Connector } from './types';

const TypeErrorCtor = TypeError;

const createMembraneMarshallSourceInStrictMode = `
'use strict';
(${createMembraneMarshall})`;
Expand Down
61 changes: 32 additions & 29 deletions packages/near-membrane-base/src/environment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import {
ArrayCtor,
ArrayProtoPush,
ErrorCtor,
noop,
ObjectAssign,
ReflectApply,
ReflectOwnKeys,
} from '@locker/near-membrane-shared';
import type { ProxyTarget } from '@locker/near-membrane-shared/types';
import type {
CallableDefineProperties,
CallableEvaluate,
Expand All @@ -9,20 +19,12 @@ import type {
GetTransferableValue,
HooksCallback,
Pointer,
ProxyTarget,
PropertyKeys,
VirtualEnvironmentOptions,
} from './types';

const LOCKER_NEAR_MEMBRANE_UNDEFINED_VALUE_SYMBOL = Symbol.for(
'@@lockerNearMembraneUndefinedValue'
);
const ArrayCtor = Array;
const ErrorCtor = Error;
const ObjectCtor = Object;
const { push: ArrayProtoPush } = ArrayCtor.prototype;
const { assign: ObjectAssign } = ObjectCtor;
const { apply: ReflectApply, ownKeys: ReflectOwnKeys } = Reflect;

export class VirtualEnvironment {
private readonly blueCallableGetPropertyValuePointer: CallableGetPropertyValuePointer;
Expand Down Expand Up @@ -155,12 +157,12 @@ export class VirtualEnvironment {
35: redCallableBatchGetPrototypeOfWhenHasNoOwnPropertyDescriptor,
} = redHooks!;
blueConnect(
undefined, // redGlobalThisPointer,
undefined, // redGetSelectedTarget,
undefined, // redGetTransferableValue,
undefined, // redCallableGetPropertyValuePointer,
undefined, // redCallableEvaluate,
undefined, // redCallableLinkPointers,
noop, // redGlobalThisPointer,
noop, // redGetSelectedTarget,
noop as GetTransferableValue, // redGetTransferableValue,
noop as unknown as CallableGetPropertyValuePointer, // redCallableGetPropertyValuePointer,
noop as CallableEvaluate, // redCallableEvaluate,
noop, // redCallableLinkPointers,
redCallablePushErrorTarget,
redCallablePushTarget,
redCallableApply,
Expand All @@ -177,13 +179,13 @@ export class VirtualEnvironment {
redCallableSet,
redCallableSetPrototypeOf,
redCallableDebugInfo,
undefined, // redCallableDefineProperties,
noop, // redCallableDefineProperties,
redCallableGetLazyPropertyDescriptorStateByTarget,
redCallableGetTargetIntegrityTraits,
redCallableGetToStringTagOfTarget,
redCallableGetTypedArrayIndexedValue,
redCallableInstallErrorPrepareStackTrace,
undefined, // redCallableInstallLazyPropertyDescriptors,
noop, // redCallableInstallLazyPropertyDescriptors,
redCallableIsTargetLive,
redCallableIsTargetRevoked,
redCallableSerializeTarget,
Expand All @@ -193,12 +195,12 @@ export class VirtualEnvironment {
redCallableBatchGetPrototypeOfWhenHasNoOwnPropertyDescriptor
);
redConnect(
undefined, // blueGlobalThisPointer,
undefined, // blueGetSelectedTarget,
undefined, // blueGetTransferableValue,
undefined, // blueCallableGetPropertyValuePointer,
undefined, // blueCallableEvaluate,
undefined, // blueCallableLinkPointers,
noop, // blueGlobalThisPointer,
noop, // blueGetSelectedTarget,
noop as GetTransferableValue, // blueGetTransferableValue,
noop as unknown as CallableGetPropertyValuePointer, // blueCallableGetPropertyValuePointer,
noop as CallableEvaluate, // blueCallableEvaluate,
noop, // blueCallableLinkPointers,
blueCallablePushErrorTarget,
blueCallablePushTarget,
blueCallableApply,
Expand All @@ -215,13 +217,13 @@ export class VirtualEnvironment {
blueCallableSet,
blueCallableSetPrototypeOf,
blueCallableDebugInfo,
undefined, // blueCallableDefineProperties,
noop, // blueCallableDefineProperties,
blueCallableGetLazyPropertyDescriptorStateByTarget,
blueCallableGetTargetIntegrityTraits,
blueCallableGetToStringTagOfTarget,
blueCallableGetTypedArrayIndexedValue,
blueCallableInstallErrorPrepareStackTrace,
undefined, // blueCallableInstallLazyPropertyDescriptors,
noop, // blueCallableInstallLazyPropertyDescriptors,
blueCallableIsTargetLive,
blueCallableIsTargetRevoked,
blueCallableSerializeTarget,
Expand Down Expand Up @@ -260,12 +262,13 @@ export class VirtualEnvironment {

lazyRemapProperties(
target: ProxyTarget,
ownKeys: PropertyKeys,
unforgeableGlobalThisKeys?: PropertyKeys
ownKeys: PropertyKey[],
unforgeableGlobalThisKeys?: PropertyKey[]
) {
if ((typeof target === 'object' && target !== null) || typeof target === 'function') {
const targetPointer = this.blueGetTransferableValue(target) as Pointer;
const args: Parameters<CallableInstallLazyPropertyDescriptors> = [targetPointer];
const args: Parameters<CallableInstallLazyPropertyDescriptors> = [
this.blueGetTransferableValue(target) as Pointer,
];
ReflectApply(ArrayProtoPush, args, ownKeys);
if (unforgeableGlobalThisKeys?.length) {
// Use `LOCKER_NEAR_MEMBRANE_UNDEFINED_VALUE_SYMBOL` to delimit
Expand All @@ -277,7 +280,7 @@ export class VirtualEnvironment {
}
}

link(...keys: PropertyKeys) {
link(...keys: PropertyKey[]) {
let bluePointer = this.blueGlobalThisPointer;
let redPointer = this.redGlobalThisPointer;
for (let i = 0, { length } = keys; i < length; i += 1) {
Expand Down
1 change: 0 additions & 1 deletion packages/near-membrane-base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ export * from './environment';
export * from './intrinsics';
export * from './membrane';
export * from './types';
export * from './utils';
10 changes: 3 additions & 7 deletions packages/near-membrane-base/src/intrinsics.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { toSafeArray } from './utils';
import { ObjectAssign, ReflectOwnKeys, toSafeArray } from '@locker/near-membrane-shared';
import { VirtualEnvironment } from './environment';
import type { PropertyKeys } from './types';

const { assign: ObjectAssign } = Object;
const { ownKeys: ReflectOwnKeys } = Reflect;

/**
* This list must be in sync with ecma-262, anything new added to the global object
Expand Down Expand Up @@ -147,8 +143,8 @@ export function assignFilteredGlobalDescriptorsFromPropertyDescriptorMap<
return descMap;
}

export function getFilteredGlobalOwnKeys(source: object): PropertyKeys {
const result: PropertyKeys = [];
export function getFilteredGlobalOwnKeys(source: object): PropertyKey[] {
const result: PropertyKey[] = [];
let resultOffset = 0;
const ownKeys = ReflectOwnKeys(source);
for (let i = 0, { length } = ownKeys; i < length; i += 1) {
Expand Down
35 changes: 18 additions & 17 deletions packages/near-membrane-base/src/membrane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
* argument of the foreign callable for proxies, and the other side can use
* it via `selectedTarget!`.
*/
import { toSafeWeakMap } from './utils';
import { toSafeWeakMap, WeakMapCtor } from '@locker/near-membrane-shared';
import type { Getter, ProxyTarget, Setter } from '@locker/near-membrane-shared/types';
import type {
Activity,
CallableApply,
Expand Down Expand Up @@ -61,22 +62,17 @@ import type {
CallableSetPrototypeOf,
ForeignPropertyDescriptor,
GetSelectedTarget,
Getter,
GlobalThisGetter,
HooksCallback,
HooksOptions,
Pointer,
PointerOrPrimitive,
PropertyKey,
PropertyKeys,
ProxyTarget,
SerializedValue,
Setter,
ShadowTarget,
} from './types';

const proxyTargetToLazyPropertyDescriptorStateMap: WeakMap<ProxyTarget, object> = toSafeWeakMap(
new WeakMap()
new WeakMapCtor()
);

// istanbul ignore next
Expand All @@ -95,6 +91,7 @@ export function createMembraneMarshall(
const StringCtor = String;
const SymbolCtor = Symbol;
const TypeErrorCtor = TypeError;
// eslint-disable-next-line @typescript-eslint/no-shadow, no-shadow
const WeakMapCtor = WeakMap;
const { for: SymbolFor, toStringTag: SymbolToStringTag } = SymbolCtor;
const {
Expand Down Expand Up @@ -180,7 +177,7 @@ export function createMembraneMarshall(
let MINIFICATION_SAFE_SERIALIZED_VALUE_PROPERTY_NAME: PropertyKey | undefined;
// Minification safe references to the private `BoundaryProxyHandler`
// 'apply' and 'construct' trap variant's property names.
let MINIFICATION_SAFE_TRAP_PROPERTY_NAMES: PropertyKeys | undefined;
let MINIFICATION_SAFE_TRAP_PROPERTY_NAMES: PropertyKey[] | undefined;
const SUPPORTS_BIG_INT = typeof BigInt === 'function';
const { isArray: isArrayOrThrowForRevoked } = ArrayCtor;
const {
Expand Down Expand Up @@ -1650,7 +1647,7 @@ export function createMembraneMarshall(
}

const installPropertyDescriptorMethodWrappers = IS_IN_SHADOW_REALM
? (unforgeableGlobalThisKeys?: PropertyKeys) => {
? (unforgeableGlobalThisKeys?: PropertyKey[]) => {
if (installedPropertyDescriptorMethodWrappersFlag) {
return;
}
Expand Down Expand Up @@ -3354,7 +3351,11 @@ export function createMembraneMarshall(
} else {
this.makeProxyStatic();
}
return this.defineProperty!(shadowTarget, key, unsafePartialDesc);
return this.defineProperty!(
shadowTarget,
key as string | symbol,
unsafePartialDesc
);
}
: (alwaysFalse as typeof Reflect.defineProperty);

Expand All @@ -3376,7 +3377,7 @@ export function createMembraneMarshall(
} else {
this.makeProxyStatic();
}
return this.deleteProperty!(shadowTarget, key);
return this.deleteProperty!(shadowTarget, key as string | symbol);
}
: (alwaysFalse as typeof Reflect.deleteProperty);

Expand Down Expand Up @@ -3443,7 +3444,7 @@ export function createMembraneMarshall(
} else {
this.makeProxyStatic();
}
return this.set!(shadowTarget, key, value, receiver);
return this.set!(shadowTarget, key as string | symbol, value, receiver);
}
: (alwaysFalse as typeof Reflect.set);

Expand Down Expand Up @@ -4199,28 +4200,28 @@ export function createMembraneMarshall(
IS_IN_SHADOW_REALM
? (
targetPointer: Pointer,
...ownKeysAndUnforgeableGlobalThisKeys: PropertyKeys
...ownKeysAndUnforgeableGlobalThisKeys: PropertyKey[]
) => {
const sliceIndex: number = ReflectApply(
ArrayProtoIndexOf,
ownKeysAndUnforgeableGlobalThisKeys,
[LOCKER_NEAR_MEMBRANE_UNDEFINED_VALUE_SYMBOL]
);
let ownKeys: PropertyKeys;
let unforgeableGlobalThisKeys: PropertyKeys | undefined;
let ownKeys: PropertyKey[];
let unforgeableGlobalThisKeys: PropertyKey[] | undefined;
if (sliceIndex === -1) {
ownKeys = ownKeysAndUnforgeableGlobalThisKeys;
} else {
ownKeys = ReflectApply(
ArrayProtoSlice,
ownKeysAndUnforgeableGlobalThisKeys,
[0, sliceIndex]
) as PropertyKeys;
) as PropertyKey[];
unforgeableGlobalThisKeys = ReflectApply(
ArrayProtoSlice,
ownKeysAndUnforgeableGlobalThisKeys,
[sliceIndex + 1]
) as PropertyKeys;
) as PropertyKey[];
}
targetPointer();
const target = selectedTarget!;
Expand Down
Loading

0 comments on commit 8c7939f

Please sign in to comment.