hackmud-script-manager 0.19.0 → 0.19.1-01c1752

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