|
| 1 | +import * as common from './runtime'; |
| 2 | +export * from './runtime'; |
| 3 | + |
| 4 | +class FudgedContinuationError { |
| 5 | + |
| 6 | + constructor(public v: any) { } |
| 7 | + |
| 8 | + toString() { |
| 9 | + return `FudgedContinuationError(${this.v})`; |
| 10 | + } |
| 11 | + |
| 12 | +} |
| 13 | + |
| 14 | +/** This runtime system doesn't actually implement any control operators. |
| 15 | + * Functions such as 'captureCC' are defined and will call their argument, |
| 16 | + * but don't save the stack. |
| 17 | + * |
| 18 | + * Unfortunately, all our program end by invoking the top continuation with |
| 19 | + * "done". Therefore, a program that runs correctly will terminate with |
| 20 | + * 'FudgedContinuationError(done)'. This is unfortunate. But, this |
| 21 | + * transformation still helps with debugging. |
| 22 | + */ |
| 23 | +export class FudgeRuntime extends common.Runtime { |
| 24 | + constructor() { |
| 25 | + super(); |
| 26 | + } |
| 27 | + |
| 28 | + captureCC(f: (k: any) => any): void { |
| 29 | + throw new common.Capture(f, []); |
| 30 | + } |
| 31 | + |
| 32 | + abortCC(f: () => any) { |
| 33 | + throw new common.Discard(f); |
| 34 | + } |
| 35 | + |
| 36 | + makeCont(stack: common.Stack) { |
| 37 | + return (v: any) => { |
| 38 | + throw new FudgedContinuationError(v); |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + runtime(body: () => any): any { |
| 43 | + try { |
| 44 | + body(); |
| 45 | + } |
| 46 | + catch (exn) { |
| 47 | + if (exn instanceof common.Capture) { |
| 48 | + return this.runtime(() => exn.f.call(global, this.makeCont(exn.stack))); |
| 49 | + } else if (exn instanceof common.Discard) { |
| 50 | + return this.runtime(() => exn.f()); |
| 51 | + } else { |
| 52 | + throw exn; // userland exception |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + handleNew(constr: any, ...args: any[]) { |
| 58 | + return new constr(...args); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +export default FudgeRuntime; |
0 commit comments