What does ReturnIfAbrupt mean in ES6 draft?

前端 未结 1 1391
粉色の甜心
粉色の甜心 2020-12-31 09:42

I\'m currently implementing some shims for the ES6 draft. I\'m wondering if anyone can tell me what ReturnIfAbrupt means. For instance, my implementation for

相关标签:
1条回答
  • 2020-12-31 10:29

    ReturnIfAbrupt is referring to an Abrupt Completion. A completion record contains a type and the value associated with it. A normal completion would be something like an expression's resulting value. A return completion from a function is the usual expected completion aside from a normal completion. Any other completion types are abrupt. That's throw, break, continue.

    if (isCompletionRecord(v)) {
      if (isAbruptCompletion(v)) {
        return v;
      } else {
        v = v.value;
      }
    }
    

    Implementing it as you are, what it would entail is wrapping the function in a try catch. A thrown value would be an abrupt completion. This isn't something you see at the JS level though, it's for implementing control flow and non-local control transfers at the engine level.

    I've implemented much of the ES6 spec in a JS virtual machine that may also help shed some light on it, here's ToInteger: https://github.com/Benvie/continuum/blob/master/lib/continuum.js#L516

    function ToInteger(argument){
      if (argument && typeof argument === OBJECT && argument.IsCompletion) {
        if (argument.IsAbruptCompletion) {
          return argument;
        }
        argument = argument.value;
      }
      return ToNumber(argument) | 0;
    }
    
    0 讨论(0)
提交回复
热议问题