Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e720250

Browse files
committed
feat(ses): anticipate interator helpers
1 parent 0dde19e commit e720250

7 files changed

Lines changed: 185 additions & 2 deletions

packages/ses/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"ava": "^5.3.0",
7070
"babel-eslint": "^10.0.3",
7171
"c8": "^7.14.0",
72+
"core-js": "^3.31.0",
7273
"eslint": "^8.42.0",
7374
"eslint-config-airbnb-base": "^15.0.0",
7475
"eslint-config-prettier": "^8.8.0",

packages/ses/src/get-anonymous-intrinsics.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
matchAllRegExp,
1414
matchAllSymbol,
1515
regexpPrototype,
16+
globalThis,
1617
} from './commons.js';
1718
import { InertCompartment } from './compartment-shim.js';
1819

@@ -134,5 +135,25 @@ export const getAnonymousIntrinsics = () => {
134135
'%InertCompartment%': InertCompartment,
135136
};
136137

138+
if (globalThis.Iterator) {
139+
intrinsics['%IteratorHelperPrototype%'] = getPrototypeOf(
140+
// eslint-disable-next-line @endo/no-polymorphic-call
141+
globalThis.Iterator.from([]).take(0));
142+
intrinsics['%WrapForValidIteratorPrototype%'] = getPrototypeOf(
143+
// eslint-disable-next-line @endo/no-polymorphic-call
144+
globalThis.Iterator.from({ next() {} }),
145+
);
146+
}
147+
148+
if (globalThis.AsyncInterator) {
149+
intrinsics['%AsyncIteratorHelperPrototype%'] = getPrototypeOf(
150+
// eslint-disable-next-line @endo/no-polymorphic-call
151+
globalThis.AsyncIterator.from([]).take(0));
152+
intrinsics['%WrapForValidAsyncIteratorPrototype%'] = getPrototypeOf(
153+
// eslint-disable-next-line @endo/no-polymorphic-call
154+
globalThis.AsyncIterator.from({ next() {} }),
155+
);
156+
}
157+
137158
return intrinsics;
138159
};

