Skip to content

Commit

Permalink
exec sep
Browse files Browse the repository at this point in the history
  • Loading branch information
hyorigo committed Feb 26, 2024
1 parent bae41b7 commit ef5e69a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
35 changes: 35 additions & 0 deletions exec.go
Original file line number Diff line number Diff line change
@@ -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
}
29 changes: 0 additions & 29 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

0 comments on commit ef5e69a

Please sign in to comment.