-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Had to disable rethrown exceptions in the builtin wrappers. This is because just one throw is sufficient. Transformed code still throws at every meter call once any meter is exhausted.
- Loading branch information
1 parent
db3acfd
commit 15adc38
Showing
21 changed files
with
428 additions
and
210 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
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 |
---|---|---|
@@ -1,39 +1,62 @@ | ||
import { makeWithMeter } from './with'; | ||
import { makeMeterAndResetters } from './meter'; | ||
import { makeMeteringTransformer } from './transform'; | ||
|
||
export function makeMeteredEvaluator({ | ||
setGlobalMeter, | ||
replaceGlobalMeter, | ||
makeEvaluator, | ||
babelCore, | ||
maxima, | ||
quiesceCallback = cb => cb(), | ||
}) { | ||
const [meter, reset] = makeMeterAndResetters(maxima); | ||
const { meterId, meteringTransform } = makeMeteringTransformer(babelCore); | ||
const meteringTransform = makeMeteringTransformer(babelCore); | ||
const transforms = [meteringTransform]; | ||
const { withMeter } = makeWithMeter(setGlobalMeter, meter); | ||
|
||
const ev = makeEvaluator({ transforms }); | ||
|
||
return (src, endowments = {}) => { | ||
// Reset all meters to their defaults. | ||
Object.values(reset).forEach(r => r()); | ||
endowments[meterId] = meter; | ||
let exhausted = true; | ||
return (meter, srcOrThunk, endowments = {}, whenQuiesced = undefined) => { | ||
let returned; | ||
let exceptionBox = false; | ||
|
||
// Enable the specific meter. | ||
const savedMeter = replaceGlobalMeter(null); | ||
try { | ||
// Evaluate the source with the meter. | ||
returned = withMeter(() => ev.evaluate(src, { [meterId]: meter })); | ||
exhausted = false; | ||
if (whenQuiesced) { | ||
// Install the quiescence callback. | ||
quiesceCallback(() => { | ||
// console.log('quiescer exited'); | ||
replaceGlobalMeter(savedMeter); | ||
whenQuiesced({ | ||
exhausted: meter.isExhausted(), | ||
returned, | ||
exceptionBox, | ||
}); | ||
}); | ||
} | ||
|
||
if (typeof srcOrThunk === 'string') { | ||
// Transform the source on our own budget, then evaluate against the meter. | ||
endowments.getGlobalMeter = m => | ||
m === true ? meter : replaceGlobalMeter(m); | ||
returned = ev.evaluate(srcOrThunk, endowments); | ||
} else { | ||
// Evaluate the thunk with the specified meter. | ||
replaceGlobalMeter(meter); | ||
returned = srcOrThunk(); | ||
} | ||
} catch (e) { | ||
exceptionBox = [e]; | ||
exhausted = reset.isExhausted(); | ||
} | ||
return { | ||
exhausted, | ||
returned, | ||
exceptionBox, | ||
}; | ||
try { | ||
replaceGlobalMeter(savedMeter); | ||
const exhausted = meter.isExhausted(); | ||
return { | ||
exhausted, | ||
returned, | ||
exceptionBox, | ||
}; | ||
} finally { | ||
if (whenQuiesced) { | ||
// Keep going with the specified meter while we're quiescing. | ||
replaceGlobalMeter(meter); | ||
} | ||
} | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export { makeMeteredEvaluator } from './evaluator'; | ||
export { makeMeter, makeMeterAndResetters } from './meter'; | ||
export { makeMeter } from './meter'; | ||
export { makeMeteringTransformer } from './transform'; | ||
export { makeWithMeter } from './with'; |
Oops, something went wrong.