Skip to content

Commit

Permalink
feat(transform-eventual-send): expose bare transform function too
Browse files Browse the repository at this point in the history
bundle-source will use this function, which doesn't include the endowments
and HandledPromise stuff that's in the default export (which is aimed at the
SES1 transformers list)
  • Loading branch information
warner committed May 12, 2020
1 parent 54f1b7e commit 0bb35eb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
33 changes: 23 additions & 10 deletions packages/transform-eventual-send/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import eventualSendBundle from './bundles/eventual-send';

export function makeTransform(parser, generate) {
function transform(source) {
// Parse with eventualSend enabled, rewriting to
// HandledPromise.get/applyFunction/applyMethod(...). Whoever finally
// evaluates this code must provide a HandledPromise object, probably as
// an endowment.
const parseFunc = parser.parse;
const ast = (parseFunc || parser)(source, {
sourceType: 'module',
plugins: ['eventualSend'],
});
// Create the source from the ast.
const output = generate(ast, { retainLines: true }, source);
return output.code;
}
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;
Expand Down Expand Up @@ -57,17 +79,9 @@ function makeEventualSendTransformer(parser, generate) {
});
}

// Parse with eventualSend enabled, rewriting to
// HandledPromise.get/applyFunction/applyMethod(...)
const parseFunc = parser.parse;
const ast = (parseFunc || parser)(source, {
plugins: ['eventualSend'],
});
// Create the source from the ast.
const output = generate(ast, { retainLines: true }, source);
const maybeSource = transformer(source);

// Work around Babel appending semicolons.
const maybeSource = output.code;
const actualSource =
ss.sourceType === 'expression' &&
maybeSource.endsWith(';') &&
Expand All @@ -77,7 +91,6 @@ function makeEventualSendTransformer(parser, generate) {

return {
...ss,
ast,
endowments,
src: actualSource,
};
Expand Down
15 changes: 15 additions & 0 deletions packages/transform-eventual-send/test/test-transformer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { test } from 'tape';
import * as babelParser from '@agoric/babel-parser';
import babelGenerate from '@babel/generator';
import { makeTransform } from '../src';

test('transformer', t => {
const source = `let p = bob~.foo(arg1, arg2);`;
const transformer = makeTransform(babelParser, babelGenerate);
const output = transformer(source);
t.equal(
output,
`let p = HandledPromise.applyMethod(bob, "foo", [arg1, arg2]);`,
);
t.end();
});

0 comments on commit 0bb35eb

Please sign in to comment.