rest.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.visitor = undefined;
  4. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  5. var _getIterator3 = _interopRequireDefault(_getIterator2);
  6. var _babelTemplate = require("babel-template");
  7. var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
  8. var _babelTypes = require("babel-types");
  9. var t = _interopRequireWildcard(_babelTypes);
  10. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. var buildRest = (0, _babelTemplate2.default)("\n for (var LEN = ARGUMENTS.length,\n ARRAY = Array(ARRAY_LEN),\n KEY = START;\n KEY < LEN;\n KEY++) {\n ARRAY[ARRAY_KEY] = ARGUMENTS[KEY];\n }\n");
  13. var restIndex = (0, _babelTemplate2.default)("\n ARGUMENTS.length <= INDEX ? undefined : ARGUMENTS[INDEX]\n");
  14. var restIndexImpure = (0, _babelTemplate2.default)("\n REF = INDEX, ARGUMENTS.length <= REF ? undefined : ARGUMENTS[REF]\n");
  15. var restLength = (0, _babelTemplate2.default)("\n ARGUMENTS.length <= OFFSET ? 0 : ARGUMENTS.length - OFFSET\n");
  16. var memberExpressionOptimisationVisitor = {
  17. Scope: function Scope(path, state) {
  18. if (!path.scope.bindingIdentifierEquals(state.name, state.outerBinding)) {
  19. path.skip();
  20. }
  21. },
  22. Flow: function Flow(path) {
  23. if (path.isTypeCastExpression()) return;
  24. path.skip();
  25. },
  26. "Function|ClassProperty": function FunctionClassProperty(path, state) {
  27. var oldNoOptimise = state.noOptimise;
  28. state.noOptimise = true;
  29. path.traverse(memberExpressionOptimisationVisitor, state);
  30. state.noOptimise = oldNoOptimise;
  31. path.skip();
  32. },
  33. ReferencedIdentifier: function ReferencedIdentifier(path, state) {
  34. var node = path.node;
  35. if (node.name === "arguments") {
  36. state.deopted = true;
  37. }
  38. if (node.name !== state.name) return;
  39. if (state.noOptimise) {
  40. state.deopted = true;
  41. } else {
  42. var parentPath = path.parentPath;
  43. if (parentPath.listKey === "params" && parentPath.key < state.offset) {
  44. return;
  45. }
  46. if (parentPath.isMemberExpression({ object: node })) {
  47. var grandparentPath = parentPath.parentPath;
  48. var argsOptEligible = !state.deopted && !(grandparentPath.isAssignmentExpression() && parentPath.node === grandparentPath.node.left || grandparentPath.isLVal() || grandparentPath.isForXStatement() || grandparentPath.isUpdateExpression() || grandparentPath.isUnaryExpression({ operator: "delete" }) || (grandparentPath.isCallExpression() || grandparentPath.isNewExpression()) && parentPath.node === grandparentPath.node.callee);
  49. if (argsOptEligible) {
  50. if (parentPath.node.computed) {
  51. if (parentPath.get("property").isBaseType("number")) {
  52. state.candidates.push({ cause: "indexGetter", path: path });
  53. return;
  54. }
  55. } else if (parentPath.node.property.name === "length") {
  56. state.candidates.push({ cause: "lengthGetter", path: path });
  57. return;
  58. }
  59. }
  60. }
  61. if (state.offset === 0 && parentPath.isSpreadElement()) {
  62. var call = parentPath.parentPath;
  63. if (call.isCallExpression() && call.node.arguments.length === 1) {
  64. state.candidates.push({ cause: "argSpread", path: path });
  65. return;
  66. }
  67. }
  68. state.references.push(path);
  69. }
  70. },
  71. BindingIdentifier: function BindingIdentifier(_ref, state) {
  72. var node = _ref.node;
  73. if (node.name === state.name) {
  74. state.deopted = true;
  75. }
  76. }
  77. };
  78. function hasRest(node) {
  79. return t.isRestElement(node.params[node.params.length - 1]);
  80. }
  81. function optimiseIndexGetter(path, argsId, offset) {
  82. var index = void 0;
  83. if (t.isNumericLiteral(path.parent.property)) {
  84. index = t.numericLiteral(path.parent.property.value + offset);
  85. } else if (offset === 0) {
  86. index = path.parent.property;
  87. } else {
  88. index = t.binaryExpression("+", path.parent.property, t.numericLiteral(offset));
  89. }
  90. var scope = path.scope;
  91. if (!scope.isPure(index)) {
  92. var temp = scope.generateUidIdentifierBasedOnNode(index);
  93. scope.push({ id: temp, kind: "var" });
  94. path.parentPath.replaceWith(restIndexImpure({
  95. ARGUMENTS: argsId,
  96. INDEX: index,
  97. REF: temp
  98. }));
  99. } else {
  100. path.parentPath.replaceWith(restIndex({
  101. ARGUMENTS: argsId,
  102. INDEX: index
  103. }));
  104. }
  105. }
  106. function optimiseLengthGetter(path, argsId, offset) {
  107. if (offset) {
  108. path.parentPath.replaceWith(restLength({
  109. ARGUMENTS: argsId,
  110. OFFSET: t.numericLiteral(offset)
  111. }));
  112. } else {
  113. path.replaceWith(argsId);
  114. }
  115. }
  116. var visitor = exports.visitor = {
  117. Function: function Function(path) {
  118. var node = path.node,
  119. scope = path.scope;
  120. if (!hasRest(node)) return;
  121. var rest = node.params.pop().argument;
  122. var argsId = t.identifier("arguments");
  123. argsId._shadowedFunctionLiteral = path;
  124. var state = {
  125. references: [],
  126. offset: node.params.length,
  127. argumentsNode: argsId,
  128. outerBinding: scope.getBindingIdentifier(rest.name),
  129. candidates: [],
  130. name: rest.name,
  131. deopted: false
  132. };
  133. path.traverse(memberExpressionOptimisationVisitor, state);
  134. if (!state.deopted && !state.references.length) {
  135. for (var _iterator = state.candidates, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  136. var _ref3;
  137. if (_isArray) {
  138. if (_i >= _iterator.length) break;
  139. _ref3 = _iterator[_i++];
  140. } else {
  141. _i = _iterator.next();
  142. if (_i.done) break;
  143. _ref3 = _i.value;
  144. }
  145. var _ref4 = _ref3;
  146. var _path = _ref4.path,
  147. cause = _ref4.cause;
  148. switch (cause) {
  149. case "indexGetter":
  150. optimiseIndexGetter(_path, argsId, state.offset);
  151. break;
  152. case "lengthGetter":
  153. optimiseLengthGetter(_path, argsId, state.offset);
  154. break;
  155. default:
  156. _path.replaceWith(argsId);
  157. }
  158. }
  159. return;
  160. }
  161. state.references = state.references.concat(state.candidates.map(function (_ref5) {
  162. var path = _ref5.path;
  163. return path;
  164. }));
  165. state.deopted = state.deopted || !!node.shadow;
  166. var start = t.numericLiteral(node.params.length);
  167. var key = scope.generateUidIdentifier("key");
  168. var len = scope.generateUidIdentifier("len");
  169. var arrKey = key;
  170. var arrLen = len;
  171. if (node.params.length) {
  172. arrKey = t.binaryExpression("-", key, start);
  173. arrLen = t.conditionalExpression(t.binaryExpression(">", len, start), t.binaryExpression("-", len, start), t.numericLiteral(0));
  174. }
  175. var loop = buildRest({
  176. ARGUMENTS: argsId,
  177. ARRAY_KEY: arrKey,
  178. ARRAY_LEN: arrLen,
  179. START: start,
  180. ARRAY: rest,
  181. KEY: key,
  182. LEN: len
  183. });
  184. if (state.deopted) {
  185. loop._blockHoist = node.params.length + 1;
  186. node.body.body.unshift(loop);
  187. } else {
  188. loop._blockHoist = 1;
  189. var target = path.getEarliestCommonAncestorFrom(state.references).getStatementParent();
  190. target.findParent(function (path) {
  191. if (path.isLoop()) {
  192. target = path;
  193. } else {
  194. return path.isFunction();
  195. }
  196. });
  197. target.insertBefore(loop);
  198. }
  199. }
  200. };