Closed Bug 595940 Opened 15 years ago Closed 10 years ago

eval doesn't parallel static restrictions

Categories

(Core :: JavaScript Engine, defect)

x86
macOS
defect
Not set
normal

Tracking

()

RESOLVED WORKSFORME

People

(Reporter: dherman, Unassigned)

References

(Blocks 1 open bug, )

Details

We statically reject certain problematic cases like a var hoisting past a let: js> function foo() { { let x; var x; } } typein:15: TypeError: redeclaration of let x: typein:15: function foo() { { let x; var x; } } typein:15: ..............................^ but dynamically, this is still allowed: js> function foo() { { let x; eval("var x;") } } This gets especially weird when you have initializers, since the var hoists outside the let, but the initializer mutates the let-bound variable. js> function foo() { var x = "untouched"; { let x = "temporary"; eval("var x = 'modified'"); print(x); } print(x); } js> foo() modified untouched How feasible would it be to parallel the static restrictions with dynamic restrictions inside eval code? Dave
A small thing to add: eval isn't aware of the let-scope at all: js> function foo() { { eval("let x = 'x'"); } print(x); } js> foo(); x
> A small thing to add: eval isn't aware of the let-scope at all: That's not accurate; eval is aware of the let-scope, but it converts its top-level lets into vars. > js> function foo() { > { > eval("let x = 'x'"); > } > print(x); > } > js> foo(); > x What's happening here is that what looks like a let is actually a var, and so it creates a function-hoisted binding for x. But you really can see the let-bindings from eval: js> function foo() { { let x = "x"; return eval("x") } } js> foo() x Dave
Ah I see, it's aware in the sense that it can do lookups in the scope chain but it doesn't seem to know that it was called from within a let-scope and its let bindings should not be converted to vars.
> it doesn't seem to know that it was called from within a let-scope and its let > bindings should not be converted to vars. I guess that "should" is subjective (and the idea that let should ever be turned into var is questionable in the first place), but that's the idea. A little more precisely: "global" means "syntactically global to the script being eval'ed," not "in a singleton scope chain." Dave
Assignee: general → nobody
Blocks: es6
I think shu has fixed this.
Status: NEW → RESOLVED
Closed: 10 years ago
Resolution: --- → WORKSFORME
You need to log in before you can comment on or make changes to this bug.