sandbox

package
v0.3.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sandbox runs the Starlark of the format — the predicate of a check, the expressions of a template and its solver — in go.starlark.net at the version the format names, in its one dialect, with the format's limits of steps and module-kit's clocks, on the caller's goroutine and with its context (format/README.md, sections 15, 23 and 24; R88 spec; R11; architecture 3.4).

CheckExpr checks an expression of the dialect and the names it may take, and Eval evaluates one on a thread of its own under Limits, of which Predicate are a predicate's, stopping it with ErrSteps, ErrClock or ErrPanic, or with the error of a context that is done (R28). Compile compiles an expression once into a Func of the names it is written on, which Call evaluates on each binding of them under the same Limits and stops: the expressions of a template, on each combination of its parameters (R32). CheckProgram checks a program of the dialect, and Exec runs its top level on a thread of its own under Limits, of which Solver are a solver's, into a Program whose Call calls its functions on the same thread, within the same steps and clock and with the same stops: the solver of a template, whose solve is called on each of its instances (R33).

The sandbox shares nothing between evaluations or programs and leaves starlark.Universe as it is: the dialect's built-in names are its 33 names at the version the format names, and an expression or a program names no other even in a process that adds to it.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSteps = errors.New("the count of steps reached the limit")
	ErrClock = errors.New("the clock stopped the evaluation")
	ErrPanic = errors.New("the interpreter panicked")
)

Why an evaluation stopped without its result. A context that is done stops it too, and then the error is the context's, wrapped: that is the caller's stop, not the sandbox's (R11).

View Source
var ErrClosed = errors.New("the program is closed")

ErrClosed is what Call gives after Close: the program has no clock and no watch of ctx left.

View Source
var Predicate = Limits{Steps: 1_000_000, Clock: time.Second}

Predicate are the limits of a predicate: the format's 1 000 000 steps, and module-kit's clock of a second, which a simple predicate does not come near, since it takes 6 ms for the steps (format/README.md, section 15; R11).

View Source
var Solver = Limits{Steps: 100_000_000, Clock: time.Minute}

Solver are the limits of the solver of a template, for the top level of its program and every call of solve together: the format's budget of 100 000 000 steps, and module-kit's clock of a minute, which a solver of simple steps does not come near, since a loop takes 0.10–0.16 s for the budget (format/README.md, section 24; R11, R33).

Functions

func CheckExpr

func CheckExpr(src string, names ...string) error

CheckExpr says whether src is an expression of the dialect that names nothing but names, the names that it binds itself, such as n in [n * n for n in range(10)], and the built-in names of the dialect, as task.predicate-expression asks of a predicate on value (format/README.md, section 15). The error says why not, at the line and the column where go.starlark.net finds it.

func CheckProgram added in v0.1.20

func CheckProgram(src string) error

CheckProgram says whether src is a program of the dialect: one that its parser parses, that holds no load, and that names nothing but the names that it binds itself and the built-in names of the dialect, as task.template-solver asks of the program of a solver (format/README.md, section 24). The error says why not, at the line and the column where go.starlark.net finds it, or where the load stands.

func ContextDone added in v0.1.20

func ContextDone(err error) bool

ContextDone says whether an error of the sandbox is that of a context that is done: the caller's stop, which gives no verdict and no breach (R11).

func Eval

func Eval(ctx context.Context, limits Limits, src string, env starlark.StringDict) (starlark.Value, error)

Eval evaluates the expression src, with env binding the names it is written on, on a thread of its own with the limits and ctx (format/README.md, sections 15 and 24; R11): a predicate, with value bound to the integer read. src is checked first as CheckExpr checks it, on the names of env, so that it names nothing else even in a process that changed starlark.Universe.

The error is the context's, wrapped, when ctx is done before the evaluation or stops it; ErrSteps, ErrClock or ErrPanic, wrapped, when the evaluation stops so; and otherwise that of the expression, which is none of the dialect, names what it may not, or fails as it runs.

Types

type Func added in v0.1.19

type Func struct {
	// contains filtered or unexported fields
}

A Func is an expression of the dialect compiled once on the names it is written on, so that it is evaluated on many bindings of them: an expression of a template, on each combination of the parameters (format/README.md, section 23). It holds nothing that changes, so one Func is called from many goroutines at once.

func Compile added in v0.1.19

func Compile(src string, names ...string) (*Func, error)

Compile compiles src, an expression of the dialect that names nothing but names, the names that it binds itself and the built-in names of the dialect, as CheckExpr checks it, into a Func of names, in their order. The error is CheckExpr's.

The Func is a program of one statement, which assigns the parsed src to a global that names do not hold: built from its tree rather than its text, so that it compiles whatever CheckExpr takes — a comment, a line break — and is as deep to the parser as src, which a lambda around it would not be; and with the steps of src, as Eval counts them.

func (*Func) Call added in v0.1.19

func (f *Func) Call(ctx context.Context, limits Limits, args ...starlark.Value) (starlark.Value, error)

Call evaluates the expression with args bound to its names, in order, on a thread of its own with the limits and ctx, as Eval does, and its error is as Eval's. Args that are not one for each name are an error.

type Limits

type Limits struct {
	Steps uint64
	Clock time.Duration
}

Limits are what one evaluation may take. Steps is the format's: the evaluation stops when the count of steps on its thread, as go.starlark.net at the version of the format counts them, reaches it. Clock is module-kit's: the wall time after which it stops the evaluation (R11). Zero is no limit.

type Program added in v0.1.20

type Program struct {
	// contains filtered or unexported fields
}

A Program is a program of the dialect whose top level has run on a thread of its own, and whose functions Call calls on the same thread: the solver of a template, whose solve is called on each of its instances in turn (format/README.md, section 24). The limits are the program's, not each call's: the steps of the top level and of every call count together against one limit, and one clock runs from Exec to Close, as does the watch of ctx.

Its globals are frozen once the top level has run, as Starlark freezes those of a module ("Immediately after execution of a Starlark module, all values in its top-level environment are frozen", the spec of Starlark of go.starlark.net, Freezing a value), so a call changes nothing that the next one reads. A Program is for one goroutine at a time.

func Exec added in v0.1.20

func Exec(ctx context.Context, limits Limits, src string) (*Program, error)

Exec runs the top level of the program src on a thread of its own with the limits and ctx, on the caller's goroutine, and gives the Program, whose Close the caller calls once it has called its functions. src is checked first as CheckProgram checks it, so that it names nothing else even in a process that changed starlark.Universe.

The error is the context's, wrapped, when ctx is done before the program runs or stops it; CheckProgram's; ErrSteps, ErrClock or ErrPanic, wrapped, when the top level stops so; and otherwise that of the top level, which fails as it runs. With an error there is no Program.

func (*Program) Call added in v0.1.20

func (p *Program) Call(fn starlark.Value, args ...starlark.Value) (starlark.Value, error)

Call calls fn with args on the thread of the program, within the steps and the clock that the top level and the calls before have left, and its error is as Exec's: the context's, ErrSteps, ErrClock or ErrPanic, wrapped, or that of the call. After a panic, Call calls nothing and gives the same error again; after Close, which ended its clock and its watch of ctx, it calls nothing and gives ErrClosed.

func (*Program) Close added in v0.1.20

func (p *Program) Close()

Close stops the clock of the program and the watch of its ctx, so that Call calls nothing after it. It is called once the program's functions have been called, and may be called again.

func (*Program) Global added in v0.1.20

func (p *Program) Global(name string) (starlark.Value, bool)

Global gives the value of a global of the program, which its top level bound, and whether it bound it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL