hackmud-script-manager 0.19.0-fa82f73 → 0.19.1-27bbd7d

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1,649 @@
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
+ transform = (
25
+ file,
26
+ sourceCode,
27
+ { uniqueID = "00000000000", scriptUser = "UNKNOWN", 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()
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
+ )
75
+ }
76
+ if (program.scope.hasGlobal("Function")) {
77
+ const FunctionReferencePaths = getReferencePathsToGlobal("Function", program)
78
+ if (1 == FunctionReferencePaths.length) {
79
+ const referencePath = FunctionReferencePaths[0]
80
+ assert(
81
+ "MemberExpression" == referencePath.parent.type,
82
+ "`Function` isn't available in hackmud, only `Function.prototype` is accessible"
83
+ )
84
+ assert(
85
+ "Identifier" == referencePath.parent.property.type,
86
+ "`Function` isn't available in hackmud, only `Function.prototype` is accessible"
87
+ )
88
+ assert(
89
+ "prototype" == referencePath.parent.property.name,
90
+ "`Function` isn't available in hackmud, only `Function.prototype` is accessible"
91
+ )
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
+ functionDotPrototypeIsReferencedMultipleTimes = !0
111
+ }
112
+ }
113
+ let detectedSeclevel = 4
114
+ const processFakeSubscriptObject = fakeSubscriptObjectName => {
115
+ for (const referencePath of getReferencePathsToGlobal(fakeSubscriptObjectName, program)) {
116
+ assert("MemberExpression" == referencePath.parent.type)
117
+ assert("Identifier" == referencePath.parent.property.type)
118
+ assert("MemberExpression" == referencePath.parentPath.parentPath?.node.type)
119
+ assert("Identifier" == referencePath.parentPath.parentPath.node.property.type)
120
+ assert(
121
+ /^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parent.property.name),
122
+ `invalid user "${referencePath.parent.property.name}" in subscript`
123
+ )
124
+ assert(
125
+ /^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parentPath.parentPath.node.property.name),
126
+ `invalid script name "${referencePath.parentPath.parentPath.node.property.name}" in subscript`
127
+ )
128
+ "CallExpression" == referencePath.parentPath.parentPath.parentPath?.type ?
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
+ )
145
+ }
146
+ }
147
+ for (const fakeSubscriptObjectName of ["$fs", "$4s", "$s"])
148
+ program.scope.hasGlobal(fakeSubscriptObjectName) && processFakeSubscriptObject(fakeSubscriptObjectName)
149
+ for (const fakeSubscriptObjectName of ["$hs", "$3s"])
150
+ if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
151
+ detectedSeclevel = 3
152
+ processFakeSubscriptObject(fakeSubscriptObjectName)
153
+ }
154
+ for (const fakeSubscriptObjectName of ["$ms", "$2s"])
155
+ if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
156
+ detectedSeclevel = 2
157
+ processFakeSubscriptObject(fakeSubscriptObjectName)
158
+ }
159
+ for (const fakeSubscriptObjectName of ["$ls", "$1s"])
160
+ if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
161
+ detectedSeclevel = 1
162
+ processFakeSubscriptObject(fakeSubscriptObjectName)
163
+ }
164
+ for (const fakeSubscriptObjectName of ["$ns", "$0s"])
165
+ if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
166
+ detectedSeclevel = 0
167
+ processFakeSubscriptObject(fakeSubscriptObjectName)
168
+ }
169
+ seclevel = Math.min(seclevel, detectedSeclevel)
170
+ if (program.scope.hasGlobal("$db"))
171
+ for (const referencePath of getReferencePathsToGlobal("$db", program)) {
172
+ assert("MemberExpression" == referencePath.parentPath.node.type)
173
+ assert("Identifier" == referencePath.parentPath.node.property.type)
174
+ const databaseOpMethodName = referencePath.parentPath.node.property.name
175
+ assert(
176
+ validDBMethods.includes(databaseOpMethodName),
177
+ `invalid db method "${databaseOpMethodName}", valid db methods are "${validDBMethods.join('", "')}"`
178
+ )
179
+ "CallExpression" == referencePath.parentPath.parentPath?.type ?
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)
252
+ }
253
+ }
254
+ const globalBlock = t.blockStatement([])
255
+ let mainFunction
256
+ for (const statement of program.node.body)
257
+ if ("VariableDeclaration" == statement.type)
258
+ for (const declarator of statement.declarations)
259
+ if (
260
+ "Identifier" != declarator.id.type ||
261
+ declarator.id.name != exportDefaultName ||
262
+ !declarator.init ||
263
+ ("FunctionExpression" != declarator.init.type &&
264
+ "ArrowFunctionExpression" != declarator.init.type) ||
265
+ declarator.init.async ||
266
+ declarator.init.generator
267
+ ) {
268
+ for (const identifierName in t.getBindingIdentifiers(declarator.id)) {
269
+ identifierName == exportDefaultName &&
270
+ (mainFunction = t.functionDeclaration(
271
+ t.identifier(topFunctionName),
272
+ [t.identifier("context"), t.identifier("args")],
273
+ t.blockStatement([
274
+ t.returnStatement(t.callExpression(t.identifier(exportDefaultName), []))
275
+ ])
276
+ ))
277
+ if ("const" != statement.kind && exports.has(identifierName)) {
278
+ liveExports.set(identifierName, exports.get(identifierName))
279
+ exports.delete(identifierName)
280
+ }
281
+ globalBlock.body.push(
282
+ t.variableDeclaration("let", [t.variableDeclarator(t.identifier(identifierName))])
283
+ )
284
+ }
285
+ declarator.init &&
286
+ globalBlock.body.push(
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)])
296
+ )
297
+ else
298
+ "FunctionDeclaration" == statement.type ?
299
+ statement.id.name == exportDefaultName ?
300
+ (mainFunction = statement)
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
+ ])
314
+ )
315
+ : globalBlock.body.push(statement)
316
+ mainFunction ||= t.functionDeclaration(
317
+ t.identifier(topFunctionName),
318
+ [t.identifier("context"), t.identifier("args")],
319
+ t.blockStatement([])
320
+ )
321
+ program.node.body = [mainFunction]
322
+ if (globalBlock.body.length) {
323
+ ;(exports.size || liveExports.size) &&
324
+ mainFunction.body.body.push(
325
+ t.returnStatement(
326
+ t.objectExpression([
327
+ ...[...exports].map(([local, exported]) =>
328
+ t.objectProperty(t.identifier(exported), t.identifier(local))
329
+ ),
330
+ ...[...liveExports].map(([local, exported]) =>
331
+ t.objectMethod(
332
+ "get",
333
+ t.identifier(exported),
334
+ [],
335
+ t.blockStatement([t.returnStatement(t.identifier(local))])
336
+ )
337
+ )
338
+ ])
339
+ )
340
+ )
341
+ program.scope.crawl()
342
+ const globalBlockVariables = new Set()
343
+ let hoistedGlobalBlockFunctions = 0
344
+ for (const [globalBlockIndex, globalBlockStatement] of [...globalBlock.body.entries()].reverse())
345
+ if ("VariableDeclaration" == globalBlockStatement.type) {
346
+ assert(1 == globalBlockStatement.declarations.length)
347
+ const declarator = globalBlockStatement.declarations[0]
348
+ assert("Identifier" == declarator.id.type, `declarator.id.type was "${declarator.id.type}"`)
349
+ program.scope.crawl()
350
+ if (program.scope.hasGlobal(declarator.id.name)) {
351
+ globalBlock.body.splice(globalBlockIndex, 1)
352
+ const [globalBlockPath] = program.unshiftContainer("body", globalBlock),
353
+ [globalBlockStatementPath] = program.unshiftContainer("body", globalBlockStatement)
354
+ program.scope.crawl()
355
+ if (
356
+ !declarator.init ||
357
+ ("FunctionExpression" != declarator.init.type &&
358
+ "ArrowFunctionExpression" != declarator.init.type) ||
359
+ Object.keys(program.scope.globals).some(global => globalBlockVariables.has(global))
360
+ ) {
361
+ const binding = program.scope.getBinding(declarator.id.name)
362
+ assert(binding)
363
+ for (const referencePath of binding.referencePaths) {
364
+ assert("Identifier" == referencePath.node.type)
365
+ referencePath.replaceWith(
366
+ t.memberExpression(
367
+ t.identifier(`$${uniqueID}$GLOBAL$`),
368
+ t.identifier(referencePath.node.name)
369
+ )
370
+ )
371
+ }
372
+ for (const referencePath of binding.constantViolations)
373
+ if ("AssignmentExpression" == referencePath.node.type)
374
+ for (const [name, node] of Object.entries(
375
+ t.getBindingIdentifiers(referencePath.node)
376
+ ))
377
+ if (name == declarator.id.name) {
378
+ clearObject(node)
379
+ Object.assign(
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) {
412
+ program.scope.crawl()
413
+ assert(globalBlockStatement.id, "src/processScript/transform.ts:564:37")
414
+ if (program.scope.hasGlobal(globalBlockStatement.id.name)) {
415
+ globalBlock.body.splice(globalBlockIndex, 1)
416
+ const [globalBlockPath] = program.unshiftContainer("body", globalBlock),
417
+ [globalBlockStatementPath] = program.unshiftContainer("body", globalBlockStatement)
418
+ program.scope.crawl()
419
+ const binding = program.scope.getBinding(globalBlockStatement.id.name)
420
+ assert(binding)
421
+ for (const referencePath of binding.referencePaths) {
422
+ assert("Identifier" == referencePath.node.type)
423
+ referencePath.replaceWith(
424
+ t.memberExpression(
425
+ t.identifier(`$${uniqueID}$GLOBAL$`),
426
+ t.identifier(referencePath.node.name)
427
+ )
428
+ )
429
+ }
430
+ globalBlockPath.remove()
431
+ globalBlockStatementPath.remove()
432
+ globalBlock.body.splice(
433
+ globalBlockIndex,
434
+ 0,
435
+ t.expressionStatement(
436
+ t.assignmentExpression(
437
+ "=",
438
+ t.memberExpression(
439
+ t.identifier(`$${uniqueID}$GLOBAL$`),
440
+ t.identifier(globalBlockStatement.id.name)
441
+ ),
442
+ t.classExpression(
443
+ void 0,
444
+ globalBlockStatement.superClass,
445
+ globalBlockStatement.body,
446
+ globalBlockStatement.decorators
447
+ )
448
+ )
449
+ )
450
+ )
451
+ }
452
+ }
453
+ if (program.scope.hasGlobal("_EXPORTS"))
454
+ for (const referencePath of getReferencePathsToGlobal("_EXPORTS", program))
455
+ referencePath.replaceWith(
456
+ t.arrayExpression([...exports.keys(), ...liveExports.keys()].map(name => t.stringLiteral(name)))
457
+ )
458
+ globalBlock.body.length &&
459
+ mainFunction.body.body.splice(
460
+ hoistedGlobalBlockFunctions,
461
+ 0,
462
+ t.ifStatement(t.unaryExpression("!", t.identifier(`$${uniqueID}$FMCL$`)), globalBlock)
463
+ )
464
+ }
465
+ functionDotPrototypeIsReferencedMultipleTimes &&
466
+ mainFunction.body.body.unshift(
467
+ t.variableDeclaration("let", [
468
+ t.variableDeclarator(
469
+ t.identifier(`_${uniqueID}_FUNCTION_DOT_PROTOTYPE_`),
470
+ createGetFunctionPrototypeNode()
471
+ )
472
+ ])
473
+ )
474
+ needSetPrototypeOf &&
475
+ mainFunction.body.body.unshift(
476
+ t.variableDeclaration("let", [
477
+ t.variableDeclarator(
478
+ t.identifier(`_${uniqueID}_SET_PROTOTYPE_OF_`),
479
+ t.callExpression(
480
+ t.memberExpression(
481
+ t.memberExpression(t.identifier("Object"), t.identifier("call")),
482
+ t.identifier("bind")
483
+ ),
484
+ [t.identifier(`_${uniqueID}_DUNDER_PROTO_SETTER_`)]
485
+ )
486
+ )
487
+ ])
488
+ )
489
+ needGetPrototypeOf &&
490
+ mainFunction.body.body.unshift(
491
+ t.variableDeclaration("let", [
492
+ t.variableDeclarator(
493
+ t.identifier(`_${uniqueID}_GET_PROTOTYPE_OF_`),
494
+ t.callExpression(
495
+ t.memberExpression(
496
+ t.memberExpression(t.identifier("Object"), t.identifier("call")),
497
+ t.identifier("bind")
498
+ ),
499
+ [t.identifier(`_${uniqueID}_DUNDER_PROTO_GETTER_`)]
500
+ )
501
+ )
502
+ ])
503
+ )
504
+ ;(needGetPrototypeOf || needSetPrototypeOf) &&
505
+ mainFunction.body.body.unshift(
506
+ t.variableDeclaration("let", [
507
+ t.variableDeclarator(
508
+ t.objectPattern(
509
+ needGetPrototypeOf ?
510
+ needSetPrototypeOf ?
511
+ [
512
+ t.objectProperty(
513
+ t.identifier("get"),
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
+ ]
535
+ )
536
+ )
537
+ ])
538
+ )
539
+ traverse(file, {
540
+ BlockStatement({ node: blockStatement }) {
541
+ for (const [index, functionDeclaration] of blockStatement.body.entries())
542
+ if ("FunctionDeclaration" == functionDeclaration.type && !functionDeclaration.generator) {
543
+ blockStatement.body.splice(index, 1)
544
+ blockStatement.body.unshift(
545
+ t.variableDeclaration("let", [
546
+ t.variableDeclarator(
547
+ functionDeclaration.id,
548
+ t.arrowFunctionExpression(
549
+ functionDeclaration.params,
550
+ functionDeclaration.body,
551
+ functionDeclaration.async
552
+ )
553
+ )
554
+ ])
555
+ )
556
+ }
557
+ },
558
+ ClassBody({ node: classBody, scope, parent }) {
559
+ assert(t.isClass(parent))
560
+ let thisIsReferenced = !1
561
+ for (const classMethod of classBody.body) {
562
+ if ("ClassMethod" != classMethod.type) continue
563
+ let methodReferencesThis = !1
564
+ traverse(
565
+ classMethod.body,
566
+ {
567
+ ThisExpression(path) {
568
+ methodReferencesThis = !0
569
+ thisIsReferenced = !0
570
+ path.replaceWith(t.identifier(`_${uniqueID}_THIS_`))
571
+ },
572
+ Function(path) {
573
+ path.skip()
574
+ }
575
+ },
576
+ scope
577
+ )
578
+ if (methodReferencesThis)
579
+ if ("constructor" != classMethod.kind)
580
+ classMethod.body.body.unshift(
581
+ t.variableDeclaration("let", [
582
+ t.variableDeclarator(
583
+ t.identifier(`_${uniqueID}_THIS_`),
584
+ t.callExpression(t.memberExpression(t.super(), t.identifier("valueOf")), [])
585
+ )
586
+ ])
587
+ )
588
+ else {
589
+ const superCalls = []
590
+ traverse(
591
+ classMethod.body,
592
+ {
593
+ CallExpression(path) {
594
+ "Super" == path.node.callee.type && superCalls.push(path)
595
+ }
596
+ },
597
+ scope
598
+ )
599
+ if (superCalls.length)
600
+ if (
601
+ 1 == superCalls.length &&
602
+ "ExpressionStatement" == superCalls[0].parent.type &&
603
+ superCalls[0].parentPath.parentPath.parent == classMethod
604
+ )
605
+ superCalls[0].parentPath.replaceWith(
606
+ t.variableDeclaration("let", [
607
+ t.variableDeclarator(t.identifier(`_${uniqueID}_THIS_`), superCalls[0].node)
608
+ ])
609
+ )
610
+ else {
611
+ for (const path of superCalls)
612
+ path.replaceWith(
613
+ t.assignmentExpression("=", t.identifier(`_${uniqueID}_THIS_`), path.node)
614
+ )
615
+ classMethod.body.body.unshift(
616
+ t.variableDeclaration("let", [
617
+ t.variableDeclarator(t.identifier(`_${uniqueID}_THIS_`))
618
+ ])
619
+ )
620
+ }
621
+ else
622
+ classMethod.body.body.unshift(
623
+ t.variableDeclaration("let", [
624
+ t.variableDeclarator(
625
+ t.identifier(`_${uniqueID}_THIS_`),
626
+ t.callExpression(t.super(), [])
627
+ )
628
+ ])
629
+ )
630
+ }
631
+ }
632
+ !parent.superClass && thisIsReferenced && (parent.superClass = t.identifier("Object"))
633
+ },
634
+ VariableDeclaration({ node: variableDeclaration }) {
635
+ "const" == variableDeclaration.kind && (variableDeclaration.kind = "let")
636
+ },
637
+ ThisExpression(path) {
638
+ path.replaceWith(t.identifier("undefined"))
639
+ },
640
+ BigIntLiteral(path) {
641
+ const bigIntAsNumber = Number(path.node.value)
642
+ BigInt(bigIntAsNumber) == BigInt(path.node.value) ?
643
+ path.replaceWith(t.callExpression(t.identifier("BigInt"), [t.numericLiteral(bigIntAsNumber)]))
644
+ : path.replaceWith(t.callExpression(t.identifier("BigInt"), [t.stringLiteral(path.node.value)]))
645
+ }
646
+ })
647
+ return { file, seclevel }
648
+ }
649
+ export { transform as default, transform }
package/pull.js CHANGED
@@ -1 +1,11 @@
1
- import{copyFilePersistent as t}from"@samual/lib/copyFilePersistent";import{resolve as r}from"path";const pull=async(s,a,i)=>{const[o,e]=i.split(".");if(!o||!e)throw new Error('`script` argument must be in "user.name" format');await t(r(a,o,"scripts",`${e}.js`),r(s,o,`${e}.js`))};export{pull as default,pull};
1
+ import { copyFilePersistent } from "@samual/lib/copyFilePersistent"
2
+ import { resolve } from "path"
3
+ const pull = async (sourceFolderPath, hackmudPath, script) => {
4
+ const [user, name] = script.split(".")
5
+ if (!user || !name) throw Error('`script` argument must be in "user.name" format')
6
+ await copyFilePersistent(
7
+ resolve(hackmudPath, user, "scripts", name + ".js"),
8
+ resolve(sourceFolderPath, user, name + ".js")
9
+ )
10
+ }
11
+ export { pull as default, pull }