js-confuser 1.5.9 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. package/.github/workflows/node.js.yml +2 -2
  2. package/CHANGELOG.md +55 -0
  3. package/README.md +346 -165
  4. package/dist/constants.js +6 -2
  5. package/dist/index.js +9 -21
  6. package/dist/obfuscator.js +19 -31
  7. package/dist/options.js +5 -5
  8. package/dist/order.js +1 -3
  9. package/dist/presets.js +6 -7
  10. package/dist/probability.js +2 -4
  11. package/dist/templates/bufferToString.js +13 -0
  12. package/dist/templates/crash.js +3 -3
  13. package/dist/templates/es5.js +18 -0
  14. package/dist/templates/functionLength.js +16 -0
  15. package/dist/transforms/calculator.js +77 -21
  16. package/dist/transforms/controlFlowFlattening/controlFlowFlattening.js +980 -367
  17. package/dist/transforms/controlFlowFlattening/expressionObfuscation.js +4 -1
  18. package/dist/transforms/controlFlowFlattening/switchCaseObfuscation.js +25 -26
  19. package/dist/transforms/deadCode.js +33 -25
  20. package/dist/transforms/dispatcher.js +8 -4
  21. package/dist/transforms/es5/antiDestructuring.js +2 -0
  22. package/dist/transforms/es5/es5.js +31 -34
  23. package/dist/transforms/extraction/duplicateLiteralsRemoval.js +92 -58
  24. package/dist/transforms/finalizer.js +82 -0
  25. package/dist/transforms/flatten.js +229 -148
  26. package/dist/transforms/identifier/globalAnalysis.js +88 -0
  27. package/dist/transforms/identifier/globalConcealing.js +10 -83
  28. package/dist/transforms/identifier/movedDeclarations.js +35 -88
  29. package/dist/transforms/identifier/renameVariables.js +124 -59
  30. package/dist/transforms/identifier/variableAnalysis.js +58 -62
  31. package/dist/transforms/lock/lock.js +0 -37
  32. package/dist/transforms/minify.js +60 -57
  33. package/dist/transforms/opaquePredicates.js +1 -1
  34. package/dist/transforms/preparation/preparation.js +2 -2
  35. package/dist/transforms/preparation.js +231 -0
  36. package/dist/transforms/renameLabels.js +1 -1
  37. package/dist/transforms/rgf.js +139 -247
  38. package/dist/transforms/stack.js +128 -26
  39. package/dist/transforms/string/encoding.js +150 -179
  40. package/dist/transforms/string/stringCompression.js +14 -15
  41. package/dist/transforms/string/stringConcealing.js +25 -8
  42. package/dist/transforms/string/stringEncoding.js +13 -24
  43. package/dist/transforms/transform.js +12 -19
  44. package/dist/traverse.js +24 -10
  45. package/dist/util/gen.js +17 -1
  46. package/dist/util/identifiers.js +37 -3
  47. package/dist/util/insert.js +35 -4
  48. package/dist/util/random.js +15 -0
  49. package/docs/ControlFlowFlattening.md +595 -0
  50. package/{Countermeasures.md → docs/Countermeasures.md} +1 -15
  51. package/{Integrity.md → docs/Integrity.md} +2 -2
  52. package/docs/RGF.md +419 -0
  53. package/package.json +5 -5
  54. package/src/constants.ts +3 -0
  55. package/src/index.ts +2 -2
  56. package/src/obfuscator.ts +19 -31
  57. package/src/options.ts +14 -103
  58. package/src/order.ts +1 -5
  59. package/src/presets.ts +6 -7
  60. package/src/probability.ts +2 -3
  61. package/src/templates/bufferToString.ts +68 -0
  62. package/src/templates/crash.ts +15 -19
  63. package/src/templates/es5.ts +131 -0
  64. package/src/templates/functionLength.ts +14 -0
  65. package/src/transforms/calculator.ts +122 -59
  66. package/src/transforms/controlFlowFlattening/controlFlowFlattening.ts +1583 -571
  67. package/src/transforms/controlFlowFlattening/expressionObfuscation.ts +4 -1
  68. package/src/transforms/deadCode.ts +383 -26
  69. package/src/transforms/dispatcher.ts +9 -4
  70. package/src/transforms/es5/antiDestructuring.ts +2 -0
  71. package/src/transforms/es5/es5.ts +32 -77
  72. package/src/transforms/extraction/duplicateLiteralsRemoval.ts +133 -129
  73. package/src/transforms/{hexadecimalNumbers.ts → finalizer.ts} +29 -13
  74. package/src/transforms/flatten.ts +357 -300
  75. package/src/transforms/identifier/globalAnalysis.ts +85 -0
  76. package/src/transforms/identifier/globalConcealing.ts +14 -103
  77. package/src/transforms/identifier/movedDeclarations.ts +49 -102
  78. package/src/transforms/identifier/renameVariables.ts +149 -78
  79. package/src/transforms/identifier/variableAnalysis.ts +66 -73
  80. package/src/transforms/lock/lock.ts +1 -42
  81. package/src/transforms/minify.ts +91 -75
  82. package/src/transforms/opaquePredicates.ts +2 -2
  83. package/src/transforms/preparation.ts +238 -0
  84. package/src/transforms/renameLabels.ts +2 -2
  85. package/src/transforms/rgf.ts +213 -405
  86. package/src/transforms/stack.ts +156 -36
  87. package/src/transforms/string/encoding.ts +115 -212
  88. package/src/transforms/string/stringCompression.ts +27 -18
  89. package/src/transforms/string/stringConcealing.ts +39 -9
  90. package/src/transforms/string/stringEncoding.ts +18 -18
  91. package/src/transforms/transform.ts +21 -23
  92. package/src/traverse.ts +23 -4
  93. package/src/types.ts +2 -1
  94. package/src/util/gen.ts +28 -3
  95. package/src/util/identifiers.ts +43 -2
  96. package/src/util/insert.ts +38 -3
  97. package/src/util/random.ts +13 -0
  98. package/test/code/Cash.test.ts +1 -1
  99. package/test/code/Dynamic.test.ts +12 -10
  100. package/test/code/ES6.src.js +146 -0
  101. package/test/code/ES6.test.ts +28 -2
  102. package/test/index.test.ts +2 -1
  103. package/test/probability.test.ts +44 -0
  104. package/test/templates/template.test.ts +1 -1
  105. package/test/transforms/antiTooling.test.ts +22 -0
  106. package/test/transforms/calculator.test.ts +40 -0
  107. package/test/transforms/controlFlowFlattening/controlFlowFlattening.test.ts +702 -160
  108. package/test/transforms/controlFlowFlattening/expressionObfuscation.test.ts +173 -0
  109. package/test/transforms/deadCode.test.ts +66 -15
  110. package/test/transforms/dispatcher.test.ts +20 -1
  111. package/test/transforms/es5/antiDestructuring.test.ts +16 -0
  112. package/test/transforms/flatten.test.ts +399 -86
  113. package/test/transforms/identifier/movedDeclarations.test.ts +63 -8
  114. package/test/transforms/identifier/renameVariables.test.ts +119 -0
  115. package/test/transforms/lock/antiDebug.test.ts +2 -2
  116. package/test/transforms/lock/lock.test.ts +1 -48
  117. package/test/transforms/minify.test.ts +104 -0
  118. package/test/transforms/preparation.test.ts +157 -0
  119. package/test/transforms/rgf.test.ts +261 -381
  120. package/test/transforms/stack.test.ts +143 -21
  121. package/test/transforms/string/stringCompression.test.ts +39 -0
  122. package/test/transforms/string/stringConcealing.test.ts +82 -0
  123. package/test/transforms/string/stringEncoding.test.ts +53 -2
  124. package/test/transforms/transform.test.ts +66 -0
  125. package/test/traverse.test.ts +139 -0
  126. package/test/util/identifiers.test.ts +113 -1
  127. package/test/util/insert.test.ts +57 -3
  128. package/src/transforms/controlFlowFlattening/choiceFlowObfuscation.ts +0 -87
  129. package/src/transforms/controlFlowFlattening/controlFlowObfuscation.ts +0 -203
  130. package/src/transforms/controlFlowFlattening/switchCaseObfuscation.ts +0 -130
  131. package/src/transforms/eval.ts +0 -89
  132. package/src/transforms/hideInitializingCode.ts +0 -432
  133. package/src/transforms/identifier/nameRecycling.ts +0 -280
  134. package/src/transforms/label.ts +0 -64
  135. package/src/transforms/preparation/nameConflicts.ts +0 -102
  136. package/src/transforms/preparation/preparation.ts +0 -176
  137. package/test/transforms/controlFlowFlattening/controlFlowObfuscation.test.ts +0 -101
  138. package/test/transforms/controlFlowFlattening/switchCaseObfuscation.test.ts +0 -120
  139. package/test/transforms/eval.test.ts +0 -131
  140. package/test/transforms/hideInitializingCode.test.ts +0 -336
  141. package/test/transforms/identifier/nameRecycling.test.ts +0 -205
  142. package/test/transforms/preparation/nameConflicts.test.ts +0 -52
  143. package/test/transforms/preparation/preparation.test.ts +0 -62
