vm

package
v1.46.0 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2025 License: Apache-2.0 Imports: 44 Imported by: 0

Documentation

Overview

vm implements Rego interpreter evaluating compiled Rego IR.

Index

Constants

View Source
const (
	RegoCompileName = "rego.compile"
)

Variables

View Source
var (
	ErrInvalidData = errors.New("invalid data")      // Unsupported data type detected.
	ErrIllegalIter = errors.New("illegal iteration") // Illegal iteration over persisted table detected.

)
View Source
var (
	ErrVarAssignConflict         = errors.New("var assignment conflict")
	ErrObjectInsertConflict      = errors.New("object insert conflict")
	ErrFunctionCallToData        = errors.New("function call to data")
	ErrInvalidExecutable         = errors.New("invalid executable")
	ErrQueryNotFound             = errors.New("query not found")
	ErrInstructionsLimitExceeded = errors.New("instructions limit exceeded")

	DefaultLimits = Limits{
		Instructions: 100000000,
	}
)

Functions

func BuiltinRegoEval

func BuiltinRegoEval(bctx topdown.BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error

Types

type BoolConst

type BoolConst bool

type CacheConfig

type CacheConfig struct {
	Enabled    bool     `json:"enabled"`
	InputPaths []string `json:"input_paths"`
	TTL        string   `json:"ttl"`
}

CacheConfig is under "extra/eval_cache" in OPA config.

type CacheHook

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

CacheHook receives OPA configuration change callbacks and holds the current cache configuration.

func NewCacheHook

func NewCacheHook() *CacheHook

func (*CacheHook) OnConfig

func (c *CacheHook) OnConfig(ctx context.Context, conf *config.Config) (*config.Config, error)

func (*CacheHook) OnConfigDiscovery

func (c *CacheHook) OnConfigDiscovery(ctx context.Context, conf *config.Config) (*config.Config, error)

type CallNamespace

type CallNamespace interface {
	Call(ctx context.Context, args []*any, caller *State) (any, bool, bool, error)
}

type Compiler

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

func NewCompiler

func NewCompiler() *Compiler

func (*Compiler) Compile

func (c *Compiler) Compile() (Executable, error)

Compile turns the IR into VM executable instructions

func (*Compiler) WithBuiltins

func (c *Compiler) WithBuiltins(bis map[string]*topdown.Builtin) *Compiler

func (*Compiler) WithPolicy

func (c *Compiler) WithPolicy(policy *ir.Policy) *Compiler

type DataOperations

type DataOperations struct{}

func (*DataOperations) ArrayAppend

func (*DataOperations) ArrayAppend(ctx context.Context, array any, value any) (any, bool, error)

func (*DataOperations) Call

func (*DataOperations) Call(ctx context.Context, value any, args []*any, caller *State) (any, bool, bool, error)

func (*DataOperations) CopyShallow

func (*DataOperations) CopyShallow(ctx context.Context, value any) (any, error)

func (*DataOperations) Equal

func (*DataOperations) Equal(ctx context.Context, a, b any) (bool, error)

func (*DataOperations) FromInterface

func (o *DataOperations) FromInterface(ctx context.Context, x any) (fjson.Json, error)

FromInterface converts a golang native data to internal representation.

func (*DataOperations) Get

func (*DataOperations) Get(ctx context.Context, value, key any) (any, bool, error)

func (*DataOperations) GetCall

func (*DataOperations) GetCall(ctx context.Context, value, key any) (any, bool, error)

func (*DataOperations) IsArray

func (*DataOperations) IsArray(ctx context.Context, v any) (bool, error)

func (*DataOperations) IsCall

func (*DataOperations) IsCall(value any) (bool, error)

func (*DataOperations) IsObject

func (*DataOperations) IsObject(ctx context.Context, v any) (bool, error)

func (*DataOperations) IsSet

func (*DataOperations) IsSet(ctx context.Context, v any) (bool, error)

func (*DataOperations) Iter

func (o *DataOperations) Iter(ctx context.Context, v any, f func(key, value any) (bool, error)) error

func (*DataOperations) Len

func (o *DataOperations) Len(ctx context.Context, v any) (fjson.Json, error)

func (*DataOperations) MakeArray

func (*DataOperations) MakeArray(capacity int32) fjson.Array

func (*DataOperations) MakeBoolean

func (*DataOperations) MakeBoolean(v bool) fjson.Json

func (*DataOperations) MakeNull

func (*DataOperations) MakeNull() fjson.Json

func (*DataOperations) MakeNumberFloat

func (*DataOperations) MakeNumberFloat(f float64) fjson.Float

func (*DataOperations) MakeNumberInt

func (o *DataOperations) MakeNumberInt(i int64) fjson.Json

func (*DataOperations) MakeNumberRef

func (*DataOperations) MakeNumberRef(n any) fjson.Float

func (*DataOperations) MakeNumberZero

func (o *DataOperations) MakeNumberZero() fjson.Json

func (*DataOperations) MakeObject

func (*DataOperations) MakeObject() fjson.Object2

func (*DataOperations) MakeSet

func (*DataOperations) MakeSet() fjson.Set

func (*DataOperations) MakeString

func (*DataOperations) MakeString(v string) *fjson.String

func (*DataOperations) ObjectGet

func (*DataOperations) ObjectGet(ctx context.Context, object, key any) (any, bool, error)

func (*DataOperations) ObjectInsert

func (*DataOperations) ObjectInsert(ctx context.Context, object, key, value any) (any, bool, error)

func (*DataOperations) ObjectMerge

func (o *DataOperations) ObjectMerge(ctx context.Context, a, b any) (any, error)

func (*DataOperations) SetAdd

func (*DataOperations) SetAdd(ctx context.Context, set, value any) (any, error)

func (*DataOperations) ToAST

func (o *DataOperations) ToAST(ctx context.Context, v any) (ast.Value, error)

func (*DataOperations) ToInterface

func (o *DataOperations) ToInterface(ctx context.Context, v any) (any, error)

ToInterface converts the data to golang native presentation.

type EvalOpts

type EvalOpts struct {
	Time                        time.Time
	PrintHook                   print.Hook
	Metrics                     metrics.Metrics
	Seed                        io.Reader
	Runtime                     any
	InterQueryBuiltinCache      cache.InterQueryCache
	InterQueryBuiltinValueCache cache.InterQueryValueCache
	Input                       *any // Input as golang native data.
	Limits                      *Limits
	Cache                       builtins.Cache
	NDBCache                    builtins.NDBCache
	Capabilities                *ast.Capabilities
	BuiltinFuncs                map[string]*topdown.Builtin
	TracingOpts                 tracing.Options
	StrictBuiltinErrors         bool
	ExternalCancel              topdown.Cancel
	QueryTracers                []topdown.QueryTracer
}

func EvalOptsFromContext

func EvalOptsFromContext(ctx context.Context) EvalOpts

type Executable

type Executable []byte

func (Executable) Functions

func (e Executable) Functions() functions

func (Executable) IsValid

func (e Executable) IsValid() bool

func (Executable) Plans

func (e Executable) Plans() plans

func (Executable) Strings

func (e Executable) Strings() strings

func (Executable) Write

func (Executable) Write(strings []byte, functions []byte, plans []byte) []byte

type GetCallNamespace

type GetCallNamespace interface {
	GetCall(ctx context.Context, key any) (any, bool, error)
}

type Globals

type Globals struct {
	Time                        time.Time
	Metrics                     metrics.Metrics
	PrintHook                   print.Hook
	InterQueryBuiltinCache      cache.InterQueryCache
	InterQueryBuiltinValueCache cache.InterQueryValueCache
	Ctx                         context.Context
	Seed                        io.Reader

	Cache builtins.Cache

	BuiltinFuncs  map[string]*topdown.Builtin
	Capabilities  *ast.Capabilities
	Input         *any
	NDBCache      builtins.NDBCache
	Runtime       *ast.Term
	ResultSet     fjson.Set
	BuiltinErrors []error
	TracingOpts   tracing.Options

	Limits              Limits
	StrictBuiltinErrors bool
	IntermediateResults map[int]any
	QueryTracers        []topdown.QueryTracer
	// contains filtered or unexported fields
}

type IterableObject

type IterableObject interface {
	Get(ctx context.Context, key any) (any, bool, error)
	Iter(ctx context.Context, f func(key, value any) (bool, error)) error
}

IterableObject is the interface for external, read-only (probably persisted) object implementations.

type Limits

type Limits struct {
	Instructions int64
}

type Local

type Local int
const (
	// Input is the local variable that refers to the global input document.
	Input  Local = iota
	Data         // Data is the local variable that refers to the global data document.
	Unused       // Unused is the free local variable that can be allocated in a plan.
)

type LocalOrConst

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

func NewBoolConst

func NewBoolConst(v bool) LocalOrConst

func NewLocal

func NewLocal(v int) LocalOrConst

func NewStringIndexConst

func NewStringIndexConst(v int) LocalOrConst

func (*LocalOrConst) BoolConst

func (l *LocalOrConst) BoolConst() BoolConst

func (*LocalOrConst) Local

func (l *LocalOrConst) Local() Local

func (*LocalOrConst) StringIndexConst

func (l *LocalOrConst) StringIndexConst() StringIndexConst

func (*LocalOrConst) Type

func (l *LocalOrConst) Type() int

type Locals

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

func (*Locals) SetReturn

func (l *Locals) SetReturn(source Local, defined bool)

type N

type N interface {
	~[]byte
	// contains filtered or unexported methods
}

type State

type State struct {
	Globals *Globals
	// contains filtered or unexported fields
}

State holds all the evaluation state and is passed along the statements as the evaluation progresses.

func (*State) Args

func (s *State) Args(n int) []Value

func (*State) DataGet

func (s *State) DataGet(ctx context.Context, value, key any) (any, bool, error)

func (*State) FindByPath

func (s *State) FindByPath(path []string) (function, int)

func (*State) Func

func (s *State) Func(f int) function

func (*State) Instr

func (s *State) Instr(i int64) error

func (*State) IsData

func (s *State) IsData(l Local) bool

func (*State) IsDefined

func (s *State) IsDefined(v LocalOrConst) bool

func (*State) IsLocalDefined

func (s *State) IsLocalDefined(v Local) bool

func (*State) Local

func (s *State) Local(v Local) Value

func (*State) MemoizeGet

func (s *State) MemoizeGet(idx int, args []Value) (Value, bool)

func (*State) MemoizeInsert

func (s *State) MemoizeInsert(idx int, args []Value, value Value)

func (*State) MemoizePop

func (s *State) MemoizePop()

func (*State) MemoizePush

func (s *State) MemoizePush()

func (*State) New

func (s *State) New() *State

func (*State) Release

func (s *State) Release()

func (*State) Return

func (s *State) Return() (Local, bool)

Return returns the variable holding the function result.

func (*State) Set

func (s *State) Set(target Local, source LocalOrConst)

func (*State) SetData

func (s *State) SetData(l Local)

func (*State) SetLocal

func (s *State) SetLocal(target Local, source Local)

func (*State) SetReturn

func (s *State) SetReturn(source Local)

func (*State) SetReturnValue

func (s *State) SetReturnValue(source Local, value Value)

func (*State) SetValue

func (s *State) SetValue(target Local, value Value)

func (*State) String

func (s *State) String(v StringIndexConst) *fjson.String

func (*State) Unset

func (s *State) Unset(target Local)

func (*State) Value

func (s *State) Value(v LocalOrConst) Value

func (*State) ValueOps

func (s *State) ValueOps() *DataOperations

type Statistics

type Statistics struct {
	EvalInstructions   int64 `json:"eval_instructions"`
	VirtualCacheHits   int64 `json:"virtual_cache_hits"`
	VirtualCacheMisses int64 `json:"virtual_cache_misses"`
}

func StatisticsGet

func StatisticsGet(ctx context.Context) *Statistics

func WithStatistics

func WithStatistics(ctx context.Context) (*Statistics, context.Context)

func (*Statistics) String

func (s *Statistics) String() string

type StringIndexConst

type StringIndexConst int

type VM

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

func NewVM

func NewVM() *VM

func (*VM) Eval

func (vm *VM) Eval(ctx context.Context, name string, opts EvalOpts) (ast.Value, error)

Eval evaluates the query with the options given. Eval is thread safe. Return value is of ast.Value for now.

func (*VM) WithDataJSON

func (vm *VM) WithDataJSON(data any) *VM

WithDataJSON stores golang native data for the evaluation to use as 'data.'.

func (*VM) WithDataNamespace

func (vm *VM) WithDataNamespace(data any) *VM

WithDataNamespace hooks an external namespace implementation to use as 'data.'.

func (*VM) WithExecutable

func (vm *VM) WithExecutable(executable Executable) *VM

type Value

type Value any

Jump to

Keyboard shortcuts

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