Skip to content

Commit

Permalink
fix(transform-eventual-send): split out rewriter.js to fix cycle
Browse files Browse the repository at this point in the history
The old default export (used for SES1's evaluator transforms array) is now in
a separate file:

```js
import makeEventualSendTransformer from '@agoric/transform-eventual-send/src/rewriter';
```

The new smaller `makeTransform` function (used by bundle-source) is a named
export of the package:

```js
import { makeTransform } from '@agoric/transform-eventual-send';
```

This breaks the dependency cycle between bundle-source (which needs
`makeTransform`) and `makeEventualSendTransformer` (which needs a file
created at `yarn build` time by running bundle-source).
  • Loading branch information
warner committed May 13, 2020
1 parent 9b69286 commit 8a54d36
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 88 deletions.
2 changes: 1 addition & 1 deletion packages/default-evaluate-options/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parse } from '@agoric/babel-parser';
import generate from '@babel/generator';
import makeEventualSendTransformer from '@agoric/transform-eventual-send';
import makeEventualSendTransformer from '@agoric/transform-eventual-send/src/rewriter';

export default function makeDefaultEvaluateOptions() {
const shims = [];
Expand Down
86 changes: 0 additions & 86 deletions packages/transform-eventual-send/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import eventualSendBundle from './bundles/eventual-send';

export function makeTransform(parser, generate) {
function transform(source) {
// Parse with eventualSend enabled, rewriting to
Expand All @@ -17,87 +15,3 @@ export function makeTransform(parser, generate) {
}
return transform;
}

// this transformer is meant for the SES1 evaluation format, with mutable
// endowments and stuff

function makeEventualSendTransformer(parser, generate) {
const transformer = makeTransform(parser, generate);
let HandledPromise;
let evaluateProgram;
let myRequire;
let recursive = false;
const transform = {
closeOverSES(s) {
// FIXME: This will go away when we can bundle an @agoric/harden that understands SES.
myRequire = name => {
if (name === '@agoric/harden') {
return s.global.SES.harden;
}
throw Error(`Unrecognized require ${name}`);
};
// FIXME: This should be replaced with ss.evaluateProgram support in SES.
evaluateProgram = (src, endowments = {}) => s.evaluate(src, endowments);
},
rewrite(ss) {
const source = ss.src;
const endowments = ss.endowments || {};
if (!recursive && !('HandledPromise' in endowments)) {
// Use a getter to postpone initialization.
Object.defineProperty(endowments, 'HandledPromise', {
get() {
if (!HandledPromise) {
// Get a HandledPromise endowment for the evaluator.
// It will be hardened in the evaluator's context.
const nestedEvaluate = src =>
(evaluateProgram || ss.evaluateProgram)(src, {
require: myRequire || require,
nestedEvaluate,
});
const {
source: evSendSrc,
moduleFormat,
sourceMap,
} = eventualSendBundle;
if (
moduleFormat === 'getExport' ||
moduleFormat === 'nestedEvaluate'
) {
recursive = true;
try {
const ns = nestedEvaluate(`(${evSendSrc}\n${sourceMap})`)();
HandledPromise = ns.HandledPromise;
} finally {
recursive = false;
}
} else {
throw Error(`Unrecognized moduleFormat ${moduleFormat}`);
}
}
return HandledPromise;
},
});
}

const maybeSource = transformer(source);

// Work around Babel appending semicolons.
const actualSource =
ss.sourceType === 'expression' &&
maybeSource.endsWith(';') &&
!source.endsWith(';')
? maybeSource.slice(0, -1)
: maybeSource;

return {
...ss,
endowments,
src: actualSource,
};
},
};

return [transform];
}

export default makeEventualSendTransformer;
86 changes: 86 additions & 0 deletions packages/transform-eventual-send/src/rewriter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { makeTransform } from './index';
import eventualSendBundle from './bundles/eventual-send';

// this transformer is meant for the SES1 evaluation format, with mutable
// endowments and stuff

function makeEventualSendTransformer(parser, generate) {
const transformer = makeTransform(parser, generate);
let HandledPromise;
let evaluateProgram;
let myRequire;
let recursive = false;
const transform = {
closeOverSES(s) {
// FIXME: This will go away when we can bundle an @agoric/harden that understands SES.
myRequire = name => {
if (name === '@agoric/harden') {
return s.global.SES.harden;
}
throw Error(`Unrecognized require ${name}`);
};
// FIXME: This should be replaced with ss.evaluateProgram support in SES.
evaluateProgram = (src, endowments = {}) => s.evaluate(src, endowments);
},
rewrite(ss) {
const source = ss.src;
const endowments = ss.endowments || {};
if (!recursive && !('HandledPromise' in endowments)) {
// Use a getter to postpone initialization.
Object.defineProperty(endowments, 'HandledPromise', {
get() {
if (!HandledPromise) {
// Get a HandledPromise endowment for the evaluator.
// It will be hardened in the evaluator's context.
const nestedEvaluate = src =>
(evaluateProgram || ss.evaluateProgram)(src, {
require: myRequire || require,
nestedEvaluate,
});
const {
source: evSendSrc,
moduleFormat,
sourceMap,
} = eventualSendBundle;
if (
moduleFormat === 'getExport' ||
moduleFormat === 'nestedEvaluate'
) {
recursive = true;
try {
const ns = nestedEvaluate(`(${evSendSrc}\n${sourceMap})`)();
HandledPromise = ns.HandledPromise;
} finally {
recursive = false;
}
} else {
throw Error(`Unrecognized moduleFormat ${moduleFormat}`);
}
}
return HandledPromise;
},
});
}

const maybeSource = transformer(source);

// Work around Babel appending semicolons.
const actualSource =
ss.sourceType === 'expression' &&
maybeSource.endsWith(';') &&
!source.endsWith(';')
? maybeSource.slice(0, -1)
: maybeSource;

return {
...ss,
endowments,
src: actualSource,
};
},
};

return [transform];
}

export default makeEventualSendTransformer;
2 changes: 1 addition & 1 deletion packages/transform-eventual-send/test/ses-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import eventualSend from '@agoric/acorn-eventual-send';
import * as acorn from 'acorn';
import * as astring from 'astring';

import makeEventualSendTransformer from '../src';
import makeEventualSendTransformer from '../src/rewriter';

// FIXME: This should be unnecessary when SES has support
// for passing `evaluateProgram` through to the rewriter state.
Expand Down

0 comments on commit 8a54d36

Please sign in to comment.