Skip to content

Commit 4646e3d

Browse files
committed
fixup! review suggestion
1 parent 830fa53 commit 4646e3d

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

packages/non-trapping-shim/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,24 @@ This package is currently organized internally as a ponyfill, and a shim based o
1111

1212
See https://github.com/endojs/endo/blob/master/packages/ses/docs/preparing-for-stabilize.md for guidance on how to prepare for the changes that will be introduced by this proposal.
1313

14+
The shim needs to be imported ***early***, in particular before other code might sample of freeze the primordials that this shim would modify or replace. Even though the `@endo/non-trapping-shim` package does not export a ponyfill, to uphold the convention on side-effecting exports, it does not export the shim as the package's default export. Rather, you need to import it as:
15+
16+
```js
17+
import '@endo/non-trapping-shim/shim.js';
18+
```
19+
1420
## Opt-in env-option `SES_NON_TRAPPING_SHIM`
1521

1622
To cope with various compat problems in linking code that uses or assumes this shim to code that does not, we have made this shim opt-in via the env-option `SES_NON_TRAPPING_SHIM`. This has two settings, `'enabled'` and the default `'disabled'`. As with all env options, this is represented at the property `process.env.SES_NON_TRAPPING_SHIM`, which typically represents the environment variable `SES_NON_TRAPPING_SHIM`. Thus, if nothing else sets `process.env.SES_NON_TRAPPING_SHIM`, you can opt-in at the shell level by
1723
```sh
1824
$ export SES_NON_TRAPPING_SHIM=enabled
1925
```
2026

27+
The shim senses the setting of this env-option at the time it is imported, so any desired setting will need to happen earlier. Using a genuine environment variable does this. Otherwise, as a convenience, this package also exports a module to opt-in by setting `process.env.SES_NON_TRAPPING_SHIM`. To use this convenience, import the exported `prepare-enable-shim.js` before importing the exported shim itself:
28+
29+
```js
30+
import '@endo/non-trapping-shim/prepare-enable-shim.js';
31+
import '@endo/non-trapping-shim/shim.js';
32+
```
33+
2134
When not opted into, importing the shim has no effect.

packages/non-trapping-shim/src/non-trapping-pony.js

