Skip to content

Commit

Permalink
[fix] uses file opts for load module (#124)
Browse files Browse the repository at this point in the history
* repro bug

* for fib

* for test cases

* for actions

* trigger Load
  • Loading branch information
hyorigo authored Dec 26, 2024
1 parent cea71c4 commit 40bc5bf
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
name: Test with ${{ matrix.go-version }} on ${{ matrix.vm-os }}
runs-on: ${{ matrix.vm-os }}
env:
CI_REPORT: ${{ matrix.vm-os == 'ubuntu-20.04' && matrix.go-version == '1.18.10' }}
CI_REPORT: ${{ matrix.vm-os == 'ubuntu-20.04' && startsWith(matrix.go-version, '1.18.') }}
strategy:
max-parallel: 10
fail-fast: false
Expand Down
9 changes: 8 additions & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"unsafe"

"go.starlark.net/starlark"
"go.starlark.net/syntax"
)

// The following code is copied and modified from the starlark-go repo,
Expand All @@ -23,6 +24,7 @@ type cache struct {
cacheMu sync.Mutex
cache map[string]*entry
globals starlark.StringDict
execOpts *syntax.FileOptions
loadMod func(s string) (starlark.StringDict, error) // load from built-in module first
readFile func(s string) ([]byte, error) // and then from file system
}
Expand Down Expand Up @@ -108,7 +110,12 @@ func (c *cache) doLoad(cc *cycleChecker, module string) (starlark.StringDict, er
if err != nil {
return nil, err
}
return starlark.ExecFile(thread, module, b, c.globals)

// 3. execute the source file
if c.execOpts == nil {
return starlark.ExecFile(thread, module, b, c.globals)
}
return starlark.ExecFileOptions(c.execOpts, thread, module, b, c.globals)
}

// -- concurrent cycle checking --
Expand Down
5 changes: 3 additions & 2 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,9 @@ func (m *Machine) prepareThread(extras StringAnyMap) (err error) {

// cache load&read + printf -> thread
m.loadCache = &cache{
cache: make(map[string]*entry),
loadMod: m.lazyloadMods.GetLazyLoader(),
cache: make(map[string]*entry),
execOpts: m.getFileOptions(),
loadMod: m.lazyloadMods.GetLazyLoader(),
readFile: func(name string) ([]byte, error) {
return readScriptFile(name, m.scriptFS)
},
Expand Down
38 changes: 38 additions & 0 deletions run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,44 @@ x = fib(10)
expectErr(t, err, `starlark: exec: function fib called recursively`)
}

func Test_Machine_Run_RecursionLoad(t *testing.T) {
code := `
load("fibonacci2.star", "fib")
x = fib(10)
`
{
// create machine
m := starlet.NewDefault()
// set code
m.SetScript("ans.star", []byte(code), os.DirFS("testdata"))
// run
_, err := m.Run()
if err == nil {
t.Errorf("expected error, got nil")
return
}
}

{
// create machine
m := starlet.NewDefault()
m.EnableRecursionSupport()
// set code
m.SetScript("ans.star", []byte(code), os.DirFS("testdata"))
// run
out, err := m.Run()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
// check result
if out == nil {
t.Errorf("unexpected nil output")
} else if out["x"] != int64(55) {
t.Errorf("unexpected output: %v", out)
}
}
}

func Test_Machine_Run_LoadErrors(t *testing.T) {
mm := starlark.NewDict(1)
_ = mm.SetKey(starlark.String("quarter"), starlark.MakeInt(100))
Expand Down
4 changes: 4 additions & 0 deletions testdata/fibonacci2.star
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def fib(n):
if n < 2:
return n
return fib(n - 1) + fib(n - 2)

0 comments on commit 40bc5bf

Please sign in to comment.