Skip to content

Commit 40bc5bf

Browse files
authored
[fix] uses file opts for load module (#124)
* repro bug * for fib * for test cases * for actions * trigger Load
1 parent cea71c4 commit 40bc5bf

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
name: Test with ${{ matrix.go-version }} on ${{ matrix.vm-os }}
1818
runs-on: ${{ matrix.vm-os }}
1919
env:
20-
CI_REPORT: ${{ matrix.vm-os == 'ubuntu-20.04' && matrix.go-version == '1.18.10' }}
20+
CI_REPORT: ${{ matrix.vm-os == 'ubuntu-20.04' && startsWith(matrix.go-version, '1.18.') }}
2121
strategy:
2222
max-parallel: 10
2323
fail-fast: false

cache.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"unsafe"
99

1010
"go.starlark.net/starlark"
11+
"go.starlark.net/syntax"
1112
)
1213

1314
// The following code is copied and modified from the starlark-go repo,
@@ -23,6 +24,7 @@ type cache struct {
2324
cacheMu sync.Mutex
2425
cache map[string]*entry
2526
globals starlark.StringDict
27+
execOpts *syntax.FileOptions
2628
loadMod func(s string) (starlark.StringDict, error) // load from built-in module first
2729
readFile func(s string) ([]byte, error) // and then from file system
2830
}
@@ -108,7 +110,12 @@ func (c *cache) doLoad(cc *cycleChecker, module string) (starlark.StringDict, er
108110
if err != nil {
109111
return nil, err
110112
}
111-
return starlark.ExecFile(thread, module, b, c.globals)
113+
114+
// 3. execute the source file
115+
if c.execOpts == nil {
116+
return starlark.ExecFile(thread, module, b, c.globals)
117+
}
118+
return starlark.ExecFileOptions(c.execOpts, thread, module, b, c.globals)
112119
}
113120

114121
// -- concurrent cycle checking --

run.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,9 @@ func (m *Machine) prepareThread(extras StringAnyMap) (err error) {
241241

242242
// cache load&read + printf -> thread
243243
m.loadCache = &cache{
244-
cache: make(map[string]*entry),
245-
loadMod: m.lazyloadMods.GetLazyLoader(),
244+
cache: make(map[string]*entry),
245+
execOpts: m.getFileOptions(),
246+
loadMod: m.lazyloadMods.GetLazyLoader(),
246247
readFile: func(name string) ([]byte, error) {
247248
return readScriptFile(name, m.scriptFS)
248249
},

run_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,44 @@ x = fib(10)
701701
expectErr(t, err, `starlark: exec: function fib called recursively`)
702702
}
703703

704+
func Test_Machine_Run_RecursionLoad(t *testing.T) {
705+
code := `
706+
load("fibonacci2.star", "fib")
707+
x = fib(10)
708+
`
709+
{
710+
// create machine
711+
m := starlet.NewDefault()
712+
// set code
713+
m.SetScript("ans.star", []byte(code), os.DirFS("testdata"))
714+
// run
715+
_, err := m.Run()
716+
if err == nil {
717+
t.Errorf("expected error, got nil")
718+
return
719+
}
720+
}
721+
722+
{
723+
// create machine
724+
m := starlet.NewDefault()
725+
m.EnableRecursionSupport()
726+
// set code
727+
m.SetScript("ans.star", []byte(code), os.DirFS("testdata"))
728+
// run
729+
out, err := m.Run()
730+
if err != nil {
731+
t.Errorf("unexpected error: %v", err)
732+
}
733+
// check result
734+
if out == nil {
735+
t.Errorf("unexpected nil output")
736+
} else if out["x"] != int64(55) {
737+
t.Errorf("unexpected output: %v", out)
738+
}
739+
}
740+
}
741+
704742
func Test_Machine_Run_LoadErrors(t *testing.T) {
705743
mm := starlark.NewDict(1)
706744
_ = mm.SetKey(starlark.String("quarter"), starlark.MakeInt(100))

testdata/fibonacci2.star

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def fib(n):
2+
if n < 2:
3+
return n
4+
return fib(n - 1) + fib(n - 2)

0 commit comments

Comments
 (0)