ReturnIfAbrupt在ES6草案中意味着什么? [英] What does ReturnIfAbrupt mean in ES6 draft?

查看:76
本文介绍了ReturnIfAbrupt在ES6草案中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为ES6草案实施一些垫片。我想知道是否有人可以告诉我 ReturnIfAbrupt 的含义。例如,我对 Number.toInt (调用内部 [[ToInteger]] 的实现如下:

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 Number.toInt (which calls internal [[ToInteger]] is as follows:

if (!('toInt' in Number))
    Object.defineProperty(Number, 'toInt', {

        value: function toInt(value) {
            // ECMA-262 Ed. 6, 9-27-12. 9.1.4

            // 1. Let number be the result of calling ToNumber on the input argument.
            var number = Number(value);

            // 2. ReturnIfAbrupt(number).
            // ?

            // 3. If number is NaN, return +0.
            if (number != number) return 0;

            // 4. If number is +0, -0, +Infinity, or -Infinity, return number.
            if (number == 0 || 1 / number == 0) return number;

            // 5. Return the result of computing sign(number) * floor(abs(number)).
            return (n < 0 ? -1 : 1) * Math.floor(Math.abs(number));

        },

        writable: true,
        configurable: true

    });

第2步是 ReturnIfAbrupt(数字)。您会注意到我目前有 //?这一步,因为我不知道该怎么做。当它说 ReturnIfAbrupt(...)时,它是什么意思?

Step 2 is ReturnIfAbrupt(number). You'll notice I currently have // ? for that step because I'm not sure what to do. What does it mean when it says ReturnIfAbrupt(...)?

我已经阅读了关于<$的部分草稿中的c $ c> ReturnIfAbrupt ,但是我无法理解第2步的内容,代替 // ?在上面的代码中。

I have read the section on ReturnIfAbrupt in the draft, however I am unable to understand what to do for step 2, what to put in place of // ? in the code above.

从我的阅读中,可能没有什么应该做的,而 ReturnIfAbrupt 步骤仅仅意味着让ToNumber中发生的任何错误传播,退出该函数。然而,这似乎过于冗长,我认为这可不言而喻。此外,我似乎不喜欢 ToNumber 甚至可以抛出错误。有人可以确认或帮助我理解真正的含义吗?

From my reading, it may be that nothing should be done, and the ReturnIfAbrupt step merely means to let any error which occurred in ToNumber to propagate up, exiting the function. However, that seems overly verbose, as I would think it could go without saying. Also, it doesn't seem to me like ToNumber can even throw an error. Could someone confirm or help me to understand the real meaning?

推荐答案

ReturnIfAbrupt指的是突然完成。完成记录包含类型和与之关联的值。正常完成将类似于表达式的结果值。除正常完成外,函数的返回完成是通常的预期完成。任何其他完成类型都是突然的。那是投掷,休息,继续。

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;
  }
}

按原样实施,它需要的是将函数包装在try catch中。抛出的值将是一个突然完成。这不是你在JS级别看到的,它是用于在引擎级实现控制流和非本地控制传输。

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.

我实现了很多JS虚拟机中的ES6规范也可能有助于阐明它,这里是ToInteger: https://github.com/Benvie/continuum/blob/master/lib/continuum.js#L516

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;
}

这篇关于ReturnIfAbrupt在ES6草案中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