unluac 1.1.1

Multi-dialect Lua decompiler written in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! 这个文件承载 `global_decl_pretty` 模块的局部不变量测试。

use crate::ast::{
    AstBindingRef, AstBlock, AstCallExpr, AstExpr, AstFunctionExpr, AstGlobalAttr,
    AstGlobalBindingTarget, AstGlobalDecl, AstLocalAttr, AstLocalBinding, AstLocalDecl, AstModule,
    AstNameRef, AstReturn, AstStmt, AstTargetDialect,
};
use crate::hir::{HirProtoRef, LocalId, ParamId};

use super::super::ReadabilityContext;
use super::apply;

#[test]
fn merges_seed_locals_into_single_multi_global_decl_in_seed_order() {
    let mut module = AstModule {
        entry_function: HirProtoRef(0),
        body: AstBlock {
            stmts: vec![
                AstStmt::LocalDecl(Box::new(AstLocalDecl {
                    bindings: vec![AstLocalBinding {
                        id: AstBindingRef::Local(LocalId(0)),
                        attr: AstLocalAttr::None,
                        origin: crate::ast::AstLocalOrigin::Recovered,
                    }],
                    values: vec![AstExpr::Integer(9)],
                })),
                AstStmt::LocalDecl(Box::new(AstLocalDecl {
                    bindings: vec![AstLocalBinding {
                        id: AstBindingRef::Local(LocalId(1)),
                        attr: AstLocalAttr::None,
                        origin: crate::ast::AstLocalOrigin::Recovered,
                    }],
                    values: vec![AstExpr::String("seed".to_owned())],
                })),
                AstStmt::GlobalDecl(Box::new(AstGlobalDecl {
                    bindings: vec![crate::ast::AstGlobalBinding {
                        target: AstGlobalBindingTarget::Name(crate::ast::AstGlobalName {
                            text: "label".to_owned(),
                        }),
                        attr: AstGlobalAttr::None,
                    }],
                    values: vec![AstExpr::Var(AstNameRef::Local(LocalId(1)))],
                })),
                AstStmt::GlobalDecl(Box::new(AstGlobalDecl {
                    bindings: vec![crate::ast::AstGlobalBinding {
                        target: AstGlobalBindingTarget::Name(crate::ast::AstGlobalName {
                            text: "counter".to_owned(),
                        }),
                        attr: AstGlobalAttr::None,
                    }],
                    values: vec![AstExpr::Var(AstNameRef::Local(LocalId(0)))],
                })),
            ],
        },
    };

    assert!(apply(
        &mut module,
        ReadabilityContext {
            target: AstTargetDialect::new(crate::ast::AstDialectVersion::Lua55),
            options: Default::default(),
        }
    ));

    let [AstStmt::GlobalDecl(global_decl)] = module.body.stmts.as_slice() else {
        panic!("expected merged global declaration");
    };
    assert_eq!(global_decl.bindings.len(), 2);
    assert!(matches!(
        &global_decl.bindings[0].target,
        AstGlobalBindingTarget::Name(name) if name.text == "counter"
    ));
    assert!(matches!(
        &global_decl.bindings[1].target,
        AstGlobalBindingTarget::Name(name) if name.text == "label"
    ));
    assert!(
        matches!(global_decl.values.as_slice(), [AstExpr::Integer(9), AstExpr::String(seed)] if seed == "seed")
    );
}

#[test]
fn lua55_does_not_infer_const_global_decl_for_missing_readonly_globals_inside_nested_functions() {
    let mut module = AstModule {
        entry_function: HirProtoRef(0),
        body: AstBlock {
            stmts: vec![AstStmt::LocalDecl(Box::new(AstLocalDecl {
                bindings: vec![AstLocalBinding {
                    id: AstBindingRef::Local(LocalId(0)),
                    attr: AstLocalAttr::None,
                    origin: crate::ast::AstLocalOrigin::Recovered,
                }],
                values: vec![AstExpr::FunctionExpr(Box::new(AstFunctionExpr {
                    function: HirProtoRef(1),
                    params: vec![ParamId(0)],
                    is_vararg: false,
                    named_vararg: None,
                    body: AstBlock {
                        stmts: vec![AstStmt::Return(Box::new(AstReturn {
                            values: vec![AstExpr::Call(Box::new(AstCallExpr {
                                callee: AstExpr::FieldAccess(Box::new(
                                    crate::ast::AstFieldAccess {
                                        base: AstExpr::Var(AstNameRef::Global(
                                            crate::ast::AstGlobalName {
                                                text: "math".to_owned(),
                                            },
                                        )),
                                        field: "max".to_owned(),
                                    },
                                )),
                                args: vec![
                                    AstExpr::Var(AstNameRef::Param(ParamId(0))),
                                    AstExpr::Integer(1),
                                ],
                            }))],
                        }))],
                    },
                    captured_bindings: Default::default(),
                }))],
            }))],
        },
    };

    assert!(!apply(
        &mut module,
        ReadabilityContext {
            target: AstTargetDialect::new(crate::ast::AstDialectVersion::Lua55),
            options: Default::default(),
        }
    ));

    let [AstStmt::LocalDecl(local_decl)] = module.body.stmts.as_slice() else {
        panic!("expected outer local decl to remain");
    };
    let [AstExpr::FunctionExpr(function)] = local_decl.values.as_slice() else {
        panic!("expected nested function expression");
    };
    assert!(matches!(
        function.body.stmts.as_slice(),
        [AstStmt::Return(_)]
    ));
}

