Skip to content

Commit 3ceaac9

Browse files
committed
fixup! adapt to non-trapping shim
1 parent be32870 commit 3ceaac9

13 files changed

+93
-57
lines changed

packages/marshal/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"@endo/eventual-send": "workspace:^",
4747
"@endo/nat": "workspace:^",
4848
"@endo/pass-style": "workspace:^",
49-
"@endo/promise-kit": "workspace:^"
49+
"@endo/promise-kit": "workspace:^",
50+
"ses": "workspace:^"
5051
},
5152
"devDependencies": {
5253
"@endo/init": "workspace:^",

packages/marshal/src/encodeToCapData.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
// encodes to CapData, a JSON-representable data structure, and leaves it to
77
// the caller (`marshal.js`) to stringify it.
88

9+
import { X, Fail, q } from '@endo/errors';
10+
import { freezeOrSuppressTrapping } from 'ses/nonTrappingShimAdapter.js';
11+
912
import {
1013
passStyleOf,
1114
isErrorLike,
@@ -17,7 +20,6 @@ import {
1720
nameForPassableSymbol,
1821
passableSymbolForName,
1922
} from '@endo/pass-style';
20-
import { X, Fail, q } from '@endo/errors';
2123

2224
/** @import {Passable, RemotableObject} from '@endo/pass-style' */
2325
/** @import {Encoding, EncodingUnion} from './types.js' */
@@ -30,8 +32,6 @@ const {
3032
is,
3133
entries,
3234
fromEntries,
33-
// @ts-expect-error TS doesn't see this on ObjectConstructor
34-
suppressTrapping,
3535
} = Object;
3636

3737
/**
@@ -177,10 +177,11 @@ export const makeEncodeToCapData = (encodeOptions = {}) => {
177177
// We harden the entire capData encoding before we return it.
178178
// `encodeToCapData` requires that its input be Passable, and
179179
// therefore hardened.
180-
// The `suppressTrapping` here is needed anyway, because the `rest` is
180+
// The `freezeOrSuppressTrapping` here is needed anyway, because
181+
// the `rest` is
181182
// freshly constructed by the `...` above, and we're using it
182-
// as imput in another call to `encodeToCapData`.
183-
result.rest = encodeToCapDataRecur(suppressTrapping(rest));
183+
// as input in another call to `encodeToCapData`.
184+
result.rest = encodeToCapDataRecur(freezeOrSuppressTrapping(rest));
184185
}
185186
return result;
186187
}

packages/pass-style/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"@endo/env-options": "workspace:^",
3838
"@endo/errors": "workspace:^",
3939
"@endo/eventual-send": "workspace:^",
40-
"@endo/promise-kit": "workspace:^"
40+
"@endo/promise-kit": "workspace:^",
41+
"ses": "workspace:^"
4142
},
4243
"devDependencies": {
4344
"@endo/init": "workspace:^",

packages/pass-style/src/passStyle-helpers.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
/** @import {PassStyle} from './types.js' */
55

66
import { X, q } from '@endo/errors';
7+
import { isFrozenOrIsNonTrapping } from 'ses/nonTrappingShimAdapter.js';
78

89
const { isArray } = Array;
910
const { prototype: functionPrototype } = Function;
1011
const {
1112
getOwnPropertyDescriptor,
1213
getPrototypeOf,
1314
hasOwnProperty: objectHasOwnProperty,
14-
prototype: objectPrototype,
1515
isFrozen,
16-
// @ts-expect-error TS does not yet have `isNonTrapping` on ObjectConstructor
17-
isNonTrapping,
16+
prototype: objectPrototype,
1817
} = Object;
1918
const { apply } = Reflect;
2019
const { toStringTag: toStringTagSymbol } = Symbol;
@@ -169,7 +168,7 @@ const makeCheckTagRecord = checkProto => {
169168
CX(check)`A non-object cannot be a tagRecord: ${tagRecord}`)) &&
170169
(isFrozen(tagRecord) ||
171170
(!!check && CX(check)`A tagRecord must be frozen: ${tagRecord}`)) &&
172-
(isNonTrapping(tagRecord) ||
171+
(isFrozenOrIsNonTrapping(tagRecord) ||
173172
(!!check &&
174173
CX(check)`A tagRecord must be non-trapping: ${tagRecord}`)) &&
175174
(!isArray(tagRecord) ||

packages/pass-style/src/passStyleOf.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import { isPromise } from '@endo/promise-kit';
66
import { X, Fail, q, annotateError, makeError } from '@endo/errors';
7+
import { isFrozenOrIsNonTrapping } from 'ses/nonTrappingShimAdapter.js';
78
import { isObject, isTypedArray, PASS_STYLE } from './passStyle-helpers.js';
89

910
import { CopyArrayHelper } from './copyArray.js';
@@ -31,13 +32,7 @@ import { assertPassableString } from './string.js';
3132
/** @typedef {Exclude<PassStyle, PrimitiveStyle | "promise">} HelperPassStyle */
3233

3334
const { ownKeys } = Reflect;
34-
const {
35-
getOwnPropertyDescriptors,
36-
values,
37-
isFrozen,
38-
// @ts-expect-error TS does not yet have `isNonTrapping` on ObjectConstructor
39-
isNonTrapping,
40-
} = Object;
35+
const { isFrozen, getOwnPropertyDescriptors, values } = Object;
4136

4237
/**
4338
* @param {PassStyleHelper[]} passStyleHelpers
@@ -149,7 +144,7 @@ const makePassStyleOf = passStyleHelpers => {
149144
if (inner === null) {
150145
return 'null';
151146
}
152-
if (!isNonTrapping(inner)) {
147+
if (!isFrozenOrIsNonTrapping(inner)) {
153148
if (!isFrozen(inner)) {
154149
throw assert.fail(
155150
// TypedArrays get special treatment in harden()
@@ -186,7 +181,7 @@ const makePassStyleOf = passStyleHelpers => {
186181
return 'remotable';
187182
}
188183
case 'function': {
189-
if (!isNonTrapping(inner)) {
184+
if (!isFrozenOrIsNonTrapping(inner)) {
190185
if (!isFrozen(inner)) {
191186
throw Fail`Cannot pass non-frozen objects like ${inner}. Use harden()`;
192187
}

packages/pass-style/src/remotable.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference types="ses"/>
22

33
import { Fail, q } from '@endo/errors';
4+
import { isFrozenOrIsNonTrapping } from 'ses/nonTrappingShimAdapter.js';
45
import {
56
assertChecker,
67
canBeMethod,
@@ -26,8 +27,6 @@ const {
2627
getPrototypeOf,
2728
prototype: objectPrototype,
2829
getOwnPropertyDescriptors,
29-
// @ts-expect-error TS does not yet have `isNonTrapping` on ObjectConstructor
30-
isNonTrapping,
3130
} = Object;
3231

3332
/**
@@ -155,7 +154,7 @@ const checkRemotable = (val, check) => {
155154
if (confirmedRemotables.has(val)) {
156155
return true;
157156
}
158-
if (!isNonTrapping(val)) {
157+
if (!isFrozenOrIsNonTrapping(val)) {
159158
return (
160159
!!check && CX(check)`cannot serialize non-frozen objects like ${val}`
161160
);

packages/pass-style/src/safe-promise.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22

33
import { isPromise } from '@endo/promise-kit';
44
import { q } from '@endo/errors';
5+
import { isFrozenOrIsNonTrapping } from 'ses/nonTrappingShimAdapter.js';
56
import { assertChecker, hasOwnPropertyOf, CX } from './passStyle-helpers.js';
67

78
/** @import {Checker} from './types.js' */
89

9-
const {
10-
getPrototypeOf,
11-
getOwnPropertyDescriptor,
12-
// @ts-expect-error TS does not yet have `isNonTrapping` on ObjectConstructor
13-
isNonTrapping,
14-
} = Object;
10+
const { getPrototypeOf, getOwnPropertyDescriptor } = Object;
1511
const { ownKeys } = Reflect;
1612
const { toStringTag } = Symbol;
1713

@@ -93,7 +89,7 @@ const checkPromiseOwnKeys = (pr, check) => {
9389
if (
9490
typeof val === 'object' &&
9591
val !== null &&
96-
isNonTrapping(val) &&
92+
isFrozenOrIsNonTrapping(val) &&
9793
getPrototypeOf(val) === Object.prototype
9894
) {
9995
const subKeys = ownKeys(val);
@@ -137,7 +133,7 @@ const checkPromiseOwnKeys = (pr, check) => {
137133
*/
138134
const checkSafePromise = (pr, check) => {
139135
return (
140-
(isNonTrapping(pr) || CX(check)`${pr} - Must be frozen`) &&
136+
(isFrozenOrIsNonTrapping(pr) || CX(check)`${pr} - Must be frozen`) &&
141137
(isPromise(pr) || CX(check)`${pr} - Must be a promise`) &&
142138
(getPrototypeOf(pr) === Promise.prototype ||
143139
CX(check)`${pr} - Must inherit from Promise.prototype: ${q(

packages/pass-style/test/passStyleOf.test.js

+24-19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
import test from '@endo/ses-ava/prepare-endo.js';
33

44
import { q } from '@endo/errors';
5+
import {
6+
hardenOrSuppressTrapping,
7+
freezeOrSuppressTrapping,
8+
} from 'ses/nonTrappingShimAdapter.js';
59

610
import { passStyleOf } from '../src/passStyleOf.js';
711
import { Far, ToFarFunction } from '../src/make-far.js';
@@ -13,7 +17,7 @@ const harden = /** @type {import('ses').Harden & { isFake?: boolean }} */ (
1317
global.harden
1418
);
1519

16-
const { getPrototypeOf, defineProperty, suppressTrapping } = Object;
20+
const { getPrototypeOf, defineProperty } = Object;
1721
const { ownKeys } = Reflect;
1822

1923
test('passStyleOf basic success cases', t => {
@@ -113,8 +117,8 @@ test('some passStyleOf rejections', t => {
113117
* For testing purposes, makes a *non-frozen* TagRecord-like object with
114118
* non-enumerable PASS_STYLE and Symbol.toStringTag properties.
115119
* A valid Remotable must inherit from a valid TagRecord.
116-
* - Before stabilize/suppressTrapping, a valid TagRecord must be frozen.
117-
* - After stabilize/suppressTrapping, a valid TagRecord must also be
120+
* - Before stabilize/hardenOrSuppressTrapping, a valid TagRecord must be frozen.
121+
* - After stabilize/hardenOrSuppressTrapping, a valid TagRecord must also be
118122
* stable/non-trapping, for example, because it was hardened.
119123
*
120124
* @param {string} [tag]
@@ -196,15 +200,15 @@ test('passStyleOf testing remotables', t => {
196200
t.is(passStyleOf(Far('foo', () => 'far function')), 'remotable');
197201

198202
const tagRecord1 = harden(makeTagishRecord('Alleged: manually constructed'));
199-
const farObj1 = suppressTrapping({ __proto__: tagRecord1 });
203+
const farObj1 = hardenOrSuppressTrapping({ __proto__: tagRecord1 });
200204
t.is(passStyleOf(farObj1), 'remotable');
201205

202206
const tagRecord2 = makeTagishRecord('Alleged: tagRecord not hardened');
203207
/**
204208
* Do not freeze `tagRecord2` in order to test that an object with
205209
* a non-frozen __proto__ is not passable.
206210
*/
207-
const farObj2 = suppressTrapping({ __proto__: tagRecord2 });
211+
const farObj2 = freezeOrSuppressTrapping({ __proto__: tagRecord2 });
208212
if (harden.isFake) {
209213
t.is(passStyleOf(farObj2), 'remotable');
210214
} else {
@@ -215,23 +219,23 @@ test('passStyleOf testing remotables', t => {
215219
}
216220

217221
const tagRecord3 = harden(makeTagishRecord('Alleged: both manually frozen'));
218-
const farObj3 = suppressTrapping({ __proto__: tagRecord3 });
222+
const farObj3 = hardenOrSuppressTrapping({ __proto__: tagRecord3 });
219223
t.is(passStyleOf(farObj3), 'remotable');
220224

221225
const tagRecord4 = harden(makeTagishRecord('Remotable'));
222-
const farObj4 = suppressTrapping({ __proto__: tagRecord4 });
226+
const farObj4 = hardenOrSuppressTrapping({ __proto__: tagRecord4 });
223227
t.is(passStyleOf(farObj4), 'remotable');
224228

225229
const tagRecord5 = harden(makeTagishRecord('Not alleging'));
226-
const farObj5 = suppressTrapping({ __proto__: tagRecord5 });
230+
const farObj5 = hardenOrSuppressTrapping({ __proto__: tagRecord5 });
227231
t.throws(() => passStyleOf(farObj5), {
228232
message:
229233
/For now, iface "Not alleging" must be "Remotable" or begin with "Alleged: " or "DebugName: "; unimplemented/,
230234
});
231235

232236
const tagRecord6 = harden(makeTagishRecord('Alleged: manually constructed'));
233-
const farObjProto6 = suppressTrapping({ __proto__: tagRecord6 });
234-
const farObj6 = suppressTrapping({ __proto__: farObjProto6 });
237+
const farObjProto6 = hardenOrSuppressTrapping({ __proto__: tagRecord6 });
238+
const farObj6 = hardenOrSuppressTrapping({ __proto__: farObjProto6 });
235239
t.is(passStyleOf(farObj6), 'remotable', 'tagRecord grandproto is accepted');
236240

237241
// Our current agoric-sdk plans for far classes are to create a class-like
@@ -285,7 +289,7 @@ test('passStyleOf testing remotables', t => {
285289
const tagRecordA1 = harden(
286290
makeTagishRecord('Alleged: null-proto tagRecord proto', null),
287291
);
288-
const farObjA1 = suppressTrapping({ __proto__: tagRecordA1 });
292+
const farObjA1 = hardenOrSuppressTrapping({ __proto__: tagRecordA1 });
289293
t.throws(
290294
() => passStyleOf(farObjA1),
291295
{ message: unusualTagRecordProtoMessage },
@@ -295,8 +299,8 @@ test('passStyleOf testing remotables', t => {
295299
const tagRecordA2 = harden(
296300
makeTagishRecord('Alleged: null-proto tagRecord grandproto', null),
297301
);
298-
const farObjProtoA2 = suppressTrapping({ __proto__: tagRecordA2 });
299-
const farObjA2 = suppressTrapping({ __proto__: farObjProtoA2 });
302+
const farObjProtoA2 = hardenOrSuppressTrapping({ __proto__: tagRecordA2 });
303+
const farObjA2 = hardenOrSuppressTrapping({ __proto__: farObjProtoA2 });
300304
t.throws(
301305
() => passStyleOf(farObjA2),
302306
{ message: unusualTagRecordProtoMessage },
@@ -310,10 +314,10 @@ test('passStyleOf testing remotables', t => {
310314
const fauxTagRecordB = harden(
311315
makeTagishRecord('Alleged: manually constructed', harden({})),
312316
);
313-
const farObjProtoB = suppressTrapping({
317+
const farObjProtoB = hardenOrSuppressTrapping({
314318
__proto__: fauxTagRecordB,
315319
});
316-
const farObjB = suppressTrapping({ __proto__: farObjProtoB });
320+
const farObjB = hardenOrSuppressTrapping({ __proto__: farObjProtoB });
317321
t.throws(() => passStyleOf(farObjB), {
318322
message:
319323
'cannot serialize Remotables with non-methods like "Symbol(passStyle)" in "[Alleged: manually constructed]"',
@@ -324,7 +328,7 @@ test('passStyleOf testing remotables', t => {
324328
);
325329
Object.defineProperty(farObjProtoWithExtra, 'extra', { value: () => {} });
326330
harden(farObjProtoWithExtra);
327-
const badFarObjExtraProtoProp = suppressTrapping({
331+
const badFarObjExtraProtoProp = hardenOrSuppressTrapping({
328332
__proto__: farObjProtoWithExtra,
329333
});
330334
t.throws(() => passStyleOf(badFarObjExtraProtoProp), {
@@ -389,7 +393,8 @@ test('remotables - safety from the gibson042 attack', t => {
389393
* explicitly make this non-trapping, which we cannot yet express.
390394
* @see https://github.com/endojs/endo/blob/master/packages/ses/docs/preparing-for-stabilize.md
391395
*/
392-
const makeInput = () => suppressTrapping({ __proto__: mercurialProto });
396+
const makeInput = () =>
397+
freezeOrSuppressTrapping({ __proto__: mercurialProto });
393398
const input1 = makeInput();
394399
const input2 = makeInput();
395400

@@ -450,12 +455,12 @@ test('Allow toStringTag overrides', t => {
450455
t.is(`${alice}`, '[object DebugName: Allison]');
451456
t.is(`${q(alice)}`, '"[DebugName: Allison]"');
452457

453-
const carol = suppressTrapping({ __proto__: alice });
458+
const carol = hardenOrSuppressTrapping({ __proto__: alice });
454459
t.is(passStyleOf(carol), 'remotable');
455460
t.is(`${carol}`, '[object DebugName: Allison]');
456461
t.is(`${q(carol)}`, '"[DebugName: Allison]"');
457462

458-
const bob = suppressTrapping({
463+
const bob = hardenOrSuppressTrapping({
459464
__proto__: carol,
460465
[Symbol.toStringTag]: 'DebugName: Robert',
461466
});
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {
2+
isFrozen,
3+
isNonTrapping as optIsNonTrapping,
4+
freeze,
5+
suppressTrapping as optSuppressTrapping,
6+
} from './src/commons.js';
7+
8+
/**
9+
* Local alias of `isFrozen` to eventually be switched to whatever tests
10+
* the non-trapping integrity trait.
11+
*/
12+
export const isFrozenOrIsNonTrapping = optIsNonTrapping || isFrozen;
13+
14+
/**
15+
* Local alias of `harden` to eventually be switched to whatever applies
16+
* the suppress-trapping integrity trait.
17+
*/
18+
export const hardenOrSuppressTrapping = optSuppressTrapping || harden;
19+
20+
/**
21+
* Local alias of `freeze` to eventually be switched to whatever applies
22+
* the suppress-trapping integrity trait.
23+
*/
24+
export const freezeOrSuppressTrapping = optSuppressTrapping || freeze;

packages/ses/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"default": "./compartment-shim.js"
6868
},
6969
"./console-shim.js": "./console-shim.js",
70+
"./ses/nonTrappingShimAdapter.js": "./ses/nonTrappingShimAdapter.js",
7071
"./package.json": "./package.json"
7172
},
7273
"scripts": {
@@ -86,7 +87,7 @@
8687
},
8788
"dependencies": {
8889
"@endo/env-options": "workspace:^",
89-
"@endo/non-trapping-shim": "^0.1.0"
90+
"@endo/non-trapping-shim": "workspace:^"
9091
},
9192
"devDependencies": {
9293
"@endo/compartment-mapper": "workspace:^",

packages/ses/src/commons.js

+2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ export const {
133133
preventExtensions: reflectPreventExtensions,
134134
set: reflectSet,
135135
// https://github.com/endojs/endo/pull/2673
136+
// @ts-expect-error TS does not yet have this on Reflect.
136137
isNonTrapping: reflectIsNonTrapping,
138+
// @ts-expect-error TS does not yet have this on Reflect.
137139
suppressTrapping: reflectSuppressTrapping,
138140
} = Reflect;
139141

0 commit comments

Comments
 (0)