JavaScript Toolkit
I would like to use JavaScript code to "register" the values of global variables. For example, I would like to know all the values that were determined by specific foo variables at runtime. Recording is not a problem.
What would be the easiest way to do this? I was thinking of using Rhino (a JavaScript implementation in Java created by Mozilla) to create an AST and modifying that AST.
source to share
This might be what you are looking for:
http://ejohn.org/blog/javascript-getters-and-setters/
Put the log into the installer and that's it. Of course, I don't know where this actually works, if it should be trusted code or any web page or what.
Based on John Resig's code, I would add something like this to my debug code:
var placeLoggerInSetter = function(variableName, obj){ //obj may be window
obj.__defineSetter__(variableName, function(val){
console.log(val); //log whatever here
obj[variableName] = val;
});
}
var something = 0;
placeLoggerInSetter("something", window);
something = 1;
//out goes "1" in the console
something = 2
//out goes "2" in the console
Is it more like this? Again, I haven't tested it and the line obj[variableName]
looked pretty recursive to me ... can you tell me here if it works? :)
Also, Firebug ,
console.dir(obj)
will allow you to see all the properties of that object in the console, instead of converting it to a string like in log()
. And also expand properties and so on. Handy.
source to share
If you're not limited to Rhino / Java, the Johnson Ruby gem integrates the Spidermonkey / Tracemonkey interpreter into Ruby. It provides access to the ACT. I have not tried modifying AST; not sure if this is possible (it might be, judging by the names in the code when looking at it).
Pearl Johnson is in the jbarnette Johnson repo on github. (Switch id, Stackoverflow doesn't want me to insert links at this point ...) I recently added tracemonkey support and is not yet integrated into the main source. This is in my (smparkes) Johnson repo on github.
source to share