diff --git a/exec.go b/exec.go new file mode 100644 index 00000000..91e881e6 --- /dev/null +++ b/exec.go @@ -0,0 +1,35 @@ +package starlet + +import ( + "bytes" + + "go.starlark.net/starlark" + "go.starlark.net/syntax" +) + +func starlarkExecFile(opts *syntax.FileOptions, thread *starlark.Thread, filename string, src interface{}, predeclared starlark.StringDict) (starlark.StringDict, error) { + // Parse, resolve, and compile a Starlark source file. + _, prog, err := starlark.SourceProgramOptions(opts, filename, src, predeclared.Has) + if err != nil { + return nil, err + } + + // Try to save it to the cache + buf := new(bytes.Buffer) + if err := prog.Write(buf); err != nil { + return nil, err + } + bs := buf.Bytes() + //cv := starlark.CompilerVersion + + // Reload as a new program + np, err := starlark.CompiledProgram(bytes.NewReader(bs)) + if err != nil { + return nil, err + } + prog = np + + g, err := prog.Init(thread, predeclared) + g.Freeze() + return g, err +} diff --git a/machine.go b/machine.go index c2e64501..dc10e22a 100644 --- a/machine.go +++ b/machine.go @@ -6,13 +6,11 @@ package starlet import ( - "bytes" "fmt" "io/fs" "sync" "go.starlark.net/starlark" - "go.starlark.net/syntax" ) // Machine is a thread-safe type that wraps Starlark runtime environments. Machine ensures thread safety by using a sync.RWMutex to control access to the environment's state. @@ -323,30 +321,3 @@ func (m *Machine) DisableGlobalReassign() { m.allowGlobalReassign = false } - -func starlarkExecFile(opts *syntax.FileOptions, thread *starlark.Thread, filename string, src interface{}, predeclared starlark.StringDict) (starlark.StringDict, error) { - // Parse, resolve, and compile a Starlark source file. - _, prog, err := starlark.SourceProgramOptions(opts, filename, src, predeclared.Has) - if err != nil { - return nil, err - } - - // Try to save it to the cache - buf := new(bytes.Buffer) - if err := prog.Write(buf); err != nil { - return nil, err - } - bs := buf.Bytes() - //cv := starlark.CompilerVersion - - // Reload as a new program - np, err := starlark.CompiledProgram(bytes.NewReader(bs)) - if err != nil { - return nil, err - } - prog = np - - g, err := prog.Init(thread, predeclared) - g.Freeze() - return g, err -}