ECMAScript® 2024 Language Specification

Draft ECMA-262 / February 15, 2024

10.3 Built-in Function Objects

A built-in function object is an ordinary object; it must satisfy the requirements for ordinary objects set out in 10.1.

In addition to the internal slots required of every ordinary object (see 10.1), a built-in function object must also have the following internal slots:

The initial value of a built-in function object's [[Prototype]] internal slot is %Function.prototype%, unless otherwise specified.

A built-in function object must have a [[Call]] internal method that conforms to the definition in 10.3.1.

A built-in function object has a [[Construct]] internal method if and only if it is described as a “constructor”, or some algorithm in this specification explicitly sets its [[Construct]] internal method. Such a [[Construct]] internal method must conform to the definition in 10.3.2.

An implementation may provide additional built-in function objects that are not defined in this specification.

10.3.1 [[Call]] ( thisArgument, argumentsList )

The [[Call]] internal method of a built-in function object F takes arguments thisArgument (an ECMAScript language value) and argumentsList (a List of ECMAScript language values) and returns either a normal completion containing an ECMAScript language value or a throw completion. It performs the following steps when called:

  1. Return ? BuiltinCallOrConstruct(F, thisArgument, argumentsList, undefined).

10.3.2 [[Construct]] ( argumentsList, newTarget )

The [[Construct]] internal method of a built-in function object F (when the method is present) takes arguments argumentsList (a List of ECMAScript language values) and newTarget (a constructor) and returns either a normal completion containing an Object or a throw completion. It performs the following steps when called:

  1. Return ? BuiltinCallOrConstruct(F, uninitialized, argumentsList, newTarget).

10.3.3 BuiltinCallOrConstruct ( F, thisArgument, argumentsList, newTarget )

The abstract operation BuiltinCallOrConstruct takes arguments F (a built-in function object), thisArgument (an ECMAScript language value or uninitialized), argumentsList (a List of ECMAScript language values), and newTarget (a constructor or undefined) and returns either a normal completion containing an ECMAScript language value or a throw completion. It performs the following steps when called:

  1. Let callerContext be the running execution context.
  2. If callerContext is not already suspended, suspend callerContext.
  3. Let calleeContext be a new execution context.
  4. Set the Function of calleeContext to F.
  5. Let calleeRealm be F.[[Realm]].
  6. Set the Realm of calleeContext to calleeRealm.
  7. Set the ScriptOrModule of calleeContext to null.
  8. Perform any necessary implementation-defined initialization of calleeContext.
  9. Push calleeContext onto the execution context stack; calleeContext is now the running execution context.
  10. Let result be the Completion Record that is the result of evaluating F in a manner that conforms to the specification of F. If thisArgument is uninitialized, the this value is uninitialized; otherwise, thisArgument provides the this value. argumentsList provides the named parameters. newTarget provides the NewTarget value.
  11. NOTE: If F is defined in this document, “the specification of F” is the behaviour specified for it via algorithm steps or other means.
  12. Remove calleeContext from the execution context stack and restore callerContext as the running execution context.
  13. Return ? result.
Note

When calleeContext is removed from the execution context stack it must not be destroyed if it has been suspended and retained by an accessible Generator for later resumption.

10.3.4 CreateBuiltinFunction ( behaviour, length, name, additionalInternalSlotsList [ , realm [ , prototype [ , prefix ] ] ] )

The abstract operation CreateBuiltinFunction takes arguments behaviour (an Abstract Closure, a set of algorithm steps, or some other definition of a function's behaviour provided in this specification), length (a non-negative integer or +∞), name (a property key or a Private Name), and additionalInternalSlotsList (a List of names of internal slots) and optional arguments realm (a Realm Record), prototype (an Object or null), and prefix (a String) and returns a function object. additionalInternalSlotsList contains the names of additional internal slots that must be defined as part of the object. This operation creates a built-in function object. It performs the following steps when called:

  1. If realm is not present, set realm to the current Realm Record.
  2. If prototype is not present, set prototype to realm.[[Intrinsics]].[[%Function.prototype%]].
  3. Let internalSlotsList be a List containing the names of all the internal slots that 10.3 requires for the built-in function object that is about to be created.
  4. Append to internalSlotsList the elements of additionalInternalSlotsList.
  5. Let func be a new built-in function object that, when called, performs the action described by behaviour using the provided arguments as the values of the corresponding parameters specified by behaviour. The new function object has internal slots whose names are the elements of internalSlotsList, and an [[InitialName]] internal slot.
  6. Set func.[[Prototype]] to prototype.
  7. Set func.[[Extensible]] to true.
  8. Set func.[[Realm]] to realm.
  9. Set func.[[InitialName]] to null.
  10. Perform SetFunctionLength(func, length).
  11. If prefix is not present, then
    1. Perform SetFunctionName(func, name).
  12. Else,
    1. Perform SetFunctionName(func, name, prefix).
  13. Return func.

Each built-in function defined in this specification is created by calling the CreateBuiltinFunction abstract operation.