Skip to content

Commit

Permalink
typeof operator
Browse files Browse the repository at this point in the history
  • Loading branch information
alongubkin committed Nov 14, 2014
1 parent 06460e6 commit 24e4a58
Show file tree
Hide file tree
Showing 4 changed files with 815 additions and 642 deletions.
112 changes: 112 additions & 0 deletions lib/ast/expressions/UnaryExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,121 @@ exports.UnaryExpression.prototype.codegen = function () {
}

this.argument = this.argument.codegen();

// typeof operator should compile to:
// ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
if (this.operator === 'typeof') {
var typeofExpression = {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"computed": false,
"object": {
"type": "MemberExpression",
"computed": true,
"object": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"computed": false,
"object": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"computed": false,
"object": {
"type": "MemberExpression",
"computed": false,
"object": {
"type": "ObjectExpression",
"properties": []
},
"property": {
"type": "Identifier",
"name": "toString"
}
},
"property": {
"type": "Identifier",
"name": "call"
}
},
"arguments": [this.argument]
},
"property": {
"type": "Identifier",
"name": "match"
}
},
"arguments": [{
"type": "Literal",
"value": "/\\s([a-zA-Z]+)/",
"raw": "/\\s([a-zA-Z]+)/"
}]
},
"property": {
"type": "Literal",
"value": 1,
"raw": "1"
}
},
"property": {
"type": "Identifier",
"name": "toLowerCase"
}
},
"arguments": []
};

if (!this.argument.hasCallExpression()) {
this.type = "ConditionalExpression";
this.test = {
"type": "BinaryExpression",
"operator": "===",
"left": {
"type": "UnaryExpression",
"operator": "typeof",
"argument": this.argument,
"prefix": true
},
"right": {
"type": "Literal",
"value": "undefined",
"raw": "'undefined'"
}
};

this.consequent = {
"type": "Literal",
"value": "undefined"
};

this.alternate = typeofExpression;
} else {
this.type = typeofExpression.type;
this.callee = typeofExpression.callee;
Object.defineProperty(this, 'arguments', {
value: typeofExpression.arguments,
enumerable: true
});
}
}

return this;
};

exports.UnaryExpression.prototype.hasCallExpression = function () {
return (this.argument !== null && this.argument.hasCallExpression());
};

exports.UnaryExpression.getNextVariableName = function () {
if (!this.unaryExpressionIndex) {
this.unaryExpressionIndex = 0;
}

return "unaryExpression" + this.unaryExpressionIndex++;
};

exports.UnaryExpression.resetVariableNames = function () {
this.unaryExpressionIndex = 0;
};
Loading

0 comments on commit 24e4a58

Please sign in to comment.