#[test]
fn lua55_does_not_infer_mutable_global_prelude_when_outer_block_reads_name_written_in_nested_function()
 {
    let mut module = AstModule {
        entry_function: HirProtoRef(0),
        body: AstBlock {
            stmts: vec![
                AstStmt::LocalDecl(Box::new(AstLocalDecl {
                    bindings: vec![AstLocalBinding {
                        id: AstBindingRef::Local(LocalId(0)),
                        attr: AstLocalAttr::None,
                        origin: crate::ast::AstLocalOrigin::Recovered,
                    }],
                    values: vec![AstExpr::FunctionExpr(Box::new(AstFunctionExpr {
                        function: HirProtoRef(1),
                        params: vec![],
                        is_vararg: false,
                        named_vararg: None,
                        body: AstBlock {
                            stmts: vec![AstStmt::FunctionDecl(Box::new(
                                crate::ast::AstFunctionDecl {
                                    target: crate::ast::AstFunctionName::Plain(
                                        crate::ast::AstNamePath {
                                            root: AstNameRef::Global(crate::ast::AstGlobalName {
                                                text: "emit".to_owned(),
                                            }),
                                            fields: vec![],
                                        },
                                    ),
                                    func: AstFunctionExpr {
                                        function: HirProtoRef(2),
                                        params: vec![],
                                        is_vararg: false,
                                        named_vararg: None,
                                        body: AstBlock { stmts: vec![] },
                                        captured_bindings: Default::default(),
                                    },
                                },
                            ))],
                        },
                        captured_bindings: Default::default(),
                    }))],
                })),
                AstStmt::CallStmt(Box::new(crate::ast::AstCallStmt {
                    call: crate::ast::common::AstCallKind::Call(Box::new(AstCallExpr {
                        callee: AstExpr::Var(AstNameRef::Global(crate::ast::AstGlobalName {
                            text: "emit".to_owned(),
                        })),
                        args: vec![],
                    })),
                })),
            ],
        },
    };

    assert!(!apply(
        &mut module,
        ReadabilityContext {
            target: AstTargetDialect::new(crate::ast::AstDialectVersion::Lua55),
            options: Default::default(),
        }
    ));

    assert!(matches!(
        module.body.stmts.as_slice(),
        [AstStmt::LocalDecl(_), AstStmt::CallStmt(_)]
    ));
}

#[test]
fn lua55_does_not_add_global_decl_without_explicit_global_evidence() {
    let mut module = AstModule {
        entry_function: HirProtoRef(0),
        body: AstBlock {
            stmts: vec![AstStmt::CallStmt(Box::new(crate::ast::AstCallStmt {
                call: crate::ast::common::AstCallKind::Call(Box::new(AstCallExpr {
                    callee: AstExpr::Var(AstNameRef::Global(crate::ast::AstGlobalName {
                        text: "print".to_owned(),
                    })),
                    args: vec![AstExpr::String("tag".to_owned())],
                })),
            }))],
        },
    };

    assert!(!apply(
        &mut module,
        ReadabilityContext {
            target: AstTargetDialect::new(crate::ast::AstDialectVersion::Lua55),
            options: Default::default(),
        }
    ));

    assert!(matches!(
        module.body.stmts.as_slice(),
        [AstStmt::CallStmt(_)]
    ));
}

#[test]
fn lua55_infers_missing_globals_after_existing_leading_global_decl_run() {
    let mut module = AstModule {
        entry_function: HirProtoRef(0),
        body: AstBlock {
            stmts: vec![
                AstStmt::GlobalDecl(Box::new(AstGlobalDecl {
                    bindings: vec![crate::ast::AstGlobalBinding {
                        target: AstGlobalBindingTarget::Name(crate::ast::AstGlobalName {
                            text: "outer_prefix".to_owned(),
                        }),
                        attr: AstGlobalAttr::None,
                    }],
                    values: vec![AstExpr::String("G".to_owned())],
                })),
                AstStmt::CallStmt(Box::new(crate::ast::AstCallStmt {
                    call: crate::ast::common::AstCallKind::Call(Box::new(AstCallExpr {
                        callee: AstExpr::Var(AstNameRef::Global(crate::ast::AstGlobalName {
                            text: "print".to_owned(),
                        })),
                        args: vec![AstExpr::String("tag".to_owned())],
                    })),
                })),
            ],
        },
    };

    assert!(apply(
        &mut module,
        ReadabilityContext {
            target: AstTargetDialect::new(crate::ast::AstDialectVersion::Lua55),
            options: Default::default(),
        }
    ));

    let [
        AstStmt::GlobalDecl(explicit),
        AstStmt::GlobalDecl(inferred),
        AstStmt::CallStmt(_),
    ] = module.body.stmts.as_slice()
    else {
        panic!("expected explicit global decl followed by inferred prelude");
    };
    assert!(matches!(
        &explicit.bindings[0].target,
        AstGlobalBindingTarget::Name(name) if name.text == "outer_prefix"
    ));
    assert!(matches!(
        &inferred.bindings[0].target,
        AstGlobalBindingTarget::Name(name) if name.text == "print"
    ));
    assert_eq!(inferred.bindings[0].attr, AstGlobalAttr::Const);
}

