簡體   English   中英

尾聲優化在Babel ES2015中不起作用

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

剛剛在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)

這是我的通天塔。

├─┬ 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

證明我正確設置環境的一件事是,函數中的默認參數確實在babel項目中起作用,但在純nodejs環境中卻沒有。 例如,

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)

這是我的輸入文件。

但是,如果我嘗試實現其他功能,例如模板字符串或箭頭功能,它們都可以正常工作。 有任何想法嗎?

尚無支持TCO的環境。 Babel使用了實驗性的TCO轉換,但在v6中已將其刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM