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

Commit

Permalink
feat: support array expression
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Mar 4, 2018
1 parent 398706e commit 79a60cd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,13 @@ const evaluate_map = {
"++": v => ($var.$set(v + 1), prefix ? ++v : v++)
}[node.operator](evaluate(node.argument, scope));
},
ThisExpression: (node: types.ThisExpression, scope: Scope) => {
ThisExpression(node: types.ThisExpression, scope: Scope) {
const this_val = scope.$find("this");
return this_val ? this_val.$get() : null;
},
ArrayExpression(node: types.ArrayExpression, scope: Scope) {
return node.elements.map(item => evaluate(item, scope));
},
ObjectExpression(node: types.ObjectExpression, scope: Scope) {
const object = {};
for (const property of node.properties) {
Expand Down
22 changes: 22 additions & 0 deletions test/ArrayExpression.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import test from "ava";
import * as fs from "fs";

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

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

const arr: any = vm.runInContext(
`
const arr = [1, 2, 3];
arr.push(4);
module.exports = arr;
`,
sandbox
);

t.true(Array.isArray(arr));
t.deepEqual(arr.length, 4);
t.deepEqual(arr, [1, 2, 3, 4]);
});
2 changes: 1 addition & 1 deletion test/ThisExpression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ module.exports = t;

func.call(ctx);

t.true(ctx.name, "hello");
t.deepEqual(ctx.name, "hello");
});

0 comments on commit 79a60cd

Please sign in to comment.