sandbox

package
v0.3.19 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 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.

CheckExpr and CheckProgram check an expression and a program of the dialect. Eval evaluates an expression on a thread of its own under Limits, stopping it with ErrSteps, ErrClock or ErrPanic, or with the error of a context that is done. Compile compiles an expression once into a Func, which Call evaluates on each binding of its names under the same Limits and stops: the expressions of a template, on each combination of its parameters. Exec runs the top level of a program 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.

The sandbox shares nothing between evaluations or programs and leaves starlark.Universe as it is: an expression or a program names no built-in name but the dialect's 33, 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.

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 steps, and module-kit's clock with a margin over them.

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 steps, and module-kit's clock with a margin over it.

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. 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. 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.

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. 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. 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, 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 format's version counts them, reaches it. Clock is module-kit's: the wall time after which it stops the evaluation. 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 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, 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, 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 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 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 that the program's 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