15.1.2.1 eval(x)

2010-07-03

mixed eval(x:mixed)

The eval function will accept any type of argument but it will only actually evaluate a string. When x is a string, the string will be evaluated as if it was a single Ecmascript Program. Note that any other type will immediately get the same value returned, unchanged.

Code: (Meta Ecma)
function eval(x){
if (Type(x) != 'string') return x;
var prog = parse(x); // todo: did i define something for this? should i?
if (!prog) throw SyntaxError; // see also clause 16 (?)
// todo: how did i do this? :p
var evalCtx = new ExecutionContext();
global.arrExectionContext[global.arrExectionContext.length] = evalCtx;
var result = evaluate(prog);
global.arrExectionContext.pop();
if (result.type == 'normal' && result.value !== undefined) return result.value;
if (result.type == 'normal' && !return.value) return undefined; // also if empty
// result.type must be 'throw'
throw result.value;
}

As you can see above, whenever you call eval an extra execution context is added as padding. So whatever variables you declare in your string, they will not persist after the call to eval (unless of course you declare them explicitly on the global object). However, you can access and change any variables already defined in the current scope chain.

As explained in 15.1.2.1.1 direct calls to eval should always use the initial eval function, regardless of what happened to global.eval.