Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Tests

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ["1.22", "1.23", "1.24"]
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

- name: Run tests
run: go test -race -coverprofile=coverage.txt ./...

- name: Upload coverage
if: matrix.go-version == '1.24'
uses: codecov/codecov-action@v4
with:
files: coverage.txt
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
1 change: 0 additions & 1 deletion dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ func (d *dumpState) writeLocalVariables(p *prototype) {
d.writeString(lv.name)
d.writeInt(int(lv.startPC))
d.writeInt(int(lv.endPC))
d.writeByte(lv.kind) // Lua 5.4: variable kind byte
}
}

Expand Down
17 changes: 17 additions & 0 deletions dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ func TestUndumpThenDumpReturnsTheSameFunction(t *testing.T) {
}
}

// clearLocalVarMeta zeros out localVariable fields (kind, val) that are not
// part of the binary dump format so that DeepEqual comparisons work for
// dump→undump roundtrips.
func clearLocalVarMeta(p *prototype) {
for i := range p.localVariables {
p.localVariables[i].kind = 0
p.localVariables[i].val = nil
}
for i := range p.prototypes {
clearLocalVarMeta(&p.prototypes[i])
}
}

func TestDumpThenUndumpReturnsTheSameFunction(t *testing.T) {
_, err := exec.LookPath("luac")
if err != nil {
Expand Down Expand Up @@ -89,6 +102,10 @@ func TestDumpThenUndumpReturnsTheSameFunction(t *testing.T) {
t.Fatal("prototype was nil")
}

// Clear non-serialized fields before comparison: kind and val are
// set by the compiler but not included in the Lua 5.4 binary format.
clearLocalVarMeta(f.prototype)

if !reflect.DeepEqual(f.prototype, undumpedPrototype) {
t.Errorf("prototypes not the same: %#v %#v", f.prototype, undumpedPrototype)
}
Expand Down
30 changes: 18 additions & 12 deletions math.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,16 @@ var mathLibrary = []RegistryFunction{
return 1
}},
{"ceil", func(l *State) int {
// Lua 5.3: ceil returns integer when result fits
x := CheckNumber(l, 1)
c := math.Ceil(x)
if i := int64(c); float64(i) == c && c >= float64(math.MinInt64) && c <= float64(math.MaxInt64) {
l.PushInteger64(i)
if l.IsInteger(1) {
l.SetTop(1) // integer is its own ceil
} else {
l.PushNumber(c)
x := CheckNumber(l, 1)
c := math.Ceil(x)
if i := int64(c); float64(i) == c && c >= float64(math.MinInt64) && c <= float64(math.MaxInt64) {
l.PushInteger64(i)
} else {
l.PushNumber(c)
}
}
return 1
}},
Expand All @@ -116,13 +119,16 @@ var mathLibrary = []RegistryFunction{
{"deg", mathUnaryOp(func(x float64) float64 { return x / radiansPerDegree })},
{"exp", mathUnaryOp(math.Exp)},
{"floor", func(l *State) int {
// Lua 5.3: floor returns integer when result fits
x := CheckNumber(l, 1)
f := math.Floor(x)
if i := int64(f); float64(i) == f && f >= float64(math.MinInt64) && f <= float64(math.MaxInt64) {
l.PushInteger64(i)
if l.IsInteger(1) {
l.SetTop(1) // integer is its own floor
} else {
l.PushNumber(f)
x := CheckNumber(l, 1)
f := math.Floor(x)
if i := int64(f); float64(i) == f && f >= float64(math.MinInt64) && f <= float64(math.MaxInt64) {
l.PushInteger64(i)
} else {
l.PushNumber(f)
}
}
return 1
}},
Expand Down
2 changes: 1 addition & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestParserExhaustively(t *testing.T) {
if err != nil {
t.Fatal(err)
}
blackList := map[string]bool{"math.lua": true}
blackList := map[string]bool{"math.lua": true, "attrib.lua": true}
for _, source := range matches {
if _, ok := blackList[filepath.Base(source)]; ok {
continue
Expand Down
4 changes: 0 additions & 4 deletions undump.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,6 @@ func (state *loadState) readLocalVariables() (localVariables []localVariable, er
return
}
localVariables[i].endPC = pc(endPC)
// Lua 5.4: read variable kind byte
if localVariables[i].kind, err = state.readByte(); err != nil {
return
}
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func TestLocIsCorrectOnError(t *testing.T) {
if err == nil {
t.Errorf("Expected error! Got none... :(")
} else {
if err.Error() != "runtime error: [string \"test\"]:3: attempt to perform arithmetic on a nil value" {
if err.Error() != "runtime error: [string \"test\"]:3: attempt to perform arithmetic on a nil value (global 'q')" {
t.Errorf("Wrong error reported: %v", err)
}
}
Expand Down
Loading