Skip to content

Commit

Permalink
refactor(ses): Use Symbol.toStringTag over toString override
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Nov 3, 2023
1 parent 68805fe commit 20d2f80
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
26 changes: 21 additions & 5 deletions packages/ses/src/compartment.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import {
TypeError,
WeakMap,
assign,
create,
defineProperties,
entries,
getOwnPropertyDescriptors,
objectPrototype,
promiseThen,
toStringTagSymbol,
weakmapGet,
weakmapSet,
} from './commons.js';
Expand Down Expand Up @@ -79,7 +83,7 @@ const compartmentImportNow = (compartment, specifier) => {
return exportsProxy;
};

export const CompartmentPrototype = {
const CompartmentPrototypeTemplate = {
constructor: InertCompartment,

get globalThis() {
Expand All @@ -105,10 +109,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 +168,22 @@ export const CompartmentPrototype = {
},
};

const CompartmentPrototypeProperties = {
...getOwnPropertyDescriptors(CompartmentPrototypeTemplate),

[toStringTagSymbol]: {
value: 'Compartment',
writable: false,
configurable: true,
enumerable: false,
},
};

export const CompartmentPrototype = create(
objectPrototype,
CompartmentPrototypeProperties,
);

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 @@ -1500,8 +1500,7 @@ export const permitted = {
evaluate: fn,
globalThis: getter,
name: getter,
// Should this be proposed?
toString: fn,
'@@toStringTag': 'string',
import: asyncFn,
load: asyncFn,
importNow: fn,
Expand Down
7 changes: 4 additions & 3 deletions packages/ses/test/test-compartment-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ 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(),
Reflect.ownKeys(Object.getPrototypeOf(c))
.filter(key => typeof key !== 'symbol')
.sort(),
[
'constructor',
'evaluate',
Expand All @@ -42,7 +44,6 @@ test('Compartment instance', t => {
'load',
'module',
'name',
'toString',
].sort(),
'prototype properties',
);
Expand Down
5 changes: 3 additions & 2 deletions packages/ses/test/test-compartment-prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ test('Compartment prototype', t => {
);

t.deepEqual(
Reflect.ownKeys(Compartment.prototype).sort(),
Reflect.ownKeys(Compartment.prototype)
.filter(key => typeof key !== 'symbol')
.sort(),
[
'constructor',
'evaluate',
Expand All @@ -23,7 +25,6 @@ test('Compartment prototype', t => {
'load',
'module',
'name',
'toString',
].sort(),
'prototype properties',
);
Expand Down

0 comments on commit 20d2f80

Please sign in to comment.