我使用下面的示例来测试使用Babel和es2016预设的尾调用递归:
'use strict';
try {
function r(n) {
if (n%5000===0)
console.log(`reached a depth of ${n}`);
r(n+1);
}
r(0);
} catch (e) {
if (!(e instanceof RangeError))
throw e;
else
console.log('stack blown');
}我的package.json文件是:
{
"name": "tail-call-optimization",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "babel es6 --out-dir es5 --source-maps",
"watch": "babel es6 --out-dir es5 --source-maps --watch",
"start": "node es5/app.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-cli": "^6.6.5",
"babel-core": "^6.7.4",
"babel-loader": "^6.2.4",
"babel-polyfill": "^6.7.4",
"babel-preset-es2016": "^6.0.10",
"babel-runtime": "^6.6.1"
},
"dependencies": {
"babel-polyfill": "^6.7.4",
"source-map-support": "^0.4.0"
}
}..。.babelrc很简单:
{
"presets": ["es2016"]
}运行上述操作时:
npm run build && npm run start..。在以下控制台输出中的结果:
reached a depth of 0
reached a depth of 5000
reached a depth of 10000
reached a depth of 15000
stack blown实际上,查看es5目录中的转移文件,没有任何迹象表明已经实现了TCO。
我是不是遗漏了什么?
我的节点版本是4.3.2。
发布于 2016-04-06 15:57:57
目前没有一个“官方”Babel 6插件/预置实现TCO。babel-preset-es2016并不是一个“官方”预设。除非TCO依赖于巴比伦的解析器支持(我不这么认为,但我不确定),那么我想一个userland插件/预置可以实现它,而且可能实现它(但据我所知不是这样)。下面是跟踪最终“正式”重新实现的问题:T2614。如果有人想要公关这个链接到学习ES2015文档@Marcus提到在这里平我,我会合并它。
发布于 2016-04-06 10:07:58
查看:https://babeljs.io/docs/learn-es2015/ 1读到:
暂时移除在Babel 6 由于全局支持尾调用的复杂性和性能影响,只支持显式自引用尾递归。删除由于其他错误,并将重新实现。
所以我想它现在还没有实现。
https://stackoverflow.com/questions/36445393
复制相似问题