Hacker News new | ask | show | jobs
by jjs 6060 days ago
Why can't scope be resolved at compile-time?

I realize that things like window.x can be added at runtime, but local scopes ought to be simple to rule out.... as long as there are no calls to eval() .....

eval(), which can introduce local bindings at runtime from arbitrary, runtime-constructed strings....

Ugh.

And since aliases to eval can also be created, in any scope, from other dynamically eval()'d code strings, you can't even be sure that any nonlocal symbol won't resolve to eval.

So the only way you can be sure that a level of scope can be skipped in the lookup chain is if there are no function calls made on any redefinable nonlocal symbols.

If there were a subset of javascript where eval was a keyword instead of an identifier, then this would be easier.

2 comments

If you don't use eval, with, and a couple of other things, V8 and most of the other good engines will actually optimize that scope level somewhat. Introducing eval, with, etc will make that scope level and any more therein to be much more dynamic/slow.
"local scopes ought to be simple to rule out"

If it's easy to do, it is probably only a matter of time until the major implementations include it. They seem to be competing on speed, after all.