BlockStatementControlFlowTransformer.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as estraverse from 'estraverse';
  4. import * as ESTree from 'estree';
  5. import { TControlFlowCustomNodeFactory } from '../../types/container/custom-nodes/TControlFlowCustomNodeFactory';
  6. import { IArrayUtils } from '../../interfaces/utils/IArrayUtils';
  7. import { ICustomNode } from '../../interfaces/custom-nodes/ICustomNode';
  8. import { IOptions } from '../../interfaces/options/IOptions';
  9. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  10. import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
  11. import { ControlFlowCustomNode } from '../../enums/container/custom-nodes/ControlFlowCustomNode';
  12. import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
  13. import { Node } from '../../node/Node';
  14. @injectable()
  15. export class BlockStatementControlFlowTransformer extends AbstractNodeTransformer {
  16. /**
  17. * @type {IArrayUtils}
  18. */
  19. private readonly arrayUtils: IArrayUtils;
  20. /**
  21. * @type {TControlFlowCustomNodeFactory}
  22. */
  23. private readonly controlFlowCustomNodeFactory: TControlFlowCustomNodeFactory;
  24. /**
  25. * @param {TControlFlowCustomNodeFactory} controlFlowCustomNodeFactory
  26. * @param {IArrayUtils} arrayUtils
  27. * @param {IRandomGenerator} randomGenerator
  28. * @param {IOptions} options
  29. */
  30. constructor (
  31. @inject(ServiceIdentifiers.Factory__IControlFlowCustomNode)
  32. controlFlowCustomNodeFactory: TControlFlowCustomNodeFactory,
  33. @inject(ServiceIdentifiers.IArrayUtils) arrayUtils: IArrayUtils,
  34. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  35. @inject(ServiceIdentifiers.IOptions) options: IOptions
  36. ) {
  37. super(randomGenerator, options);
  38. this.controlFlowCustomNodeFactory = controlFlowCustomNodeFactory;
  39. this.arrayUtils = arrayUtils;
  40. }
  41. /**
  42. * @param {BlockStatement} blockStatementNode
  43. * @returns {boolean}
  44. */
  45. private static blockStatementHasProhibitedStatements (blockStatementNode: ESTree.BlockStatement): boolean {
  46. return blockStatementNode.body.some((statement: ESTree.Statement) => {
  47. const isBreakOrContinueStatement: boolean = Node.isBreakStatementNode(statement) || Node.isContinueStatementNode(statement);
  48. const isVariableDeclarationWithLetOrConstKind: boolean = Node.isVariableDeclarationNode(statement)
  49. && (statement.kind === 'const' || statement.kind === 'let');
  50. return Node.isFunctionDeclarationNode(statement) || isBreakOrContinueStatement || isVariableDeclarationWithLetOrConstKind;
  51. });
  52. }
  53. /**
  54. * @param {BlockStatement} blockStatementNode
  55. * @returns {boolean}
  56. */
  57. private static canTransformBlockStatementNode (blockStatementNode: ESTree.BlockStatement): boolean {
  58. let canTransform: boolean = true;
  59. estraverse.traverse(blockStatementNode, {
  60. enter: (node: ESTree.Node): any => {
  61. if (Node.isWhileStatementNode(node)) {
  62. return estraverse.VisitorOption.Skip;
  63. }
  64. if (
  65. Node.isBlockStatementNode(node)
  66. && BlockStatementControlFlowTransformer.blockStatementHasProhibitedStatements(node)
  67. ) {
  68. canTransform = false;
  69. }
  70. }
  71. });
  72. if (blockStatementNode.body.length <= 4) {
  73. canTransform = false;
  74. }
  75. return canTransform;
  76. }
  77. /**
  78. * @return {IVisitor}
  79. */
  80. public getVisitor (): IVisitor {
  81. return {
  82. leave: (node: ESTree.Node, parentNode: ESTree.Node) => {
  83. if (Node.isBlockStatementNode(node)) {
  84. return this.transformNode(node, parentNode);
  85. }
  86. }
  87. };
  88. }
  89. /**
  90. * @param {BlockStatement} blockStatementNode
  91. * @param {Node} parentNode
  92. * @returns {Node}
  93. */
  94. public transformNode (blockStatementNode: ESTree.BlockStatement, parentNode: ESTree.Node): ESTree.Node {
  95. if (
  96. this.randomGenerator.getMathRandom() > this.options.controlFlowFlatteningThreshold ||
  97. !BlockStatementControlFlowTransformer.canTransformBlockStatementNode(blockStatementNode)
  98. ) {
  99. return blockStatementNode;
  100. }
  101. const blockStatementBody: ESTree.Statement[] = blockStatementNode.body;
  102. const originalKeys: number[] = this.arrayUtils.arrayRange(blockStatementBody.length);
  103. const shuffledKeys: number[] = this.arrayUtils.arrayShuffle(originalKeys);
  104. const originalKeysIndexesInShuffledArray: number[] = originalKeys.map((key: number) => shuffledKeys.indexOf(key));
  105. const blockStatementControlFlowFlatteningCustomNode: ICustomNode = this.controlFlowCustomNodeFactory(
  106. ControlFlowCustomNode.BlockStatementControlFlowFlatteningNode
  107. );
  108. blockStatementControlFlowFlatteningCustomNode.initialize(
  109. blockStatementBody,
  110. shuffledKeys,
  111. originalKeysIndexesInShuffledArray
  112. );
  113. return blockStatementControlFlowFlatteningCustomNode.getNode()[0];
  114. }
  115. }