Skip to content

Commit

Permalink
fix: copy SourceMap in vm OpClosure branch (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyuge authored Sep 24, 2022
1 parent e338512 commit 8a3f5bd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
32 changes: 32 additions & 0 deletions script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ func (v *VM) run() {
NumLocals: fn.NumLocals,
NumParameters: fn.NumParameters,
VarArgs: fn.VarArgs,
SourceMap: fn.SourceMap,
Free: free,
}
v.allocs--
Expand Down

0 comments on commit 8a3f5bd

Please sign in to comment.