From 79a60cd9e58b35b22fb1da045f9e6000b2b4ea2e Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 5 Mar 2018 03:21:42 +0800 Subject: [PATCH] feat: support array expression --- src/evaluate.ts | 5 ++++- test/ArrayExpression.test.ts | 22 ++++++++++++++++++++++ test/ThisExpression.test.ts | 2 +- 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 test/ArrayExpression.test.ts diff --git a/src/evaluate.ts b/src/evaluate.ts index ff47f0cb..3553db38 100644 --- a/src/evaluate.ts +++ b/src/evaluate.ts @@ -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) { diff --git a/test/ArrayExpression.test.ts b/test/ArrayExpression.test.ts new file mode 100644 index 00000000..12ed11cc --- /dev/null +++ b/test/ArrayExpression.test.ts @@ -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]); +}); diff --git a/test/ThisExpression.test.ts b/test/ThisExpression.test.ts index 3d848006..c01f95ce 100644 --- a/test/ThisExpression.test.ts +++ b/test/ThisExpression.test.ts @@ -22,5 +22,5 @@ module.exports = t; func.call(ctx); - t.true(ctx.name, "hello"); + t.deepEqual(ctx.name, "hello"); });