google / starlark-go

Starlark in Go: the Starlark configuration language, implemented in Go
BSD 3-Clause "New" or "Revised" License
2.26k stars 204 forks source link

starlark: add debug API for Locals and FreeVars #539

Closed adonovan closed 2 months ago

adonovan commented 2 months ago

This change adds the following new API to allow debugging tools and built-in functions access to the internals of Function values and call frames:

package starlark

type Binding struct { Name string Pos syntax.Position }

func (fr frame) NumLocals() int func (fr frame) Local(i int) (Binding, Value)

type DebugFrame interface { ... NumLocals() int Local(i int) (Binding, Value) }

func (fn Function) NumFreeVars() int func (fn Function) FreeVar(i int) (Binding, Value)

This is strictly a breaking change, but the changed functions (the Local methods) were previously documented as experimental. The fix is straightforward.

Also, a test of DebugFrame to write an 'env' function in a similar vein to Python's 'dir' function.

Fixes #538