Skip to content

Commit

Permalink
fix(pointfree): update safeMode handling
Browse files Browse the repository at this point in the history
- actually disable checks if safeMode(false) is called
  • Loading branch information
postspectacular committed May 5, 2019
1 parent 2a9d076 commit d27bcba
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions packages/pointfree/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@ import {
StackProgram
} from "./api";

let SAFE = true;
let $: (stack: Stack, n: number) => void;
let $n: (m: number, n: number) => void;

export const safeMode = (state: boolean) => (SAFE = state);
export const safeMode = (state: boolean) => {
if (state) {
$n = (m: number, n: number) => m < n && illegalState(`stack underflow`);
$ = (stack: Stack, n: number) => $n(stack.length, n);
} else {
$ = $n = NO_OP;
}
};
safeMode(true);

/**
* Executes program / quotation with given stack context (initial D/R
Expand Down Expand Up @@ -86,14 +95,6 @@ export const ctx = (stack: Stack = [], env: StackEnv = {}): StackContext => [
env
];

const $n = SAFE
? (m: number, n: number) => m < n && <any>illegalState(`stack underflow`)
: NO_OP;

const $ = SAFE ? (stack: Stack, n: number) => $n(stack.length, n) : NO_OP;

export { $ as ensureStack, $n as ensureStackN };

const $stackFn = (f: StackProc) => (isArray(f) ? word(f) : f);

const tos = (stack: Stack) => stack[stack.length - 1];
Expand Down Expand Up @@ -1061,7 +1062,7 @@ export const keep = word([over, [exec], dip]);
export const keep2 = word([[dup2], dip, dip2]);

/**
* Call a quotation with two values on the stack, restoring the values
* Call a quotation with three values on the stack, restoring the values
* after quotation finished.
*
* ( x y z q -- .. x y z )
Expand Down Expand Up @@ -1809,3 +1810,5 @@ export const printds = (ctx: StackContext) => (console.log(ctx[0]), ctx);
export const printrs = (ctx: StackContext) => (console.log(ctx[1]), ctx);

export * from "./api";

export { $ as ensureStack, $n as ensureStackN };

0 comments on commit d27bcba

Please sign in to comment.