10.2 ECMAScript Function Objects
ECMAScript
In addition to [[Extensible]] and [[Prototype]], ECMAScript
Internal Slot | Type | Description |
---|---|---|
[[Environment]] |
an |
The |
[[PrivateEnvironment]] |
a |
The |
[[FormalParameters]] |
a |
The root parse node of the source text that defines the function's formal parameter list. |
[[ECMAScriptCode]] |
a |
The root parse node of the source text that defines the function's body. |
[[ConstructorKind]] |
|
Whether or not the function is a derived class |
[[Realm]] |
a |
The |
[[ScriptOrModule]] |
a |
The script or module in which the function was created. |
[[ThisMode]] |
|
Defines how this references are interpreted within the formal parameters and code body of the function. this refers to the |
[[Strict]] | a Boolean |
|
[[HomeObject]] | an Object |
If the function uses super , this is the object whose [[GetPrototypeOf]] provides the object where super property lookups begin.
|
[[SourceText]] | a sequence of Unicode code points |
The |
[[Fields]] |
a |
If the function is a class, this is a list of |
[[PrivateMethods]] |
a |
If the function is a class, this is a list representing the non-static private methods and accessors of the class. |
[[ClassFieldInitializerName]] |
a String, a Symbol, a |
If the function is created as the initializer of a class field, the name to use for |
[[IsClassConstructor]] | a Boolean |
Indicates whether the function is a class |
All ECMAScript
10.2.1 [[Call]] ( thisArgument, argumentsList )
The [[Call]] internal method of an ECMAScript
- Let callerContext be the
running execution context . - Let calleeContext be
PrepareForOrdinaryCall (F,undefined ). Assert : calleeContext is now therunning execution context .- If F.[[IsClassConstructor]] is
true , then- Let error be a newly created
TypeError object. - NOTE: error is created in calleeContext with F's associated
Realm Record . - Remove calleeContext from the
execution context stack and restore callerContext as therunning execution context . - Return
ThrowCompletion (error).
- Let error be a newly created
- Perform
OrdinaryCallBindThis (F, calleeContext, thisArgument). - Let result be
Completion (OrdinaryCallEvaluateBody (F, argumentsList)). - Remove calleeContext from the
execution context stack and restore callerContext as therunning execution context . - If result is a
return completion , return result.[[Value]]. ReturnIfAbrupt (result).- Return
undefined .
When calleeContext is removed from the
10.2.1.1 PrepareForOrdinaryCall ( F, newTarget )
The abstract operation PrepareForOrdinaryCall takes arguments F (an ECMAScript
- Let callerContext be the
running execution context . - Let calleeContext be a new
ECMAScript code execution context . - Set the Function of calleeContext to F.
- Let calleeRealm be F.[[Realm]].
- Set the
Realm of calleeContext to calleeRealm. - Set the ScriptOrModule of calleeContext to F.[[ScriptOrModule]].
- Let localEnv be
NewFunctionEnvironment (F, newTarget). - Set the LexicalEnvironment of calleeContext to localEnv.
- Set the VariableEnvironment of calleeContext to localEnv.
- Set the PrivateEnvironment of calleeContext to F.[[PrivateEnvironment]].
- If callerContext is not already suspended, suspend callerContext.
- Push calleeContext onto the
execution context stack ; calleeContext is now therunning execution context . - NOTE: Any exception objects produced after this point are associated with calleeRealm.
- Return calleeContext.
10.2.1.2 OrdinaryCallBindThis ( F, calleeContext, thisArgument )
The abstract operation OrdinaryCallBindThis takes arguments F (an ECMAScript
- Let thisMode be F.[[ThisMode]].
- If thisMode is
lexical , returnunused . - Let calleeRealm be F.[[Realm]].
- Let localEnv be the LexicalEnvironment of calleeContext.
- If thisMode is
strict , then- Let thisValue be thisArgument.
- Else,
- If thisArgument is either
undefined ornull , then- Let globalEnv be calleeRealm.[[GlobalEnv]].
Assert : globalEnv is aGlobal Environment Record .- Let thisValue be globalEnv.[[GlobalThisValue]].
- Else,
- If thisArgument is either
Assert : localEnv is aFunction Environment Record .Assert : The next step never returns anabrupt completion because localEnv.[[ThisBindingStatus]] is notinitialized .- Perform ! localEnv.BindThisValue(thisValue).
- Return
unused .
10.2.1.3 Runtime Semantics: EvaluateBody
The
- Return ?
EvaluateFunctionBody ofFunctionBody with arguments functionObject and argumentsList.
- Return ?
EvaluateConciseBody ofConciseBody with arguments functionObject and argumentsList.
- Return ?
EvaluateGeneratorBody ofGeneratorBody with arguments functionObject and argumentsList.
- Return ?
EvaluateAsyncGeneratorBody ofAsyncGeneratorBody with arguments functionObject and argumentsList.
- Return ?
EvaluateAsyncFunctionBody ofAsyncFunctionBody with arguments functionObject and argumentsList.
- Return ?
EvaluateAsyncConciseBody ofAsyncConciseBody with arguments functionObject and argumentsList.
Assert : argumentsList is empty.Assert : functionObject.[[ClassFieldInitializerName]] is notempty .- If
IsAnonymousFunctionDefinition (AssignmentExpression ) istrue , then- Let value be ?
NamedEvaluation ofInitializer with argument functionObject.[[ClassFieldInitializerName]].
- Let value be ?
- Else,
- Let rhs be ?
Evaluation ofAssignmentExpression . - Let value be ?
GetValue (rhs).
- Let rhs be ?
- Return
Completion Record { [[Type]]:return , [[Value]]: value, [[Target]]:empty }.
Even though field initializers constitute a function boundary, calling
Assert : argumentsList is empty.- Return ?
EvaluateClassStaticBlockBody ofClassStaticBlockBody with argument functionObject.
10.2.1.4 OrdinaryCallEvaluateBody ( F, argumentsList )
The abstract operation OrdinaryCallEvaluateBody takes arguments F (an ECMAScript
- Return ?
EvaluateBody of F.[[ECMAScriptCode]] with arguments F and argumentsList.
10.2.2 [[Construct]] ( argumentsList, newTarget )
The [[Construct]] internal method of an ECMAScript
- Let callerContext be the
running execution context . - Let kind be F.[[ConstructorKind]].
- If kind is
base , then- Let thisArgument be ?
OrdinaryCreateFromConstructor (newTarget,"%Object.prototype%" ).
- Let thisArgument be ?
- Let calleeContext be
PrepareForOrdinaryCall (F, newTarget). Assert : calleeContext is now therunning execution context .- If kind is
base , then- Perform
OrdinaryCallBindThis (F, calleeContext, thisArgument). - Let initializeResult be
Completion (InitializeInstanceElements (thisArgument, F)). - If initializeResult is an
abrupt completion , then- Remove calleeContext from the
execution context stack and restore callerContext as therunning execution context . - Return ? initializeResult.
- Remove calleeContext from the
- Perform
- Let constructorEnv be the LexicalEnvironment of calleeContext.
- Let result be
Completion (OrdinaryCallEvaluateBody (F, argumentsList)). - Remove calleeContext from the
execution context stack and restore callerContext as therunning execution context . - If result is a
return completion , then- If result.[[Value]]
is an Object , return result.[[Value]]. - If kind is
base , return thisArgument. - If result.[[Value]] is not
undefined , throw aTypeError exception.
- If result.[[Value]]
- Else,
ReturnIfAbrupt (result).
- Let thisBinding be ? constructorEnv.GetThisBinding().
Assert : thisBindingis an Object .- Return thisBinding.
10.2.3 OrdinaryFunctionCreate ( functionPrototype, sourceText, ParameterList, Body, thisMode, env, privateEnv )
The abstract operation OrdinaryFunctionCreate takes arguments functionPrototype (an Object), sourceText (a sequence of Unicode code points), ParameterList (a
- Let internalSlotsList be the internal slots listed in
Table 30 . - Let F be
OrdinaryObjectCreate (functionPrototype, internalSlotsList). - Set F.[[Call]] to the definition specified in
10.2.1 . - Set F.[[SourceText]] to sourceText.
- Set F.[[FormalParameters]] to ParameterList.
- Set F.[[ECMAScriptCode]] to Body.
- If the
source text matched by Body isstrict mode code , let Strict betrue ; else let Strict befalse . - Set F.[[Strict]] to Strict.
- If thisMode is
lexical-this , set F.[[ThisMode]] tolexical . - Else if Strict is
true , set F.[[ThisMode]] tostrict . - Else, set F.[[ThisMode]] to
global . - Set F.[[IsClassConstructor]] to
false . - Set F.[[Environment]] to env.
- Set F.[[PrivateEnvironment]] to privateEnv.
- Set F.[[ScriptOrModule]] to
GetActiveScriptOrModule (). - Set F.[[Realm]] to
the current Realm Record . - Set F.[[HomeObject]] to
undefined . - Set F.[[Fields]] to a new empty
List . - Set F.[[PrivateMethods]] to a new empty
List . - Set F.[[ClassFieldInitializerName]] to
empty . - Let len be the
ExpectedArgumentCount of ParameterList. - Perform
SetFunctionLength (F, len). - Return F.
10.2.4 AddRestrictedFunctionProperties ( F, realm )
The abstract operation AddRestrictedFunctionProperties takes arguments F (a
Assert : realm.[[Intrinsics]].[[%ThrowTypeError% ]] exists and has been initialized.- Let thrower be realm.[[Intrinsics]].[[
%ThrowTypeError% ]]. - Perform !
DefinePropertyOrThrow (F,"caller" , PropertyDescriptor { [[Get]]: thrower, [[Set]]: thrower, [[Enumerable]]:false , [[Configurable]]:true }). - Perform !
DefinePropertyOrThrow (F,"arguments" , PropertyDescriptor { [[Get]]: thrower, [[Set]]: thrower, [[Enumerable]]:false , [[Configurable]]:true }). - Return
unused .
10.2.4.1 %ThrowTypeError% ( )
This function is the %ThrowTypeError% intrinsic object.
It is an anonymous built-in
It performs the following steps when called:
- Throw a
TypeError exception.
The value of the [[Extensible]] internal slot of this function is
The
The
10.2.5 MakeConstructor ( F [ , writablePrototype [ , prototype ] ] )
The abstract operation MakeConstructor takes argument F (an ECMAScript
- If F is an ECMAScript
function object , thenAssert :IsConstructor (F) isfalse .Assert : F is an extensible object that does not have a"prototype" own property.- Set F.[[Construct]] to the definition specified in
10.2.2 .
- Else,
- Set F.[[Construct]] to the definition specified in
10.3.2 .
- Set F.[[Construct]] to the definition specified in
- Set F.[[ConstructorKind]] to
base . - If writablePrototype is not present, set writablePrototype to
true . - If prototype is not present, then
- Set prototype to
OrdinaryObjectCreate (%Object.prototype% ). - Perform !
DefinePropertyOrThrow (prototype,"constructor" , PropertyDescriptor { [[Value]]: F, [[Writable]]: writablePrototype, [[Enumerable]]:false , [[Configurable]]:true }).
- Set prototype to
- Perform !
DefinePropertyOrThrow (F,"prototype" , PropertyDescriptor { [[Value]]: prototype, [[Writable]]: writablePrototype, [[Enumerable]]:false , [[Configurable]]:false }). - Return
unused .
10.2.6 MakeClassConstructor ( F )
The abstract operation MakeClassConstructor takes argument F (an ECMAScript
Assert : F.[[IsClassConstructor]] isfalse .- Set F.[[IsClassConstructor]] to
true . - Return
unused .
10.2.7 MakeMethod ( F, homeObject )
The abstract operation MakeMethod takes arguments F (an ECMAScript
- Set F.[[HomeObject]] to homeObject.
- Return
unused .
10.2.8 DefineMethodProperty ( homeObject, key, closure, enumerable )
The abstract operation DefineMethodProperty takes arguments homeObject (an Object), key (a
Assert : homeObject is an ordinary, extensible object with no non-configurable properties.- If key is a
Private Name , then- Return
PrivateElement { [[Key]]: key, [[Kind]]:method , [[Value]]: closure }.
- Return
- Else,
- Let desc be the PropertyDescriptor { [[Value]]: closure, [[Writable]]:
true , [[Enumerable]]: enumerable, [[Configurable]]:true }. - Perform !
DefinePropertyOrThrow (homeObject, key, desc). - Return
unused .
- Let desc be the PropertyDescriptor { [[Value]]: closure, [[Writable]]:
10.2.9 SetFunctionName ( F, name [ , prefix ] )
The abstract operation SetFunctionName takes arguments F (a
Assert : F is an extensible object that does not have a"name" own property.- If name
is a Symbol , then- Let description be name's [[Description]] value.
- If description is
undefined , set name to the empty String. - Else, set name to the
string-concatenation of"[" , description, and"]" .
- Else if name is a
Private Name , then- Set name to name.[[Description]].
- If F has an [[InitialName]] internal slot, then
- Set F.[[InitialName]] to name.
- If prefix is present, then
- Set name to the
string-concatenation of prefix, the code unit 0x0020 (SPACE), and name. - If F has an [[InitialName]] internal slot, then
- Optionally, set F.[[InitialName]] to name.
- Set name to the
- Perform !
DefinePropertyOrThrow (F,"name" , PropertyDescriptor { [[Value]]: name, [[Writable]]:false , [[Enumerable]]:false , [[Configurable]]:true }). - Return
unused .
10.2.10 SetFunctionLength ( F, length )
The abstract operation SetFunctionLength takes arguments F (a
Assert : F is an extensible object that does not have a"length" own property.- Perform !
DefinePropertyOrThrow (F,"length" , PropertyDescriptor { [[Value]]:𝔽 (length), [[Writable]]:false , [[Enumerable]]:false , [[Configurable]]:true }). - Return
unused .
10.2.11 FunctionDeclarationInstantiation ( func, argumentsList )
The abstract operation FunctionDeclarationInstantiation takes arguments func (an ECMAScript
When an
It performs the following steps when called:
- Let calleeContext be the
running execution context . - Let code be func.[[ECMAScriptCode]].
- Let strict be func.[[Strict]].
- Let formals be func.[[FormalParameters]].
- Let parameterNames be the
BoundNames of formals. - If parameterNames has any duplicate entries, let hasDuplicates be
true . Otherwise, let hasDuplicates befalse . - Let simpleParameterList be
IsSimpleParameterList of formals. - Let hasParameterExpressions be
ContainsExpression of formals. - Let varNames be the
VarDeclaredNames of code. - Let varDeclarations be the
VarScopedDeclarations of code. - Let lexicalNames be the
LexicallyDeclaredNames of code. - Let functionNames be a new empty
List . - Let functionsToInitialize be a new empty
List . - For each element d of varDeclarations, in reverse
List order, do- If d is neither a
VariableDeclaration nor aForBinding nor aBindingIdentifier , thenAssert : d is either aFunctionDeclaration , aGeneratorDeclaration , anAsyncFunctionDeclaration , or anAsyncGeneratorDeclaration .- Let fn be the sole element of the
BoundNames of d. - If functionNames does not contain fn, then
- Insert fn as the first element of functionNames.
- NOTE: If there are multiple function declarations for the same name, the last declaration is used.
- Insert d as the first element of functionsToInitialize.
- If d is neither a
- Let argumentsObjectNeeded be
true . - If func.[[ThisMode]] is
lexical , then- NOTE: Arrow functions never have an arguments object.
- Set argumentsObjectNeeded to
false .
- Else if parameterNames contains
"arguments" , then- Set argumentsObjectNeeded to
false .
- Set argumentsObjectNeeded to
- Else if hasParameterExpressions is
false , then- If functionNames contains
"arguments" or lexicalNames contains"arguments" , then- Set argumentsObjectNeeded to
false .
- Set argumentsObjectNeeded to
- If functionNames contains
- If strict is
true or hasParameterExpressions isfalse , then- NOTE: Only a single
Environment Record is needed for the parameters, since calls toeval
instrict mode code cannot create new bindings which are visible outside of theeval
. - Let env be the LexicalEnvironment of calleeContext.
- NOTE: Only a single
- Else,
- NOTE: A separate
Environment Record is needed to ensure that bindings created bydirect eval calls in the formal parameter list are outside the environment where parameters are declared. - Let calleeEnv be the LexicalEnvironment of calleeContext.
- Let env be
NewDeclarativeEnvironment (calleeEnv). Assert : The VariableEnvironment of calleeContext is calleeEnv.- Set the LexicalEnvironment of calleeContext to env.
- NOTE: A separate
- For each String paramName of parameterNames, do
- Let alreadyDeclared be ! env.HasBinding(paramName).
- NOTE:
Early errors ensure that duplicate parameter names can only occur innon-strict functions that do not have parameter default values or rest parameters. - If alreadyDeclared is
false , then- Perform ! env.CreateMutableBinding(paramName,
false ). - If hasDuplicates is
true , then- Perform ! env.InitializeBinding(paramName,
undefined ).
- Perform ! env.InitializeBinding(paramName,
- Perform ! env.CreateMutableBinding(paramName,
- If argumentsObjectNeeded is
true , then- If strict is
true or simpleParameterList isfalse , then- Let ao be
CreateUnmappedArgumentsObject (argumentsList).
- Let ao be
- Else,
- NOTE: A mapped argument object is only provided for
non-strict functions that don't have a rest parameter, any parameter default value initializers, or any destructured parameters. - Let ao be
CreateMappedArgumentsObject (func, formals, argumentsList, env).
- NOTE: A mapped argument object is only provided for
- If strict is
true , then- Perform ! env.CreateImmutableBinding(
"arguments" ,false ). - NOTE: In
strict mode code early errors prevent attempting to assign to this binding, so its mutability is not observable.
- Perform ! env.CreateImmutableBinding(
- Else,
- Perform ! env.CreateMutableBinding(
"arguments" ,false ).
- Perform ! env.CreateMutableBinding(
- Perform ! env.InitializeBinding(
"arguments" , ao). - Let parameterBindings be the
list-concatenation of parameterNames and «"arguments" ».
- If strict is
- Else,
- Let parameterBindings be parameterNames.
- Let iteratorRecord be
CreateListIteratorRecord (argumentsList). - If hasDuplicates is
true , then- Perform ?
IteratorBindingInitialization of formals with arguments iteratorRecord andundefined .
- Perform ?
- Else,
- Perform ?
IteratorBindingInitialization of formals with arguments iteratorRecord and env.
- Perform ?
- If hasParameterExpressions is
false , then- NOTE: Only a single
Environment Record is needed for the parameters and top-level vars. - Let instantiatedVarNames be a copy of the
List parameterBindings. - For each element n of varNames, do
- If instantiatedVarNames does not contain n, then
- Append n to instantiatedVarNames.
- Perform ! env.CreateMutableBinding(n,
false ). - Perform ! env.InitializeBinding(n,
undefined ).
- If instantiatedVarNames does not contain n, then
- Let varEnv be env.
- NOTE: Only a single
- Else,
- NOTE: A separate
Environment Record is needed to ensure that closures created by expressions in the formal parameter list do not have visibility of declarations in the function body. - Let varEnv be
NewDeclarativeEnvironment (env). - Set the VariableEnvironment of calleeContext to varEnv.
- Let instantiatedVarNames be a new empty
List . - For each element n of varNames, do
- If instantiatedVarNames does not contain n, then
- Append n to instantiatedVarNames.
- Perform ! varEnv.CreateMutableBinding(n,
false ). - If parameterBindings does not contain n, or if functionNames contains n, then
- Let initialValue be
undefined .
- Let initialValue be
- Else,
- Let initialValue be ! env.GetBindingValue(n,
false ).
- Let initialValue be ! env.GetBindingValue(n,
- Perform ! varEnv.InitializeBinding(n, initialValue).
- NOTE: A var with the same name as a formal parameter initially has the same value as the corresponding initialized parameter.
- If instantiatedVarNames does not contain n, then
- NOTE: A separate
- NOTE: Annex
B.3.2.1 adds additional steps at this point. - If strict is
false , then- Let lexEnv be
NewDeclarativeEnvironment (varEnv). - NOTE:
Non-strict functions use a separateEnvironment Record for top-level lexical declarations so that adirect eval can determine whether any var scoped declarations introduced by the eval code conflict with pre-existing top-level lexically scoped declarations. This is not needed forstrict functions because a strictdirect eval always places all declarations into a newEnvironment Record .
- Let lexEnv be
- Else,
- Let lexEnv be varEnv.
- Set the LexicalEnvironment of calleeContext to lexEnv.
- Let lexDeclarations be the
LexicallyScopedDeclarations of code. - For each element d of lexDeclarations, do
- NOTE: A lexically declared name cannot be the same as a function/generator declaration, formal parameter, or a var name. Lexically declared names are only instantiated here but not initialized.
- For each element dn of the
BoundNames of d, do- If
IsConstantDeclaration of d istrue , then- Perform ! lexEnv.CreateImmutableBinding(dn,
true ).
- Perform ! lexEnv.CreateImmutableBinding(dn,
- Else,
- Perform ! lexEnv.CreateMutableBinding(dn,
false ).
- Perform ! lexEnv.CreateMutableBinding(dn,
- If
- Let privateEnv be the PrivateEnvironment of calleeContext.
- For each
Parse Node f of functionsToInitialize, do- Let fn be the sole element of the
BoundNames of f. - Let fo be
InstantiateFunctionObject of f with arguments lexEnv and privateEnv. - Perform ! varEnv.SetMutableBinding(fn, fo,
false ).
- Let fn be the sole element of the
- Return
unused .