Strict mode eval

Oliver Hunt oliver at
Tue May 10 10:50:55 PDT 2011

On May 10, 2011, at 10:16 AM, Andreas Rossberg wrote:

> I'm a bit puzzled regarding the meaning of "use strict" in the context
> of higher-order uses of eval.
> Consider the following program. What is the expected result of the latter calls?
> ---------------------
> var x = 3
> function call_eval() { x = 3; eval("var x = 4"); return x }
sets global x to 3, eval operator introduces a local binding for x and sets it to 4.  We return the local binding (4) global x is 3

> function call_eval_strict() { "use strict"; x = 3; eval("var x = 4"); return x }
sets global x binding to 3, eval operator gets its own environment record with a binding for x, it sets it to 4, function returns value of global x binding
> function get_eval() { return eval }
> function get_eval_strict() { "use strict"; return eval }
Both return the same eval function (not the operator)

> function call_f(f) { x = 3; f("var x = 4"); return x }
> function call_f_strict(f) { "use strict"; x = 3; f("var x = 4"); return x }
> call_eval()  // 4
> call_eval_strict()  // 3

All your other calls are calls to the eval function, not the eval operator, and on those cases the containing environment record is the global scope.

> ---------------------
> V8 bleeding edge currently returns 4 for all of the latter calls, but
> that does not seem quite right to me. Especially for the last two
> cases
> that would practically amount to a loop hole in strict mode. But where
> does the spec say differently?

eval is a little gnarly in ES5, essentially there are two modes: eval the function and eval the operator.

Any piece of code (ignoring overrides/shadowing) that does

Is using the eval operator, in which case the containing environment record of the code executed by eval, is the environment record in which the call is made.

Any form of indirection to eval that leads to it being called by a name other than eval means you're calling the function form.  In the function form the containing environment record for the code executed by eval is the global env. record.

I can't recall off the top of my head whether 
function f(eval) { eval(...) }
will result in behaviour that's considered to be the operator or function form (i'can't recall whether th semantics define the env. record the resolved eval must be boun


> I'd be happy for any enlightenment.
> Thanks,
> /Andreas
> _______________________________________________
> es-discuss mailing list
> es-discuss at

More information about the es-discuss mailing list