Skip to content

Latest commit

 

History

History
143 lines (101 loc) · 9.66 KB

File metadata and controls

143 lines (101 loc) · 9.66 KB

Spec Text

Unless otherwise specified, the following text goes in Annex B (normative-optional features needed by web browsers).

NOTE. Implementations have historically provided “caller” and “arguments” magic properties on functions. The following semantics allow them to keep backward compatibility with those deprecated features while limiting the API surface and safely restricting their functionality.

IsAllowedReceiverFunctionForCallerAndArguments(func, expectedRealm)

The abstract operation IsAllowedReceiverFunctionForCallerAndArguments accepts as arguments a function object func and a realm expectedRealm, and takes the following steps:

  1. Assert: IsCallable(func) is true.
  2. If func is not an ECMAScript function object, return false.
  3. If func.[[Realm]] is not expectedRealm, return false.
  4. If func.[[Strict]] is true, return false.
  5. If func does not have a [[Construct]] internal method, return false.
  6. Return true.

NOTE. The functions for which IsAllowedReceiverFunctionForCallerAndArguments returns true are non-strict functions from the expected realm that were created with FunctionDeclaration or FunctionExpression syntax or using the Function constructor.

Additional component of execution contexts

The following component is added to ECMAScript code execution contexts:

  • ArgumentsList: The List of arguments that has been passed during the function invocation, if this execution context is evaluating the code of a function object. Otherwise, null.

The PrepareForOrdinaryCall abstract operation is modified in order to take a third parameter:

PrepareForOrdinaryCall(F, newTarget, argumentsList)

and takes the following additional step after Step 4:

  1. Set the ArgumentsList of calleeContext to argumentsList.

(Other algorithms that initialize an ECMAScript code execution context shall set its ArgumentsList component to null.)

The two existing uses of PrepareForOrdinaryCall are modified in order to forward their argumentsList.

NOTE: Within ECMA-262, the ArgumentList component is inspected only by Function.prototype.arguments when IsAllowedReceiverFunctionForCallerAndArguments(F, current realm) is true.

Modification of the CreateUnmappedArgumentsObject abstract operation

The existing abstract operation CreateUnmappedArgumentsObject is modified in order to take a second parameter:

CreateUnmappedArgumentsObject(argumentsList, callee)

The existing uses of that abstract operation are modified in order to pass undefined as their second argument.

The penultimate step of the algorithm:

  1. Perform ! DefinePropertyOrThrow(obj, "callee", PropertyDescriptor { [[Get]]: %ThrowTypeError%, [[Set]]: %ThrowTypeError%, [[Enumerable]]: false, [[Configurable]]: false }).

is replaced with:

  1. If callee is undefined, then
    1. Perform ! DefinePropertyOrThrow(obj, "callee", PropertyDescriptor { [[Get]]: %ThrowTypeError%, [[Set]]: %ThrowTypeError%, [[Enumerable]]: false, [[Configurable]]: false }).
  2. Else,
    1. Assert: callee is a function object.
    2. Perform ! DefinePropertyOrThrow(obj, "callee", PropertyDescriptor { [[Value]]: callee, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }).

get Function.prototype.caller

Function.prototype.caller is a configurable, non-enumerable accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Let func be the this value.
  2. If IsCallable(func) is false, throw a TypeError exception.
  3. Let currentRealm be the current Realm Record.
  4. If IsAllowedReceiverFunctionForCallerAndArguments(func, currentRealm) is false, throw a TypeError exception.
  5. If there is no execution context in the execution context stack whose Function component is func, return null.
  6. Let funcContext be the top-most execution context in the execution context stack whose Function component is func.
  7. If funcContext has no parent execution context in the execution context stack, return null.
  8. Let callerContext be the parent execution context of funcContext.
  9. Let caller be the Function component of callerContext.
  10. If caller is null, return null.
  11. If caller is not an ECMAScript function object, return null.
  12. If caller.[[Realm]] is not currentRealm, return null.
  13. If caller.[[Strict]] is true, return null.
  14. If caller.[[ECMAScriptCode]] is a GeneratorBody, an AsyncFunctionBody, an AsyncGeneratorBody, or an AsyncConciseBody, return null.
  15. Return caller.

NOTE. The purported caller will not be the real caller if its corresponding execution context has been removed from the execution context stack as a result of a tail position call. In particular, the algorithm will return a false positive when applied to a non-strict function which has been called in tail position by a strict function which has itself been called by a non-strict function. However, the algorithm will give the correct answer when applied to a non-strict function called by another non-strict function, because tail position call is not defined in non-strict mode.

NOTE 2. Proxy functions and bound functions are never considered as “caller” for the purpose of this algorithm, because they never appear in the execution context stack.

get Function.prototype.arguments

Function.prototype.arguments is a configurable, non-enumerable accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Let func be the this value.
  2. If IsCallable(func) is false, throw a TypeError exception.
  3. Let currentRealm be the current Realm Record.
  4. If IsAllowedReceiverFunctionForCallerAndArguments(func, currentRealm) is false, throw a TypeError exception.
  5. If there is no execution context in the execution context stack whose Function component is func, return null.
  6. Let funcContext be the top-most execution context in the execution context stack whose Function component is func.
  7. Let argumentsList be the ArgumentsList component of funcContext.
  8. If IsSimpleParameterList of func.[[FormalParameters]] is true, let callee be func.
  9. Else, let callee be undefined.
  10. Return CreateUnmappedArgumentsObject(argumentsList, callee) — where CreateUnmappedArgumentsObject has been patched as described above.

Modifications of the Forbidden Extensions section

The two items in Forbidden Extensions related to the properties “caller” and “arguments” of function objects are replaced with the following one:

  • An implementation must not extend any function object with own properties named “caller” or “arguments“, except for the corresponding properties on %Function.prototype% that are defined in this specification.

Other adjustments