luau-obfuscator 1.0.5 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/release.yml +56 -56
- package/dist/index.cjs +55 -0
- package/dist/index.js +55 -0
- package/generated/grammers.luau +3277 -3420
- package/generated/tests.luau +1953 -1816
- package/package.json +1 -1
- package/scripts/test.js +21 -21
- package/smoketest/grammers.luau +1174 -1174
- package/smoketest/tests.luau +114 -114
- package/src/passes/ConstantArray.ts +90 -90
- package/src/passes/EncryptNumbers.ts +122 -122
- package/src/passes/EncryptStrings.ts +162 -162
- package/src/passes/GlobalMapping.ts +193 -193
- package/src/passes/InsertJunk.ts +184 -184
- package/src/passes/Minify.ts +5 -5
- package/src/passes/StripTypes.ts +191 -191
- package/src/passes/WrapInFunction.ts +31 -31
- package/src/passes/vmify/compiler.ts +6 -0
- package/src/passes/vmify/names.ts +4 -0
- package/src/passes/vmify/opcodes.ts +4 -0
- package/src/passes/vmify/runtime.ts +52 -0
- package/tsconfig.json +20 -20
- package/tsup.config.ts +9 -9
package/src/passes/StripTypes.ts
CHANGED
|
@@ -1,192 +1,192 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
Program, Block, Statement, Expression, FunctionBody,
|
|
3
|
-
} from "luau-parser"
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 파이프라인 맨 앞에서 실행되어야 하는 패스.
|
|
7
|
-
*
|
|
8
|
-
* 이후 패스들(GlobalMapping, RenameVariables 등)은 값 트리만 순회하고
|
|
9
|
-
* `typeof(x)`, `x :: T` 같은 타입 노드 내부의 expression은 건드리지 않는다.
|
|
10
|
-
* 타입을 나중까지 들고 있으면 값 쪽 식별자는 바뀌는데 타입 쪽 참조는 원래
|
|
11
|
-
* 이름 그대로 남아 출력물이 타입체크 불가능한 상태가 된다
|
|
12
|
-
* (`typeof(runtimeValue)`가 obfuscate 이후에도 원본 이름을 참조하는 것이 그 예).
|
|
13
|
-
*
|
|
14
|
-
* 그래서 타입 정보 자체를 파이프라인 시작 시점에 전부 지워서 이 클래스의
|
|
15
|
-
* 버그를 원천 차단한다:
|
|
16
|
-
* - TypeAliasStatement / ExportTypeAliasStatement 문 자체를 블록에서 제거
|
|
17
|
-
* - TypedIdentifier / FunctionParameter 의 typeAnnotation 제거
|
|
18
|
-
* - FunctionBody 의 generics / varargTypeAnnotation / returnType 제거
|
|
19
|
-
* - `expr :: T` (TypeAssertionExpression) 은 T를 버리고 expr로 치환
|
|
20
|
-
*/
|
|
21
|
-
export function runStripTypes(program: Program): void {
|
|
22
|
-
stripBlock(program.body)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function stripBlock(block: Block): void {
|
|
26
|
-
const kept: Statement[] = []
|
|
27
|
-
for (const stmt of block.statements) {
|
|
28
|
-
if (stmt.type === "TypeAliasStatement" || stmt.type === "ExportTypeAliasStatement") {
|
|
29
|
-
continue
|
|
30
|
-
}
|
|
31
|
-
stripStatement(stmt)
|
|
32
|
-
kept.push(stmt)
|
|
33
|
-
}
|
|
34
|
-
block.statements = kept
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function stripFunctionBody(func: FunctionBody): void {
|
|
38
|
-
func.generics = []
|
|
39
|
-
for (const param of func.params) delete param.typeAnnotation
|
|
40
|
-
delete func.varargTypeAnnotation
|
|
41
|
-
delete func.returnType
|
|
42
|
-
stripBlock(func.body)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function stripStatement(stmt: Statement): void {
|
|
46
|
-
switch (stmt.type) {
|
|
47
|
-
case "LocalStatement":
|
|
48
|
-
for (const name of stmt.names) delete name.typeAnnotation
|
|
49
|
-
for (let i = 0; i < stmt.init.length; i++) stmt.init[i] = stripExpr(stmt.init[i])
|
|
50
|
-
return
|
|
51
|
-
|
|
52
|
-
case "LocalFunctionStatement":
|
|
53
|
-
stripFunctionBody(stmt.func)
|
|
54
|
-
return
|
|
55
|
-
|
|
56
|
-
case "FunctionDeclarationStatement":
|
|
57
|
-
stripFunctionBody(stmt.func)
|
|
58
|
-
return
|
|
59
|
-
|
|
60
|
-
case "AssignmentStatement":
|
|
61
|
-
for (let i = 0; i < stmt.targets.length; i++) stmt.targets[i] = stripExpr(stmt.targets[i])
|
|
62
|
-
for (let i = 0; i < stmt.values.length; i++) stmt.values[i] = stripExpr(stmt.values[i])
|
|
63
|
-
return
|
|
64
|
-
|
|
65
|
-
case "CompoundAssignmentStatement":
|
|
66
|
-
stmt.target = stripExpr(stmt.target)
|
|
67
|
-
stmt.value = stripExpr(stmt.value)
|
|
68
|
-
return
|
|
69
|
-
|
|
70
|
-
case "CallStatement":
|
|
71
|
-
stmt.expression = stripExpr(stmt.expression) as typeof stmt.expression
|
|
72
|
-
return
|
|
73
|
-
|
|
74
|
-
case "DoStatement":
|
|
75
|
-
stripBlock(stmt.body)
|
|
76
|
-
return
|
|
77
|
-
|
|
78
|
-
case "WhileStatement":
|
|
79
|
-
stmt.condition = stripExpr(stmt.condition)
|
|
80
|
-
stripBlock(stmt.body)
|
|
81
|
-
return
|
|
82
|
-
|
|
83
|
-
case "RepeatStatement":
|
|
84
|
-
stripBlock(stmt.body)
|
|
85
|
-
stmt.condition = stripExpr(stmt.condition)
|
|
86
|
-
return
|
|
87
|
-
|
|
88
|
-
case "IfStatement":
|
|
89
|
-
for (const clause of stmt.clauses) {
|
|
90
|
-
clause.condition = stripExpr(clause.condition)
|
|
91
|
-
stripBlock(clause.body)
|
|
92
|
-
}
|
|
93
|
-
if (stmt.alternate) stripBlock(stmt.alternate)
|
|
94
|
-
return
|
|
95
|
-
|
|
96
|
-
case "NumericForStatement":
|
|
97
|
-
delete stmt.variable.typeAnnotation
|
|
98
|
-
stmt.start = stripExpr(stmt.start)
|
|
99
|
-
stmt.end = stripExpr(stmt.end)
|
|
100
|
-
if (stmt.step) stmt.step = stripExpr(stmt.step)
|
|
101
|
-
stripBlock(stmt.body)
|
|
102
|
-
return
|
|
103
|
-
|
|
104
|
-
case "GenericForStatement":
|
|
105
|
-
for (const v of stmt.variables) delete v.typeAnnotation
|
|
106
|
-
for (let i = 0; i < stmt.iterators.length; i++) stmt.iterators[i] = stripExpr(stmt.iterators[i])
|
|
107
|
-
stripBlock(stmt.body)
|
|
108
|
-
return
|
|
109
|
-
|
|
110
|
-
case "ReturnStatement":
|
|
111
|
-
for (let i = 0; i < stmt.arguments.length; i++) stmt.arguments[i] = stripExpr(stmt.arguments[i])
|
|
112
|
-
return
|
|
113
|
-
|
|
114
|
-
case "BreakStatement":
|
|
115
|
-
case "ContinueStatement":
|
|
116
|
-
return
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function stripExpr(expr: Expression): Expression {
|
|
121
|
-
switch (expr.type) {
|
|
122
|
-
case "InterpolatedStringExpression":
|
|
123
|
-
for (const part of expr.parts) {
|
|
124
|
-
if (part.kind === "expression") part.expression = stripExpr(part.expression)
|
|
125
|
-
}
|
|
126
|
-
return expr
|
|
127
|
-
|
|
128
|
-
case "FunctionExpression":
|
|
129
|
-
stripFunctionBody(expr.func)
|
|
130
|
-
return expr
|
|
131
|
-
|
|
132
|
-
case "TableExpression":
|
|
133
|
-
for (const field of expr.fields) {
|
|
134
|
-
if (field.type === "TableFieldPositional") {
|
|
135
|
-
field.value = stripExpr(field.value)
|
|
136
|
-
} else if (field.type === "TableFieldNamed") {
|
|
137
|
-
field.value = stripExpr(field.value)
|
|
138
|
-
} else if (field.type === "TableFieldComputed") {
|
|
139
|
-
field.key = stripExpr(field.key)
|
|
140
|
-
field.value = stripExpr(field.value)
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
return expr
|
|
144
|
-
|
|
145
|
-
case "BinaryExpression":
|
|
146
|
-
expr.left = stripExpr(expr.left)
|
|
147
|
-
expr.right = stripExpr(expr.right)
|
|
148
|
-
return expr
|
|
149
|
-
|
|
150
|
-
case "UnaryExpression":
|
|
151
|
-
expr.argument = stripExpr(expr.argument)
|
|
152
|
-
return expr
|
|
153
|
-
|
|
154
|
-
case "MemberExpression":
|
|
155
|
-
expr.object = stripExpr(expr.object)
|
|
156
|
-
return expr
|
|
157
|
-
|
|
158
|
-
case "IndexExpression":
|
|
159
|
-
expr.object = stripExpr(expr.object)
|
|
160
|
-
expr.index = stripExpr(expr.index)
|
|
161
|
-
return expr
|
|
162
|
-
|
|
163
|
-
case "CallExpression":
|
|
164
|
-
expr.callee = stripExpr(expr.callee)
|
|
165
|
-
for (let i = 0; i < expr.arguments.length; i++) expr.arguments[i] = stripExpr(expr.arguments[i])
|
|
166
|
-
return expr
|
|
167
|
-
|
|
168
|
-
case "MethodCallExpression":
|
|
169
|
-
expr.object = stripExpr(expr.object)
|
|
170
|
-
for (let i = 0; i < expr.arguments.length; i++) expr.arguments[i] = stripExpr(expr.arguments[i])
|
|
171
|
-
return expr
|
|
172
|
-
|
|
173
|
-
case "ParenthesizedExpression":
|
|
174
|
-
expr.expression = stripExpr(expr.expression)
|
|
175
|
-
return expr
|
|
176
|
-
|
|
177
|
-
case "TypeAssertionExpression":
|
|
178
|
-
// `expr :: T` -> T를 버리고 expr만 남김 (괄호로 감싸서 우선순위 보존)
|
|
179
|
-
return stripExpr(expr.expression)
|
|
180
|
-
|
|
181
|
-
case "IfElseExpression":
|
|
182
|
-
for (const clause of expr.clauses) {
|
|
183
|
-
clause.condition = stripExpr(clause.condition)
|
|
184
|
-
clause.body = stripExpr(clause.body)
|
|
185
|
-
}
|
|
186
|
-
expr.alternate = stripExpr(expr.alternate)
|
|
187
|
-
return expr
|
|
188
|
-
|
|
189
|
-
default:
|
|
190
|
-
return expr
|
|
191
|
-
}
|
|
1
|
+
import type {
|
|
2
|
+
Program, Block, Statement, Expression, FunctionBody,
|
|
3
|
+
} from "luau-parser"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 파이프라인 맨 앞에서 실행되어야 하는 패스.
|
|
7
|
+
*
|
|
8
|
+
* 이후 패스들(GlobalMapping, RenameVariables 등)은 값 트리만 순회하고
|
|
9
|
+
* `typeof(x)`, `x :: T` 같은 타입 노드 내부의 expression은 건드리지 않는다.
|
|
10
|
+
* 타입을 나중까지 들고 있으면 값 쪽 식별자는 바뀌는데 타입 쪽 참조는 원래
|
|
11
|
+
* 이름 그대로 남아 출력물이 타입체크 불가능한 상태가 된다
|
|
12
|
+
* (`typeof(runtimeValue)`가 obfuscate 이후에도 원본 이름을 참조하는 것이 그 예).
|
|
13
|
+
*
|
|
14
|
+
* 그래서 타입 정보 자체를 파이프라인 시작 시점에 전부 지워서 이 클래스의
|
|
15
|
+
* 버그를 원천 차단한다:
|
|
16
|
+
* - TypeAliasStatement / ExportTypeAliasStatement 문 자체를 블록에서 제거
|
|
17
|
+
* - TypedIdentifier / FunctionParameter 의 typeAnnotation 제거
|
|
18
|
+
* - FunctionBody 의 generics / varargTypeAnnotation / returnType 제거
|
|
19
|
+
* - `expr :: T` (TypeAssertionExpression) 은 T를 버리고 expr로 치환
|
|
20
|
+
*/
|
|
21
|
+
export function runStripTypes(program: Program): void {
|
|
22
|
+
stripBlock(program.body)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function stripBlock(block: Block): void {
|
|
26
|
+
const kept: Statement[] = []
|
|
27
|
+
for (const stmt of block.statements) {
|
|
28
|
+
if (stmt.type === "TypeAliasStatement" || stmt.type === "ExportTypeAliasStatement") {
|
|
29
|
+
continue
|
|
30
|
+
}
|
|
31
|
+
stripStatement(stmt)
|
|
32
|
+
kept.push(stmt)
|
|
33
|
+
}
|
|
34
|
+
block.statements = kept
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function stripFunctionBody(func: FunctionBody): void {
|
|
38
|
+
func.generics = []
|
|
39
|
+
for (const param of func.params) delete param.typeAnnotation
|
|
40
|
+
delete func.varargTypeAnnotation
|
|
41
|
+
delete func.returnType
|
|
42
|
+
stripBlock(func.body)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function stripStatement(stmt: Statement): void {
|
|
46
|
+
switch (stmt.type) {
|
|
47
|
+
case "LocalStatement":
|
|
48
|
+
for (const name of stmt.names) delete name.typeAnnotation
|
|
49
|
+
for (let i = 0; i < stmt.init.length; i++) stmt.init[i] = stripExpr(stmt.init[i])
|
|
50
|
+
return
|
|
51
|
+
|
|
52
|
+
case "LocalFunctionStatement":
|
|
53
|
+
stripFunctionBody(stmt.func)
|
|
54
|
+
return
|
|
55
|
+
|
|
56
|
+
case "FunctionDeclarationStatement":
|
|
57
|
+
stripFunctionBody(stmt.func)
|
|
58
|
+
return
|
|
59
|
+
|
|
60
|
+
case "AssignmentStatement":
|
|
61
|
+
for (let i = 0; i < stmt.targets.length; i++) stmt.targets[i] = stripExpr(stmt.targets[i])
|
|
62
|
+
for (let i = 0; i < stmt.values.length; i++) stmt.values[i] = stripExpr(stmt.values[i])
|
|
63
|
+
return
|
|
64
|
+
|
|
65
|
+
case "CompoundAssignmentStatement":
|
|
66
|
+
stmt.target = stripExpr(stmt.target)
|
|
67
|
+
stmt.value = stripExpr(stmt.value)
|
|
68
|
+
return
|
|
69
|
+
|
|
70
|
+
case "CallStatement":
|
|
71
|
+
stmt.expression = stripExpr(stmt.expression) as typeof stmt.expression
|
|
72
|
+
return
|
|
73
|
+
|
|
74
|
+
case "DoStatement":
|
|
75
|
+
stripBlock(stmt.body)
|
|
76
|
+
return
|
|
77
|
+
|
|
78
|
+
case "WhileStatement":
|
|
79
|
+
stmt.condition = stripExpr(stmt.condition)
|
|
80
|
+
stripBlock(stmt.body)
|
|
81
|
+
return
|
|
82
|
+
|
|
83
|
+
case "RepeatStatement":
|
|
84
|
+
stripBlock(stmt.body)
|
|
85
|
+
stmt.condition = stripExpr(stmt.condition)
|
|
86
|
+
return
|
|
87
|
+
|
|
88
|
+
case "IfStatement":
|
|
89
|
+
for (const clause of stmt.clauses) {
|
|
90
|
+
clause.condition = stripExpr(clause.condition)
|
|
91
|
+
stripBlock(clause.body)
|
|
92
|
+
}
|
|
93
|
+
if (stmt.alternate) stripBlock(stmt.alternate)
|
|
94
|
+
return
|
|
95
|
+
|
|
96
|
+
case "NumericForStatement":
|
|
97
|
+
delete stmt.variable.typeAnnotation
|
|
98
|
+
stmt.start = stripExpr(stmt.start)
|
|
99
|
+
stmt.end = stripExpr(stmt.end)
|
|
100
|
+
if (stmt.step) stmt.step = stripExpr(stmt.step)
|
|
101
|
+
stripBlock(stmt.body)
|
|
102
|
+
return
|
|
103
|
+
|
|
104
|
+
case "GenericForStatement":
|
|
105
|
+
for (const v of stmt.variables) delete v.typeAnnotation
|
|
106
|
+
for (let i = 0; i < stmt.iterators.length; i++) stmt.iterators[i] = stripExpr(stmt.iterators[i])
|
|
107
|
+
stripBlock(stmt.body)
|
|
108
|
+
return
|
|
109
|
+
|
|
110
|
+
case "ReturnStatement":
|
|
111
|
+
for (let i = 0; i < stmt.arguments.length; i++) stmt.arguments[i] = stripExpr(stmt.arguments[i])
|
|
112
|
+
return
|
|
113
|
+
|
|
114
|
+
case "BreakStatement":
|
|
115
|
+
case "ContinueStatement":
|
|
116
|
+
return
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function stripExpr(expr: Expression): Expression {
|
|
121
|
+
switch (expr.type) {
|
|
122
|
+
case "InterpolatedStringExpression":
|
|
123
|
+
for (const part of expr.parts) {
|
|
124
|
+
if (part.kind === "expression") part.expression = stripExpr(part.expression)
|
|
125
|
+
}
|
|
126
|
+
return expr
|
|
127
|
+
|
|
128
|
+
case "FunctionExpression":
|
|
129
|
+
stripFunctionBody(expr.func)
|
|
130
|
+
return expr
|
|
131
|
+
|
|
132
|
+
case "TableExpression":
|
|
133
|
+
for (const field of expr.fields) {
|
|
134
|
+
if (field.type === "TableFieldPositional") {
|
|
135
|
+
field.value = stripExpr(field.value)
|
|
136
|
+
} else if (field.type === "TableFieldNamed") {
|
|
137
|
+
field.value = stripExpr(field.value)
|
|
138
|
+
} else if (field.type === "TableFieldComputed") {
|
|
139
|
+
field.key = stripExpr(field.key)
|
|
140
|
+
field.value = stripExpr(field.value)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return expr
|
|
144
|
+
|
|
145
|
+
case "BinaryExpression":
|
|
146
|
+
expr.left = stripExpr(expr.left)
|
|
147
|
+
expr.right = stripExpr(expr.right)
|
|
148
|
+
return expr
|
|
149
|
+
|
|
150
|
+
case "UnaryExpression":
|
|
151
|
+
expr.argument = stripExpr(expr.argument)
|
|
152
|
+
return expr
|
|
153
|
+
|
|
154
|
+
case "MemberExpression":
|
|
155
|
+
expr.object = stripExpr(expr.object)
|
|
156
|
+
return expr
|
|
157
|
+
|
|
158
|
+
case "IndexExpression":
|
|
159
|
+
expr.object = stripExpr(expr.object)
|
|
160
|
+
expr.index = stripExpr(expr.index)
|
|
161
|
+
return expr
|
|
162
|
+
|
|
163
|
+
case "CallExpression":
|
|
164
|
+
expr.callee = stripExpr(expr.callee)
|
|
165
|
+
for (let i = 0; i < expr.arguments.length; i++) expr.arguments[i] = stripExpr(expr.arguments[i])
|
|
166
|
+
return expr
|
|
167
|
+
|
|
168
|
+
case "MethodCallExpression":
|
|
169
|
+
expr.object = stripExpr(expr.object)
|
|
170
|
+
for (let i = 0; i < expr.arguments.length; i++) expr.arguments[i] = stripExpr(expr.arguments[i])
|
|
171
|
+
return expr
|
|
172
|
+
|
|
173
|
+
case "ParenthesizedExpression":
|
|
174
|
+
expr.expression = stripExpr(expr.expression)
|
|
175
|
+
return expr
|
|
176
|
+
|
|
177
|
+
case "TypeAssertionExpression":
|
|
178
|
+
// `expr :: T` -> T를 버리고 expr만 남김 (괄호로 감싸서 우선순위 보존)
|
|
179
|
+
return stripExpr(expr.expression)
|
|
180
|
+
|
|
181
|
+
case "IfElseExpression":
|
|
182
|
+
for (const clause of expr.clauses) {
|
|
183
|
+
clause.condition = stripExpr(clause.condition)
|
|
184
|
+
clause.body = stripExpr(clause.body)
|
|
185
|
+
}
|
|
186
|
+
expr.alternate = stripExpr(expr.alternate)
|
|
187
|
+
return expr
|
|
188
|
+
|
|
189
|
+
default:
|
|
190
|
+
return expr
|
|
191
|
+
}
|
|
192
192
|
}
|
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
import type { Program } from "luau-parser"
|
|
2
|
-
import {
|
|
3
|
-
block, functionBody, functionExpression, paren, call, vararg, returnStatement,
|
|
4
|
-
} from "./nodeFactory"
|
|
5
|
-
|
|
6
|
-
export interface WrapInFunctionOptions {}
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 전체 프로그램을:
|
|
10
|
-
* return (function(...)
|
|
11
|
-
* <원래 코드>
|
|
12
|
-
* end)(...)
|
|
13
|
-
* 로 감쌈.
|
|
14
|
-
*
|
|
15
|
-
* - `return`을 쓰는 이유: Script/LocalScript에서는 top-level return이 그냥 청크를
|
|
16
|
-
* 조기 종료시킬 뿐 무해하고, ModuleScript라면 IIFE의 결과값이 그대로 require()
|
|
17
|
-
* 호출자에게 전달돼야 하므로 필요함. 즉 스크립트 종류를 가리지 않고 안전.
|
|
18
|
-
* - `...`을 파라미터로 받아서 다시 그대로 넘겨주는 이유: 원본 청크가 최상위에서
|
|
19
|
-
* `...`(스크립트 인자)을 참조하는 경우를 대비. 그냥 지워버리면 그런 코드가
|
|
20
|
-
* 깨짐.
|
|
21
|
-
*
|
|
22
|
-
* 반드시 파이프라인 맨 마지막 근처에서 실행돼야 함 — 이 패스 이후에 실행되는
|
|
23
|
-
* 다른 패스가 top-level 스코프를 순회/변형한다면 이미 감싸인 함수 내부까지
|
|
24
|
-
* 안 보고 지나칠 수 있음.
|
|
25
|
-
*/
|
|
26
|
-
export function runWrapInFunction(program: Program, _options: WrapInFunctionOptions): void {
|
|
27
|
-
const innerBody = program.body
|
|
28
|
-
const wrapper = functionExpression(functionBody([], innerBody, true))
|
|
29
|
-
const iife = call(paren(wrapper), [vararg()])
|
|
30
|
-
|
|
31
|
-
program.body = block([returnStatement([iife])])
|
|
1
|
+
import type { Program } from "luau-parser"
|
|
2
|
+
import {
|
|
3
|
+
block, functionBody, functionExpression, paren, call, vararg, returnStatement,
|
|
4
|
+
} from "./nodeFactory"
|
|
5
|
+
|
|
6
|
+
export interface WrapInFunctionOptions {}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 전체 프로그램을:
|
|
10
|
+
* return (function(...)
|
|
11
|
+
* <원래 코드>
|
|
12
|
+
* end)(...)
|
|
13
|
+
* 로 감쌈.
|
|
14
|
+
*
|
|
15
|
+
* - `return`을 쓰는 이유: Script/LocalScript에서는 top-level return이 그냥 청크를
|
|
16
|
+
* 조기 종료시킬 뿐 무해하고, ModuleScript라면 IIFE의 결과값이 그대로 require()
|
|
17
|
+
* 호출자에게 전달돼야 하므로 필요함. 즉 스크립트 종류를 가리지 않고 안전.
|
|
18
|
+
* - `...`을 파라미터로 받아서 다시 그대로 넘겨주는 이유: 원본 청크가 최상위에서
|
|
19
|
+
* `...`(스크립트 인자)을 참조하는 경우를 대비. 그냥 지워버리면 그런 코드가
|
|
20
|
+
* 깨짐.
|
|
21
|
+
*
|
|
22
|
+
* 반드시 파이프라인 맨 마지막 근처에서 실행돼야 함 — 이 패스 이후에 실행되는
|
|
23
|
+
* 다른 패스가 top-level 스코프를 순회/변형한다면 이미 감싸인 함수 내부까지
|
|
24
|
+
* 안 보고 지나칠 수 있음.
|
|
25
|
+
*/
|
|
26
|
+
export function runWrapInFunction(program: Program, _options: WrapInFunctionOptions): void {
|
|
27
|
+
const innerBody = program.body
|
|
28
|
+
const wrapper = functionExpression(functionBody([], innerBody, true))
|
|
29
|
+
const iife = call(paren(wrapper), [vararg()])
|
|
30
|
+
|
|
31
|
+
program.body = block([returnStatement([iife])])
|
|
32
32
|
}
|
|
@@ -642,6 +642,12 @@ export class VmCompiler {
|
|
|
642
642
|
}
|
|
643
643
|
}
|
|
644
644
|
|
|
645
|
+
// Luau 제네릭 반복은 iterator 표현식이 함수가 아니라 테이블일 수 있다
|
|
646
|
+
// (플레인 테이블 → array-then-hash 순회, __iter 메타메서드 → 그 결과). base가
|
|
647
|
+
// 함수가 아니면 루프 진입 전에 한 번 정규화한다 — 안 하면 아래 CALL이 테이블을
|
|
648
|
+
// 호출하려다 "attempt to call a table value"로 터진다.
|
|
649
|
+
this.emit(state, Opcode.NORMITER, base, 0, 0, "normalize-generic-iter")
|
|
650
|
+
|
|
645
651
|
const loopStart = state.proto.code.length
|
|
646
652
|
const nVars = stmt.variables.length
|
|
647
653
|
const callBase = state.regs.top()
|
|
@@ -20,6 +20,9 @@ export interface VmNames {
|
|
|
20
20
|
dispatch: string
|
|
21
21
|
/** local function __vm_rk(frame, x) */
|
|
22
22
|
rk: string
|
|
23
|
+
/** local function __vm_tableIter(t) — 제네릭 for가 플레인 테이블을 순회할 때 쓰는
|
|
24
|
+
* array-then-hash 이터레이터 클로저 팩토리 (NORMITER opcode에서 사용) */
|
|
25
|
+
tableIter: string
|
|
23
26
|
/** __vm_execute/핸들러의 frame 파라미터 이름 */
|
|
24
27
|
frame: string
|
|
25
28
|
// frame 테이블 필드
|
|
@@ -63,6 +66,7 @@ export function generateVmNames(random: () => number = Math.random): VmNames {
|
|
|
63
66
|
const id = makeIdGenerator(random)
|
|
64
67
|
return {
|
|
65
68
|
globals: id(), protoRoot: id(), execute: id(), handlers: id(), dispatch: id(), rk: id(),
|
|
69
|
+
tableIter: id(),
|
|
66
70
|
frame: id(),
|
|
67
71
|
R: id(), K: id(), code: id(), protos: id(), upvals: id(), varargs: id(),
|
|
68
72
|
nVarargs: id(), multiTop: id(),
|
|
@@ -41,6 +41,10 @@ export enum Opcode {
|
|
|
41
41
|
// 생성자 마지막 필드가 Call/MethodCall/Vararg로 다중값을 낼 때 씀 (`{f()}`, `{...}`)
|
|
42
42
|
IDIV, // R[A] = RK(B) // RK(C) (Luau 바닥 나눗셈 연산자, 뒤에 추가되어 기존 opcode
|
|
43
43
|
// 번호를 안 건드림 — enum 중간에 끼워넣으면 runtime.ts의 숫자 인덱스가 전부 밀림)
|
|
44
|
+
NORMITER, // 제네릭 for 진입 시 R[A..A+2] 이터레이터 3-튜플을 정규화. R[A]가 함수가
|
|
45
|
+
// 아니라 테이블이면(플레인 테이블 or __iter 메타메서드) Luau 제네릭 반복
|
|
46
|
+
// 의미대로 (fn, state, ctrl)로 바꿔준다. 안 하면 뒤의 CALL이 테이블을
|
|
47
|
+
// 호출하려다 "attempt to call a table value"로 터짐. (enum 끝에 추가)
|
|
44
48
|
}
|
|
45
49
|
|
|
46
50
|
export interface Instr {
|
|
@@ -60,6 +60,34 @@ local function ${N.rk}(${N.frame}, x)
|
|
|
60
60
|
end
|
|
61
61
|
end
|
|
62
62
|
|
|
63
|
+
-- 플레인 테이블에 대한 Luau 제네릭 반복(for k, v in t) 이터레이터.
|
|
64
|
+
-- 배열부(1..경계)를 순서대로 먼저, 그다음 해시부(이미 낸 정수 키는 건너뜀)를 낸다.
|
|
65
|
+
-- 상태를 클로저에 담으므로 (fn, state, ctrl) 중 state/ctrl은 안 쓴다.
|
|
66
|
+
local function ${N.tableIter}(t)
|
|
67
|
+
local i = 0
|
|
68
|
+
local arrayDone = false
|
|
69
|
+
local hk = nil
|
|
70
|
+
return function()
|
|
71
|
+
if not arrayDone then
|
|
72
|
+
i = i + 1
|
|
73
|
+
local v = t[i]
|
|
74
|
+
if v ~= nil then
|
|
75
|
+
return i, v
|
|
76
|
+
end
|
|
77
|
+
arrayDone = true
|
|
78
|
+
end
|
|
79
|
+
local k, v = next(t, hk)
|
|
80
|
+
while k ~= nil do
|
|
81
|
+
if type(k) ~= "number" or k ~= math.floor(k) or k < 1 or k >= i then
|
|
82
|
+
hk = k
|
|
83
|
+
return k, v
|
|
84
|
+
end
|
|
85
|
+
k, v = next(t, k)
|
|
86
|
+
end
|
|
87
|
+
return nil
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
63
91
|
-- MOVE
|
|
64
92
|
${N.handlers}[${op(Opcode.MOVE)}] = function(${N.frame}, pc, instr)
|
|
65
93
|
${N.frame}.${N.R}[instr[2]] = ${N.frame}.${N.R}[instr[3]]
|
|
@@ -261,6 +289,30 @@ ${N.handlers}[${op(Opcode.RETURN)}] = function(${N.frame}, pc, instr)
|
|
|
261
289
|
return table.pack(table.unpack(${N.frame}.${N.R}, a, a + b - 2))
|
|
262
290
|
end
|
|
263
291
|
|
|
292
|
+
-- NORMITER: 제네릭 for 진입 시 이터레이터 3-튜플(R[A..A+2])을 정규화.
|
|
293
|
+
-- R[A]가 함수면 그대로 두고(pairs/ipairs/커스텀 이터레이터), 테이블이면 __iter
|
|
294
|
+
-- 메타메서드가 있으면 그걸 호출해 (fn,state,ctrl)로, 없으면 array-then-hash
|
|
295
|
+
-- 클로저 이터레이터로 바꾼다. 이후 CALL 핸들러가 함수를 정상 호출하게 된다.
|
|
296
|
+
${N.handlers}[${op(Opcode.NORMITER)}] = function(${N.frame}, pc, instr)
|
|
297
|
+
local a = instr[2]
|
|
298
|
+
local it = ${N.frame}.${N.R}[a]
|
|
299
|
+
if type(it) ~= "function" then
|
|
300
|
+
local mt = getmetatable(it)
|
|
301
|
+
local customIter = mt and rawget(mt, "__iter")
|
|
302
|
+
if customIter ~= nil then
|
|
303
|
+
local f, s, c = customIter(it)
|
|
304
|
+
${N.frame}.${N.R}[a] = f
|
|
305
|
+
${N.frame}.${N.R}[a + 1] = s
|
|
306
|
+
${N.frame}.${N.R}[a + 2] = c
|
|
307
|
+
elseif type(it) == "table" then
|
|
308
|
+
${N.frame}.${N.R}[a] = ${N.tableIter}(it)
|
|
309
|
+
${N.frame}.${N.R}[a + 1] = nil
|
|
310
|
+
${N.frame}.${N.R}[a + 2] = nil
|
|
311
|
+
end
|
|
312
|
+
end
|
|
313
|
+
return pc + 1
|
|
314
|
+
end
|
|
315
|
+
|
|
264
316
|
-- FORPREP
|
|
265
317
|
${N.handlers}[${op(Opcode.FORPREP)}] = function(${N.frame}, pc, instr)
|
|
266
318
|
local a = instr[2]
|
package/tsconfig.json
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ESNext",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"lib": ["ESNext", "DOM", "DOM.Iterable"],
|
|
6
|
-
"declaration": false,
|
|
7
|
-
"declarationMap": false,
|
|
8
|
-
"sourceMap": false,
|
|
9
|
-
"outDir": "./dist",
|
|
10
|
-
"rootDir": "./src",
|
|
11
|
-
"moduleResolution": "bundler",
|
|
12
|
-
"allowSyntheticDefaultImports": true,
|
|
13
|
-
"esModuleInterop": true,
|
|
14
|
-
"forceConsistentCasingInFileNames": true,
|
|
15
|
-
"strict": true,
|
|
16
|
-
"skipLibCheck": true,
|
|
17
|
-
"stripInternal": true,
|
|
18
|
-
},
|
|
19
|
-
"include": ["src/**/*"],
|
|
20
|
-
"exclude": ["node_modules", "dist"]
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ESNext",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"lib": ["ESNext", "DOM", "DOM.Iterable"],
|
|
6
|
+
"declaration": false,
|
|
7
|
+
"declarationMap": false,
|
|
8
|
+
"sourceMap": false,
|
|
9
|
+
"outDir": "./dist",
|
|
10
|
+
"rootDir": "./src",
|
|
11
|
+
"moduleResolution": "bundler",
|
|
12
|
+
"allowSyntheticDefaultImports": true,
|
|
13
|
+
"esModuleInterop": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
"strict": true,
|
|
16
|
+
"skipLibCheck": true,
|
|
17
|
+
"stripInternal": true,
|
|
18
|
+
},
|
|
19
|
+
"include": ["src/**/*"],
|
|
20
|
+
"exclude": ["node_modules", "dist"]
|
|
21
21
|
}
|
package/tsup.config.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { defineConfig } from "tsup";
|
|
2
|
-
|
|
3
|
-
export default defineConfig({
|
|
4
|
-
entry: ["src/index.ts"],
|
|
5
|
-
format: ["esm", "cjs"],
|
|
6
|
-
dts: true,
|
|
7
|
-
clean: true,
|
|
8
|
-
sourcemap: false,
|
|
9
|
-
target: "esnext",
|
|
1
|
+
import { defineConfig } from "tsup";
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
entry: ["src/index.ts"],
|
|
5
|
+
format: ["esm", "cjs"],
|
|
6
|
+
dts: true,
|
|
7
|
+
clean: true,
|
|
8
|
+
sourcemap: false,
|
|
9
|
+
target: "esnext",
|
|
10
10
|
});
|