From 2448a392a8a8943fdf7e4073324276d96a4b63a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Thu, 26 Dec 2024 22:15:13 +0800 Subject: [PATCH 1/5] repro bug --- run_test.go | 23 +++++++++++++++++++++++ testdata/fibonacci2.star | 4 ++++ 2 files changed, 27 insertions(+) create mode 100644 testdata/fibonacci2.star diff --git a/run_test.go b/run_test.go index 8c4a5add..05ace090 100644 --- a/run_test.go +++ b/run_test.go @@ -701,6 +701,29 @@ x = fib(10) expectErr(t, err, `starlark: exec: function fib called recursively`) } +func Test_Machine_Run_LoadRecursion(t *testing.T) { + // create machine + m := starlet.NewDefault() + m.EnableRecursionSupport() + // set code + code := ` +load("fibonacci2.star", "fib") +x = fib(10) +` + m.SetScript("test.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)) diff --git a/testdata/fibonacci2.star b/testdata/fibonacci2.star new file mode 100644 index 00000000..1350a442 --- /dev/null +++ b/testdata/fibonacci2.star @@ -0,0 +1,4 @@ +def fib(n): + if n < 2: + return 1 + return fib(n - 1) + fib(n - 2) From 20e643fc545368dd1c779e6da1ddcad0246a98cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Thu, 26 Dec 2024 22:25:45 +0800 Subject: [PATCH 2/5] for fib --- cache.go | 12 +++++++++--- run.go | 5 +++-- run_test.go | 2 +- testdata/fibonacci2.star | 2 +- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/cache.go b/cache.go index 306448da..b174d03a 100644 --- a/cache.go +++ b/cache.go @@ -3,11 +3,11 @@ package starlet import ( "errors" "fmt" + "go.starlark.net/starlark" + "go.starlark.net/syntax" "sync" "sync/atomic" "unsafe" - - "go.starlark.net/starlark" ) // The following code is copied and modified from the starlark-go repo, @@ -23,6 +23,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 } @@ -108,7 +109,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 -- diff --git a/run.go b/run.go index e5d9ef62..05fe64d7 100644 --- a/run.go +++ b/run.go @@ -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) }, diff --git a/run_test.go b/run_test.go index 05ace090..03be4058 100644 --- a/run_test.go +++ b/run_test.go @@ -710,7 +710,7 @@ func Test_Machine_Run_LoadRecursion(t *testing.T) { load("fibonacci2.star", "fib") x = fib(10) ` - m.SetScript("test.star", []byte(code), os.DirFS("testdata")) + m.SetScript("ans.star", []byte(code), os.DirFS("testdata")) // run out, err := m.Run() if err != nil { diff --git a/testdata/fibonacci2.star b/testdata/fibonacci2.star index 1350a442..10099d9a 100644 --- a/testdata/fibonacci2.star +++ b/testdata/fibonacci2.star @@ -1,4 +1,4 @@ def fib(n): if n < 2: - return 1 + return n return fib(n - 1) + fib(n - 2) From 7f47052f0777c2056dc50824cd682cbdd0daf6d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Thu, 26 Dec 2024 22:41:56 +0800 Subject: [PATCH 3/5] for test cases --- cache.go | 5 +++-- run_test.go | 43 +++++++++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/cache.go b/cache.go index b174d03a..869ee5a4 100644 --- a/cache.go +++ b/cache.go @@ -3,11 +3,12 @@ package starlet import ( "errors" "fmt" - "go.starlark.net/starlark" - "go.starlark.net/syntax" "sync" "sync/atomic" "unsafe" + + "go.starlark.net/starlark" + "go.starlark.net/syntax" ) // The following code is copied and modified from the starlark-go repo, diff --git a/run_test.go b/run_test.go index 03be4058..6ef5f9b7 100644 --- a/run_test.go +++ b/run_test.go @@ -702,25 +702,40 @@ x = fib(10) } func Test_Machine_Run_LoadRecursion(t *testing.T) { - // create machine - m := starlet.NewDefault() - m.EnableRecursionSupport() - // set code code := ` load("fibonacci2.star", "fib") x = fib(10) ` - m.SetScript("ans.star", []byte(code), os.DirFS("testdata")) - // run - out, err := m.Run() - if err != nil { - t.Errorf("unexpected error: %v", err) + { + // 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 + } } - // check result - if out == nil { - t.Errorf("unexpected nil output") - } else if out["x"] != int64(55) { - t.Errorf("unexpected output: %v", out) + + { + // 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) + } } } From 7f0be0a556103f1a2c0d1e76e1aaf47bc189dd33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Fri, 27 Dec 2024 00:31:48 +0800 Subject: [PATCH 4/5] for actions --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1459a384..d77b54d0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 From b0552f6cb86ba5dd09513fffdb668c721520d835 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Fri, 27 Dec 2024 00:43:33 +0800 Subject: [PATCH 5/5] trigger Load --- run_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run_test.go b/run_test.go index 6ef5f9b7..f9493ef4 100644 --- a/run_test.go +++ b/run_test.go @@ -701,7 +701,7 @@ x = fib(10) expectErr(t, err, `starlark: exec: function fib called recursively`) } -func Test_Machine_Run_LoadRecursion(t *testing.T) { +func Test_Machine_Run_RecursionLoad(t *testing.T) { code := ` load("fibonacci2.star", "fib") x = fib(10)