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

Commit

Permalink
feat: support NewExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Mar 4, 2018
1 parent d85afbd commit aafd2b6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,16 @@ const evaluate_map = {
"&&": () => evaluate(node.left, scope) && evaluate(node.right, scope)
}[node.operator]();
},
ConditionalExpression: (node: types.ConditionalExpression, scope: Scope) => {
ConditionalExpression(node: types.ConditionalExpression, scope: Scope) {
return evaluate(node.test, scope)
? evaluate(node.consequent, scope)
: evaluate(node.alternate, scope);
},
NewExpression(node: types.NewExpression, scope: Scope) {
const func = evaluate(node.callee, scope);
Object.defineProperty(func, "length", {value: node.arguments.length});
const args = node.arguments.map(arg => evaluate(arg, scope));
return new (func.bind.apply(func, [null].concat(args)))();
}
};

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

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

test("NewExpression", t => {
const sandbox: any = vm.createContext({});

const output: any = vm.runInContext(
`
function People(name){
this.name = name;
}
module.exports = new People("axetroy");
module.exports.People = People;
`,
sandbox
);

t.deepEqual(output.People.length, 1);
t.deepEqual(output.name, "axetroy");
});

0 comments on commit aafd2b6

Please sign in to comment.