Yesterday I found bugs in one of our projects similar to bug #25951 [1] : when a variable is used without prior declaration with "var", it is implicitely attached to the the global scope (mandatory declaration of variables is a planned feature of JS 2.0 [2] but isn't available yet).

This is the cause of weird bugs when working with continuations that are very difficult to track down, as what the developper believes to be a local variable is actually a global variable shared by all continuations, just because of this missing "var".

We found badly declared variables by examining the global scope with the Rhino debugger. That's convenient, but doesn't allow early detection of such problems.

Early detection is possible though by "locking" the global Rhino scope when a function or continuation are called, preventing the creation of new global variables. That way, only global variables explicitely declared within the script (i.e. created a script load time) can be modified during the execution of a function or continuation.

I implemented this feature locally and it works beautifully. I firmly believe this should be the default as it prevents weird errors that can be very confusing.

The question is: do we want this to be configurable? Are there some valid use cases where one would like to create global variables that weren't previously declared. I can't find some.

Sylvain

[1] http://issues.apache.org/bugzilla/show_bug.cgi?id=25951
[2] http://www.mozilla.org/js/language/js20/core/pragmas.html#strict-mode

--
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }



Reply via email to