I wrote a "Karel the robot" for teaching Javascript (and Python version using Brython). Instead of having only a mini-language, like the original Karel the robot, I want to give the user access to the entire Javascript language. In order to do so, I simply use eval() to evaluate the user program. (*See http://reeborg.ca/learn_js.html for my first version [which will be replaced by the newer version when it is completed] and http://reeborg.ca/learn_js_new_dev.html for a newer version. I'm writing a new version as the original one had a memory leak that made the browser crash.*)
It would be helpful, for enhancing the user experience, if I could keep track of objects/variables that are/have been created by the user script.
One crude way to do this that I can think of would be to pre-processs the script and try to identify any obvious variables (declared with the var keyword, or function) but I was wondering if there was an easy and more reliable way to do so without writing a javascript interpreter. (Perhaps hacking jshint/jslint and run it on the script to identify variables...)
Edit:
Here is the code where I actually do the evaluation. As it is done within a function (and enforcing the use of variable declaration), perhaps this makes it easier to accomplish what I'm hoping to do.
RUR.runner.eval_javascript = function (src) {
// Note: by having "use strict;" here, it has the interesting effect of requiring user
// programs to conform to "strict" usage, meaning that all variables have to be declared,
// etc.
"use strict"; // will propagate to user's code, enforcing good programming habits.
// lint, then eval
editorUpdateHints();
if(editor.widgets.length === 0) {
libraryUpdateHints();
if(library.widgets.length !== 0) {
$('#library-problem').show().fadeOut(4000);
}
}
RUR.reset_definitions();
eval(src); // jshint ignore:line
};