SelfDefendingCodeHelperGroup.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
  3. import { TCustomCodeHelperFactory } from '../../../types/container/custom-code-helpers/TCustomCodeHelperFactory';
  4. import { TIdentifierNamesGeneratorFactory } from '../../../types/container/generators/TIdentifierNamesGeneratorFactory';
  5. import { TInitialData } from '../../../types/TInitialData';
  6. import { TNodeWithLexicalScope } from '../../../types/node/TNodeWithLexicalScope';
  7. import { TNodeWithStatements } from '../../../types/node/TNodeWithStatements';
  8. import { ICustomCodeHelper } from '../../../interfaces/custom-code-helpers/ICustomCodeHelper';
  9. import { IOptions } from '../../../interfaces/options/IOptions';
  10. import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
  11. import { ICallsGraphData } from '../../../interfaces/analyzers/calls-graph-analyzer/ICallsGraphData';
  12. import { initializable } from '../../../decorators/Initializable';
  13. import { CustomCodeHelper } from '../../../enums/custom-code-helpers/CustomCodeHelper';
  14. import { NodeTransformationStage } from '../../../enums/node-transformers/NodeTransformationStage';
  15. import { AbstractCustomCodeHelperGroup } from '../../AbstractCustomCodeHelperGroup';
  16. import { CallsControllerFunctionCodeHelper } from '../../calls-controller/CallsControllerFunctionCodeHelper';
  17. import { NodeAppender } from '../../../node/NodeAppender';
  18. import { NodeLexicalScopeUtils } from '../../../node/NodeLexicalScopeUtils';
  19. import { SelfDefendingCodeHelper } from '../SelfDefendingCodeHelper';
  20. @injectable()
  21. export class SelfDefendingCodeHelperGroup extends AbstractCustomCodeHelperGroup {
  22. /**
  23. * @type {Map<CustomCodeHelper, ICustomCodeHelper>}
  24. */
  25. @initializable()
  26. protected customCodeHelpers!: Map <CustomCodeHelper, ICustomCodeHelper>;
  27. /**
  28. * @type {TCustomCodeHelperFactory}
  29. */
  30. private readonly customCodeHelperFactory: TCustomCodeHelperFactory;
  31. /**
  32. * @param {TCustomCodeHelperFactory} customCodeHelperFactory
  33. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  34. * @param {IRandomGenerator} randomGenerator
  35. * @param {IOptions} options
  36. */
  37. public constructor (
  38. @inject(ServiceIdentifiers.Factory__ICustomCodeHelper) customCodeHelperFactory: TCustomCodeHelperFactory,
  39. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  40. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  41. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  42. @inject(ServiceIdentifiers.IOptions) options: IOptions
  43. ) {
  44. super(identifierNamesGeneratorFactory, randomGenerator, options);
  45. this.customCodeHelperFactory = customCodeHelperFactory;
  46. }
  47. /**
  48. * @param {TNodeWithStatements} nodeWithStatements
  49. * @param {ICallsGraphData[]} callsGraphData
  50. */
  51. public appendOnPreparingStage (nodeWithStatements: TNodeWithStatements, callsGraphData: ICallsGraphData[]): void {
  52. if (!this.options.selfDefending) {
  53. return;
  54. }
  55. const randomCallsGraphIndex: number = this.getRandomCallsGraphIndex(callsGraphData.length);
  56. const selfDefendingFunctionHostNode: TNodeWithStatements = callsGraphData.length
  57. ? NodeAppender.getOptimalBlockScope(callsGraphData, randomCallsGraphIndex)
  58. : nodeWithStatements;
  59. const callsControllerHostNode: TNodeWithStatements = callsGraphData.length
  60. ? NodeAppender.getOptimalBlockScope(callsGraphData, randomCallsGraphIndex, 1)
  61. : nodeWithStatements;
  62. const selfDefendingFunctionLexicalScopeNode: TNodeWithLexicalScope | null = NodeLexicalScopeUtils
  63. .getLexicalScope(selfDefendingFunctionHostNode) ?? null;
  64. const selfDefendingFunctionName: string = selfDefendingFunctionLexicalScopeNode
  65. ? this.identifierNamesGenerator.generate(selfDefendingFunctionLexicalScopeNode)
  66. : this.identifierNamesGenerator.generateNext();
  67. const callsControllerFunctionName: string = selfDefendingFunctionLexicalScopeNode
  68. ? this.identifierNamesGenerator.generate(selfDefendingFunctionLexicalScopeNode)
  69. : this.identifierNamesGenerator.generateNext();
  70. // selfDefendingUnicode helper nodes append
  71. this.appendCustomNodeIfExist(
  72. CustomCodeHelper.SelfDefending,
  73. (customCodeHelper: ICustomCodeHelper<TInitialData<SelfDefendingCodeHelper>>) => {
  74. customCodeHelper.initialize(callsControllerFunctionName, selfDefendingFunctionName);
  75. NodeAppender.prepend(selfDefendingFunctionHostNode, customCodeHelper.getNode());
  76. }
  77. );
  78. // nodeCallsControllerFunction helper nodes append
  79. this.appendCustomNodeIfExist(
  80. CustomCodeHelper.CallsControllerFunction,
  81. (customCodeHelper: ICustomCodeHelper<TInitialData<CallsControllerFunctionCodeHelper>>) => {
  82. customCodeHelper.initialize(NodeTransformationStage.Preparing, callsControllerFunctionName);
  83. NodeAppender.prepend(callsControllerHostNode, customCodeHelper.getNode());
  84. }
  85. );
  86. }
  87. public initialize (): void {
  88. this.customCodeHelpers = new Map <CustomCodeHelper, ICustomCodeHelper>();
  89. if (!this.options.selfDefending) {
  90. return;
  91. }
  92. const selfDefendingCodeHelper: ICustomCodeHelper<TInitialData<SelfDefendingCodeHelper>> =
  93. this.customCodeHelperFactory(CustomCodeHelper.SelfDefending);
  94. const callsControllerFunctionCodeHelper: ICustomCodeHelper<TInitialData<CallsControllerFunctionCodeHelper>> =
  95. this.customCodeHelperFactory(CustomCodeHelper.CallsControllerFunction);
  96. this.customCodeHelpers.set(CustomCodeHelper.SelfDefending, selfDefendingCodeHelper);
  97. this.customCodeHelpers.set(CustomCodeHelper.CallsControllerFunction, callsControllerFunctionCodeHelper);
  98. }
  99. }