|
1 |
| -import eventualSendBundle from './bundles/eventual-send'; |
2 |
| - |
3 |
| -function makeEventualSendTransformer(parser, generate) { |
4 |
| - let HandledPromise; |
5 |
| - let evaluateProgram; |
6 |
| - let myRequire; |
7 |
| - let recursive = false; |
8 |
| - const transform = { |
9 |
| - closeOverSES(s) { |
10 |
| - // FIXME: This will go away when we can bundle an @agoric/harden that understands SES. |
11 |
| - myRequire = name => { |
12 |
| - if (name === '@agoric/harden') { |
13 |
| - return s.global.SES.harden; |
14 |
| - } |
15 |
| - throw Error(`Unrecognized require ${name}`); |
16 |
| - }; |
17 |
| - // FIXME: This should be replaced with ss.evaluateProgram support in SES. |
18 |
| - evaluateProgram = (src, endowments = {}) => s.evaluate(src, endowments); |
19 |
| - }, |
20 |
| - rewrite(ss) { |
21 |
| - const source = ss.src; |
22 |
| - const endowments = ss.endowments || {}; |
23 |
| - if (!recursive && !('HandledPromise' in endowments)) { |
24 |
| - // Use a getter to postpone initialization. |
25 |
| - Object.defineProperty(endowments, 'HandledPromise', { |
26 |
| - get() { |
27 |
| - if (!HandledPromise) { |
28 |
| - // Get a HandledPromise endowment for the evaluator. |
29 |
| - // It will be hardened in the evaluator's context. |
30 |
| - const nestedEvaluate = src => |
31 |
| - (evaluateProgram || ss.evaluateProgram)(src, { |
32 |
| - require: myRequire || require, |
33 |
| - nestedEvaluate, |
34 |
| - }); |
35 |
| - const { |
36 |
| - source: evSendSrc, |
37 |
| - moduleFormat, |
38 |
| - sourceMap, |
39 |
| - } = eventualSendBundle; |
40 |
| - if ( |
41 |
| - moduleFormat === 'getExport' || |
42 |
| - moduleFormat === 'nestedEvaluate' |
43 |
| - ) { |
44 |
| - recursive = true; |
45 |
| - try { |
46 |
| - const ns = nestedEvaluate(`(${evSendSrc}\n${sourceMap})`)(); |
47 |
| - HandledPromise = ns.HandledPromise; |
48 |
| - } finally { |
49 |
| - recursive = false; |
50 |
| - } |
51 |
| - } else { |
52 |
| - throw Error(`Unrecognized moduleFormat ${moduleFormat}`); |
53 |
| - } |
54 |
| - } |
55 |
| - return HandledPromise; |
56 |
| - }, |
57 |
| - }); |
58 |
| - } |
59 |
| - |
60 |
| - // Parse with eventualSend enabled, rewriting to |
61 |
| - // HandledPromise.get/applyFunction/applyMethod(...) |
62 |
| - const parseFunc = parser.parse; |
63 |
| - const ast = (parseFunc || parser)(source, { |
64 |
| - plugins: ['eventualSend'], |
65 |
| - }); |
66 |
| - // Create the source from the ast. |
67 |
| - const output = generate(ast, { retainLines: true }, source); |
68 |
| - |
69 |
| - // Work around Babel appending semicolons. |
70 |
| - const maybeSource = output.code; |
71 |
| - const actualSource = |
72 |
| - ss.sourceType === 'expression' && |
73 |
| - maybeSource.endsWith(';') && |
74 |
| - !source.endsWith(';') |
75 |
| - ? maybeSource.slice(0, -1) |
76 |
| - : maybeSource; |
77 |
| - |
78 |
| - return { |
79 |
| - ...ss, |
80 |
| - ast, |
81 |
| - endowments, |
82 |
| - src: actualSource, |
83 |
| - }; |
84 |
| - }, |
85 |
| - }; |
86 |
| - |
87 |
| - return [transform]; |
| 1 | +export function makeTransform(parser, generate) { |
| 2 | + function transform(source) { |
| 3 | + // Parse with eventualSend enabled, rewriting to |
| 4 | + // HandledPromise.get/applyFunction/applyMethod(...). Whoever finally |
| 5 | + // evaluates this code must provide a HandledPromise object, probably as |
| 6 | + // an endowment. |
| 7 | + const parseFunc = parser.parse; |
| 8 | + const ast = (parseFunc || parser)(source, { |
| 9 | + sourceType: 'module', |
| 10 | + plugins: ['eventualSend'], |
| 11 | + }); |
| 12 | + // Create the source from the ast. |
| 13 | + const output = generate(ast, { retainLines: true }, source); |
| 14 | + return output.code; |
| 15 | + } |
| 16 | + return transform; |
88 | 17 | }
|
89 |
| - |
90 |
| -export default makeEventualSendTransformer; |
0 commit comments