From 2b82131218e4eab1910584f5b09ce1de7a46b1fd Mon Sep 17 00:00:00 2001 From: "Mark S. Miller" Date: Fri, 24 Jan 2025 12:39:02 -0800 Subject: [PATCH] fixup! make shim opt-in via process.env --- packages/non-trapping-shim/package.json | 4 ++++ .../non-trapping-shim/prepare-enable-shim.js | 7 ++++++ packages/non-trapping-shim/shim.js | 22 ++++++++++++++----- .../test/non-trapping-shim.test.js | 1 + yarn.lock | 1 + 5 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 packages/non-trapping-shim/prepare-enable-shim.js diff --git a/packages/non-trapping-shim/package.json b/packages/non-trapping-shim/package.json index 315acd9aed..787db3bf88 100644 --- a/packages/non-trapping-shim/package.json +++ b/packages/non-trapping-shim/package.json @@ -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": { @@ -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", diff --git a/packages/non-trapping-shim/prepare-enable-shim.js b/packages/non-trapping-shim/prepare-enable-shim.js new file mode 100644 index 0000000000..6e0a069b5f --- /dev/null +++ b/packages/non-trapping-shim/prepare-enable-shim.js @@ -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'; diff --git a/packages/non-trapping-shim/shim.js b/packages/non-trapping-shim/shim.js index d6f4837849..dbcf001d16 100644 --- a/packages/non-trapping-shim/shim.js +++ b/packages/non-trapping-shim/shim.js @@ -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; +} diff --git a/packages/non-trapping-shim/test/non-trapping-shim.test.js b/packages/non-trapping-shim/test/non-trapping-shim.test.js index 6c4695aa8e..aa004cebec 100644 --- a/packages/non-trapping-shim/test/non-trapping-shim.test.js +++ b/packages/non-trapping-shim/test/non-trapping-shim.test.js @@ -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. diff --git a/yarn.lock b/yarn.lock index a23c803f13..f21811980d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"