简体   繁体   English

尾声优化在Babel ES2015中不起作用

[英]Tail call optimization is not working in babel ES2015

Just implemented the tail calls sample code in Node.js which will be translated by using babel (enabled the ES2015 Syntax support), but it just throws the exception Maximum call stack size exceeded . 刚刚在Node.js中实现了尾调用示例代码,该示例代码将通过使用babel进行翻译(启用了ES2015语法支持),但是它只会抛出异常Maximum call stack size exceeded beyond。

//index.js
require('babel-core/register');
require('./sample.js');

//sample.js
function factorial(n, acc = 1) {
    'use strict';
    if (n <= 1) return acc;
    return factorial(n - 1, n * acc);
}

// Stack overflow in most implementations today,
// // but safe on arbitrary inputs in ES6
factorial(100000)

Here is my babel depens. 这是我的通天塔。

├─┬ babel-core@6.2.1
│ ├─┬ babel-code-frame@6.1.18
│ │ ├─┬ chalk@1.1.1
│ │ │ ├── ansi-styles@2.1.0
│ │ │ ├── escape-string-regexp@1.0.3
│ │ │ ├─┬ has-ansi@2.0.0
│ │ │ │ └── ansi-regex@2.0.0
│ │ │ ├── strip-ansi@3.0.0
│ │ │ └── supports-color@2.0.0
│ │ ├── esutils@2.0.2
│ │ ├── js-tokens@1.0.2
│ │ ├─┬ line-numbers@0.2.0
│ │ │ └── left-pad@0.0.3
│ │ └─┬ repeating@1.1.3
│ │   └─┬ is-finite@1.0.1
│ │     └── number-is-nan@1.0.0
│ ├─┬ babel-generator@6.2.0
│ │ ├─┬ detect-indent@3.0.1
│ │ │ ├── get-stdin@4.0.1
│ │ │ └── minimist@1.2.0
│ │ ├── is-integer@1.0.6
│ │ └── trim-right@1.0.1
│ ├── babel-helpers@6.1.20
│ ├── babel-messages@6.2.1
│ ├─┬ babel-register@6.2.0
│ │ ├── core-js@1.2.6
│ │ ├─┬ home-or-tmp@1.0.0
│ │ │ ├── os-tmpdir@1.0.1
│ │ │ └── user-home@1.1.1
│ │ └─┬ source-map-support@0.2.10
│ │   └─┬ source-map@0.1.32
│ │     └── amdefine@1.0.0
│ ├── babel-runtime@5.8.34
│ ├── babel-template@6.2.0
│ ├─┬ babel-traverse@6.2.0
│ │ ├── globals@8.12.1
│ │ └─┬ invariant@2.2.0
│ │   └── loose-envify@1.1.0
│ ├─┬ babel-types@6.2.0
│ │ └── to-fast-properties@1.0.1
│ ├── babylon@6.2.0
│ ├── convert-source-map@1.1.2
│ ├─┬ debug@2.2.0
│ │ └── ms@0.7.1
│ ├── json5@0.4.0
│ ├── lodash@3.10.1
│ ├─┬ minimatch@2.0.10
│ │ └─┬ brace-expansion@1.1.1
│ │   ├── balanced-match@0.2.1
│ │   └── concat-map@0.0.1
│ ├── path-exists@1.0.0
│ ├── path-is-absolute@1.0.0
│ ├── private@0.1.6
│ ├── shebang-regex@1.0.0
│ ├── slash@1.0.0
│ └── source-map@0.5.3
└─┬ babel-preset-es2015@6.1.18
  ├── babel-plugin-check-es2015-constants@6.2.0
  ├── babel-plugin-transform-es2015-arrow-functions@6.1.18
  ├── babel-plugin-transform-es2015-block-scoped-functions@6.1.18
  ├── babel-plugin-transform-es2015-block-scoping@6.1.18
  ├─┬ babel-plugin-transform-es2015-classes@6.2.2
  │ ├── babel-helper-define-map@6.2.0
  │ ├── babel-helper-function-name@6.2.0
  │ ├── babel-helper-optimise-call-expression@6.1.18
  │ └── babel-helper-replace-supers@6.2.0
  ├── babel-plugin-transform-es2015-computed-properties@6.1.18
  ├── babel-plugin-transform-es2015-destructuring@6.1.18
  ├── babel-plugin-transform-es2015-for-of@6.1.18
  ├── babel-plugin-transform-es2015-function-name@6.1.18
  ├── babel-plugin-transform-es2015-literals@6.1.18
  ├─┬ babel-plugin-transform-es2015-modules-commonjs@6.2.0
  │ └── babel-plugin-transform-strict-mode@6.2.0
  ├── babel-plugin-transform-es2015-object-super@6.1.18
  ├─┬ babel-plugin-transform-es2015-parameters@6.1.18
  │ ├─┬ babel-helper-call-delegate@6.2.0
  │ │ └── babel-helper-hoist-variables@6.1.18
  │ └── babel-helper-get-function-arity@6.2.0
  ├── babel-plugin-transform-es2015-shorthand-properties@6.1.18
  ├── babel-plugin-transform-es2015-spread@6.1.18
  ├─┬ babel-plugin-transform-es2015-sticky-regex@6.1.18
  │ └── babel-helper-regex@6.1.18
  ├── babel-plugin-transform-es2015-template-literals@6.1.18
  ├── babel-plugin-transform-es2015-typeof-symbol@6.1.18

And one thing to prove I setup up my environment correctly is, the default parameter in a function did work in my babel project, but not in a pure nodejs environment. 证明我正确设置环境的一件事是,函数中的默认参数确实在babel项目中起作用,但在纯nodejs环境中却没有。 For example, 例如,

function  add(a, b=2) {
    console.log(a + b);
}
add(3); //In babel project this will output 5
//But it just threw an exception in pure nodejs file.(without require babel/register and setup the es2015 subset in .babelrc)

Here is my entry files. 这是我的输入文件。

However, if I try to implement other feature,such as template string or arrows function, they are all working correctly. 但是,如果我尝试实现其他功能,例如模板字符串或箭头功能,它们都可以正常工作。 Any ideas? 有任何想法吗?

There is no environment yet that supports TCO. 尚无支持TCO的环境。 Babel used have an experimental TCO transform, but it was removed in v6. Babel使用了实验性的TCO转换,但在v6中已将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM