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

refactor(ses): Replace Compartment toString property with @@toStringTag #1962

Merged
merged 1 commit into from
Jan 19, 2024
Merged
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
17 changes: 13 additions & 4 deletions packages/ses/src/compartment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
defineProperties,
entries,
promiseThen,
toStringTagSymbol,
weakmapGet,
weakmapSet,
} from './commons.js';
Expand Down Expand Up @@ -105,10 +106,6 @@ export const CompartmentPrototype = {
return compartmentEvaluate(compartmentFields, source, options);
},

toString() {
return '[object Compartment]';
},

module(specifier) {
if (typeof specifier !== 'string') {
throw TypeError('first argument of module() must be a string');
Expand Down Expand Up @@ -168,6 +165,18 @@ export const CompartmentPrototype = {
},
};

// This causes `String(new Compartment())` to evaluate to `[object Compartment]`.
// The descriptor follows the conventions of other globals with @@toStringTag
// properties, e.g. Math.
defineProperties(CompartmentPrototype, {
[toStringTagSymbol]: {
value: 'Compartment',
writable: false,
enumerable: false,
configurable: true,
},
});

defineProperties(InertCompartment, {
prototype: { value: CompartmentPrototype },
});
Expand Down
3 changes: 1 addition & 2 deletions packages/ses/src/permits.js
Original file line number Diff line number Diff line change
Expand Up @@ -1516,12 +1516,11 @@ export const permitted = {
evaluate: fn,
globalThis: getter,
name: getter,
// Should this be proposed?
toString: fn,
import: asyncFn,
load: asyncFn,
importNow: fn,
module: fn,
'@@toStringTag': 'string',
},

lockdown: fn,
Expand Down
25 changes: 8 additions & 17 deletions packages/ses/test/error/throws-and-logs.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/* global globalThis */

import { freeze, getPrototypeOf, is } from '../../src/commons.js';
import { loggedErrorHandler, assert } from '../../src/error/assert.js';
import { freeze, getPrototypeOf } from '../../src/commons.js';
import { loggedErrorHandler } from '../../src/error/assert.js';
import {
makeLoggingConsoleKit,
makeCausalConsole,
} from '../../src/error/console.js';

const { quote: q } = assert;

// For our internal debugging purposes
// const internalDebugConsole = console;

Expand All @@ -19,11 +17,6 @@ const compareLogs = freeze((t, log, goldenLog) => {
t.is(log.length, goldenLog.length, 'wrong log length');
log.forEach((logRecord, i) => {
const goldenRecord = goldenLog[i];
t.is(
logRecord.length,
goldenRecord.length,
`wrong length of log record ${i}`,
);
rekmarks marked this conversation as resolved.
Show resolved Hide resolved
logRecord.forEach((logEntry, j) => {
const goldenEntry = goldenRecord[j];
if (
Expand All @@ -33,16 +26,14 @@ const compareLogs = freeze((t, log, goldenLog) => {
) {
t.assert(logEntry instanceof goldenEntry, 'not the right error');
} else {
// tap uses `===` instead of `Object.is`.
// Assuming ava does the right thing, switch back to this when
// switching back to ava.
// t.is(logEntry, goldenEntry);
t.assert(
is(logEntry, goldenEntry),
`${q(logEntry)} not same as ${q(goldenEntry)}`,
);
kriskowal marked this conversation as resolved.
Show resolved Hide resolved
t.is(logEntry, goldenEntry);
}
});
t.is(
logRecord.length,
goldenRecord.length,
`wrong length of log record ${i}`,
);
});
});

Expand Down
19 changes: 2 additions & 17 deletions packages/ses/test/test-compartment-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava';
import '../index.js';

test('Compartment instance', t => {
t.plan(12);
t.plan(11);

const c = new Compartment();

Expand All @@ -28,22 +28,7 @@ test('Compartment instance', t => {
);

t.is(c.toString(), '[object Compartment]', 'toString()');
t.is(c[Symbol.toStringTag], undefined, '"Symbol.toStringTag" property');
t.is(c[Symbol.toStringTag], 'Compartment', '"Symbol.toStringTag" property');

t.deepEqual(Reflect.ownKeys(c), [], 'static properties');
t.deepEqual(
Reflect.ownKeys(Object.getPrototypeOf(c)).sort(),
[
'constructor',
'evaluate',
'globalThis',
'import',
'importNow',
'load',
'module',
'name',
'toString',
].sort(),
'prototype properties',
);
kriskowal marked this conversation as resolved.
Show resolved Hide resolved
});
29 changes: 16 additions & 13 deletions packages/ses/test/test-compartment-prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ test('Compartment prototype', t => {
'The initial value of Compartment.prototype.constructor',
);

t.deepEqual(
Reflect.ownKeys(Compartment.prototype).sort(),
[
'constructor',
'evaluate',
'globalThis',
'import',
'importNow',
'load',
'module',
'name',
'toString',
].sort(),
const expectedProps = new Set([
'constructor',
'evaluate',
'globalThis',
'import',
'importNow',
'load',
'module',
'name',
Symbol.toStringTag,
]);
const actualProps = Reflect.ownKeys(Compartment.prototype);

t.assert(
actualProps.length === expectedProps.size &&
actualProps.every(key => expectedProps.has(key)),
'prototype properties',
);
});