hackmud-script-manager 0.19.1-98e81f8 → 0.19.1-b720a68
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +1 -1
- package/bin/hsm.d.ts +0 -0
- package/bin/hsm.js +146 -300
- package/generateTypeDeclaration.d.ts +1 -2
- package/generateTypeDeclaration.js +19 -19
- package/index.js +2 -1
- package/package.json +6 -7
- package/processScript/index.d.ts +15 -25
- package/processScript/index.js +258 -241
- package/processScript/minify.d.ts +13 -19
- package/processScript/minify.js +381 -395
- package/processScript/postprocess.d.ts +0 -1
- package/processScript/postprocess.js +1 -1
- package/processScript/preprocess.d.ts +4 -8
- package/processScript/preprocess.js +94 -94
- package/processScript/shared.d.ts +1 -1
- package/processScript/shared.js +11 -11
- package/processScript/transform.d.ts +11 -17
- package/processScript/transform.js +597 -551
- package/pull.d.ts +5 -9
- package/pull.js +2 -2
- package/push.d.ts +22 -31
- package/push.js +46 -40
- package/syncMacros.d.ts +1 -2
- package/syncMacros.js +6 -5
- package/watch.d.ts +8 -14
- package/watch.js +22 -23
- package/tsconfig.tsbuildinfo +0 -1
@@ -20,406 +20,321 @@ const { default: traverse } = babelTraverse,
|
|
20
20
|
"String",
|
21
21
|
"Symbol",
|
22
22
|
"BigInt"
|
23
|
-
]
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
}
|
38
|
-
})
|
39
|
-
if (program.scope.hasGlobal("_SOURCE"))
|
40
|
-
for (const referencePath of getReferencePathsToGlobal("_SOURCE", program))
|
41
|
-
referencePath.replaceWith(t.stringLiteral(sourceCode))
|
42
|
-
if (program.scope.hasGlobal("_BUILD_DATE"))
|
43
|
-
for (const referencePath of getReferencePathsToGlobal("_BUILD_DATE", program))
|
44
|
-
referencePath.replaceWith(t.numericLiteral(Date.now()))
|
45
|
-
if (program.scope.hasGlobal("_SCRIPT_USER"))
|
46
|
-
for (const referencePath of getReferencePathsToGlobal("_SCRIPT_USER", program))
|
47
|
-
1 == scriptUser ?
|
48
|
-
referencePath.replaceWith(t.stringLiteral(`$${uniqueID}$SCRIPT_USER$`))
|
49
|
-
: referencePath.replaceWith(t.stringLiteral(scriptUser))
|
50
|
-
if (program.scope.hasGlobal("_SCRIPT_NAME"))
|
51
|
-
for (const referencePath of getReferencePathsToGlobal("_SCRIPT_NAME", program))
|
52
|
-
1 == scriptName ?
|
53
|
-
referencePath.replaceWith(t.stringLiteral(`$${uniqueID}$SCRIPT_NAME$`))
|
54
|
-
: referencePath.replaceWith(t.stringLiteral(scriptName))
|
55
|
-
if (program.scope.hasGlobal("_FULL_SCRIPT_NAME"))
|
56
|
-
for (const referencePath of getReferencePathsToGlobal("_FULL_SCRIPT_NAME", program))
|
57
|
-
1 == scriptUser || 1 == scriptName ?
|
58
|
-
referencePath.replaceWith(t.stringLiteral(`$${uniqueID}$FULL_SCRIPT_NAME$`))
|
59
|
-
: referencePath.replaceWith(t.stringLiteral(`${scriptUser}.${scriptName}`))
|
60
|
-
let functionDotPrototypeIsReferencedMultipleTimes = !1
|
61
|
-
const createGetFunctionPrototypeNode = () => {
|
62
|
-
for (const globalFunction of globalFunctionsUnder7Characters)
|
63
|
-
if (!program.scope.hasOwnBinding(globalFunction))
|
64
|
-
return t.memberExpression(
|
65
|
-
t.memberExpression(t.identifier(globalFunction), t.identifier("constructor")),
|
66
|
-
t.identifier("prototype")
|
67
|
-
)
|
68
|
-
return t.memberExpression(
|
69
|
-
t.memberExpression(
|
70
|
-
t.arrowFunctionExpression([t.identifier("_")], t.identifier("_")),
|
71
|
-
t.identifier("constructor")
|
72
|
-
),
|
73
|
-
t.identifier("prototype")
|
74
|
-
)
|
23
|
+
]
|
24
|
+
function transform(
|
25
|
+
file,
|
26
|
+
sourceCode,
|
27
|
+
{ uniqueID = "00000000000", scriptUser, scriptName = "UNKNOWN", seclevel = 4 } = {}
|
28
|
+
) {
|
29
|
+
const topFunctionName = `_${uniqueID}_SCRIPT_`,
|
30
|
+
exports = new Map(),
|
31
|
+
liveExports = new Map()
|
32
|
+
let program
|
33
|
+
traverse(file, {
|
34
|
+
Program(path) {
|
35
|
+
program = path
|
36
|
+
path.skip()
|
75
37
|
}
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
38
|
+
})
|
39
|
+
if (program.scope.hasGlobal("_SOURCE"))
|
40
|
+
for (const referencePath of getReferencePathsToGlobal("_SOURCE", program))
|
41
|
+
referencePath.replaceWith(t.stringLiteral(sourceCode))
|
42
|
+
if (program.scope.hasGlobal("_BUILD_DATE"))
|
43
|
+
for (const referencePath of getReferencePathsToGlobal("_BUILD_DATE", program))
|
44
|
+
referencePath.replaceWith(t.numericLiteral(Date.now()))
|
45
|
+
let uniqueIdScriptUserNeeded = !1
|
46
|
+
if (program.scope.hasGlobal("_SCRIPT_USER"))
|
47
|
+
for (const referencePath of getReferencePathsToGlobal("_SCRIPT_USER", program))
|
48
|
+
if (null == scriptUser) {
|
49
|
+
uniqueIdScriptUserNeeded = !0
|
50
|
+
referencePath.replaceWith(t.identifier(`_${uniqueID}_SCRIPT_USER_`))
|
51
|
+
} else
|
52
|
+
referencePath.replaceWith(t.stringLiteral(1 == scriptUser ? `$${uniqueID}$SCRIPT_USER$` : scriptUser))
|
53
|
+
if (program.scope.hasGlobal("_SCRIPT_NAME"))
|
54
|
+
for (const referencePath of getReferencePathsToGlobal("_SCRIPT_NAME", program))
|
55
|
+
referencePath.replaceWith(t.stringLiteral(1 == scriptName ? `$${uniqueID}$SCRIPT_NAME$` : scriptName))
|
56
|
+
if (program.scope.hasGlobal("_FULL_SCRIPT_NAME"))
|
57
|
+
for (const referencePath of getReferencePathsToGlobal("_FULL_SCRIPT_NAME", program))
|
58
|
+
referencePath.replaceWith(
|
59
|
+
t.stringLiteral(
|
60
|
+
1 == scriptUser || 1 == scriptName ?
|
61
|
+
`$${uniqueID}$FULL_SCRIPT_NAME$`
|
62
|
+
: `${scriptUser}.${scriptName}`
|
63
|
+
)
|
64
|
+
)
|
65
|
+
let functionDotPrototypeIsReferencedMultipleTimes = !1
|
66
|
+
if (program.scope.hasGlobal("Function")) {
|
67
|
+
const FunctionReferencePaths = getReferencePathsToGlobal("Function", program)
|
68
|
+
if (1 == FunctionReferencePaths.length) {
|
69
|
+
const referencePath = FunctionReferencePaths[0]
|
70
|
+
assert(
|
71
|
+
"MemberExpression" == referencePath.parent.type,
|
72
|
+
"src/processScript/transform.ts:99:8 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
|
73
|
+
)
|
74
|
+
assert(
|
75
|
+
"Identifier" == referencePath.parent.property.type,
|
76
|
+
"src/processScript/transform.ts:104:8 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
|
77
|
+
)
|
78
|
+
assert(
|
79
|
+
"prototype" == referencePath.parent.property.name,
|
80
|
+
"src/processScript/transform.ts:109:8 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
|
81
|
+
)
|
82
|
+
referencePath.parentPath.replaceWith(createGetFunctionPrototypeNode())
|
83
|
+
} else {
|
84
|
+
for (const referencePath of FunctionReferencePaths) {
|
80
85
|
assert(
|
81
86
|
"MemberExpression" == referencePath.parent.type,
|
82
|
-
"`Function` isn't available in hackmud, only `Function.prototype` is accessible"
|
87
|
+
"src/processScript/transform.ts:117:9 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
|
83
88
|
)
|
84
89
|
assert(
|
85
90
|
"Identifier" == referencePath.parent.property.type,
|
86
|
-
"`Function` isn't available in hackmud, only `Function.prototype` is accessible"
|
91
|
+
"src/processScript/transform.ts:122:9 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
|
87
92
|
)
|
88
93
|
assert(
|
89
94
|
"prototype" == referencePath.parent.property.name,
|
90
|
-
"`Function` isn't available in hackmud, only `Function.prototype` is accessible"
|
95
|
+
"src/processScript/transform.ts:127:9 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
|
91
96
|
)
|
92
|
-
referencePath.parentPath.replaceWith(createGetFunctionPrototypeNode())
|
93
|
-
} else {
|
94
|
-
for (const referencePath of FunctionReferencePaths) {
|
95
|
-
assert(
|
96
|
-
"MemberExpression" == referencePath.parent.type,
|
97
|
-
"`Function` isn't available in hackmud, only `Function.prototype` is accessible"
|
98
|
-
)
|
99
|
-
assert(
|
100
|
-
"Identifier" == referencePath.parent.property.type,
|
101
|
-
"`Function` isn't available in hackmud, only `Function.prototype` is accessible"
|
102
|
-
)
|
103
|
-
assert(
|
104
|
-
"prototype" == referencePath.parent.property.name,
|
105
|
-
"`Function` isn't available in hackmud, only `Function.prototype` is accessible"
|
106
|
-
)
|
107
|
-
functionDotPrototypeIsReferencedMultipleTimes = !0
|
108
|
-
referencePath.parentPath.replaceWith(t.identifier(`_${uniqueID}_FUNCTION_DOT_PROTOTYPE_`))
|
109
|
-
}
|
110
97
|
functionDotPrototypeIsReferencedMultipleTimes = !0
|
98
|
+
referencePath.parentPath.replaceWith(t.identifier(`_${uniqueID}_FUNCTION_DOT_PROTOTYPE_`))
|
111
99
|
}
|
100
|
+
functionDotPrototypeIsReferencedMultipleTimes = !0
|
112
101
|
}
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
102
|
+
}
|
103
|
+
const neededSubscriptLets = new Set()
|
104
|
+
let detectedSeclevel = 4
|
105
|
+
for (const fakeSubscriptObjectName of ["$fs", "$4s", "$s"])
|
106
|
+
program.scope.hasGlobal(fakeSubscriptObjectName) && processFakeSubscriptObject(fakeSubscriptObjectName)
|
107
|
+
for (const fakeSubscriptObjectName of ["$hs", "$3s"])
|
108
|
+
if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
|
109
|
+
detectedSeclevel = 3
|
110
|
+
processFakeSubscriptObject(fakeSubscriptObjectName)
|
111
|
+
}
|
112
|
+
for (const fakeSubscriptObjectName of ["$ms", "$2s"])
|
113
|
+
if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
|
114
|
+
detectedSeclevel = 2
|
115
|
+
processFakeSubscriptObject(fakeSubscriptObjectName)
|
116
|
+
}
|
117
|
+
for (const fakeSubscriptObjectName of ["$ls", "$1s"])
|
118
|
+
if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
|
119
|
+
detectedSeclevel = 1
|
120
|
+
processFakeSubscriptObject(fakeSubscriptObjectName)
|
121
|
+
}
|
122
|
+
for (const fakeSubscriptObjectName of ["$ns", "$0s"])
|
123
|
+
if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
|
124
|
+
detectedSeclevel = 0
|
125
|
+
processFakeSubscriptObject(fakeSubscriptObjectName)
|
126
|
+
}
|
127
|
+
seclevel = Math.min(seclevel, detectedSeclevel)
|
128
|
+
const neededDbMethodLets = new Set()
|
129
|
+
if (program.scope.hasGlobal("$db"))
|
130
|
+
for (const referencePath of getReferencePathsToGlobal("$db", program)) {
|
131
|
+
assert("MemberExpression" == referencePath.parentPath.node.type, "src/processScript/transform.ts:181:69")
|
132
|
+
assert("Identifier" == referencePath.parentPath.node.property.type, "src/processScript/transform.ts:182:72")
|
133
|
+
const databaseOpMethodName = referencePath.parentPath.node.property.name
|
134
|
+
assert(
|
135
|
+
validDBMethods.includes(databaseOpMethodName),
|
136
|
+
`src/processScript/transform.ts:188:8 invalid db method "${databaseOpMethodName}", valid db methods are "${validDBMethods.join('", "')}"`
|
137
|
+
)
|
138
|
+
if ("CallExpression" == referencePath.parentPath.parentPath?.type)
|
139
|
+
referencePath.parentPath.replaceWith(t.identifier(`$${uniqueID}$DB$${databaseOpMethodName}$`))
|
140
|
+
else {
|
141
|
+
referencePath.parentPath.replaceWith(
|
142
|
+
t.identifier(`_${uniqueID}_CONSOLE_METHOD_${databaseOpMethodName}_`)
|
127
143
|
)
|
128
|
-
|
129
|
-
referencePath.parentPath.parentPath.replaceWith(
|
130
|
-
t.identifier(
|
131
|
-
`$${uniqueID}$SUBSCRIPT$${referencePath.parent.property.name}$${referencePath.parentPath.parentPath.node.property.name}$`
|
132
|
-
)
|
133
|
-
)
|
134
|
-
: referencePath.parentPath.parentPath.replaceWith(
|
135
|
-
t.arrowFunctionExpression(
|
136
|
-
[t.restElement(t.identifier("args"))],
|
137
|
-
t.callExpression(
|
138
|
-
t.identifier(
|
139
|
-
`$${uniqueID}$SUBSCRIPT$${referencePath.parent.property.name}$${referencePath.parentPath.parentPath.node.property.name}$`
|
140
|
-
),
|
141
|
-
[t.spreadElement(t.identifier("args"))]
|
142
|
-
)
|
143
|
-
)
|
144
|
-
)
|
144
|
+
neededDbMethodLets.add(databaseOpMethodName)
|
145
145
|
}
|
146
146
|
}
|
147
|
-
|
148
|
-
|
149
|
-
for (const
|
150
|
-
if (
|
151
|
-
|
152
|
-
|
147
|
+
let needDebugLet = !1
|
148
|
+
if (program.scope.hasGlobal("$D"))
|
149
|
+
for (const referencePath of getReferencePathsToGlobal("$D", program))
|
150
|
+
if ("CallExpression" == referencePath.parentPath.type)
|
151
|
+
referencePath.replaceWith(t.identifier(`$${uniqueID}$DEBUG$`))
|
152
|
+
else {
|
153
|
+
referencePath.replaceWith(t.identifier(`_${uniqueID}_DEBUG_`))
|
154
|
+
needDebugLet = !0
|
153
155
|
}
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
156
|
+
if (program.scope.hasGlobal("$FMCL"))
|
157
|
+
for (const referencePath of getReferencePathsToGlobal("$FMCL", program))
|
158
|
+
referencePath.replaceWith(t.identifier(`$${uniqueID}$FMCL$`))
|
159
|
+
if (program.scope.hasGlobal("$G"))
|
160
|
+
for (const referencePath of getReferencePathsToGlobal("$G", program))
|
161
|
+
referencePath.replaceWith(t.identifier(`$${uniqueID}$GLOBAL$`))
|
162
|
+
if (program.scope.hasGlobal("_SECLEVEL"))
|
163
|
+
for (const referencePath of getReferencePathsToGlobal("_SECLEVEL", program))
|
164
|
+
referencePath.replaceWith(t.numericLiteral(seclevel))
|
165
|
+
let needGetPrototypeOf = !1
|
166
|
+
if (program.scope.hasGlobal("Object"))
|
167
|
+
for (const referencePath of getReferencePathsToGlobal("Object", program))
|
168
|
+
if ("MemberExpression" == referencePath.parent.type && !referencePath.parent.computed) {
|
169
|
+
assert("Identifier" == referencePath.parent.property.type, "src/processScript/transform.ts:235:64")
|
170
|
+
if ("getPrototypeOf" == referencePath.parent.property.name) {
|
171
|
+
referencePath.parentPath.replaceWith(t.identifier(`_${uniqueID}_GET_PROTOTYPE_OF_`))
|
172
|
+
needGetPrototypeOf = !0
|
173
|
+
}
|
168
174
|
}
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
assert("Identifier" == referencePath.
|
174
|
-
|
175
|
-
|
176
|
-
validDBMethods.includes(databaseOpMethodName),
|
177
|
-
`invalid db method "${databaseOpMethodName}", valid db methods are "${validDBMethods.join('", "')}"`
|
175
|
+
const consoleMethodsReferenced = new Set()
|
176
|
+
if (program.scope.hasGlobal("console"))
|
177
|
+
for (const referencePath of getReferencePathsToGlobal("console", program))
|
178
|
+
if ("MemberExpression" == referencePath.parent.type && !referencePath.parent.computed) {
|
179
|
+
assert("Identifier" == referencePath.parent.property.type, "src/processScript/transform.ts:250:64")
|
180
|
+
referencePath.parentPath.replaceWith(
|
181
|
+
t.identifier(`_${uniqueID}_CONSOLE_METHOD_${referencePath.parent.property.name}_`)
|
178
182
|
)
|
179
|
-
|
180
|
-
referencePath.parentPath.replaceWith(t.identifier(`$${uniqueID}$DB$${databaseOpMethodName}$`))
|
181
|
-
: "ObjectId" == databaseOpMethodName ?
|
182
|
-
referencePath.parentPath.replaceWith(
|
183
|
-
t.arrowFunctionExpression(
|
184
|
-
[],
|
185
|
-
t.callExpression(t.identifier(`$${uniqueID}$DB$${databaseOpMethodName}$`), [])
|
186
|
-
)
|
187
|
-
)
|
188
|
-
: "i" == databaseOpMethodName || "r" == databaseOpMethodName ?
|
189
|
-
referencePath.parentPath.replaceWith(
|
190
|
-
t.arrowFunctionExpression(
|
191
|
-
[t.identifier("a")],
|
192
|
-
t.callExpression(t.identifier(`$${uniqueID}$DB$${databaseOpMethodName}$`), [
|
193
|
-
t.identifier("a")
|
194
|
-
])
|
195
|
-
)
|
196
|
-
)
|
197
|
-
: referencePath.parentPath.replaceWith(
|
198
|
-
t.arrowFunctionExpression(
|
199
|
-
[t.identifier("a"), t.identifier("b")],
|
200
|
-
t.callExpression(t.identifier(`$${uniqueID}$DB$${databaseOpMethodName}$`), [
|
201
|
-
t.identifier("a"),
|
202
|
-
t.identifier("b")
|
203
|
-
])
|
204
|
-
)
|
205
|
-
)
|
206
|
-
}
|
207
|
-
if (program.scope.hasGlobal("$D"))
|
208
|
-
for (const referencePath of getReferencePathsToGlobal("$D", program))
|
209
|
-
"CallExpression" == referencePath.parentPath.type ?
|
210
|
-
referencePath.replaceWith(t.identifier(`$${uniqueID}$DEBUG$`))
|
211
|
-
: referencePath.replaceWith(
|
212
|
-
t.arrowFunctionExpression(
|
213
|
-
[t.identifier("a")],
|
214
|
-
t.callExpression(t.identifier(`$${uniqueID}$DEBUG$`), [t.identifier("a")])
|
215
|
-
)
|
216
|
-
)
|
217
|
-
if (program.scope.hasGlobal("$FMCL"))
|
218
|
-
for (const referencePath of getReferencePathsToGlobal("$FMCL", program))
|
219
|
-
referencePath.replaceWith(t.identifier(`$${uniqueID}$FMCL$`))
|
220
|
-
if (program.scope.hasGlobal("$G"))
|
221
|
-
for (const referencePath of getReferencePathsToGlobal("$G", program))
|
222
|
-
referencePath.replaceWith(t.identifier(`$${uniqueID}$GLOBAL$`))
|
223
|
-
if (program.scope.hasGlobal("_SECLEVEL"))
|
224
|
-
for (const referencePath of getReferencePathsToGlobal("_SECLEVEL", program))
|
225
|
-
referencePath.replaceWith(t.numericLiteral(seclevel))
|
226
|
-
let needGetPrototypeOf = !1,
|
227
|
-
needSetPrototypeOf = !1
|
228
|
-
if (program.scope.hasGlobal("Object"))
|
229
|
-
for (const referencePath of getReferencePathsToGlobal("Object", program))
|
230
|
-
if ("MemberExpression" == referencePath.parent.type && !referencePath.parent.computed) {
|
231
|
-
assert("Identifier" == referencePath.parent.property.type)
|
232
|
-
if ("getPrototypeOf" == referencePath.parent.property.name) {
|
233
|
-
referencePath.parentPath.replaceWith(t.identifier(`_${uniqueID}_GET_PROTOTYPE_OF_`))
|
234
|
-
needGetPrototypeOf = !0
|
235
|
-
} else if ("setPrototypeOf" == referencePath.parent.property.name) {
|
236
|
-
referencePath.parentPath.replaceWith(t.identifier(`_${uniqueID}_SET_PROTOTYPE_OF_`))
|
237
|
-
needSetPrototypeOf = !0
|
238
|
-
}
|
239
|
-
}
|
240
|
-
const lastStatement = program.node.body.at(-1)
|
241
|
-
let exportDefaultName
|
242
|
-
assert(lastStatement, "program is empty")
|
243
|
-
if ("ExportNamedDeclaration" == lastStatement.type) {
|
244
|
-
program.node.body.pop()
|
245
|
-
for (const specifier of lastStatement.specifiers) {
|
246
|
-
assert("ExportSpecifier" == specifier.type, specifier.type + " is currently unsupported")
|
247
|
-
const exportedName =
|
248
|
-
"Identifier" == specifier.exported.type ? specifier.exported.name : specifier.exported.value
|
249
|
-
"default" == exportedName ?
|
250
|
-
(exportDefaultName = specifier.local.name)
|
251
|
-
: exports.set(specifier.local.name, exportedName)
|
183
|
+
consoleMethodsReferenced.add(referencePath.parent.property.name)
|
252
184
|
}
|
185
|
+
const lastStatement = program.node.body.at(-1)
|
186
|
+
let exportDefaultName
|
187
|
+
assert(lastStatement, "src/processScript/transform.ts:264:27 program is empty")
|
188
|
+
if ("ExportNamedDeclaration" == lastStatement.type) {
|
189
|
+
program.node.body.pop()
|
190
|
+
for (const specifier of lastStatement.specifiers) {
|
191
|
+
assert(
|
192
|
+
"ExportSpecifier" == specifier.type,
|
193
|
+
`src/processScript/transform.ts:270:51 ${specifier.type} is currently unsupported`
|
194
|
+
)
|
195
|
+
const exportedName =
|
196
|
+
"Identifier" == specifier.exported.type ? specifier.exported.name : specifier.exported.value
|
197
|
+
"default" == exportedName ?
|
198
|
+
(exportDefaultName = specifier.local.name)
|
199
|
+
: exports.set(specifier.local.name, exportedName)
|
253
200
|
}
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
declarator.init.
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
])
|
276
|
-
)
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
globalBlock.body.push(
|
282
|
-
t.variableDeclaration("let", [t.variableDeclarator(t.identifier(identifierName))])
|
283
|
-
)
|
201
|
+
}
|
202
|
+
const globalBlock = t.blockStatement([])
|
203
|
+
let mainFunction
|
204
|
+
for (const statement of program.node.body)
|
205
|
+
if ("VariableDeclaration" == statement.type)
|
206
|
+
for (const declarator of statement.declarations)
|
207
|
+
if (
|
208
|
+
"Identifier" != declarator.id.type ||
|
209
|
+
declarator.id.name != exportDefaultName ||
|
210
|
+
!declarator.init ||
|
211
|
+
("FunctionExpression" != declarator.init.type &&
|
212
|
+
"ArrowFunctionExpression" != declarator.init.type) ||
|
213
|
+
declarator.init.async ||
|
214
|
+
declarator.init.generator
|
215
|
+
) {
|
216
|
+
for (const identifierName in t.getBindingIdentifiers(declarator.id)) {
|
217
|
+
identifierName == exportDefaultName &&
|
218
|
+
(mainFunction = t.functionDeclaration(
|
219
|
+
t.identifier(topFunctionName),
|
220
|
+
[t.identifier("context"), t.identifier("args")],
|
221
|
+
t.blockStatement([
|
222
|
+
t.returnStatement(t.callExpression(t.identifier(exportDefaultName), []))
|
223
|
+
])
|
224
|
+
))
|
225
|
+
if ("const" != statement.kind && exports.has(identifierName)) {
|
226
|
+
liveExports.set(identifierName, exports.get(identifierName))
|
227
|
+
exports.delete(identifierName)
|
284
228
|
}
|
285
|
-
|
286
|
-
|
287
|
-
t.expressionStatement(t.assignmentExpression("=", declarator.id, declarator.init))
|
288
|
-
)
|
289
|
-
} else
|
290
|
-
mainFunction = t.functionDeclaration(
|
291
|
-
t.identifier(topFunctionName),
|
292
|
-
declarator.init.params,
|
293
|
-
"BlockStatement" == declarator.init.body.type ?
|
294
|
-
declarator.init.body
|
295
|
-
: t.blockStatement([t.returnStatement(declarator.init.body)])
|
229
|
+
globalBlock.body.push(
|
230
|
+
t.variableDeclaration("let", [t.variableDeclarator(t.identifier(identifierName))])
|
296
231
|
)
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
: globalBlock.body.push(
|
302
|
-
t.variableDeclaration("let", [
|
303
|
-
t.variableDeclarator(
|
304
|
-
statement.id,
|
305
|
-
t.functionExpression(
|
306
|
-
void 0,
|
307
|
-
statement.params,
|
308
|
-
statement.body,
|
309
|
-
statement.generator,
|
310
|
-
statement.async
|
311
|
-
)
|
312
|
-
)
|
313
|
-
])
|
232
|
+
}
|
233
|
+
declarator.init &&
|
234
|
+
globalBlock.body.push(
|
235
|
+
t.expressionStatement(t.assignmentExpression("=", declarator.id, declarator.init))
|
314
236
|
)
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
t.
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
237
|
+
} else
|
238
|
+
mainFunction = t.functionDeclaration(
|
239
|
+
t.identifier(topFunctionName),
|
240
|
+
declarator.init.params,
|
241
|
+
"BlockStatement" == declarator.init.body.type ?
|
242
|
+
declarator.init.body
|
243
|
+
: t.blockStatement([t.returnStatement(declarator.init.body)])
|
244
|
+
)
|
245
|
+
else
|
246
|
+
"FunctionDeclaration" == statement.type ?
|
247
|
+
statement.id.name == exportDefaultName ?
|
248
|
+
(mainFunction = statement)
|
249
|
+
: globalBlock.body.push(
|
250
|
+
t.variableDeclaration("let", [
|
251
|
+
t.variableDeclarator(
|
252
|
+
statement.id,
|
253
|
+
t.functionExpression(
|
254
|
+
void 0,
|
255
|
+
statement.params,
|
256
|
+
statement.body,
|
257
|
+
statement.generator,
|
258
|
+
statement.async
|
336
259
|
)
|
337
260
|
)
|
338
261
|
])
|
339
262
|
)
|
263
|
+
: globalBlock.body.push(statement)
|
264
|
+
mainFunction ||= t.functionDeclaration(
|
265
|
+
t.identifier(topFunctionName),
|
266
|
+
[t.identifier("context"), t.identifier("args")],
|
267
|
+
t.blockStatement([])
|
268
|
+
)
|
269
|
+
if (uniqueIdScriptUserNeeded) {
|
270
|
+
const mainFunctionParams = mainFunction.params
|
271
|
+
mainFunction.params = [t.restElement(t.identifier(`_${uniqueID}_PARAMS_`))]
|
272
|
+
mainFunction.body.body.unshift(
|
273
|
+
t.variableDeclaration("let", [
|
274
|
+
t.variableDeclarator(t.arrayPattern(mainFunctionParams), t.identifier(`_${uniqueID}_PARAMS_`)),
|
275
|
+
t.variableDeclarator(
|
276
|
+
t.arrayPattern([t.identifier(`_${uniqueID}_SCRIPT_USER_`)]),
|
277
|
+
t.callExpression(
|
278
|
+
t.memberExpression(
|
279
|
+
t.memberExpression(
|
280
|
+
t.memberExpression(t.identifier(`_${uniqueID}_PARAMS_`), t.numericLiteral(0), !0),
|
281
|
+
t.identifier("this_script")
|
282
|
+
),
|
283
|
+
t.identifier("split")
|
284
|
+
),
|
285
|
+
[t.stringLiteral(".")]
|
286
|
+
)
|
340
287
|
)
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
)
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
node,
|
381
|
-
t.memberExpression(
|
382
|
-
t.identifier(`$${uniqueID}$GLOBAL$`),
|
383
|
-
t.identifier(name)
|
384
|
-
)
|
385
|
-
)
|
386
|
-
}
|
387
|
-
globalBlockPath.remove()
|
388
|
-
globalBlockStatementPath.remove()
|
389
|
-
declarator.init &&
|
390
|
-
globalBlock.body.splice(
|
391
|
-
globalBlockIndex,
|
392
|
-
0,
|
393
|
-
t.expressionStatement(
|
394
|
-
t.assignmentExpression(
|
395
|
-
"=",
|
396
|
-
t.memberExpression(
|
397
|
-
t.identifier(`$${uniqueID}$GLOBAL$`),
|
398
|
-
t.identifier(declarator.id.name)
|
399
|
-
),
|
400
|
-
declarator.init
|
401
|
-
)
|
402
|
-
)
|
403
|
-
)
|
404
|
-
} else {
|
405
|
-
globalBlockPath.remove()
|
406
|
-
globalBlockStatementPath.remove()
|
407
|
-
mainFunction.body.body.unshift(globalBlockStatement)
|
408
|
-
hoistedGlobalBlockFunctions++
|
409
|
-
}
|
410
|
-
} else globalBlockVariables.add(declarator.id.name)
|
411
|
-
} else if ("ClassDeclaration" == globalBlockStatement.type) {
|
288
|
+
])
|
289
|
+
)
|
290
|
+
}
|
291
|
+
program.node.body = [mainFunction]
|
292
|
+
if (globalBlock.body.length) {
|
293
|
+
;(exports.size || liveExports.size) &&
|
294
|
+
mainFunction.body.body.push(
|
295
|
+
t.returnStatement(
|
296
|
+
t.objectExpression([
|
297
|
+
...[...exports].map(([local, exported]) =>
|
298
|
+
t.objectProperty(t.identifier(exported), t.identifier(local))
|
299
|
+
),
|
300
|
+
...[...liveExports].map(([local, exported]) =>
|
301
|
+
t.objectMethod(
|
302
|
+
"get",
|
303
|
+
t.identifier(exported),
|
304
|
+
[],
|
305
|
+
t.blockStatement([t.returnStatement(t.identifier(local))])
|
306
|
+
)
|
307
|
+
)
|
308
|
+
])
|
309
|
+
)
|
310
|
+
)
|
311
|
+
program.scope.crawl()
|
312
|
+
const globalBlockVariables = new Set()
|
313
|
+
let hoistedGlobalBlockFunctions = 0
|
314
|
+
for (const [globalBlockIndex, globalBlockStatement] of [...globalBlock.body.entries()].reverse())
|
315
|
+
if ("VariableDeclaration" == globalBlockStatement.type) {
|
316
|
+
assert(1 == globalBlockStatement.declarations.length, "src/processScript/transform.ts:404:59")
|
317
|
+
const declarator = globalBlockStatement.declarations[0]
|
318
|
+
assert(
|
319
|
+
"Identifier" == declarator.id.type,
|
320
|
+
`src/processScript/transform.ts:408:51 declarator.id.type was "${declarator.id.type}"`
|
321
|
+
)
|
322
|
+
program.scope.crawl()
|
323
|
+
if (program.scope.hasGlobal(declarator.id.name)) {
|
324
|
+
globalBlock.body.splice(globalBlockIndex, 1)
|
325
|
+
const [globalBlockPath] = program.unshiftContainer("body", globalBlock),
|
326
|
+
[globalBlockStatementPath] = program.unshiftContainer("body", globalBlockStatement)
|
412
327
|
program.scope.crawl()
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
const binding = program.scope.getBinding(
|
420
|
-
assert(binding)
|
328
|
+
if (
|
329
|
+
!declarator.init ||
|
330
|
+
("FunctionExpression" != declarator.init.type &&
|
331
|
+
"ArrowFunctionExpression" != declarator.init.type) ||
|
332
|
+
Object.keys(program.scope.globals).some(global => globalBlockVariables.has(global))
|
333
|
+
) {
|
334
|
+
const binding = program.scope.getBinding(declarator.id.name)
|
335
|
+
assert(binding, "src/processScript/transform.ts:427:23")
|
421
336
|
for (const referencePath of binding.referencePaths) {
|
422
|
-
assert("Identifier" == referencePath.node.type)
|
337
|
+
assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:430:56")
|
423
338
|
referencePath.replaceWith(
|
424
339
|
t.memberExpression(
|
425
340
|
t.identifier(`$${uniqueID}$GLOBAL$`),
|
@@ -427,223 +342,354 @@ const { default: traverse } = babelTraverse,
|
|
427
342
|
)
|
428
343
|
)
|
429
344
|
}
|
345
|
+
for (const referencePath of binding.constantViolations)
|
346
|
+
if ("AssignmentExpression" == referencePath.node.type)
|
347
|
+
for (const [name, node] of Object.entries(t.getBindingIdentifiers(referencePath.node)))
|
348
|
+
if (name == declarator.id.name) {
|
349
|
+
clearObject(node)
|
350
|
+
Object.assign(
|
351
|
+
node,
|
352
|
+
t.memberExpression(t.identifier(`$${uniqueID}$GLOBAL$`), t.identifier(name))
|
353
|
+
)
|
354
|
+
}
|
430
355
|
globalBlockPath.remove()
|
431
356
|
globalBlockStatementPath.remove()
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
t.
|
437
|
-
|
438
|
-
|
439
|
-
t.
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
globalBlockStatement.superClass,
|
445
|
-
globalBlockStatement.body,
|
446
|
-
globalBlockStatement.decorators
|
357
|
+
declarator.init &&
|
358
|
+
globalBlock.body.splice(
|
359
|
+
globalBlockIndex,
|
360
|
+
0,
|
361
|
+
t.expressionStatement(
|
362
|
+
t.assignmentExpression(
|
363
|
+
"=",
|
364
|
+
t.memberExpression(
|
365
|
+
t.identifier(`$${uniqueID}$GLOBAL$`),
|
366
|
+
t.identifier(declarator.id.name)
|
367
|
+
),
|
368
|
+
declarator.init
|
447
369
|
)
|
448
370
|
)
|
449
371
|
)
|
372
|
+
} else {
|
373
|
+
globalBlockPath.remove()
|
374
|
+
globalBlockStatementPath.remove()
|
375
|
+
mainFunction.body.body.unshift(globalBlockStatement)
|
376
|
+
hoistedGlobalBlockFunctions++
|
377
|
+
}
|
378
|
+
} else globalBlockVariables.add(declarator.id.name)
|
379
|
+
} else if ("ClassDeclaration" == globalBlockStatement.type) {
|
380
|
+
program.scope.crawl()
|
381
|
+
assert(globalBlockStatement.id, "src/processScript/transform.ts:481:37")
|
382
|
+
if (program.scope.hasGlobal(globalBlockStatement.id.name)) {
|
383
|
+
globalBlock.body.splice(globalBlockIndex, 1)
|
384
|
+
const [globalBlockPath] = program.unshiftContainer("body", globalBlock),
|
385
|
+
[globalBlockStatementPath] = program.unshiftContainer("body", globalBlockStatement)
|
386
|
+
program.scope.crawl()
|
387
|
+
const binding = program.scope.getBinding(globalBlockStatement.id.name)
|
388
|
+
assert(binding, "src/processScript/transform.ts:493:22")
|
389
|
+
for (const referencePath of binding.referencePaths) {
|
390
|
+
assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:496:55")
|
391
|
+
referencePath.replaceWith(
|
392
|
+
t.memberExpression(
|
393
|
+
t.identifier(`$${uniqueID}$GLOBAL$`),
|
394
|
+
t.identifier(referencePath.node.name)
|
395
|
+
)
|
450
396
|
)
|
451
397
|
}
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
398
|
+
globalBlockPath.remove()
|
399
|
+
globalBlockStatementPath.remove()
|
400
|
+
globalBlock.body.splice(
|
401
|
+
globalBlockIndex,
|
402
|
+
0,
|
403
|
+
t.expressionStatement(
|
404
|
+
t.assignmentExpression(
|
405
|
+
"=",
|
406
|
+
t.memberExpression(
|
407
|
+
t.identifier(`$${uniqueID}$GLOBAL$`),
|
408
|
+
t.identifier(globalBlockStatement.id.name)
|
409
|
+
),
|
410
|
+
t.classExpression(
|
411
|
+
void 0,
|
412
|
+
globalBlockStatement.superClass,
|
413
|
+
globalBlockStatement.body,
|
414
|
+
globalBlockStatement.decorators
|
415
|
+
)
|
416
|
+
)
|
417
|
+
)
|
457
418
|
)
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
419
|
+
}
|
420
|
+
}
|
421
|
+
if (program.scope.hasGlobal("_EXPORTS"))
|
422
|
+
for (const referencePath of getReferencePathsToGlobal("_EXPORTS", program))
|
423
|
+
referencePath.replaceWith(
|
424
|
+
t.arrayExpression([...exports.keys(), ...liveExports.keys()].map(name => t.stringLiteral(name)))
|
463
425
|
)
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
t.identifier(`_${uniqueID}_FUNCTION_DOT_PROTOTYPE_`),
|
470
|
-
createGetFunctionPrototypeNode()
|
471
|
-
)
|
472
|
-
])
|
426
|
+
globalBlock.body.length &&
|
427
|
+
mainFunction.body.body.splice(
|
428
|
+
hoistedGlobalBlockFunctions,
|
429
|
+
0,
|
430
|
+
t.ifStatement(t.unaryExpression("!", t.identifier(`$${uniqueID}$FMCL$`)), globalBlock)
|
473
431
|
)
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
432
|
+
}
|
433
|
+
functionDotPrototypeIsReferencedMultipleTimes &&
|
434
|
+
mainFunction.body.body.unshift(
|
435
|
+
t.variableDeclaration("let", [
|
436
|
+
t.variableDeclarator(
|
437
|
+
t.identifier(`_${uniqueID}_FUNCTION_DOT_PROTOTYPE_`),
|
438
|
+
createGetFunctionPrototypeNode()
|
439
|
+
)
|
440
|
+
])
|
441
|
+
)
|
442
|
+
needGetPrototypeOf &&
|
443
|
+
mainFunction.body.body.unshift(
|
444
|
+
t.variableDeclaration("let", [
|
445
|
+
t.variableDeclarator(
|
446
|
+
t.objectPattern([
|
447
|
+
t.objectProperty(t.identifier("get"), t.identifier(`_${uniqueID}_DUNDER_PROTO_GETTER_`))
|
448
|
+
]),
|
449
|
+
t.callExpression(
|
450
|
+
t.memberExpression(t.identifier("Object"), t.identifier("getOwnPropertyDescriptor")),
|
451
|
+
[
|
452
|
+
t.memberExpression(t.identifier("Object"), t.identifier("prototype")),
|
453
|
+
t.stringLiteral("__proto__")
|
454
|
+
]
|
455
|
+
)
|
456
|
+
),
|
457
|
+
t.variableDeclarator(
|
458
|
+
t.identifier(`_${uniqueID}_GET_PROTOTYPE_OF_`),
|
459
|
+
t.callExpression(
|
460
|
+
t.memberExpression(
|
480
461
|
t.memberExpression(
|
481
|
-
t.
|
482
|
-
|
462
|
+
t.identifier(
|
463
|
+
globalFunctionsUnder7Characters.find(name => !program.scope.hasOwnBinding(name))
|
464
|
+
),
|
465
|
+
t.identifier("call")
|
483
466
|
),
|
484
|
-
|
467
|
+
t.identifier("bind")
|
468
|
+
),
|
469
|
+
[t.identifier(`_${uniqueID}_DUNDER_PROTO_GETTER_`)]
|
470
|
+
)
|
471
|
+
)
|
472
|
+
])
|
473
|
+
)
|
474
|
+
consoleMethodsReferenced.size &&
|
475
|
+
mainFunction.body.body.unshift(
|
476
|
+
t.variableDeclaration(
|
477
|
+
"let",
|
478
|
+
[...consoleMethodsReferenced].map(name =>
|
479
|
+
t.variableDeclarator(
|
480
|
+
t.identifier(`_${uniqueID}_CONSOLE_METHOD_${name}_`),
|
481
|
+
t.arrowFunctionExpression(
|
482
|
+
[t.restElement(t.identifier("args"))],
|
483
|
+
t.unaryExpression(
|
484
|
+
"void",
|
485
|
+
t.callExpression(t.identifier(`$${uniqueID}$DEBUG$`), [t.identifier("args")])
|
486
|
+
)
|
485
487
|
)
|
486
488
|
)
|
487
|
-
|
489
|
+
)
|
488
490
|
)
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
491
|
+
)
|
492
|
+
neededDbMethodLets.size &&
|
493
|
+
mainFunction.body.body.unshift(
|
494
|
+
t.variableDeclaration(
|
495
|
+
"let",
|
496
|
+
[...neededDbMethodLets].map(name => {
|
497
|
+
const getArgs = () =>
|
498
|
+
"ObjectId" == name ? []
|
499
|
+
: "i" == name || "r" == name ? [t.identifier("a")]
|
500
|
+
: [t.identifier("a"), t.identifier("b")]
|
501
|
+
return t.variableDeclarator(
|
502
|
+
t.identifier(`_${uniqueID}_CONSOLE_METHOD_${name}_`),
|
503
|
+
t.arrowFunctionExpression(
|
504
|
+
getArgs(),
|
505
|
+
t.callExpression(t.identifier(`$${uniqueID}$DB$${name}$`), getArgs())
|
500
506
|
)
|
501
507
|
)
|
502
|
-
|
508
|
+
})
|
503
509
|
)
|
504
|
-
|
505
|
-
|
506
|
-
|
510
|
+
)
|
511
|
+
needDebugLet &&
|
512
|
+
mainFunction.body.body.unshift(
|
513
|
+
t.variableDeclaration("let", [
|
514
|
+
t.variableDeclarator(
|
515
|
+
t.identifier(`_${uniqueID}_DEBUG_`),
|
516
|
+
t.callExpression(t.identifier(`$${uniqueID}$DEBUG$`), [t.identifier("a")])
|
517
|
+
)
|
518
|
+
])
|
519
|
+
)
|
520
|
+
neededSubscriptLets.size &&
|
521
|
+
mainFunction.body.body.unshift(
|
522
|
+
t.variableDeclaration(
|
523
|
+
"let",
|
524
|
+
[...neededSubscriptLets].map(name =>
|
507
525
|
t.variableDeclarator(
|
508
|
-
t.
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
t.identifier(`_${uniqueID}_DUNDER_PROTO_GETTER_`)
|
515
|
-
),
|
516
|
-
t.objectProperty(
|
517
|
-
t.identifier("set"),
|
518
|
-
t.identifier(`_${uniqueID}_DUNDER_PROTO_SETTER_`)
|
519
|
-
)
|
520
|
-
]
|
521
|
-
: [
|
522
|
-
t.objectProperty(
|
523
|
-
t.identifier("get"),
|
524
|
-
t.identifier(`_${uniqueID}_DUNDER_PROTO_GETTER_`)
|
525
|
-
)
|
526
|
-
]
|
527
|
-
: [t.objectProperty(t.identifier("set"), t.identifier(`_${uniqueID}_DUNDER_PROTO_SETTER_`))]
|
528
|
-
),
|
529
|
-
t.callExpression(
|
530
|
-
t.memberExpression(t.identifier("Object"), t.identifier("getOwnPropertyDescriptor")),
|
531
|
-
[
|
532
|
-
t.memberExpression(t.identifier("Object"), t.identifier("prototype")),
|
533
|
-
t.stringLiteral("__proto__")
|
534
|
-
]
|
526
|
+
t.identifier(`_${uniqueID}_SUBSCRIPT_${name}_`),
|
527
|
+
t.arrowFunctionExpression(
|
528
|
+
[t.restElement(t.identifier("args"))],
|
529
|
+
t.callExpression(t.identifier(`$${uniqueID}$SUBSCRIPT$${name}$`), [
|
530
|
+
t.spreadElement(t.identifier("args"))
|
531
|
+
])
|
535
532
|
)
|
536
533
|
)
|
537
|
-
|
534
|
+
)
|
538
535
|
)
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
536
|
+
)
|
537
|
+
traverse(file, {
|
538
|
+
BlockStatement({ node: blockStatement }) {
|
539
|
+
for (const [index, functionDeclaration] of blockStatement.body.entries())
|
540
|
+
if ("FunctionDeclaration" == functionDeclaration.type && !functionDeclaration.generator) {
|
541
|
+
blockStatement.body.splice(index, 1)
|
542
|
+
blockStatement.body.unshift(
|
543
|
+
t.variableDeclaration("let", [
|
544
|
+
t.variableDeclarator(
|
545
|
+
functionDeclaration.id,
|
546
|
+
t.arrowFunctionExpression(
|
547
|
+
functionDeclaration.params,
|
548
|
+
functionDeclaration.body,
|
549
|
+
functionDeclaration.async
|
550
|
+
)
|
551
|
+
)
|
552
|
+
])
|
553
|
+
)
|
554
|
+
}
|
555
|
+
},
|
556
|
+
ClassBody({ node: classBody, scope, parent }) {
|
557
|
+
assert(t.isClass(parent), "src/processScript/transform.ts:663:30")
|
558
|
+
let thisIsReferenced = !1
|
559
|
+
for (const classMethod of classBody.body) {
|
560
|
+
if ("ClassMethod" != classMethod.type) continue
|
561
|
+
let methodReferencesThis = !1
|
562
|
+
traverse(
|
563
|
+
classMethod.body,
|
564
|
+
{
|
565
|
+
ThisExpression(path) {
|
566
|
+
methodReferencesThis = !0
|
567
|
+
thisIsReferenced = !0
|
568
|
+
path.replaceWith(t.identifier(`_${uniqueID}_THIS_`))
|
569
|
+
},
|
570
|
+
Function: path => path.skip()
|
571
|
+
},
|
572
|
+
scope
|
573
|
+
)
|
574
|
+
if (methodReferencesThis)
|
575
|
+
if ("constructor" != classMethod.kind)
|
576
|
+
classMethod.body.body.unshift(
|
545
577
|
t.variableDeclaration("let", [
|
546
578
|
t.variableDeclarator(
|
547
|
-
|
548
|
-
t.
|
549
|
-
functionDeclaration.params,
|
550
|
-
functionDeclaration.body,
|
551
|
-
functionDeclaration.async
|
552
|
-
)
|
579
|
+
t.identifier(`_${uniqueID}_THIS_`),
|
580
|
+
t.callExpression(t.memberExpression(t.super(), t.identifier("valueOf")), [])
|
553
581
|
)
|
554
582
|
])
|
555
583
|
)
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
traverse(
|
565
|
-
classMethod.body,
|
566
|
-
{
|
567
|
-
ThisExpression(path) {
|
568
|
-
methodReferencesThis = !0
|
569
|
-
thisIsReferenced = !0
|
570
|
-
path.replaceWith(t.identifier(`_${uniqueID}_THIS_`))
|
584
|
+
else {
|
585
|
+
const superCalls = []
|
586
|
+
traverse(
|
587
|
+
classMethod.body,
|
588
|
+
{
|
589
|
+
CallExpression(path) {
|
590
|
+
"Super" == path.node.callee.type && superCalls.push(path)
|
591
|
+
}
|
571
592
|
},
|
572
|
-
|
573
|
-
|
593
|
+
scope
|
594
|
+
)
|
595
|
+
if (superCalls.length)
|
596
|
+
if (
|
597
|
+
1 == superCalls.length &&
|
598
|
+
"ExpressionStatement" == superCalls[0].parent.type &&
|
599
|
+
superCalls[0].parentPath.parentPath.parent == classMethod
|
600
|
+
)
|
601
|
+
superCalls[0].parentPath.replaceWith(
|
602
|
+
t.variableDeclaration("let", [
|
603
|
+
t.variableDeclarator(t.identifier(`_${uniqueID}_THIS_`), superCalls[0].node)
|
604
|
+
])
|
605
|
+
)
|
606
|
+
else {
|
607
|
+
for (const path of superCalls)
|
608
|
+
path.replaceWith(
|
609
|
+
t.assignmentExpression("=", t.identifier(`_${uniqueID}_THIS_`), path.node)
|
610
|
+
)
|
611
|
+
classMethod.body.body.unshift(
|
612
|
+
t.variableDeclaration("let", [
|
613
|
+
t.variableDeclarator(t.identifier(`_${uniqueID}_THIS_`))
|
614
|
+
])
|
615
|
+
)
|
574
616
|
}
|
575
|
-
|
576
|
-
scope
|
577
|
-
)
|
578
|
-
if (methodReferencesThis)
|
579
|
-
if ("constructor" != classMethod.kind)
|
617
|
+
else
|
580
618
|
classMethod.body.body.unshift(
|
581
619
|
t.variableDeclaration("let", [
|
582
620
|
t.variableDeclarator(
|
583
621
|
t.identifier(`_${uniqueID}_THIS_`),
|
584
|
-
t.callExpression(t.
|
622
|
+
t.callExpression(t.super(), [])
|
585
623
|
)
|
586
624
|
])
|
587
625
|
)
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
626
|
+
}
|
627
|
+
}
|
628
|
+
!parent.superClass && thisIsReferenced && (parent.superClass = t.identifier("Object"))
|
629
|
+
},
|
630
|
+
VariableDeclaration({ node: variableDeclaration }) {
|
631
|
+
"const" == variableDeclaration.kind && (variableDeclaration.kind = "let")
|
632
|
+
},
|
633
|
+
ThisExpression: path => path.replaceWith(t.identifier("undefined")),
|
634
|
+
BigIntLiteral(path) {
|
635
|
+
const bigIntAsNumber = Number(path.node.value)
|
636
|
+
path.replaceWith(
|
637
|
+
t.callExpression(t.identifier("BigInt"), [
|
638
|
+
BigInt(bigIntAsNumber) == BigInt(path.node.value) ?
|
639
|
+
t.numericLiteral(bigIntAsNumber)
|
640
|
+
: t.stringLiteral(path.node.value)
|
641
|
+
])
|
642
|
+
)
|
643
|
+
}
|
644
|
+
})
|
645
|
+
return { file, seclevel }
|
646
|
+
function createGetFunctionPrototypeNode() {
|
647
|
+
for (const globalFunction of globalFunctionsUnder7Characters)
|
648
|
+
if (!program.scope.hasOwnBinding(globalFunction))
|
649
|
+
return t.memberExpression(
|
650
|
+
t.memberExpression(t.identifier(globalFunction), t.identifier("constructor")),
|
651
|
+
t.identifier("prototype")
|
652
|
+
)
|
653
|
+
return t.memberExpression(
|
654
|
+
t.memberExpression(
|
655
|
+
t.arrowFunctionExpression([t.identifier("_")], t.identifier("_")),
|
656
|
+
t.identifier("constructor")
|
657
|
+
),
|
658
|
+
t.identifier("prototype")
|
659
|
+
)
|
660
|
+
}
|
661
|
+
function processFakeSubscriptObject(fakeSubscriptObjectName) {
|
662
|
+
for (const referencePath of getReferencePathsToGlobal(fakeSubscriptObjectName, program)) {
|
663
|
+
assert("MemberExpression" == referencePath.parent.type, "src/processScript/transform.ts:777:60")
|
664
|
+
assert("Identifier" == referencePath.parent.property.type)
|
665
|
+
assert(
|
666
|
+
"MemberExpression" == referencePath.parentPath.parentPath?.node.type,
|
667
|
+
"src/processScript/transform.ts:779:81"
|
668
|
+
)
|
669
|
+
assert(
|
670
|
+
"Identifier" == referencePath.parentPath.parentPath.node.property.type,
|
671
|
+
"src/processScript/transform.ts:780:83"
|
672
|
+
)
|
673
|
+
assert(
|
674
|
+
/^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parent.property.name),
|
675
|
+
`src/processScript/transform.ts:784:8 invalid user "${referencePath.parent.property.name}" in subscript`
|
676
|
+
)
|
677
|
+
assert(
|
678
|
+
/^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parentPath.parentPath.node.property.name),
|
679
|
+
`src/processScript/transform.ts:789:8 invalid script name "${referencePath.parentPath.parentPath.node.property.name}" in subscript`
|
680
|
+
)
|
681
|
+
if ("CallExpression" == referencePath.parentPath.parentPath.parentPath?.type)
|
682
|
+
referencePath.parentPath.parentPath.replaceWith(
|
683
|
+
t.identifier(
|
684
|
+
`$${uniqueID}$SUBSCRIPT$${referencePath.parent.property.name}$${referencePath.parentPath.parentPath.node.property.name}$`
|
685
|
+
)
|
686
|
+
)
|
687
|
+
else {
|
688
|
+
const name = `${referencePath.parent.property.name}$${referencePath.parentPath.parentPath.node.property.name}`
|
689
|
+
referencePath.parentPath.parentPath.replaceWith(t.identifier(`_${uniqueID}_SUBSCRIPT_${name}_`))
|
690
|
+
neededSubscriptLets.add(name)
|
645
691
|
}
|
646
|
-
}
|
647
|
-
return { file, seclevel }
|
692
|
+
}
|
648
693
|
}
|
649
|
-
|
694
|
+
}
|
695
|
+
export { transform }
|