#[test]
fn lua55_wraps_terminal_missing_const_globals_in_collective_do_block() {
    let mut module = AstModule {
        entry_function: HirProtoRef(0),
        body: AstBlock {
            stmts: vec![
                AstStmt::GlobalDecl(Box::new(AstGlobalDecl {
                    bindings: vec![crate::ast::AstGlobalBinding {
                        target: AstGlobalBindingTarget::Name(crate::ast::AstGlobalName {
                            text: "score".to_owned(),
                        }),
                        attr: AstGlobalAttr::None,
                    }],
                    values: vec![AstExpr::Integer(6)],
                })),
                AstStmt::LocalDecl(Box::new(AstLocalDecl {
                    bindings: vec![AstLocalBinding {
                        id: AstBindingRef::Local(LocalId(0)),
                        attr: AstLocalAttr::None,
                        origin: crate::ast::AstLocalOrigin::Recovered,
                    }],
                    values: vec![AstExpr::FunctionExpr(Box::new(AstFunctionExpr {
                        function: HirProtoRef(1),
                        params: vec![ParamId(0)],
                        is_vararg: false,
                        named_vararg: None,
                        body: AstBlock {
                            stmts: vec![
                                AstStmt::LocalDecl(Box::new(AstLocalDecl {
                                    bindings: vec![AstLocalBinding {
                                        id: AstBindingRef::Local(LocalId(1)),
                                        attr: AstLocalAttr::None,
                                        origin: crate::ast::AstLocalOrigin::Recovered,
                                    }],
                                    values: vec![AstExpr::Integer(1)],
                                })),
                                AstStmt::LocalDecl(Box::new(AstLocalDecl {
                                    bindings: vec![AstLocalBinding {
                                        id: AstBindingRef::Local(LocalId(2)),
                                        attr: AstLocalAttr::None,
                                        origin: crate::ast::AstLocalOrigin::Recovered,
                                    }],
                                    values: vec![AstExpr::Call(Box::new(AstCallExpr {
                                        callee: AstExpr::FieldAccess(Box::new(
                                            crate::ast::AstFieldAccess {
                                                base: AstExpr::Var(AstNameRef::Global(
                                                    crate::ast::AstGlobalName {
                                                        text: "math".to_owned(),
                                                    },
                                                )),
                                                field: "max".to_owned(),
                                            },
                                        )),
                                        args: vec![
                                            AstExpr::Var(AstNameRef::Local(LocalId(1))),
                                            AstExpr::Var(AstNameRef::Global(
                                                crate::ast::AstGlobalName {
                                                    text: "score".to_owned(),
                                                },
                                            )),
                                        ],
                                    }))],
                                })),
                                AstStmt::Return(Box::new(AstReturn {
                                    values: vec![AstExpr::Var(AstNameRef::Local(LocalId(2)))],
                                })),
                            ],
                        },
                        captured_bindings: Default::default(),
                    }))],
                })),
            ],
        },
    };

    assert!(apply(
        &mut module,
        ReadabilityContext {
            target: AstTargetDialect::new(crate::ast::AstDialectVersion::Lua55),
            options: Default::default(),
        }
    ));

    let [AstStmt::GlobalDecl(_), AstStmt::LocalDecl(local_decl)] = module.body.stmts.as_slice()
    else {
        panic!("expected explicit outer global and local function binding");
    };
    let [AstExpr::FunctionExpr(function)] = local_decl.values.as_slice() else {
        panic!("expected function expression to remain in local decl");
    };
    let [AstStmt::LocalDecl(_), AstStmt::DoBlock(block)] = function.body.stmts.as_slice() else {
        panic!("expected trailing collective do block");
    };
    let [
        AstStmt::GlobalDecl(global_decl),
        AstStmt::LocalDecl(_),
        AstStmt::Return(_),
    ] = block.stmts.as_slice()
    else {
        panic!("expected wildcard gate plus wrapped suffix");
    };
    assert!(matches!(
        global_decl.bindings.as_slice(),
        [crate::ast::AstGlobalBinding {
            target: AstGlobalBindingTarget::Wildcard,
            attr: AstGlobalAttr::Const,
        }]
    ));
}