Skip to content

Commit

Permalink
fixup! make shim opt-in via process.env
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Jan 25, 2025
1 parent c9b61ee commit 5561646
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 deletions.
11 changes: 9 additions & 2 deletions packages/non-trapping-shim/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Shim for Non-trapping Integrity Trait
# Opt-in Shim for Non-trapping Integrity Trait

Emulates support for the non-trapping integrity trait from the
[Stabilize proposal](https://github.com/tc39/proposal-stabilize).
Expand All @@ -11,4 +11,11 @@ This package is currently organized internally as a ponyfill, and a shim based o

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.

TODO: More explanation.
## Opt-in env-option `SES_NON_TRAPPING_SHIM`

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
```sh
$ export SES_NON_TRAPPING_SHIM=enabled
```

When not opted into, importing the shim has no effect.
4 changes: 4 additions & 0 deletions packages/non-trapping-shim/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"module": "./index.js",
"exports": {
"./shim.js": "./shim.js",
"./prepare-enable-shim.js": "./prepare-enable-shim.js",
"./package.json": "./package.json"
},
"scripts": {
Expand All @@ -35,6 +36,9 @@
"test:c8": "c8 $C8_OPTIONS ava --config=ava-nesm.config.js",
"test:xs": "exit 0"
},
"dependencies": {
"@endo/env-options": "workspace:^"
},
"devDependencies": {
"ava": "^6.1.3",
"c8": "^7.14.0",
Expand Down
7 changes: 7 additions & 0 deletions packages/non-trapping-shim/prepare-enable-shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* global globalThis */

// TODO consider adding env option setting APIs to @endo/env-options
// TODO should set up globalThis.process.env if absent
const env = (globalThis.process || {}).env || {};

env.SES_NON_TRAPPING_SHIM = 'enabled';
22 changes: 17 additions & 5 deletions packages/non-trapping-shim/shim.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
/* global globalThis */
import { getEnvironmentOption } from '@endo/env-options';
import { ReflectPlus, ObjectPlus, ProxyPlus } from './src/non-trapping-pony.js';

globalThis.Reflect = ReflectPlus;
const nonTrappingShimOption = getEnvironmentOption(
'SES_NON_TRAPPING_SHIM',
'disabled',
['enabled'],
);

globalThis.Object = ObjectPlus;
// eslint-disable-next-line no-extend-native
Object.prototype.constructor = ObjectPlus;
if (nonTrappingShimOption === 'enabled') {
// TODO figure this out, either remove directive or change to
// at-ts-expect-error.
// @ts-ignore type of ReflectPlus vs Reflect, I think
globalThis.Reflect = ReflectPlus;

globalThis.Proxy = ProxyPlus;
globalThis.Object = ObjectPlus;
// eslint-disable-next-line no-extend-native
Object.prototype.constructor = ObjectPlus;

globalThis.Proxy = ProxyPlus;
}
1 change: 1 addition & 0 deletions packages/non-trapping-shim/test/non-trapping-shim.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '../prepare-enable-shim.js';
// Uses 'ava' rather than @endo/ses-ava to avoid worries about cyclic
// dependencies. We will need similar tests is higher level packages, in order
// to test compat with ses and ses-ava.
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@endo/non-trapping-shim@workspace:packages/non-trapping-shim"
dependencies:
"@endo/env-options": "workspace:^"
ava: "npm:^6.1.3"
c8: "npm:^7.14.0"
tsd: "npm:^0.31.2"
Expand Down

0 comments on commit 5561646

Please sign in to comment.