fix: copy SourceMap in vm OpClosure branch ()

This commit is contained in:
shiyuge 2022-09-24 23:48:53 +08:00 committed by GitHub
parent e338512259
commit 8a3f5bdb11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View file

@ -541,6 +541,38 @@ func (n *customNumber) binaryOpInt(op token.Token, rhs *tengo.Int) (tengo.Object
return nil, tengo.ErrInvalidOperator
}
func TestScript_ImportError(t *testing.T) {
m := `
exp := import("expression")
r := exp(ctx)
`
src := `
export func(ctx) {
closure := func() {
if ctx.actiontimes < 0 { // an error is thrown here because actiontimes is undefined
return true
}
return false
}
return closure()
}`
s := tengo.NewScript([]byte(m))
mods := tengo.NewModuleMap()
mods.AddSourceModule("expression", []byte(src))
s.SetImports(mods)
err := s.Add("ctx", map[string]interface{}{
"ctx": 12,
})
require.NoError(t, err)
_, err = s.Run()
require.True(t, strings.Contains(err.Error(), "expression:4:6"))
}
func compile(t *testing.T, input string, vars M) *tengo.Compiled {
s := tengo.NewScript([]byte(input))
for vn, vv := range vars {

1
vm.go
View file

@ -767,6 +767,7 @@ func (v *VM) run() {
NumLocals: fn.NumLocals,
NumParameters: fn.NumParameters,
VarArgs: fn.VarArgs,
SourceMap: fn.SourceMap,
Free: free,
}
v.allocs--