+19-27
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@ const isPrimitive = specimen => OriginalObject(specimen) !== specimen;
2020
* `Reflect.isNonTrapping`.
2121
*
2222
* @param {any} specimen
23-
* @param {boolean} shouldThrow
2423
* @returns {boolean}
2524
*/
26-
const isNonTrappingInternal = (specimen, shouldThrow) => {
25+
const isNonTrappingInternal = specimen => {
2726
if (nonTrappingSet.has(specimen)) {
2827
return true;
2928
}
3029
if (!proxyHandlerMap.has(specimen)) {
3130
return false;
3231
}
3332
const [target, handler] = proxyHandlerMap.get(specimen);
34-
if (isNonTrappingInternal(target, shouldThrow)) {
33+
if (isNonTrappingInternal(target)) {
3534
nonTrappingSet.add(specimen);
3635
return true;
3736
}
@@ -40,14 +39,11 @@ const isNonTrappingInternal = (specimen, shouldThrow) => {
4039
return false;
4140
}
4241
const result = apply(trap, handler, [target]);
43-
const ofTarget = isNonTrappingInternal(target, shouldThrow);
42+
const ofTarget = isNonTrappingInternal(target);
4443
if (result !== ofTarget) {
45-
if (shouldThrow) {
46-
throw TypeError(
47-
`'isNonTrapping' proxy trap does not reflect 'isNonTrapping' of proxy target (which is '${ofTarget}')`,
48-
);
49-
}
50-
return false;
44+
throw TypeError(
45+
`'isNonTrapping' proxy trap does not reflect 'isNonTrapping' of proxy target (which is '${ofTarget}')`,
46+
);
5147
}
5248
if (result) {
5349
nonTrappingSet.add(specimen);
@@ -60,10 +56,9 @@ const isNonTrappingInternal = (specimen, shouldThrow) => {
6056
* `Reflect.suppressTrapping`.
6157
*
6258
* @param {any} specimen
63-
* @param {boolean} shouldThrow
6459
* @returns {boolean}
6560
*/
66-
const suppressTrappingInternal = (specimen, shouldThrow) => {
61+
const suppressTrappingInternal = specimen => {
6762
if (nonTrappingSet.has(specimen)) {
6863
return true;
6964
}
@@ -73,27 +68,24 @@ const suppressTrappingInternal = (specimen, shouldThrow) => {
7368
return true;
7469
}
7570
const [target, handler] = proxyHandlerMap.get(specimen);
76-
if (isNonTrappingInternal(target, shouldThrow)) {
71+
if (isNonTrappingInternal(target)) {
7772
nonTrappingSet.add(specimen);
7873
return true;
7974
}
8075
const trap = handler.suppressTrapping;
8176
if (trap === undefined) {
82-
const result = suppressTrappingInternal(target, shouldThrow);
77+
const result = suppressTrappingInternal(target);
8378
if (result) {
8479
nonTrappingSet.add(specimen);
8580
}
8681
return result;
8782
}
8883
const result = apply(trap, handler, [target]);
89-
const ofTarget = isNonTrappingInternal(target, shouldThrow);
84+
const ofTarget = isNonTrappingInternal(target);
9085
if (result !== ofTarget) {
91-
if (shouldThrow) {
92-
throw TypeError(
93-
`'suppressTrapping' proxy trap does not reflect 'isNonTrapping' of proxy target (which is '${ofTarget}')`,
94-
);
95-
}
96-
return false;
86+
throw TypeError(
87+
`'suppressTrapping' proxy trap does not reflect 'isNonTrapping' of proxy target (which is '${ofTarget}')`,
88+
);
9789
}
9890
if (result) {
9991
nonTrappingSet.add(specimen);
@@ -106,13 +98,13 @@ export const extraReflectMethods = freeze({
10698
if (isPrimitive(target)) {
10799
throw TypeError('Reflect.isNonTrapping called on non-object');
108100
}
109-
return isNonTrappingInternal(target, false);
101+
return isNonTrappingInternal(target);
110102
},
111103
suppressTrapping(target) {
112104
if (isPrimitive(target)) {
113105
throw TypeError('Reflect.suppressTrapping called on non-object');
114106
}
115-
return suppressTrappingInternal(target, false);
107+
return suppressTrappingInternal(target);
116108
},
117109
});
118110

@@ -121,13 +113,13 @@ export const extraObjectMethods = freeze({
121113
if (isPrimitive(target)) {
122114
return true;
123115
}
124-
return isNonTrappingInternal(target, true);
116+
return isNonTrappingInternal(target);
125117
},
126118
suppressTrapping(target) {
127119
if (isPrimitive(target)) {
128120
return target;
129121
}
130-
if (suppressTrappingInternal(target, true)) {
122+
if (suppressTrappingInternal(target)) {
131123
return target;
132124
}
133125
throw TypeError('suppressTrapping trap returned falsy');
@@ -198,7 +190,7 @@ const metaHandler = freeze({
198190
* @param {any[]} rest
199191
*/
200192
const trapPlus = freeze((target, ...rest) => {
201-
if (isNonTrappingInternal(target, true)) {
193+
if (isNonTrappingInternal(target)) {
202194
defineProperty(handlerPlus, trapName, {
203195
value: undefined,
204196
writable: false,
@@ -284,7 +276,7 @@ ProxyPlus.revocable = (target, handler) => {
284276
return {
285277
proxy,
286278
revoke() {
287-
if (isNonTrappingInternal(target, true)) {
279+
if (isNonTrappingInternal(target)) {
288280
throw TypeError('Cannot revoke non-trapping proxy');
289281
}
290282
revoke();

0 commit comments

Comments
 (0)