-
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.
feat(eval): end-to-end metered evaluator
- Loading branch information
1 parent
d421bc5
commit db3acfd
Showing
14 changed files
with
201 additions
and
198 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 was deleted.
Oops, something went wrong.
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,39 @@ | ||
import { makeWithMeter } from './with'; | ||
import { makeMeterAndResetters } from './meter'; | ||
import { makeMeteringTransformer } from './transform'; | ||
|
||
export function makeMeteredEvaluator({ | ||
setGlobalMeter, | ||
makeEvaluator, | ||
babelCore, | ||
maxima, | ||
}) { | ||
const [meter, reset] = makeMeterAndResetters(maxima); | ||
const { meterId, 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; | ||
let returned; | ||
let exceptionBox = false; | ||
try { | ||
// Evaluate the source with the meter. | ||
returned = withMeter(() => ev.evaluate(src, { [meterId]: meter })); | ||
exhausted = false; | ||
} catch (e) { | ||
exceptionBox = [e]; | ||
exhausted = reset.isExhausted(); | ||
} | ||
return { | ||
exhausted, | ||
returned, | ||
exceptionBox, | ||
}; | ||
}; | ||
} |
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,3 +1,4 @@ | ||
export { makeMeteredEvaluator } from './evaluator'; | ||
export { makeMeter, makeMeterAndResetters } from './meter'; | ||
export { makeMeteringEndowments } from './endow'; | ||
export { makeMeteringTransformer } from './transform'; | ||
export { makeWithMeter } from './with'; |
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,13 @@ | ||
export function makeWithMeter(setGlobalMeter, defaultMeter = null) { | ||
const withMeter = (thunk, newMeter = defaultMeter) => { | ||
let oldMeter; | ||
try { | ||
oldMeter = setGlobalMeter(newMeter); | ||
return thunk(); | ||
} finally { | ||
setGlobalMeter(oldMeter); | ||
} | ||
}; | ||
const withoutMeter = thunk => withMeter(thunk, null); | ||
return { withMeter, withoutMeter }; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.