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

Commit

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

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

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

const func: any = vm.runInContext(
`
function t(){
this.name = "hello";
return this;
}
module.exports = t;
`,
sandbox
);

const ctx: any = {};

func.call(ctx);

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

0 comments on commit 398706e

Please sign in to comment.