Search code examples
javascriptecmascript-6ecmascript-5ecmascript-2017

Understanding ECMAScript's ReturnIfAbrupt(AbstractOperation())


I know there are a few posts about this, but I have yet to find a good explanation of what a ReturnIfAbrupt(AbstractOperation()) statement evaluates to when the AbstractOperation() is not an abrupt completion?

According to ECMAScript ReturnIfAbrupt(AbstractOperation()) means:

  1. Let hygienicTemp be AbstractOperation().
  2. If hygienicTemp is an abrupt completion, return hygienicTemp.
  3. Else if hygienicTemp is a Completion Record, set hygienicTemp to hygienicTemp.[[Value]].

Where hygienicTemp is ephemeral and visible only in the steps pertaining to ReturnIfAbrupt.

Taking the last line in the [[Construct]] operation as an example:

  1. That line is Return ? envRec.GetThisBinding()
  2. Which is equivalent to: Return ReturnIfAbrupt(envRec.GetThisBinding())
  3. Assuming GetThisBinding() does not throw an Error (i.e. no abrupt completion), I have no idea what is actually returned
  4. The quoted ReturnIfAbrupt definition above specifically says hygenicTemp only relates to the steps within the definition itself. It gives no hint of what the expression as a whole evaluates to

Solution

  • The bit you've quoted is for when the value isn't used. But your [[Construct]] example is using the value.

    For that, you needed to scroll down a little bit:

    Algorithms steps that say or are otherwise equivalent to:

    1. Let result be AbstractOperation(ReturnIfAbrupt(argument)).

    mean the same thing as:

    1. If argument is an abrupt completion, return argument.

    2. If argument is a Completion Record, set argument to argument.[[Value]].

    3. Let result be AbstractOperation(argument).

    So for Return ? envRec.GetThisBinding(), when envRec.GetThisBinding() doesn't return an abrupt completion, that line in [[Construct]] returns the [[Value]] of the non-abrupt completion record from envRec.GetThisBinding() (the this value).