package/src/options.ts CHANGED
@@ -10,9 +10,9 @@ export interface ObfuscateOptions {
10
10
  *
11
11
  * | Preset | Transforms | Performance Reduction | Sample |
12
12
  * | --- | --- | --- | --- |
13
- * | High | 21/22 | 98% | [Sample](https://github.com/MichaelXF/js-confuser/blob/master/samples/high.js) |
14
- * | Medium | 15/22 | 52% | [Sample](https://github.com/MichaelXF/js-confuser/blob/master/samples/medium.js) |
15
- * | Low | 10/22 | 30% | [Sample](https://github.com/MichaelXF/js-confuser/blob/master/samples/low.js) |
13
+ * | High | 22/25 | 98% | [Sample](https://github.com/MichaelXF/js-confuser/blob/master/samples/high.js) |
14
+ * | Medium | 19/25 | 52% | [Sample](https://github.com/MichaelXF/js-confuser/blob/master/samples/medium.js) |
15
+ * | Low | 15/25 | 30% | [Sample](https://github.com/MichaelXF/js-confuser/blob/master/samples/low.js) |
16
16
  *
17
17
  * You can extend each preset or all go without them entirely. (`"high"/"medium"/"low"`)
18
18
  *
@@ -53,7 +53,7 @@ export interface ObfuscateOptions {
53
53
  /**
54
54
  * ### `hexadecimalNumbers`
55
55
  *
56
- * Uses the hexadecimal representation (`50` -> `0x32`) for numbers. (`true/false`)
56
+ * Uses the hexadecimal representation for numbers. (`true/false`)
57
57
  */
58
58
  hexadecimalNumbers?: boolean;
59
59
 
@@ -141,37 +141,6 @@ export interface ObfuscateOptions {
141
141
  "hexadecimal" | "randomized" | "zeroWidth" | "mangled" | "number"
142
142
  >;
143
143
 
144
- /**
145
- * ### `nameRecycling`
146
- *
147
- * Attempts to reuse released names.
148
- *
149
- * - Potency Medium
150
- * - Resilience High
151
- * - Cost Low
152
- *
153
- * ```js
154
- * // Input
155
- * function percentage(x) {
156
- * var multiplied = x * 100;
157
- * var floored = Math.floor(multiplied);
158
- * var output = floored + "%"
159
- * return output;
160
- * }
161
- *
162
- * // Output
163
- * function percentage(x) {
164
- * var multiplied = x * 100;
165
- * var floored = Math.floor(multiplied);
166
- * multiplied = floored + "%";
167
- * return multiplied;
168
- * }
169
- * ```
170
- *
171
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
172
- */
173
- nameRecycling?: ProbabilityMap<boolean>;
174
-
175
144
  /**
176
145
  * ### `controlFlowFlattening`
177
146
  *
@@ -189,19 +158,6 @@ export interface ObfuscateOptions {
189
158
  */
190
159
  controlFlowFlattening?: ProbabilityMap<boolean>;
191
160
 
192
- /**
193
- * ### `hideInitializingCode`
194
- *
195
- * Hides initializing code with complex ternary expressions. (`true/false`)
196
- *
197
- * - Potency High
198
- * - Resilience High
199
- * - Cost High
200
- *
201
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
202
- */
203
- hideInitializingCode?: ProbabilityMap<boolean>;
204
-
205
161
  /**
206
162
  * ### `globalConcealing`
207
163
  *
@@ -297,48 +253,15 @@ export interface ObfuscateOptions {
297
253
  */
298
254
  dispatcher?: ProbabilityMap<boolean>;
299
255
 
300
- /**
301
- * ### `eval`
302
- *
303
- * #### **`Security Warning`**
304
- *
305
- * From [MDN](<(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval)**>): Executing JavaScript from a string is an enormous security risk. It is far too easy
306
- * for a bad actor to run arbitrary code when you use eval(). Never use eval()!
307
- *
308
- * Wraps defined functions within eval statements.
309
- *
310
- * - **`false`** - Avoids using the `eval` function. _Default_.
311
- * - **`true`** - Wraps function's code into an `eval` statement.
312
- *
313
- * ```js
314
- * // Output.js
315
- * var Q4r1__ = {
316
- * Oo$Oz8t: eval(
317
- * "(function(YjVpAp){var gniSBq6=kHmsJrhOO;switch(gniSBq6){case'RW11Hj5x':return console;}});"
318
- * ),
319
- * };
320
- * Q4r1__.Oo$Oz8t("RW11Hj5x");
321
- * ```
322
- *
323
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
324
- */
325
- eval?: ProbabilityMap<boolean>;
326
-
327
256
  /**
328
257
  * ### `rgf`
329
258
  *
330
259
  * RGF (Runtime-Generated-Functions) uses the [`new Function(code...)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function) syntax to construct executable code from strings. (`"all"/true/false`)
331
260
  *
332
- * - **This can break your code. This is also as dangerous as `eval`.**
261
+ * - **This can break your code.
333
262
  * - **Due to the security concerns of arbitrary code execution, you must enable this yourself.**
334
263
  * - The arbitrary code is also obfuscated.
335
264
  *
336
- * | Mode | Description |
337
- * | --- | --- |
338
- * | `"all"` | Recursively applies to every scope (slow) |
339
- * | `true` | Applies to the top level only |
340
- * | `false` | Feature disabled |
341
- *
342
265
  * ```js
343
266
  * // Input
344
267
  * function log(x){
@@ -353,7 +276,7 @@ export interface ObfuscateOptions {
353
276
  *
354
277
  * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
355
278
  */
356
- rgf?: ProbabilityMap<boolean | "all">;
279
+ rgf?: ProbabilityMap<boolean>;
357
280
 
358
281
  /**
359
282
  * ### `stack`
@@ -483,20 +406,6 @@ export interface ObfuscateOptions {
483
406
  */
484
407
  context?: string[];
485
408
 
486
- /**
487
- * ### `lock.nativeFunctions`
488
- *
489
- * Set of global functions that are native. Such as `require`, `fetch`. If these variables are modified the program crashes.
490
- * Set to `true` to use the default set of native functions. (`string[]/true/false`)
491
- *
492
- * - Potency Low
493
- * - Resilience Medium
494
- * - Cost Medium
495
- *
496
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
497
- */
498
- nativeFunctions?: string[] | Set<string> | boolean;
499
-
500
409
  /**
501
410
  * ### `lock.startDate`
502
411
  *
@@ -689,9 +598,7 @@ const validProperties = new Set([
689
598
  "renameVariables",
690
599
  "renameGlobals",
691
600
  "identifierGenerator",
692
- "nameRecycling",
693
601
  "controlFlowFlattening",
694
- "hideInitializingCode",
695
602
  "globalConcealing",
696
603
  "stringCompression",
697
604
  "stringConcealing",
@@ -699,7 +606,6 @@ const validProperties = new Set([
699
606
  "stringSplitting",
700
607
  "duplicateLiteralsRemoval",
701
608
  "dispatcher",
702
- "eval",
703
609
  "rgf",
704
610
  "objectExtraction",
705
611
  "flatten",
@@ -726,7 +632,7 @@ const validBrowsers = new Set([
726
632
  ]);
727
633
 
728
634
  export function validateOptions(options: ObfuscateOptions) {
729
- if (Object.keys(options).length <= 1) {
635
+ if (!options || Object.keys(options).length <= 1) {
730
636
  /**
731
637
  * Give a welcoming introduction to those who skipped the documentation.
732
638
  */
@@ -880,6 +786,10 @@ export async function correctOptions(
880
786
  "alert",
881
787
  "confirm",
882
788
  "location",
789
+ "btoa",
790
+ "atob",
791
+ "unescape",
792
+ "encodeURIComponent",
883
793
  ].forEach((x) => options.globalVariables.add(x));
884
794
  } else {
885
795
  // node
@@ -888,6 +798,8 @@ export async function correctOptions(
888
798
  "Buffer",
889
799
  "require",
890
800
  "process",
801
+ "exports",
802
+ "module",
891
803
  "__dirname",
892
804
  "__filename",
893
805
  ].forEach((x) => options.globalVariables.add(x));
@@ -899,6 +811,7 @@ export async function correctOptions(
899
811
  "parseInt",
900
812
  "parseFloat",
901
813
  "Math",
814
+ "JSON",
902
815
  "Promise",
903
816
  "String",
904
817
  "Boolean",
@@ -918,8 +831,6 @@ export async function correctOptions(
918
831
  "setImmediate",
919
832
  "clearImmediate",
920
833
  "queueMicrotask",
921
- "exports",
922
- "module",
923
834
  "isNaN",
924
835
  "isFinite",
925
836
  "Set",
package/src/order.ts CHANGED
@@ -32,8 +32,6 @@ export enum ObfuscateOrder {
32
32
 
33
33
  StringCompression = 18,
34
34
 
35
- HideInitializingCode = 19,
36
-
37
35
  Stack = 20,
38
36
 
39
37
  DuplicateLiteralsRemoval = 22,
@@ -52,9 +50,7 @@ export enum ObfuscateOrder {
52
50
 
53
51
  ES5 = 31,
54
52
 
55
- StringEncoding = 32,
56
-
57
53
  AntiTooling = 34,
58
54
 
59
- HexadecimalNumbers = 35,
55
+ Finalizer = 35,
60
56
  }
package/src/presets.ts CHANGED
@@ -17,7 +17,7 @@ import { ObfuscateOptions } from "./options";
17
17
  * 10. Minified output
18
18
  *
19
19
  * ## **`Disabled features`**
20
- * - `eval` Use at your own risk!
20
+ * - `rgf` Use at your own risk!
21
21
  *
22
22
  * ### Potential Issues
23
23
  * 1. *String Encoding* can corrupt files. Disable `stringEncoding` manually if this happens.
@@ -51,7 +51,6 @@ const highPreset: ObfuscateOptions = {
51
51
  stringSplitting: 0.75,
52
52
 
53
53
  // Use at own risk
54
- eval: false,
55
54
  rgf: false,
56
55
  };
57
56
 
@@ -66,9 +65,9 @@ const mediumPreset: ObfuscateOptions = {
66
65
  calculator: true,
67
66
  compact: true,
68
67
  hexadecimalNumbers: true,
69
- controlFlowFlattening: 0.5,
68
+ controlFlowFlattening: 0.25,
70
69
  deadCode: 0.025,
71
- dispatcher: 0.75,
70
+ dispatcher: 0.5,
72
71
  duplicateLiteralsRemoval: 0.5,
73
72
  globalConcealing: true,
74
73
  identifierGenerator: "randomized",
@@ -95,10 +94,10 @@ const lowPreset: ObfuscateOptions = {
95
94
  calculator: true,
96
95
  compact: true,
97
96
  hexadecimalNumbers: true,
98
- controlFlowFlattening: 0.25,
97
+ controlFlowFlattening: 0.1,
99
98
  deadCode: 0.01,
100
- dispatcher: 0.5,
101
- duplicateLiteralsRemoval: true,
99
+ dispatcher: 0.25,
100
+ duplicateLiteralsRemoval: 0.5,
102
101
  identifierGenerator: "randomized",
103
102
  minify: true,
104
103
  movedDeclarations: true,
@@ -87,6 +87,8 @@ export function ComputeProbabilityMap<T>(
87
87
  * @param map
88
88
  */
89
89
  export function isProbabilityMapProbable<T>(map: ProbabilityMap<T>): boolean {
90
+ ok(!Number.isNaN(map), "Numbers cannot be NaN");
91
+
90
92
  if (!map || typeof map === "undefined") {
91
93
  return false;
92
94
  }
@@ -97,9 +99,6 @@ export function isProbabilityMapProbable<T>(map: ProbabilityMap<T>): boolean {
97
99
  if (map > 1 || map < 0) {
98
100
  throw new Error(`Numbers must be between 0 and 1 for 0% - 100%`);
99
101
  }
100
- if (isNaN(map)) {
101
- throw new Error("Numbers cannot be NaN");
102
- }
103
102
  }
104
103
  if (Array.isArray(map)) {
105
104
  ok(
@@ -0,0 +1,68 @@
1
+ import Template from "./template";
2
+
3
+ export const BufferToStringTemplate = Template(`
4
+ function __getGlobal(){
5
+ try {
6
+ return global||window|| ( new Function("return this") )();
7
+ } catch ( e ) {
8
+ try {
9
+ return this;
10
+ } catch ( e ) {
11
+ return {};
12
+ }
13
+ }
14
+ }
15
+
16
+ var __globalObject = __getGlobal() || {};
17
+ var __TextDecoder = __globalObject["TextDecoder"];
18
+ var __Uint8Array = __globalObject["Uint8Array"];
19
+ var __Buffer = __globalObject["Buffer"];
20
+ var __String = __globalObject["String"] || String;
21
+ var __Array = __globalObject["Array"] || Array;
22
+
23
+ var utf8ArrayToStr = (function () {
24
+ var charCache = new __Array(128); // Preallocate the cache for the common single byte chars
25
+ var charFromCodePt = __String["fromCodePoint"] || __String["fromCharCode"];
26
+ var result = [];
27
+
28
+ return function (array) {
29
+ var codePt, byte1;
30
+ var buffLen = array["length"];
31
+
32
+ result["length"] = 0;
33
+
34
+ for (var i = 0; i < buffLen;) {
35
+ byte1 = array[i++];
36
+
37
+ if (byte1 <= 0x7F) {
38
+ codePt = byte1;
39
+ } else if (byte1 <= 0xDF) {
40
+ codePt = ((byte1 & 0x1F) << 6) | (array[i++] & 0x3F);
41
+ } else if (byte1 <= 0xEF) {
42
+ codePt = ((byte1 & 0x0F) << 12) | ((array[i++] & 0x3F) << 6) | (array[i++] & 0x3F);
43
+ } else if (__String["fromCodePoint"]) {
44
+ codePt = ((byte1 & 0x07) << 18) | ((array[i++] & 0x3F) << 12) | ((array[i++] & 0x3F) << 6) | (array[i++] & 0x3F);
45
+ } else {
46
+ codePt = 63; // Cannot convert four byte code points, so use "?" instead
47
+ i += 3;
48
+ }
49
+
50
+ result["push"](charCache[codePt] || (charCache[codePt] = charFromCodePt(codePt)));
51
+ }
52
+
53
+ return result["join"]('');
54
+ };
55
+ })();
56
+
57
+ function {name}(buffer){
58
+ if(typeof __TextDecoder !== "undefined" && __TextDecoder) {
59
+ return new __TextDecoder()["decode"](new __Uint8Array(buffer));
60
+ } else if(typeof __Buffer !== "undefined" && __Buffer) {
61
+ return __Buffer["from"](buffer)["toString"]("utf-8");
62
+ } else {
63
+ return utf8ArrayToStr(buffer);
64
+ }
65
+ }
66
+
67
+
68
+ `);
@@ -1,28 +1,24 @@
1
1
  import Template from "./template";
2
2
 
3
3
  export const CrashTemplate1 = Template(`
4
- try {
5
- if(typeof process !== "undefined") {
6
- Math.random() > 0.5 && process && process.exit();
7
- }
8
- } catch ( e ) {
9
-
10
- }
11
-
12
4
  var {var} = "a";
13
5
  while(1){
14
- {var} = {var} += "a"; //add as much as the browser can handle
6
+ {var} = {var} += "a";
15
7
  }
16
8
  `);
17
9
 
18
10
  export const CrashTemplate2 = Template(`
19
11
  while(true) {
20
- for(var {var} = 99; {var} == {var}; {var} *= {var}) {
12
+ var {var} = 99;
13
+ for({var} = 99; {var} == {var}; {var} *= {var}) {
21
14
  !{var} && console.log({var});
22
15
  if ({var} <= 10){
23
16
  break;
24
17
  }
25
18
  };
19
+ if({var} === 100) {
20
+ {var}--
21
+ }
26
22
  };`);
27
23
 
28
24
  export const CrashTemplate3 = Template(`
@@ -44,16 +40,16 @@ try {
44
40
  });
45
41
 
46
42
  {$1}();
47
- } catch ( e ) {
48
- while(e ? e : !e){
49
- var b;
50
- var c = 0;
51
- (e ? !e : e) ? (function(e){
52
- c = e ? 0 : !e ? 1 : 0;
53
- })(e) : b = 1;
43
+ } catch ( {$1}e ) {
44
+ while({$1}e ? {$1}e : !{$1}e){
45
+ var {$1}b;
46
+ var {$1}c = 0;
47
+ ({$1}e ? !{$1}e : {$1}e) ? (function({$1}e){
48
+ {$1}c = {$1}e ? 0 : !{$1}e ? 1 : 0;
49
+ })({$1}e) : {$1}b = 1;
54
50
 
55
- if(b&&c){break;}
56
- if(b){continue;}
51
+ if({$1}b&&{$1}c){break;}
52
+ if({$1}b){continue;}
57
53
  }
58
54
  }
59
55
  `);
@@ -0,0 +1,131 @@
1
+ import Template from "./template";
2
+
3
+ /**
4
+ * Provides ES5 polyfills for Array methods
5
+ *
6
+ * Source: https://vanillajstoolkit.com/polyfills/
7
+ */
8
+ export const ES5Template = Template(`
9
+ if (!Array.prototype.forEach) {
10
+ Array.prototype.forEach = function forEach (callback, thisArg) {
11
+ if (typeof callback !== 'function') {
12
+ throw new TypeError(callback + ' is not a function');
13
+ }
14
+ var array = this;
15
+ thisArg = thisArg || this;
16
+ for (var i = 0, l = array.length; i !== l; ++i) {
17
+ callback.call(thisArg, array[i], i, array);
18
+ }
19
+ };
20
+ }
21
+ if (!Array.prototype.filter)
22
+ Array.prototype.filter = function(func, thisArg) {
23
+ 'use strict';
24
+ if ( ! ((typeof func === 'Function' || typeof func === 'function') && this) )
25
+ throw new TypeError();
26
+
27
+ var len = this.length >>> 0,
28
+ res = new Array(len), // preallocate array
29
+ t = this, c = 0, i = -1;
30
+ if (thisArg === undefined)
31
+ while (++i !== len)
32
+ // checks to see if the key was set
33
+ if (i in this)
34
+ if (func(t[i], i, t))
35
+ res[c++] = t[i];
36
+ else
37
+ while (++i !== len)
38
+ // checks to see if the key was set
39
+ if (i in this)
40
+ if (func.call(thisArg, t[i], i, t))
41
+ res[c++] = t[i];
42
+
43
+ res.length = c; // shrink down array to proper size
44
+ return res;
45
+ };
46
+ if (!Array.prototype.map) {
47
+ Array.prototype.map = function(callback, thisArg) {
48
+ var T, A, k;
49
+
50
+ if (this == null) {
51
+ throw new TypeError('this is null or not defined');
52
+ }
53
+
54
+ // 1. Let O be the result of calling ToObject passing the |this|
55
+ // value as the argument.
56
+ var O = Object(this);
57
+
58
+ // 2. Let lenValue be the result of calling the Get internal
59
+ // method of O with the argument "length".
60
+ // 3. Let len be ToUint32(lenValue).
61
+ var len = O.length >>> 0;
62
+
63
+ // 4. If IsCallable(callback) is false, throw a TypeError exception.
64
+ // See: http://es5.github.com/#x9.11
65
+ if (typeof callback !== 'function') {
66
+ throw new TypeError(callback + ' is not a function');
67
+ }
68
+
69
+ // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
70
+ if (arguments.length > 1) {
71
+ T = arguments[1];
72
+ }
73
+
74
+ // 6. Let A be a new array created as if by the expression new Array(len)
75
+ // where Array is the standard built-in constructor with that name and
76
+ // len is the value of len.
77
+ A = new Array(len);
78
+
79
+ // 7. Let k be 0
80
+ k = 0;
81
+
82
+ // 8. Repeat, while k < len
83
+ while (k < len) {
84
+
85
+ var kValue, mappedValue;
86
+
87
+ // a. Let Pk be ToString(k).
88
+ // This is implicit for LHS operands of the in operator
89
+ // b. Let kPresent be the result of calling the HasProperty internal
90
+ // method of O with argument Pk.
91
+ // This step can be combined with c
92
+ // c. If kPresent is true, then
93
+ if (k in O) {
94
+
95
+ // i. Let kValue be the result of calling the Get internal
96
+ // method of O with argument Pk.
97
+ kValue = O[k];
98
+
99
+ // ii. Let mappedValue be the result of calling the Call internal
100
+ // method of callback with T as the this value and argument
101
+ // list containing kValue, k, and O.
102
+ mappedValue = callback.call(T, kValue, k, O);
103
+
104
+ // iii. Call the DefineOwnProperty internal method of A with arguments
105
+ // Pk, Property Descriptor
106
+ // { Value: mappedValue,
107
+ // Writable: true,
108
+ // Enumerable: true,
109
+ // Configurable: true },
110
+ // and false.
111
+
112
+ // In browsers that support Object.defineProperty, use the following:
113
+ // Object.defineProperty(A, k, {
114
+ // value: mappedValue,
115
+ // writable: true,
116
+ // enumerable: true,
117
+ // configurable: true
118
+ // });
119
+
120
+ // For best browser support, use the following:
121
+ A[k] = mappedValue;
122
+ }
123
+ // d. Increase k by 1.
124
+ k++;
125
+ }
126
+
127
+ // 9. return A
128
+ return A;
129
+ };
130
+ }
131
+ `);
@@ -0,0 +1,14 @@
1
+ import Template from "./template";
2
+
3
+ /**
4
+ * Helper function to set `function.length` property.
5
+ */
6
+ export const FunctionLengthTemplate = Template(`
7
+ function {name}(functionObject, functionLength){
8
+ Object["defineProperty"](functionObject, "length", {
9
+ "value": functionLength,
10
+ "configurable": true
11
+ });
12
+ return functionObject;
13
+ }
14
+ `);