Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] uses file opts for load module #124

Merged
merged 5 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
"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 @@
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 @@
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)
}

Check warning on line 117 in cache.go

View check run for this annotation

Codecov / codecov/patch

cache.go#L116-L117

Added lines #L116 - L117 were not covered by tests
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)