10.5 Declaration Binding Instantiation

2010-05-12

When your code is executed it has a Variable Lexical Environment associated with it. Any variables and functions you declare are recorded in this Variable Lexical Environment, this process is called "binding". For function code, the parameters are also bound to the Variable Lexical Environment Record.

When entering an Execution Context bindings are created in its VariableEnvironment as follows, using caller provided code and, if it's function code, argument list args.

Code: (Meta Ecma)
function DeclarationBindingInstantiation(code, args) {
var env = global.arrExecutionContexts[global.arrExecutionContexts.length-1];
var configurableBindings = (code.type == 'eval');
var strict = !!code.strict;
if (code.type == 'function') {
var func = code.func;
var names = func.[[FormalParameters]];
var argCount = args.length;
var n = 0;
var v, argAlreadyDeclared;
for (argName in names) {
n = n + 1;
if (n>argCount) v = undefined;
else v = args[n];

argAlreadyDeclared = env.HasBinding(argName);
if (!argAlreadyDeclared) env.CreateMutableBinding(argName);

env.SetMutableBinding(argName, v, strict);
}
}
var fn, fo, funcAlreadyDeclared;
for (f in code.arrFunctionDeclarations) {
fn = f.Identifier;
fo = (??) // whatever clause 13 does when instantiating a function declaration...
funcAlreadyDeclared = env.HasBinding(fn);
if (!funcAlreadyDeclared) env.CreateMutableBinding(fn, configurableBindings);
env.SetMutableBinding(fn, fo, strict);
}
var argumentsAlreadyDeclared = env.HasBinding('arguments');
if (code.type == 'function' && !argumentsAlreadyDeclared) {
var argsObj = CreateArgumentsObject(func, names, arg, env, strict); // see 10.6, the next paragraph
if (strict) {
// one of the few rare places where immutable bindings are used...
env.CreateImmutableBinding('arguments');
env.InitializeImmutableBinding('arguments', argsObj);
} else {
env.CreateMutableBinding('arguments');
env.SetMutableBinding('arguments', argsObj, false);
}
}
var dn, varAlreadyDeclared;
// code.arrVariableDeclarations holds both the regular and noin version...
for (d in code.arrVariableDeclarations) {
dn = d.Identifier;
varAlreadyDeclared = env.HasBinding(dn);
if (!varAlreadyDeclared) env.CreateMutableBinding(dn, configurableBindings);
env.SetMutableBinding(dn, undefined, strict);
}
}