Skip to content

Commit

Permalink
fix(ses): fixup async iterator helpers (#1670)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhofman authored Jul 7, 2023
2 parents 7f2d29f + 0c56f2d commit 31298f7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/ses/src/get-anonymous-intrinsics.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const getAnonymousIntrinsics = () => {
);
}

if (globalThis.AsyncInterator) {
if (globalThis.AsyncIterator) {
intrinsics['%AsyncIteratorHelperPrototype%'] = getPrototypeOf(
// eslint-disable-next-line @endo/no-polymorphic-call
globalThis.AsyncIterator.from([]).take(0),
Expand Down
2 changes: 1 addition & 1 deletion packages/ses/src/permits.js
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,7 @@ export const permitted = {

// https://github.com/tc39/proposal-async-iterator-helpers
'%AsyncIteratorHelperPrototype%': {
'[[Proto]]': 'Async%IteratorPrototype%',
'[[Proto]]': '%AsyncIteratorPrototype%',
next: fn,
return: fn,
'@@toStringTag': 'string',
Expand Down
3 changes: 3 additions & 0 deletions packages/ses/test/core-js-configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import configurator from 'core-js/configurator.js';

configurator({ USE_FUNCTION_CONSTRUCTOR: true });
3 changes: 3 additions & 0 deletions packages/ses/test/enforce-cjs-strict.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import module from 'node:module';

module.wrapper[0] += '"use strict";';
37 changes: 17 additions & 20 deletions packages/ses/test/test-anticipate-async-iterator-helpers-shimmed.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
// KLUDGE HAZARD The core-js shims are written as sloppy code
// and so introduce sloppy functions.
import './enforce-cjs-strict.js';
import './core-js-configuration.js';
import 'core-js/actual/async-iterator/index.js';
import test from 'ava';
import '../index.js';
import './lockdown-safe.js';
import test from 'ava';

// KLUDGE HAZARD only for testing with the sloppy modules of the
// core-js iterator shim.
// We mutate the permits to tolerates the sloppy functions for testing
// by sacrificing security. The caller and arguments properties of
// sloppy functions violate ocap encapsulation rules.
import { AsyncFunctionInstance } from '../src/permits.js';

AsyncFunctionInstance.arguments = {};
AsyncFunctionInstance.caller = {};

// Skipped because the core-js shim seems to miss the
// actual %AsyncIteratorPrototype%,
// so it creates a new one, causing us to fail because lockdown correctly
// detects the conflicting definitions.
// TODO report the bug to core-js
test.skip('shimmed async-iterator helpers', t => {
lockdown();
test('shimmed async-iterator helpers', async t => {
t.deepEqual(
await (async function* g(i) {
// eslint-disable-next-line no-plusplus
while (true) yield i++;
})(1)
.drop(1)
.take(5)
.filter(it => it % 2)
.map(it => it ** 2)
.toArray(),
[9, 25],
);

const AsyncIteratorHelperPrototype = Object.getPrototypeOf(
AsyncIterator.from([]).take(0),
Expand Down
19 changes: 4 additions & 15 deletions packages/ses/test/test-anticipate-iterator-helpers-shimmed.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
// KLUDGE HAZARD The core-js shims are written as sloppy code
// and so introduce sloppy functions.
import './enforce-cjs-strict.js';
import './core-js-configuration.js';
import 'core-js/actual/iterator/index.js';
import test from 'ava';
import '../index.js';

// KLUDGE HAZARD only for testing with the sloppy modules of the
// core-js iterator shim.
// We mutate the permits to tolerates the sloppy functions for testing
// by sacrificing security. The caller and arguments properties of
// sloppy functions violate ocap encapsulation rules.
import { FunctionInstance } from '../src/permits.js';

FunctionInstance.arguments = {};
FunctionInstance.caller = {};
import './lockdown-safe.js';
import test from 'ava';

test('shimmed iterator helpers', t => {
lockdown();

t.deepEqual(
(function* g(i) {
// eslint-disable-next-line no-plusplus
Expand Down

0 comments on commit 31298f7

Please sign in to comment.