forked from Agoric/agoric-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(transform-eventual-send): split out rewriter.js to fix cycle
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
Showing
4 changed files
with
88 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters