js-confuser 1.7.1 → 1.7.3
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.
- package/.github/workflows/node.js.yml +1 -1
- package/CHANGELOG.md +73 -0
- package/README.md +32 -31
- package/dist/compiler.js +2 -8
- package/dist/constants.js +22 -10
- package/dist/index.js +15 -30
- package/dist/obfuscator.js +15 -62
- package/dist/options.js +33 -40
- package/dist/order.js +4 -7
- package/dist/parser.js +5 -13
- package/dist/precedence.js +6 -8
- package/dist/presets.js +4 -6
- package/dist/probability.js +13 -24
- package/dist/templates/bufferToString.js +121 -5
- package/dist/templates/core.js +35 -0
- package/dist/templates/crash.js +22 -11
- package/dist/templates/es5.js +125 -6
- package/dist/templates/functionLength.js +24 -6
- package/dist/templates/globals.js +9 -0
- package/dist/templates/template.js +189 -43
- package/dist/transforms/antiTooling.js +26 -22
- package/dist/transforms/calculator.js +19 -55
- package/dist/transforms/controlFlowFlattening/controlFlowFlattening.js +242 -333
- package/dist/transforms/controlFlowFlattening/expressionObfuscation.js +46 -25
- package/dist/transforms/deadCode.js +542 -31
- package/dist/transforms/dispatcher.js +112 -112
- package/dist/transforms/es5/antiClass.js +70 -44
- package/dist/transforms/es5/antiDestructuring.js +14 -38
- package/dist/transforms/es5/antiES6Object.js +39 -48
- package/dist/transforms/es5/antiSpreadOperator.js +5 -14
- package/dist/transforms/es5/antiTemplate.js +10 -19
- package/dist/transforms/es5/es5.js +7 -40
- package/dist/transforms/extraction/classExtraction.js +83 -0
- package/dist/transforms/extraction/duplicateLiteralsRemoval.js +41 -80
- package/dist/transforms/extraction/objectExtraction.js +24 -56
- package/dist/transforms/finalizer.js +6 -20
- package/dist/transforms/flatten.js +51 -99
- package/dist/transforms/identifier/globalAnalysis.js +21 -26
- package/dist/transforms/identifier/globalConcealing.js +72 -56
- package/dist/transforms/identifier/movedDeclarations.js +66 -38
- package/dist/transforms/identifier/renameVariables.js +36 -68
- package/dist/transforms/identifier/variableAnalysis.js +21 -48
- package/dist/transforms/lock/antiDebug.js +20 -25
- package/dist/transforms/lock/integrity.js +53 -52
- package/dist/transforms/lock/lock.js +161 -126
- package/dist/transforms/minify.js +77 -108
- package/dist/transforms/opaquePredicates.js +12 -49
- package/dist/transforms/preparation.js +28 -49
- package/dist/transforms/renameLabels.js +5 -22
- package/dist/transforms/rgf.js +125 -72
- package/dist/transforms/shuffle.js +42 -47
- package/dist/transforms/stack.js +41 -98
- package/dist/transforms/string/encoding.js +76 -27
- package/dist/transforms/string/stringCompression.js +75 -68
- package/dist/transforms/string/stringConcealing.js +127 -135
- package/dist/transforms/string/stringEncoding.js +6 -26
- package/dist/transforms/string/stringSplitting.js +5 -30
- package/dist/transforms/transform.js +76 -104
- package/dist/traverse.js +11 -18
- package/dist/util/compare.js +27 -29
- package/dist/util/gen.js +32 -86
- package/dist/util/guard.js +5 -1
- package/dist/util/identifiers.js +9 -72
- package/dist/util/insert.js +27 -77
- package/dist/util/math.js +0 -3
- package/dist/util/object.js +3 -7
- package/dist/util/random.js +31 -36
- package/dist/util/scope.js +6 -3
- package/docs/Countermeasures.md +13 -6
- package/docs/Integrity.md +35 -28
- package/docs/RGF.md +6 -1
- package/docs/RenameVariables.md +116 -0
- package/docs/TamperProtection.md +100 -0
- package/docs/Template.md +117 -0
- package/package.json +3 -3
- package/src/constants.ts +17 -0
- package/src/index.ts +7 -5
- package/src/options.ts +60 -7
- package/src/order.ts +2 -2
- package/src/templates/bufferToString.ts +79 -11
- package/src/templates/core.ts +29 -0
- package/src/templates/crash.ts +6 -38
- package/src/templates/es5.ts +1 -1
- package/src/templates/functionLength.ts +21 -3
- package/src/templates/globals.ts +3 -0
- package/src/templates/template.ts +205 -46
- package/src/transforms/antiTooling.ts +33 -11
- package/src/transforms/calculator.ts +4 -2
- package/src/transforms/controlFlowFlattening/controlFlowFlattening.ts +12 -5
- package/src/transforms/controlFlowFlattening/expressionObfuscation.ts +46 -10
- package/src/transforms/deadCode.ts +74 -42
- package/src/transforms/dispatcher.ts +99 -73
- package/src/transforms/es5/antiClass.ts +25 -12
- package/src/transforms/es5/antiDestructuring.ts +1 -1
- package/src/transforms/es5/antiES6Object.ts +2 -2
- package/src/transforms/es5/antiTemplate.ts +1 -1
- package/src/transforms/extraction/classExtraction.ts +168 -0
- package/src/transforms/extraction/duplicateLiteralsRemoval.ts +11 -16
- package/src/transforms/extraction/objectExtraction.ts +4 -15
- package/src/transforms/flatten.ts +20 -5
- package/src/transforms/identifier/globalAnalysis.ts +18 -1
- package/src/transforms/identifier/globalConcealing.ts +119 -72
- package/src/transforms/identifier/movedDeclarations.ts +90 -24
- package/src/transforms/identifier/renameVariables.ts +16 -1
- package/src/transforms/lock/antiDebug.ts +2 -2
- package/src/transforms/lock/integrity.ts +13 -11
- package/src/transforms/lock/lock.ts +122 -30
- package/src/transforms/minify.ts +28 -13
- package/src/transforms/opaquePredicates.ts +2 -2
- package/src/transforms/preparation.ts +16 -0
- package/src/transforms/rgf.ts +139 -12
- package/src/transforms/shuffle.ts +3 -3
- package/src/transforms/stack.ts +19 -4
- package/src/transforms/string/encoding.ts +88 -51
- package/src/transforms/string/stringCompression.ts +86 -17
- package/src/transforms/string/stringConcealing.ts +148 -118
- package/src/transforms/string/stringEncoding.ts +1 -2
- package/src/transforms/string/stringSplitting.ts +1 -2
- package/src/transforms/transform.ts +63 -46
- package/src/types.ts +2 -0
- package/src/util/compare.ts +39 -5
- package/src/util/gen.ts +10 -3
- package/src/util/guard.ts +10 -0
- package/src/util/insert.ts +17 -0
- package/src/util/random.ts +81 -1
- package/src/util/scope.ts +14 -2
- package/test/code/Cash.test.ts +94 -5
- package/test/code/StrictMode.src.js +65 -0
- package/test/code/StrictMode.test.js +37 -0
- package/test/compare.test.ts +62 -2
- package/test/options.test.ts +129 -55
- package/test/templates/template.test.ts +211 -1
- package/test/transforms/controlFlowFlattening/expressionObfuscation.test.ts +37 -18
- package/test/transforms/dispatcher.test.ts +55 -0
- package/test/transforms/extraction/classExtraction.test.ts +86 -0
- package/test/transforms/extraction/duplicateLiteralsRemoval.test.ts +8 -0
- package/test/transforms/extraction/objectExtraction.test.ts +2 -0
- package/test/transforms/identifier/globalConcealing.test.ts +89 -0
- package/test/transforms/identifier/movedDeclarations.test.ts +61 -0
- package/test/transforms/identifier/renameVariables.test.ts +75 -1
- package/test/transforms/lock/tamperProtection.test.ts +336 -0
- package/test/transforms/minify.test.ts +37 -0
- package/test/transforms/rgf.test.ts +50 -0
- package/dist/transforms/controlFlowFlattening/choiceFlowObfuscation.js +0 -62
- package/dist/transforms/controlFlowFlattening/controlFlowObfuscation.js +0 -159
- package/dist/transforms/controlFlowFlattening/switchCaseObfuscation.js +0 -106
- package/dist/transforms/eval.js +0 -84
- package/dist/transforms/hexadecimalNumbers.js +0 -63
- package/dist/transforms/hideInitializingCode.js +0 -270
- package/dist/transforms/identifier/nameRecycling.js +0 -218
- package/dist/transforms/label.js +0 -67
- package/dist/transforms/preparation/nameConflicts.js +0 -116
- package/dist/transforms/preparation/preparation.js +0 -188
|
@@ -4,31 +4,21 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _assert = require("assert");
|
|
9
|
-
|
|
10
8
|
var _constants = require("../constants");
|
|
11
|
-
|
|
12
9
|
var _order = require("../order");
|
|
13
|
-
|
|
14
10
|
var _traverse = require("../traverse");
|
|
15
|
-
|
|
16
11
|
var _gen = require("../util/gen");
|
|
17
|
-
|
|
18
12
|
var _identifiers = require("../util/identifiers");
|
|
19
|
-
|
|
20
13
|
var _insert = require("../util/insert");
|
|
21
|
-
|
|
22
14
|
var _random = require("../util/random");
|
|
23
|
-
|
|
24
15
|
var _transform = _interopRequireDefault(require("./transform"));
|
|
25
|
-
|
|
26
16
|
var _functionLength = require("../templates/functionLength");
|
|
27
|
-
|
|
28
|
-
function _interopRequireDefault(
|
|
29
|
-
|
|
30
|
-
function
|
|
31
|
-
|
|
17
|
+
var _globals = require("../templates/globals");
|
|
18
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
19
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
20
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
21
|
+
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
32
22
|
/**
|
|
33
23
|
* Flatten takes functions and isolates them from their original scope, and brings it to the top level of the program.
|
|
34
24
|
*
|
|
@@ -71,67 +61,52 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
|
|
|
71
61
|
* - `myFunction_flat` is now eligible because it does not rely on outside scoped variables
|
|
72
62
|
*/
|
|
73
63
|
class Flatten extends _transform.default {
|
|
74
|
-
// Array of FunctionDeclaration nodes
|
|
75
64
|
constructor(o) {
|
|
76
65
|
super(o, _order.ObfuscateOrder.Flatten);
|
|
77
|
-
|
|
78
66
|
_defineProperty(this, "isDebug", false);
|
|
79
|
-
|
|
80
67
|
_defineProperty(this, "definedNames", void 0);
|
|
81
|
-
|
|
68
|
+
// Array of FunctionDeclaration nodes
|
|
82
69
|
_defineProperty(this, "flattenedFns", void 0);
|
|
83
|
-
|
|
84
70
|
_defineProperty(this, "gen", void 0);
|
|
85
|
-
|
|
86
71
|
_defineProperty(this, "functionLengthName", void 0);
|
|
87
|
-
|
|
88
72
|
this.definedNames = new Map();
|
|
89
73
|
this.flattenedFns = [];
|
|
90
74
|
this.gen = this.getGenerator("mangled");
|
|
91
|
-
|
|
92
75
|
if (this.isDebug) {
|
|
93
76
|
console.warn("Flatten debug mode");
|
|
94
77
|
}
|
|
95
78
|
}
|
|
96
|
-
|
|
97
79
|
apply(tree) {
|
|
98
80
|
super.apply(tree);
|
|
99
|
-
|
|
100
81
|
if (this.flattenedFns.length) {
|
|
101
82
|
(0, _insert.prepend)(tree, ...this.flattenedFns);
|
|
102
83
|
}
|
|
103
84
|
}
|
|
104
|
-
|
|
105
85
|
match(object, parents) {
|
|
106
86
|
return (object.type == "FunctionDeclaration" || object.type === "FunctionExpression") && object.body.type == "BlockStatement" && !object.$requiresEval && !object.generator && !object.params.find(x => x.type !== "Identifier");
|
|
107
87
|
}
|
|
108
|
-
|
|
109
88
|
transform(object, parents) {
|
|
110
89
|
return () => {
|
|
111
90
|
var _object$id, _parents$, _parents$0$id, _parents$0$id2, _parents$2, _parents$3;
|
|
112
|
-
|
|
113
91
|
if (parents[0]) {
|
|
114
92
|
// Don't change class methods
|
|
115
93
|
if (parents[0].type === "MethodDefinition" && parents[0].value === object) {
|
|
116
94
|
return;
|
|
117
|
-
}
|
|
118
|
-
|
|
95
|
+
}
|
|
119
96
|
|
|
97
|
+
// Don't change getter/setter methods
|
|
120
98
|
if (parents[0].type === "Property" && parents[0].value === object && (parents[0].kind !== "init" || parents[0].method)) {
|
|
121
99
|
return;
|
|
122
100
|
}
|
|
123
101
|
}
|
|
102
|
+
(0, _assert.ok)(object.type === "FunctionDeclaration" || object.type === "FunctionExpression");
|
|
124
103
|
|
|
125
|
-
|
|
126
|
-
|
|
104
|
+
// The name is purely for debugging purposes
|
|
127
105
|
var currentFnName = object.type === "FunctionDeclaration" ? (_object$id = object.id) === null || _object$id === void 0 ? void 0 : _object$id.name : ((_parents$ = parents[0]) === null || _parents$ === void 0 ? void 0 : _parents$.type) === "VariableDeclarator" && ((_parents$0$id = parents[0].id) === null || _parents$0$id === void 0 ? void 0 : _parents$0$id.type) === "Identifier" && ((_parents$0$id2 = parents[0].id) === null || _parents$0$id2 === void 0 ? void 0 : _parents$0$id2.name);
|
|
128
|
-
|
|
129
106
|
if (((_parents$2 = parents[0]) === null || _parents$2 === void 0 ? void 0 : _parents$2.type) === "Property" && (_parents$3 = parents[0]) !== null && _parents$3 !== void 0 && _parents$3.key) {
|
|
130
107
|
var _parents$4, _parents$4$key;
|
|
131
|
-
|
|
132
108
|
currentFnName = currentFnName || String((_parents$4 = parents[0]) === null || _parents$4 === void 0 ? void 0 : (_parents$4$key = _parents$4.key) === null || _parents$4$key === void 0 ? void 0 : _parents$4$key.name);
|
|
133
109
|
}
|
|
134
|
-
|
|
135
110
|
if (!currentFnName) currentFnName = "unnamed";
|
|
136
111
|
var definedMap = new Map();
|
|
137
112
|
var illegal = new Set();
|
|
@@ -142,218 +117,195 @@ class Flatten extends _transform.default {
|
|
|
142
117
|
isIllegal = true;
|
|
143
118
|
return "EXIT";
|
|
144
119
|
}
|
|
145
|
-
|
|
146
120
|
if (o.type == "Identifier" && o !== object.id && !this.options.globalVariables.has(o.name) && !_constants.reservedIdentifiers.has(o.name)) {
|
|
147
121
|
var info = (0, _identifiers.getIdentifierInfo)(o, p);
|
|
148
|
-
|
|
149
122
|
if (!info.spec.isReferenced) {
|
|
150
123
|
return;
|
|
151
124
|
}
|
|
152
|
-
|
|
153
125
|
if (info.spec.isExported || o.name.startsWith(_constants.noRenameVariablePrefix)) {
|
|
154
126
|
illegal.add(o.name);
|
|
155
127
|
return;
|
|
156
128
|
}
|
|
157
|
-
|
|
158
129
|
if (info.spec.isDefined) {
|
|
159
130
|
var definingContext = (0, _insert.getDefiningContext)(o, p);
|
|
160
|
-
|
|
161
131
|
if (!definedMap.has(definingContext)) {
|
|
162
132
|
definedMap.set(definingContext, new Set([o.name]));
|
|
163
133
|
} else {
|
|
164
134
|
definedMap.get(definingContext).add(o.name);
|
|
165
135
|
}
|
|
166
|
-
|
|
167
136
|
return;
|
|
168
137
|
}
|
|
169
|
-
|
|
170
138
|
var isDefined = p.find(x => definedMap.has(x) && definedMap.get(x).has(o.name));
|
|
171
|
-
|
|
172
139
|
if (!isDefined) {
|
|
173
140
|
identifierNodes.push([o, p, info]);
|
|
174
141
|
}
|
|
175
142
|
}
|
|
176
|
-
|
|
177
143
|
if (o.type == "TryStatement") {
|
|
178
144
|
isIllegal = true;
|
|
179
145
|
return "EXIT";
|
|
180
146
|
}
|
|
181
147
|
});
|
|
182
|
-
|
|
183
148
|
if (isIllegal) {
|
|
184
149
|
return;
|
|
185
150
|
}
|
|
186
|
-
|
|
187
151
|
if (illegal.size) {
|
|
188
152
|
return;
|
|
189
153
|
}
|
|
190
|
-
|
|
191
|
-
var newFnName = this.getPlaceholder() + "_flat_" + currentFnName;
|
|
154
|
+
var newFnName = this.getPlaceholder() + "_flat_" + currentFnName + _constants.predictableFunctionTag;
|
|
192
155
|
var flatObjectName = this.getPlaceholder() + "_flat_object";
|
|
193
|
-
|
|
194
156
|
const getFlatObjectMember = propertyName => {
|
|
195
157
|
return (0, _gen.MemberExpression)((0, _gen.Identifier)(flatObjectName), (0, _gen.Literal)(propertyName), true);
|
|
196
158
|
};
|
|
197
|
-
|
|
198
159
|
var getterPropNames = Object.create(null);
|
|
199
160
|
var setterPropNames = Object.create(null);
|
|
200
161
|
var typeofPropNames = Object.create(null);
|
|
201
162
|
var callPropNames = Object.create(null);
|
|
202
|
-
|
|
203
163
|
for (var [o, p, info] of identifierNodes) {
|
|
204
164
|
var identifierName = o.name;
|
|
205
165
|
if (p.find(x => definedMap.has(x) && definedMap.get(x).has(identifierName))) continue;
|
|
206
166
|
(0, _assert.ok)(!info.spec.isDefined);
|
|
207
167
|
var type = info.spec.isModified ? "setter" : "getter";
|
|
208
|
-
|
|
209
168
|
switch (type) {
|
|
210
169
|
case "setter":
|
|
211
170
|
var setterPropName = setterPropNames[identifierName];
|
|
212
|
-
|
|
213
171
|
if (typeof setterPropName === "undefined") {
|
|
214
172
|
// No getter function made yet, make it (Try to re-use getter name if available)
|
|
215
173
|
setterPropName = getterPropNames[identifierName] || (this.isDebug ? "set_" + identifierName : this.gen.generate());
|
|
216
174
|
setterPropNames[identifierName] = setterPropName;
|
|
217
|
-
}
|
|
218
|
-
|
|
175
|
+
}
|
|
219
176
|
|
|
177
|
+
// If an update expression, ensure a getter function is also available. Ex: a++
|
|
220
178
|
if (p[0].type === "UpdateExpression") {
|
|
221
179
|
getterPropNames[identifierName] = setterPropName;
|
|
222
180
|
} else {
|
|
223
181
|
// If assignment on member expression, ensure a getter function is also available: Ex. myObject.property = ...
|
|
224
182
|
var assignmentIndex = p.findIndex(x => x.type === "AssignmentExpression");
|
|
225
|
-
|
|
226
183
|
if (assignmentIndex !== -1 && p[assignmentIndex].left.type !== "Identifier") {
|
|
227
184
|
getterPropNames[identifierName] = setterPropName;
|
|
228
185
|
}
|
|
229
|
-
}
|
|
230
|
-
|
|
186
|
+
}
|
|
231
187
|
|
|
188
|
+
// calls flatObject.set_identifier_value(newValue)
|
|
232
189
|
this.replace(o, getFlatObjectMember(setterPropName));
|
|
233
190
|
break;
|
|
234
|
-
|
|
235
191
|
case "getter":
|
|
236
192
|
var getterPropName = getterPropNames[identifierName];
|
|
237
|
-
|
|
238
193
|
if (typeof getterPropName === "undefined") {
|
|
239
194
|
// No getter function made yet, make it (Try to re-use setter name if available)
|
|
240
195
|
getterPropName = setterPropNames[identifierName] || (this.isDebug ? "get_" + identifierName : this.gen.generate());
|
|
241
196
|
getterPropNames[identifierName] = getterPropName;
|
|
242
|
-
}
|
|
243
|
-
|
|
197
|
+
}
|
|
244
198
|
|
|
199
|
+
// Typeof expression check
|
|
245
200
|
if (p[0].type === "UnaryExpression" && p[0].operator === "typeof" && p[0].argument === o) {
|
|
246
201
|
var typeofPropName = typeofPropNames[identifierName];
|
|
247
|
-
|
|
248
202
|
if (typeof typeofPropName === "undefined") {
|
|
249
203
|
// No typeof getter function made yet, make it (Don't re-use getter/setter names)
|
|
250
204
|
typeofPropName = this.isDebug ? "get_typeof_" + identifierName : this.gen.generate();
|
|
251
205
|
typeofPropNames[identifierName] = typeofPropName;
|
|
252
|
-
}
|
|
253
|
-
// calls flatObject.get_typeof_identifier()
|
|
254
|
-
|
|
206
|
+
}
|
|
255
207
|
|
|
208
|
+
// Replace the entire unary expression not just the identifier node
|
|
209
|
+
// calls flatObject.get_typeof_identifier()
|
|
256
210
|
this.replace(p[0], getFlatObjectMember(typeofPropName));
|
|
257
211
|
break;
|
|
258
|
-
}
|
|
259
|
-
|
|
212
|
+
}
|
|
260
213
|
|
|
214
|
+
// Bound call-expression check
|
|
261
215
|
if (p[0].type === "CallExpression" && p[0].callee === o) {
|
|
262
216
|
var callPropName = callPropNames[identifierName];
|
|
263
|
-
|
|
264
217
|
if (typeof callPropName === "undefined") {
|
|
265
218
|
callPropName = this.isDebug ? "call_" + identifierName : this.gen.generate();
|
|
266
219
|
callPropNames[identifierName] = callPropName;
|
|
267
|
-
}
|
|
268
|
-
// calls flatObject.call_identifier(...arguments)
|
|
269
|
-
|
|
220
|
+
}
|
|
270
221
|
|
|
222
|
+
// Replace the entire call expression not just the identifier node
|
|
223
|
+
// calls flatObject.call_identifier(...arguments)
|
|
271
224
|
this.replace(p[0], (0, _gen.CallExpression)(getFlatObjectMember(callPropName), p[0].arguments));
|
|
272
225
|
break;
|
|
273
|
-
}
|
|
274
|
-
|
|
226
|
+
}
|
|
275
227
|
|
|
228
|
+
// calls flatObject.get_identifier_value()
|
|
276
229
|
this.replace(o, getFlatObjectMember(getterPropName));
|
|
277
230
|
break;
|
|
278
231
|
}
|
|
279
|
-
}
|
|
280
|
-
|
|
232
|
+
}
|
|
281
233
|
|
|
282
|
-
|
|
234
|
+
// Create the getter and setter functions
|
|
235
|
+
var flatObjectProperties = [];
|
|
283
236
|
|
|
237
|
+
// Getter functions
|
|
284
238
|
for (var identifierName in getterPropNames) {
|
|
285
239
|
var getterPropName = getterPropNames[identifierName];
|
|
286
240
|
flatObjectProperties.push((0, _gen.Property)((0, _gen.Literal)(getterPropName), (0, _gen.FunctionExpression)([], [(0, _gen.ReturnStatement)((0, _gen.Identifier)(identifierName))]), true, "get"));
|
|
287
|
-
}
|
|
288
|
-
|
|
241
|
+
}
|
|
289
242
|
|
|
243
|
+
// Get typeof functions
|
|
290
244
|
for (var identifierName in typeofPropNames) {
|
|
291
245
|
var typeofPropName = typeofPropNames[identifierName];
|
|
292
246
|
flatObjectProperties.push((0, _gen.Property)((0, _gen.Literal)(typeofPropName), (0, _gen.FunctionExpression)([], [(0, _gen.ReturnStatement)((0, _gen.UnaryExpression)("typeof", (0, _gen.Identifier)(identifierName)))]), true, "get"));
|
|
293
|
-
}
|
|
294
|
-
|
|
247
|
+
}
|
|
295
248
|
|
|
249
|
+
// Call functions
|
|
296
250
|
for (var identifierName in callPropNames) {
|
|
297
251
|
var callPropName = callPropNames[identifierName];
|
|
298
252
|
var argumentsName = this.getPlaceholder();
|
|
299
253
|
flatObjectProperties.push((0, _gen.Property)((0, _gen.Literal)(callPropName), (0, _gen.FunctionExpression)([(0, _gen.RestElement)((0, _gen.Identifier)(argumentsName))], [(0, _gen.ReturnStatement)((0, _gen.CallExpression)((0, _gen.Identifier)(identifierName), [(0, _gen.SpreadElement)((0, _gen.Identifier)(argumentsName))]))]), true));
|
|
300
|
-
}
|
|
301
|
-
|
|
254
|
+
}
|
|
302
255
|
|
|
256
|
+
// Setter functions
|
|
303
257
|
for (var identifierName in setterPropNames) {
|
|
304
258
|
var setterPropName = setterPropNames[identifierName];
|
|
305
259
|
var newValueParameterName = this.getPlaceholder();
|
|
306
260
|
flatObjectProperties.push((0, _gen.Property)((0, _gen.Literal)(setterPropName), (0, _gen.FunctionExpression)([(0, _gen.Identifier)(newValueParameterName)], [(0, _gen.ExpressionStatement)((0, _gen.AssignmentExpression)("=", (0, _gen.Identifier)(identifierName), (0, _gen.Identifier)(newValueParameterName)))]), true, "set"));
|
|
307
261
|
}
|
|
308
|
-
|
|
309
262
|
if (!this.isDebug) {
|
|
310
263
|
(0, _random.shuffle)(flatObjectProperties);
|
|
311
264
|
}
|
|
265
|
+
var newBody = (0, _insert.getBlockBody)(object.body);
|
|
312
266
|
|
|
313
|
-
|
|
314
|
-
|
|
267
|
+
// Remove 'use strict' directive
|
|
315
268
|
if (newBody.length > 0 && newBody[0].directive) {
|
|
316
269
|
newBody.shift();
|
|
317
270
|
}
|
|
318
|
-
|
|
319
271
|
var newFunctionDeclaration = (0, _gen.FunctionDeclaration)(newFnName, [(0, _gen.ArrayPattern)((0, _insert.clone)(object.params)), (0, _gen.Identifier)(flatObjectName)], newBody);
|
|
320
272
|
newFunctionDeclaration.async = !!object.async;
|
|
321
273
|
newFunctionDeclaration.generator = false;
|
|
322
274
|
this.flattenedFns.push(newFunctionDeclaration);
|
|
323
|
-
var argumentsName = this.getPlaceholder();
|
|
275
|
+
var argumentsName = this.getPlaceholder();
|
|
324
276
|
|
|
277
|
+
// newFn.call([...arguments], flatObject)
|
|
325
278
|
var callExpression = (0, _gen.CallExpression)((0, _gen.Identifier)(newFnName), [(0, _gen.Identifier)(argumentsName), (0, _gen.Identifier)(flatObjectName)]);
|
|
326
|
-
var newObjectBody = [
|
|
279
|
+
var newObjectBody = [
|
|
280
|
+
// var flatObject = { get(), set() };
|
|
327
281
|
(0, _gen.VariableDeclaration)([(0, _gen.VariableDeclarator)(flatObjectName, (0, _gen.ObjectExpression)(flatObjectProperties))]), (0, _gen.ReturnStatement)(newFunctionDeclaration.async ? (0, _gen.AwaitExpression)(callExpression) : callExpression)];
|
|
328
|
-
object.body = (0, _gen.BlockStatement)(newObjectBody);
|
|
282
|
+
object.body = (0, _gen.BlockStatement)(newObjectBody);
|
|
329
283
|
|
|
284
|
+
// Preserve function.length property
|
|
330
285
|
var originalFunctionLength = (0, _insert.computeFunctionLength)(object.params);
|
|
331
|
-
object.params = [(0, _gen.
|
|
332
|
-
|
|
333
|
-
if (originalFunctionLength !== 0) {
|
|
286
|
+
object.params = [(0, _gen.RestElement)((0, _gen.Identifier)(argumentsName))];
|
|
287
|
+
if (this.options.preserveFunctionLength && originalFunctionLength !== 0) {
|
|
334
288
|
if (!this.functionLengthName) {
|
|
335
289
|
this.functionLengthName = this.getPlaceholder();
|
|
336
290
|
(0, _insert.prepend)(parents[parents.length - 1] || object, _functionLength.FunctionLengthTemplate.single({
|
|
337
|
-
name: this.functionLengthName
|
|
291
|
+
name: this.functionLengthName,
|
|
292
|
+
ObjectDefineProperty: this.createInitVariable(_globals.ObjectDefineProperty, parents)
|
|
338
293
|
}));
|
|
339
294
|
}
|
|
340
|
-
|
|
341
295
|
if (object.type === "FunctionDeclaration") {
|
|
342
296
|
var body = parents[0];
|
|
343
|
-
|
|
344
297
|
if (Array.isArray(body)) {
|
|
345
298
|
var index = body.indexOf(object);
|
|
346
299
|
body.splice(index + 1, 0, (0, _gen.ExpressionStatement)((0, _gen.CallExpression)((0, _gen.Identifier)(this.functionLengthName), [(0, _gen.Identifier)(object.id.name), (0, _gen.Literal)(originalFunctionLength)])));
|
|
347
300
|
}
|
|
348
301
|
} else {
|
|
349
302
|
(0, _assert.ok)(object.type === "FunctionExpression");
|
|
350
|
-
this.replace(object, (0, _gen.CallExpression)((0, _gen.Identifier)(this.functionLengthName), [{
|
|
303
|
+
this.replace(object, (0, _gen.CallExpression)((0, _gen.Identifier)(this.functionLengthName), [{
|
|
304
|
+
...object
|
|
351
305
|
}, (0, _gen.Literal)(originalFunctionLength)]));
|
|
352
306
|
}
|
|
353
307
|
}
|
|
354
308
|
};
|
|
355
309
|
}
|
|
356
|
-
|
|
357
310
|
}
|
|
358
|
-
|
|
359
311
|
exports.default = Flatten;
|
|
@@ -4,17 +4,14 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _constants = require("../../constants");
|
|
9
|
-
|
|
8
|
+
var _guard = require("../../util/guard");
|
|
10
9
|
var _identifiers = require("../../util/identifiers");
|
|
11
|
-
|
|
12
10
|
var _transform = _interopRequireDefault(require("../transform"));
|
|
13
|
-
|
|
14
|
-
function
|
|
15
|
-
|
|
16
|
-
function
|
|
17
|
-
|
|
11
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
13
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
14
|
+
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
18
15
|
/**
|
|
19
16
|
* Global Analysis is responsible for finding all the global variables used in the code.
|
|
20
17
|
*
|
|
@@ -25,64 +22,62 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
|
|
|
25
22
|
class GlobalAnalysis extends _transform.default {
|
|
26
23
|
constructor(o) {
|
|
27
24
|
super(o);
|
|
28
|
-
|
|
29
25
|
_defineProperty(this, "notGlobals", void 0);
|
|
30
|
-
|
|
31
26
|
_defineProperty(this, "globals", void 0);
|
|
32
|
-
|
|
33
27
|
this.globals = Object.create(null);
|
|
34
28
|
this.notGlobals = new Set();
|
|
35
29
|
}
|
|
36
|
-
|
|
37
30
|
match(object, parents) {
|
|
38
31
|
return object.type == "Identifier" && !_constants.reservedKeywords.has(object.name);
|
|
39
32
|
}
|
|
40
|
-
|
|
41
33
|
transform(object, parents) {
|
|
42
34
|
// no touching `import()` or `import x from ...`
|
|
43
35
|
var importIndex = parents.findIndex(x => x.type == "ImportExpression" || x.type == "ImportDeclaration");
|
|
44
|
-
|
|
45
36
|
if (importIndex !== -1) {
|
|
46
37
|
if (parents[importIndex].source === (parents[importIndex - 1] || object)) {
|
|
47
38
|
return;
|
|
48
39
|
}
|
|
49
40
|
}
|
|
50
|
-
|
|
51
41
|
var info = (0, _identifiers.getIdentifierInfo)(object, parents);
|
|
52
|
-
|
|
53
42
|
if (!info.spec.isReferenced) {
|
|
54
43
|
return;
|
|
55
|
-
}
|
|
56
|
-
|
|
44
|
+
}
|
|
45
|
+
if ((0, _guard.isJSConfuserVar)(parents)) {
|
|
46
|
+
delete this.globals[object.name];
|
|
47
|
+
this.notGlobals.add(object.name);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
57
50
|
|
|
51
|
+
// Cannot be defined or overridden
|
|
58
52
|
if (info.spec.isDefined || info.spec.isModified) {
|
|
53
|
+
if (info.spec.isModified) {
|
|
54
|
+
// Only direct overwrites should be considered
|
|
55
|
+
// Changing object properties is allowed
|
|
56
|
+
if (parents[0].type === "MemberExpression" && parents[0].object === object) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
59
60
|
delete this.globals[object.name];
|
|
60
61
|
this.notGlobals.add(object.name);
|
|
61
62
|
return;
|
|
62
|
-
}
|
|
63
|
-
|
|
63
|
+
}
|
|
64
64
|
|
|
65
|
+
// Add to globals
|
|
65
66
|
if (!this.notGlobals.has(object.name)) {
|
|
66
67
|
if (!this.globals[object.name]) {
|
|
67
68
|
this.globals[object.name] = [];
|
|
68
69
|
}
|
|
69
|
-
|
|
70
70
|
this.globals[object.name].push([object, parents]);
|
|
71
71
|
}
|
|
72
|
-
|
|
73
72
|
var assignmentIndex = parents.findIndex(x => x.type == "AssignmentExpression");
|
|
74
73
|
var updateIndex = parents.findIndex(x => x.type == "UpdateExpression");
|
|
75
|
-
|
|
76
74
|
if (assignmentIndex != -1 && parents[assignmentIndex].left === (parents[assignmentIndex - 1] || object) || updateIndex != -1) {
|
|
77
75
|
var memberIndex = parents.findIndex(x => x.type == "MemberExpression");
|
|
78
|
-
|
|
79
76
|
if (memberIndex == -1 || memberIndex > (assignmentIndex == -1 ? assignmentIndex : updateIndex)) {
|
|
80
77
|
delete this.globals[object.name];
|
|
81
78
|
this.notGlobals.add(object.name);
|
|
82
79
|
}
|
|
83
80
|
}
|
|
84
81
|
}
|
|
85
|
-
|
|
86
82
|
}
|
|
87
|
-
|
|
88
83
|
exports.default = GlobalAnalysis;
|