10.4.2 Entering Eval Code

2010-05-11

Code: (Meta Ecma)
function EnterEvalCode(C) {
// if there is no calling context or execution not directly by eval call
var curCtx = global.arrExecutionContexts[global.arrExecutionContexts.length-1];
var callingCtx = global.arrExecutionContexts[global.arrExecutionContexts.length-2];
if (curCtx == global.ExecutionContext && curCtx.invokedBy == 'eval') {
InitGlobalExecutionContext(C, curCtx);
} else {
curCtx.ThisBinding = callingCtx.ThisBinding;
curCtx.LexicalEnvironment = callingCtx.LexicalEnvironment;
curCtx.VariableEnvironment = callingCtx.VariableEnvironment;
}

if (curCtx.strict) {
var newCtx = NewDeclarativeEnvironment(curCtx.LexicalEnvironment);
curCtx.LexicalEnvironment = newCtx;
curCtx.VariableEnvironment = newCtx;
}
DeclarationBindingInstantiation(C);
}


As explained in 10.4.2.1 (and demonstrated above) eval calls in strict mode have no way of defining new variables (so no functions either) in the context of the code that invoked the eval code. This prevents eval from being abused in several classic ways. When defining new variables in strict eval code they are only (directly) accessible for that code, while eval'ed. Of course it's still possible to access the parent context. And this restriction does not prevent strict eval code from accessing the global object directly.