Skip to content
Open
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
4 changes: 2 additions & 2 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ func init() {
c := e.k(i.c())
if nb, ok := b.(float64); ok {
if nc, ok := c.(float64); ok {
e.frame[i.a()] = math.Mod(nb, nc)
e.frame[i.a()] = arith(OpMod, nb, nc)
if e.hooked() {
e.hook()
}
Expand Down Expand Up @@ -1083,7 +1083,7 @@ func (l *State) executeSwitch() {
c := k(i.c(), constants, frame)
if nb, ok := b.(float64); ok {
if nc, ok := c.(float64); ok {
frame[i.a()] = math.Mod(nb, nc)
frame[i.a()] = arith(OpMod, nb, nc)
break
}
}
Expand Down
18 changes: 18 additions & 0 deletions vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ func TestProtectedCall(t *testing.T) {
l.Call(0, 0)
}

func TestModuloNegativeOperands(t *testing.T) {
l := NewState()
OpenLibraries(l)

if err := DoString(l, `local a,b,c,d=-3,2,3,-2
return -3%2, a%b, "-3"%b, c%d, math.fmod(a,b)`); err != nil {
t.Fatal(err)
}

want := []float64{1, 1, 1, -1, -1}
for i, expected := range want {
got, ok := l.ToNumber(i + 1)
if !ok || got != expected {
t.Errorf("result %d = %v (number: %t), want %v", i+1, got, ok, expected)
}
}
}

func TestLua(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading