Skip to content

Commit

Permalink
Use Object.Copy when cloning globals in Compiled (d5#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
ipsusila authored Sep 25, 2022
1 parent 8a3f5bd commit dfcfd66
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion script.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (c *Compiled) Clone() *Compiled {
// copy global objects
for idx, g := range c.globals {
if g != nil {
clone.globals[idx] = g
clone.globals[idx] = g.Copy()
}
}
return clone
Expand Down
25 changes: 25 additions & 0 deletions script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,3 +640,28 @@ func compiledIsDefined(
) {
require.Equal(t, expected, c.IsDefined(name))
}
func TestCompiled_Clone(t *testing.T) {
script := tengo.NewScript([]byte(`
count += 1
data["b"] = 2
`))

err := script.Add("data", map[string]interface{}{"a": 1})
require.NoError(t, err)

err = script.Add("count", 1000)
require.NoError(t, err)

compiled, err := script.Compile()
require.NoError(t, err)

clone := compiled.Clone()
err = clone.RunContext(context.Background())
require.NoError(t, err)

require.Equal(t, 1000, compiled.Get("count").Int())
require.Equal(t, 1, len(compiled.Get("data").Map()))

require.Equal(t, 1001, clone.Get("count").Int())
require.Equal(t, 2, len(clone.Get("data").Map()))
}

0 comments on commit dfcfd66

Please sign in to comment.