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

Commit

Permalink
feat: support new.target for function
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Mar 29, 2018
1 parent 900849b commit 6ab8f3c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,13 @@ const visitors: EvaluateMap = {
}
}
funcScope.const("this", this);
// support new.target
funcScope.const("new", {
target:
this && this.__proto__ && this.__proto__.constructor
? this.__proto__.constructor
: undefined
});
funcScope.const("arguments", arguments);
funcScope.isolated = false;

Expand Down Expand Up @@ -1471,7 +1478,8 @@ const visitors: EvaluateMap = {
return func(templateObject, ...expressionResultList);
},
MetaProperty(path) {
//
const obj = evaluate(path.createChild(path.node.meta));
return obj[path.node.property.name];
},
AwaitExpression(path) {
const { next } = path.ctx;
Expand Down
40 changes: 40 additions & 0 deletions test/ecma5/function/new.target.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import test from "ava";
import vm from "../../../src/vm";

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

const Person = vm.runInContext(
`
function Person(name){
return new.target;
}
module.exports = Person;
`,
sandbox
);

t.deepEqual(Person(), undefined);
});

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

const { Person, target } = vm.runInContext(
`
var target;
function Person(name){
target = new.target;
}
new Person();
module.exports = {target: target, Person: Person};
`,
sandbox
);

t.true(target === Person);
});

0 comments on commit 6ab8f3c

Please sign in to comment.