Skip to content
This repository has been archived by the owner on Sep 10, 2023. It is now read-only.

Commit

Permalink
feat: suppor logicExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Mar 4, 2018
1 parent 26d5e36 commit 36e5b7b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,12 @@ const evaluate_map = {
"^=": v => ($var.$set($var.$get() ^ v), $var.$get()),
"&=": v => ($var.$set($var.$get() & v), $var.$get())
}[node.operator](evaluate(node.right, scope));
},
LogicalExpression(node: types.LogicalExpression, scope: Scope) {
return {
"||": () => evaluate(node.left, scope) || evaluate(node.right, scope),
"&&": () => evaluate(node.left, scope) && evaluate(node.right, scope)
}[node.operator]();
}
};

Expand Down
56 changes: 56 additions & 0 deletions test/LogicalExpression.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import test from "ava";
import * as fs from "fs";

import vm from "../src/vm";

test("LogicalExpression-or-1", t => {
const sandbox: any = vm.createContext({});

const num: any = vm.runInContext(
`
module.exports = 0 || 2;
`,
sandbox
);

t.deepEqual(num, 2);
});

test("LogicalExpression-or-2", t => {
const sandbox: any = vm.createContext({});

const num: any = vm.runInContext(
`
module.exports = 1 || 2;
`,
sandbox
);

t.deepEqual(num, 1);
});

test("LogicalExpression-and-1", t => {
const sandbox: any = vm.createContext({});

const num: any = vm.runInContext(
`
module.exports = 1 && 2;
`,
sandbox
);

t.deepEqual(num, 2);
});

test("LogicalExpression-and-2", t => {
const sandbox: any = vm.createContext({});

const num: any = vm.runInContext(
`
module.exports = 0 && 2;
`,
sandbox
);

t.deepEqual(num, 0);
});

0 comments on commit 36e5b7b

Please sign in to comment.