packages/ses/src/permits-intrinsics.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ export default function whitelistIntrinsics(
253253
* Visit all properties for a permit.
254254
*/
255255
function visitProperties(path, obj, permit) {
256-
if (obj === undefined) {
256+
if (obj === undefined || obj === null) {
257257
return;
258258
}
259259

packages/ses/src/permits.js

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ export const universalPropertyNames = {
7676
URIError: 'URIError',
7777
WeakMap: 'WeakMap',
7878
WeakSet: 'WeakSet',
79+
// https://github.com/tc39/proposal-iterator-helpers
80+
Iterator: 'Iterator',
81+
// https://github.com/tc39/proposal-async-iterator-helpers
82+
AsyncIterator: 'AsyncIterator',
7983

8084
// *** Other Properties of the Global Object
8185

@@ -246,7 +250,7 @@ export const FunctionInstance = {
246250
};
247251

248252
// AsyncFunction Instances
249-
const AsyncFunctionInstance = {
253+
export const AsyncFunctionInstance = {
250254
// This property is not mentioned in ECMA 262, but is present in V8 and
251255
// necessary for lockdown to succeed.
252256
'[[Proto]]': '%AsyncFunctionPrototype%',
@@ -1233,14 +1237,90 @@ export const permitted = {
12331237

12341238
// *** Control Abstraction Objects
12351239

1240+
// https://github.com/tc39/proposal-iterator-helpers
1241+
Iterator: {
1242+
// Properties of the Iterator Constructor
1243+
'[[Proto]]': '%FunctionPrototype%',
1244+
prototype: '%IteratorPrototype%',
1245+
from: fn,
1246+
},
1247+
12361248
'%IteratorPrototype%': {
12371249
// The %IteratorPrototype% Object
12381250
'@@iterator': fn,
1251+
// https://github.com/tc39/proposal-iterator-helpers
1252+
constructor: 'Iterator',
1253+
map: fn,
1254+
filter: fn,
1255+
take: fn,
1256+
drop: fn,
1257+
flatMap: fn,
1258+
reduce: fn,
1259+
toArray: fn,
1260+
forEach: fn,
1261+
some: fn,
1262+
every: fn,
1263+
find: fn,
1264+
'@@toStringTag': 'string',
1265+
// https://github.com/tc39/proposal-async-iterator-helpers
1266+
toAsync: fn,
1267+
},
1268+
1269+
// https://github.com/tc39/proposal-iterator-helpers
1270+
'%WrapForValidIteratorPrototype%': {
1271+
'[[Proto]]': '%IteratorPrototype%',
1272+
next: fn,
1273+
return: fn,
1274+
},
1275+
1276+
// https://github.com/tc39/proposal-iterator-helpers
1277+
'%IteratorHelperPrototype%': {
1278+
'[[Proto]]': '%IteratorPrototype%',
1279+
next: fn,
1280+
return: fn,
1281+
'@@toStringTag': 'string',
1282+
},
1283+
1284+
// https://github.com/tc39/proposal-async-iterator-helpers
1285+
AsyncIterator: {
1286+
// Properties of the Iterator Constructor
1287+
'[[Proto]]': '%FunctionPrototype%',
1288+
prototype: '%AsyncIteratorPrototype%',
1289+
from: fn,
12391290
},
12401291

12411292
'%AsyncIteratorPrototype%': {
12421293
// The %AsyncIteratorPrototype% Object
12431294
'@@asyncIterator': fn,
1295+
// https://github.com/tc39/proposal-async-iterator-helpers
1296+
constructor: 'AsyncIterator',
1297+
map: fn,
1298+
filter: fn,
1299+
take: fn,
1300+
drop: fn,
1301+
flatMap: fn,
1302+
reduce: fn,
1303+
toArray: fn,
1304+
forEach: fn,
1305+
some: fn,
1306+
every: fn,
1307+
find: fn,
1308+
'@@toStringTag': 'string',
1309+
},
1310+
1311+
// https://github.com/tc39/proposal-async-iterator-helpers
1312+
'%WrapForValidAsyncIteratorPrototype%': {
1313+
'[[Proto]]': '%AsyncIteratorPrototype%',
1314+
next: fn,
1315+
return: fn,
1316+
},
1317+
1318+
// https://github.com/tc39/proposal-async-iterator-helpers
1319+
'%AsyncIteratorHelperPrototype%': {
1320+
'[[Proto]]': 'Async%IteratorPrototype%',
1321+
next: fn,
1322+
return: fn,
1323+
'@@toStringTag': 'string',
12441324
},
12451325

12461326
'%InertGeneratorFunction%': {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// KLUDGE HAZARD The core-js shims are written as sloppy code
2+
// and so introduce sloppy functions.
3+
import 'core-js/actual/async-iterator/index.js';
4+
import test from 'ava';
5+
import '../index.js';
6+
7+
// KLUDGE HAZARD only for testing with the sloppy modules of the
8+
// core-js iterator shim.
9+
// We mutate the permits to tolerates the sloppy functions for testing
10+
// by sacrificing security. The caller and arguments properties of
11+
// sloppy functions violate ocap encapsulation rules.
12+
import { AsyncFunctionInstance } from '../src/permits.js';
13+
14+
AsyncFunctionInstance.arguments = {};
15+
AsyncFunctionInstance.caller = {};
16+
17+
// Skipped because the core-js shim seems to miss the
18+
// actual %AsyncIteratorPrototype%,
19+
// so it creates a new one, causing us to fail because lockdown correctly
20+
// detects the conflicting definitions.
21+
// TODO report the bug to core-js
22+
test.skip('shimmed async-iterator helpers', t => {
23+
lockdown();
24+
25+
const AsyncIteratorHelperPrototype = Object.getPrototypeOf(
26+
AsyncIterator.from([]).take(0),
27+
);
28+
t.assert(Object.isFrozen(AsyncIteratorHelperPrototype));
29+
30+
const WrapForValidAsyncIteratorPrototype = Object.getPrototypeOf(
31+
AsyncIterator.from({ async next() { return undefined; } }),
32+
);
33+
t.assert(Object.isFrozen(WrapForValidAsyncIteratorPrototype));
34+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// KLUDGE HAZARD The core-js shims are written as sloppy code
2+
// and so introduce sloppy functions.
3+
import 'core-js/actual/iterator/index.js';
4+
import test from 'ava';
5+
import '../index.js';
6+
7+
// KLUDGE HAZARD only for testing with the sloppy modules of the
8+
// core-js iterator shim.
9+
// We mutate the permits to tolerates the sloppy functions for testing
10+
// by sacrificing security. The caller and arguments properties of
11+
// sloppy functions violate ocap encapsulation rules.
12+
import { FunctionInstance } from '../src/permits.js';
13+
14+
FunctionInstance.arguments = {};
15+
FunctionInstance.caller = {};
16+
17+
test('shimmed iterator helpers', t => {
18+
lockdown();
19+
20+
t.deepEqual(
21+
(function* g(i) {
22+
// eslint-disable-next-line no-plusplus
23+
while (true) yield i++;
24+
})(1)
25+
.drop(1)
26+
.take(5)
27+
.filter(it => it % 2)
28+
.map(it => it ** 2)
29+
.toArray(),
30+
[9, 25],
31+
);
32+
33+
const IteratorHelperPrototype = Object.getPrototypeOf(
34+
Iterator.from([]).take(0),
35+
);
36+
t.assert(Object.isFrozen(IteratorHelperPrototype));
37+
38+
const WrapForValidIteratorPrototype = Object.getPrototypeOf(
39+
Iterator.from({ next() {} }),
40+
);
41+
t.assert(Object.isFrozen(WrapForValidIteratorPrototype));
42+
});

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4204,6 +4204,11 @@ core-js@^2.4.0:
42044204
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.12.tgz#d9333dfa7b065e347cc5682219d6f690859cc2ec"
42054205
integrity sha512-Kb2wC0fvsWfQrgk8HU5lW6U/Lcs8+9aaYcy4ZFc6DDlo4nZ7n70dEgE5rtR0oG6ufKDUnrwfWL1mXR5ljDatrQ==
42064206

4207+
core-js@^3.31.0:
4208+
version "3.31.0"
4209+
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.31.0.tgz#4471dd33e366c79d8c0977ed2d940821719db344"
4210+
integrity sha512-NIp2TQSGfR6ba5aalZD+ZQ1fSxGhDo/s1w0nx3RYzf2pnJxt7YynxFlFScP6eV7+GZsKO95NSjGxyJsU3DZgeQ==
4211+
42074212
42084213
version "1.0.2"
42094214
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"

0 commit comments

Comments
 (0)