3

I would like to instrument JavaScript code in order to "log" values of global variables. For example I would like to know all the values a particular variables foo had during execution. The logging is not the problem.

What would be the easiest way to implement this? I was thinking of using Rhino (the JavaScript implementation in Java created by Mozilla) to generate an AST and modifying this AST.

7 Answers 7

1

If you're not restricted to Rhino/Java, the Johnson Ruby gem integrates the Spidermonkey/Tracemonkey interpreter into Ruby. It does provide access to the AST. I haven't tried modifying the AST; not sure if that's possible (it might be, judging from the names in the code at looked at).

The Johnson gem is in jbarnette's Johnson repo at github. (Switch id's; Stackoverflow doesn't want me to put links in at this point ...) I added tracemonkey support recently and it's not yet integrated into the master source. It's in my (smparkes)'s Johnson repo at github.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Sadly, Ruby is not an option :(
1

This may be what you are looking for:

http://ejohn.org/blog/javascript-getters-and-setters/

Put a log in the setter and that's it. Of course, I do not know where does this actually work, if it must be trusted code, or any web page, or what.

Based on John Resig's code, I would add to my debug code something like:

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 that more like it? Again, I did not test it and the obj[variableName] line looks quite recursive to me... will you tell here if it works? :)

Also, with Firebug,

console.dir(obj)

will let you see all the properties of that object in the console, instead of converting it to string as with log(). And also expand the properties and so on. Handy.

4 Comments

Ok, thanks. But the real question is, how to insert this code automatically?
I'll edit this answer with a (totally untested) function to do what I think you want (and which would be, by the way, a totally sweet feature for firebug!)
If you meant to add this automatically to every variable and every property.... are you sure???
The code sample doesn't seem to work, "too much recursion". The setter keeps calling the setter or something like that.
0

Firebug (plugin for firefox) has a logging feature, to which you can write to a console. Much better than using alert("foo"); for everything

example log statement:

console.log("foo="+foo);

See this post for more info (including more advanced usage)

1 Comment

Sorry, but I'm not asking how to log stuff, I'm asking how to instrument Javascript code (to add logging automatically after each variable assignment for instance).
0

I have no idea for a generic solution or anything that exists, but simply wrap those variables around a logging class that deals with get/sets and stuff.

Comments

0

You can use Dtrace:

http://opensolaris.org/os/project/mozilla-dtrace/

Comments

0

You can set debugger; as first thing on the document ready event and just step through your code step by step.

..fredrik

Comments

0

Using the cvshead version of Rhino, I instrumented the code.

1 Comment

could you share some code of the basics of the way you did it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.