Skip to content

Commit fd51825

Browse files
committed
Use ShortSource in errors thrown from Go
1 parent d583214 commit fd51825

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

aux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func countLevels(l *State) int {
3434
le *= 2
3535
}
3636
for li < le {
37-
m := (li + le)/2
37+
m := (li + le) / 2
3838
if Stack(l, m, &d) {
3939
li = m + 1
4040
} else {
@@ -169,7 +169,7 @@ func Where(l *State, level int) {
169169
if Stack(l, level, &activationRecord) { // check function at level
170170
Info(l, "Sl", &activationRecord) // get info about it
171171
if activationRecord.CurrentLine > 0 { // is there info?
172-
PushString(l, fmt.Sprintf("%s:%d: ", activationRecord.Source, activationRecord.CurrentLine))
172+
PushString(l, fmt.Sprintf("%s:%d: ", activationRecord.ShortSource, activationRecord.CurrentLine))
173173
return
174174
}
175175
}

vm_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package lua
22

33
import (
44
"fmt"
5+
"strings"
56
"testing"
67
)
78

@@ -155,7 +156,7 @@ func TestError(t *testing.T) {
155156
t.Error("error handler received no arguments")
156157
} else if errorMessage, ok := ToString(l, -1); !ok {
157158
t.Errorf("error handler received %s instead of string", TypeNameOf(l, -1))
158-
} else if errorMessage != program+":1: error" {
159+
} else if errorMessage != chunkID(program)+":1: error" {
159160
t.Errorf("error handler received '%s' instead of 'error'", errorMessage)
160161
}
161162
errorHandled = true
@@ -168,6 +169,35 @@ func TestError(t *testing.T) {
168169
}
169170
}
170171

172+
func TestErrorf(t *testing.T) {
173+
l := NewState()
174+
BaseOpen(l)
175+
program := "-- script that is bigger than the max ID size\nhelper()\n" + strings.Repeat("--", idSize)
176+
expectedErrorMessage := chunkID(program) + ":2: error"
177+
PushGoFunction(l, func(l *State) int {
178+
Errorf(l, "error")
179+
return 0
180+
})
181+
SetGlobal(l, "helper")
182+
errorHandled := false
183+
PushGoFunction(l, func(l *State) int {
184+
if Top(l) == 0 {
185+
t.Error("error handler received no arguments")
186+
} else if errorMessage, ok := ToString(l, -1); !ok {
187+
t.Errorf("error handler received %s instead of string", TypeNameOf(l, -1))
188+
} else if errorMessage != expectedErrorMessage {
189+
t.Errorf("error handler received '%s' instead of '%s'", errorMessage, expectedErrorMessage)
190+
}
191+
errorHandled = true
192+
return 1
193+
})
194+
LoadString(l, program)
195+
ProtectedCall(l, 0, 0, -2)
196+
if !errorHandled {
197+
t.Error("error not handled")
198+
}
199+
}
200+
171201
func Example() {
172202
type step struct {
173203
name string

0 commit comments

Comments
 (0)