Skip to content

Commit

Permalink
hash无原型链
Browse files Browse the repository at this point in the history
  • Loading branch information
army8735 committed Apr 3, 2014
1 parent 38aa7d8 commit bcb1b60
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/parser/js/Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ var Context = Class(function(parent, name) {
this.parent = parent || null; //父上下文,如果是全局则为空
this.name = name || null; //上下文名称,即函数名,函数表达式为空,全局也为空
this.children = []; //函数声明或函数表达式所产生的上下文
this.childrenMap = {}; //键是函数名,值是上下文,匿名函数表达式键为cid
this.childrenMap = Object.create(null); //键是函数名,值是上下文,匿名函数表达式键为cid
this.vars = []; //变量var声明
this.varsMap = {}; //键为id字面量,值是它的token的节点
this.varsMap = Object.create(null); //键为id字面量,值是它的token的节点
this.params = []; //形参,函数上下文才有,即全局无
this.paramsMap = {}; //键为id字面量,值是它的token的节点
this.paramsMap = Object.create(null); //键为id字面量,值是它的token的节点
this.aParams = []; //实参,函数表达式才有
this.vids = []; //上下文环境里用到的变量id
this.vidsMap = {}; //键为id字面量,值是它的token的节点
this.vidsMap = Object.create(null); //键为id字面量,值是它的token的节点
this.returns = []; //上下文环境里return语句
this.node = null; //对应的ast的节点
if(!this.isTop()) {
Expand Down Expand Up @@ -50,14 +50,14 @@ var Context = Class(function(parent, name) {
return !this.isTop() && !this.name;
},
hasParam: function(p) {
return this.paramsMap.hasOwnProperty(p);
return !!this.paramsMap[p];
},
getParams: function() {
return this.params;
},
addParam: function(p) {
//形参不可能重复,无需判断
this.paramsMap[p] = this.params.length;
this.paramsMap[p] = true;
this.params.push(p);
return this;
},
Expand All @@ -76,7 +76,7 @@ var Context = Class(function(parent, name) {
},
//通过name查找函数声明,id查找表达式
hasChild: function(name) {
return this.childrenMap.hasOwnProperty(name);
return !!this.childrenMap[name];
},
addChild: function(child) {
var name = child.getName();
Expand All @@ -102,7 +102,7 @@ var Context = Class(function(parent, name) {
return this;
},
hasVar: function(v) {
return this.varsMap.hasOwnProperty(v);
return !!this.varsMap[v];
},
addVar: function(node, assign) {
var v = node.leaves()[0].token().content();
Expand Down Expand Up @@ -138,7 +138,7 @@ var Context = Class(function(parent, name) {
return this.returns;
},
hasVid: function(v) {
return this.vidsMap.hasOwnProperty(v);
return !!this.vidsMap[v];
},
getVid: function(v) {
return this.vidsMap[v];
Expand Down
5 changes: 5 additions & 0 deletions tests/jscontext.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ describe('jscontext', function() {
expect(first.hasVid('a')).to.be(false);
expect(first.hasVid('b')).to.be(true);
});
it('vid toString', function() {
var context = homunculus.getContext('js');
context.parse('toString.call(this)');
expect(context.hasVid('toString')).to.be(true);
});
it('#getVid', function() {
var context = homunculus.getContext('js');
context.parse('a = 1');
Expand Down

0 comments on commit bcb1b60

Please sign in to comment.