| 43 | } |
| 44 | |
| 45 | func TestUndump(t *testing.T) { |
| 46 | _, err := exec.LookPath("luac") |
| 47 | if err != nil { |
| 48 | t.Skipf("testing undump requires luac: %s", err) |
| 49 | } |
| 50 | source := filepath.Join("lua-tests", "checktable.lua") |
| 51 | binary := filepath.Join("lua-tests", "checktable.bin") |
| 52 | if err := exec.Command("luac", "-o", binary, source).Run(); err != nil { |
| 53 | t.Fatalf("luac failed to compile %s: %s", source, err) |
| 54 | } |
| 55 | file, err := os.Open(binary) |
| 56 | if err != nil { |
| 57 | t.Fatal("couldn't open checktable.bin") |
| 58 | } |
| 59 | l := NewState() |
| 60 | closure, err := l.undump(file, "test") |
| 61 | if err != nil { |
| 62 | offset, _ := file.Seek(0, 1) |
| 63 | t.Error("unexpected error", err, "at file offset", offset) |
| 64 | } |
| 65 | if closure == nil { |
| 66 | t.Error("closure was nil") |
| 67 | } |
| 68 | p := closure.prototype |
| 69 | if p == nil { |
| 70 | t.Fatal("prototype was nil") |
| 71 | } |
| 72 | validate("@lua-tests/checktable.lua", p.source, "as source file name", t) |
| 73 | validate(23, len(p.code), "instructions", t) |
| 74 | validate(8, len(p.constants), "constants", t) |
| 75 | validate(4, len(p.prototypes), "prototypes", t) |
| 76 | validate(1, len(p.upValues), "upvalues", t) |
| 77 | validate(0, len(p.localVariables), "local variables", t) |
| 78 | validate(0, p.parameterCount, "parameters", t) |
| 79 | validate(4, p.maxStackSize, "stack slots", t) |
| 80 | if !p.isVarArg { |
| 81 | t.Error("expected main function to be var arg, but wasn't") |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func validate(expected, actual interface{}, description string, t *testing.T) { |
| 86 | if expected != actual { |