Created
July 28, 2012 00:16
-
-
Save ityonemo/3191167 to your computer and use it in GitHub Desktop.
Javascript mayhem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function.prototype.__defineGetter__("head",function(){return this.toString().split("{")[0].trim();}); | |
Function.prototype.__defineGetter__("body",function(){return this.toString().split("{").slice(1).join("{").slice(0,-1).trim();}); | |
Function.prototype.__defineGetter__("args",function(){return this.toString().split("{")[0].match(/\([\w\s\,]*\)/)[0].slice(1,-1).split(",");}); | |
Function.prototype.__defineGetter__("stmts",function() | |
{ | |
var str = this.body; | |
var stmtlist = []; | |
var pcount = 0; | |
var lastpos = str.length - 1; | |
for (var i = str.length - 1; i >= 0; i--) | |
{ | |
switch (str[i]) | |
{ | |
case ";": | |
if (!pcount) | |
{ | |
var s = (str.slice(i + 1,lastpos)).trim(); | |
if (s) | |
{ | |
stmtlist.unshift(s); | |
lastpos = i + 1; | |
} | |
} | |
break; | |
case "}": | |
if (!pcount) | |
{ | |
var s = (str.slice(i + 1,lastpos)).trim(); | |
if (s) | |
{ | |
stmtlist.unshift(s); | |
lastpos = i + 1; | |
} | |
} | |
pcount ++; | |
break; | |
case "{": | |
pcount --; | |
break; | |
} | |
} | |
stmtlist.unshift(str.slice(0, lastpos)); | |
return stmtlist; | |
}); | |
Function.prototype.__defineGetter__("freevars",function() | |
{ | |
var fvlist = [] | |
var str = this.body | |
var endpos = this.length - 1; | |
for (var i = str.length - 1; i >= 0; i--) | |
{ | |
if (str[i] == ";") //then reset the position. | |
endpos = i; | |
else if (str.substr(i,4).search(/var\s/) == 0) | |
{ | |
var a = str.slice(i+4, endpos).split(","); | |
for (j = 0; j < a.length; j++) | |
fvlist.unshift(a[j].trim().match(/^\w+/)[0]); | |
} | |
} | |
return fvlist; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"stmts" and "freevars" need to be modified to take into account comments and string embeddings.