Skip to content

Commit

Permalink
chore: rename isNearMembraneProxyMaskedFunction to isProxyMaskedFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Feb 16, 2023
1 parent bdf98a7 commit b911ac2
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 33 deletions.
12 changes: 12 additions & 0 deletions packages/near-membrane-shared/src/Function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ export function noop() {
// No operation performed.
}

export function isProxyMaskedFunction(value: any): boolean {
// To extract the flag value of a blue near-membrane proxy we must perform
// a two step handshake. First, we trigger the "has" trap for the
// `LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL` property which must report
// `false`. Second, we trigger the "get" trap to return the flag value.
return (
typeof value === 'function' &&
!(LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL in value) &&
(value as any)[LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL] === true
);
}

export function proxyMaskFunction<T extends Function>(
func: Function,
maskFunc: T,
Expand Down
13 changes: 0 additions & 13 deletions packages/near-membrane-shared/src/NearMembrane.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL,
LOCKER_NEAR_MEMBRANE_SERIALIZED_VALUE_SYMBOL,
LOCKER_NEAR_MEMBRANE_SYMBOL,
} from './constants';
Expand Down Expand Up @@ -32,15 +31,3 @@ export function isNearMembraneProxy(value: any): boolean {
}
return false;
}

export function isNearMembraneProxyMaskedFunction(value: any): boolean {
// To extract the flag value of a blue near-membrane proxy we must perform
// a two step handshake. First, we trigger the "has" trap for the
// `LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL` property which must report
// `false`. Second, we trigger the "get" trap to return the flag value.
return (
typeof value === 'function' &&
!(LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL in value) &&
(value as any)[LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL] === true
);
}
14 changes: 14 additions & 0 deletions packages/near-membrane-shared/src/__tests__/Function.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import {
ERR_ILLEGAL_PROPERTY_ACCESS,
isProxyMaskedFunction,
LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL,
noop,
proxyMaskFunction,
} from '../../dist/index.mjs.js';

describe('Function', () => {
it('isProxyMaskedFunction', () => {
expect(isProxyMaskedFunction({})).toBe(false);
expect(isProxyMaskedFunction(null)).toBe(false);
expect(isProxyMaskedFunction(undefined)).toBe(false);
const func = () => 'func';
func[LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL] = true;
expect(isProxyMaskedFunction(func)).toBe(false);
const mask = () => 'mask';
expect(isProxyMaskedFunction(mask)).toBe(false);
const masked = proxyMaskFunction(func, mask);
expect(isProxyMaskedFunction(masked)).toBe(true);
});

it('noop', () => {
expect(noop()).toBe(undefined);
expect(noop(null)).toBe(undefined);
Expand Down
16 changes: 0 additions & 16 deletions packages/near-membrane-shared/src/__tests__/NearMembrane.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import {
getNearMembraneProxySerializedValue,
isNearMembraneProxy,
isNearMembraneProxyMaskedFunction,
LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL,
LOCKER_NEAR_MEMBRANE_SERIALIZED_VALUE_SYMBOL,
LOCKER_NEAR_MEMBRANE_SYMBOL,
proxyMaskFunction,
} from '../../dist/index.mjs.js';

describe('NearMembrane', () => {
Expand All @@ -30,17 +27,4 @@ describe('NearMembrane', () => {
})
).toBe(false);
});

it('isNearMembraneProxyMaskedFunction', () => {
expect(isNearMembraneProxyMaskedFunction({})).toBe(false);
expect(isNearMembraneProxyMaskedFunction(null)).toBe(false);
expect(isNearMembraneProxyMaskedFunction(undefined)).toBe(false);
const func = () => 'func';
func[LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL] = true;
expect(isNearMembraneProxyMaskedFunction(func)).toBe(false);
const mask = () => 'mask';
expect(isNearMembraneProxyMaskedFunction(mask)).toBe(false);
const masked = proxyMaskFunction(func, mask);
expect(isNearMembraneProxyMaskedFunction(masked)).toBe(true);
});
});
4 changes: 2 additions & 2 deletions test/membrane/date.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNearMembraneProxyMaskedFunction } from '@locker/near-membrane-shared';
import { isProxyMaskedFunction } from '@locker/near-membrane-shared';
import createVirtualEnvironment from '@locker/near-membrane-dom';

describe('Date', () => {
Expand All @@ -11,7 +11,7 @@ describe('Date', () => {
env.evaluate(`
expect(Date.prototype.toJSON.toString()).toContain('[native code]');
`);
expect(isNearMembraneProxyMaskedFunction(Date.prototype.toJSON)).toBe(false);
expect(isProxyMaskedFunction(Date.prototype.toJSON)).toBe(false);
});

it('Date.prototype.toJSON does not support proxy masked symbol handshake', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/membrane/json.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNearMembraneProxyMaskedFunction } from '@locker/near-membrane-shared';
import { isProxyMaskedFunction } from '@locker/near-membrane-shared';
import createVirtualEnvironment from '@locker/near-membrane-dom';

describe('JSON', () => {
Expand All @@ -11,7 +11,7 @@ describe('JSON', () => {
env.evaluate(`
expect(JSON.stringify.toString()).toContain('[native code]');
`);
expect(isNearMembraneProxyMaskedFunction(JSON.stringify)).toBe(false);
expect(isProxyMaskedFunction(JSON.stringify)).toBe(false);
});

it('JSON.stringify does not support proxy masked symbol handshake', () => {
Expand Down

0 comments on commit b911ac2

Please sign in to comment.