vm

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2023 License: Apache-2.0 Imports: 21 Imported by: 4

Documentation

Overview

Package vm provides a VirtualMachine that executes compiled Risor code.

Index

Constants

View Source
const (
	MaxArgs       = 255
	MaxFrameDepth = 1024
	MaxStackDepth = 1024
	StopSignal    = -1
	MB            = 1024 * 1024
)
View Source
const DefaultFrameLocals = 8

Variables

This section is empty.

Functions

func Run added in v0.14.0

func Run(ctx context.Context, main *compiler.Code, options ...Option) (object.Object, error)

Run the given code in a new Virtual Machine and return the result.

Types

type Option

type Option func(*VirtualMachine)

Option is a configuration function for a Virtual Machine.

func WithGlobals added in v0.14.0

func WithGlobals(globals map[string]any) Option

WithGlobals provides global variables with the given names.

func WithImporter

func WithImporter(importer importer.Importer) Option

WithImporter is used to supply an Importer to the Virtual Machine.

func WithInstructionOffset

func WithInstructionOffset(offset int) Option

WithInstructionOffset sets the initial instruction offset.

func WithLimits

func WithLimits(limits limits.Limits) Option

WithLimits sets the limits for the Virtual Machine.

type VirtualMachine

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

func New

func New(main *compiler.Code, options ...Option) *VirtualMachine

New creates a new Virtual Machine.

func (*VirtualMachine) Call added in v0.14.0

func (vm *VirtualMachine) Call(ctx context.Context, fn *object.Function, args []object.Object) (object.Object, error)

Call a function with the supplied arguments. If isolation between VMs is important to you, do not provide a function here that was obtained from another VM, since it could be a closure over variables in that VM. This method should only be called after this VM stops running. Otherwise, an error is returned.

func (*VirtualMachine) Clone added in v0.15.0

func (vm *VirtualMachine) Clone() (*VirtualMachine, error)

Clone a stopped Virtual Machine. An error is returned if the Virtual Machine is running. The returned clone will have the same code, globals, modules, stack, and limits as the original. Any Risor objects present as global variables will be carried over to the clone. And since multiple clones can be created, the caller is responsible for ensuring that the global variables are not mutated, or that the mutations are safe for concurrent use. Do not use this function if you need isolation between clones, as this is not provided.

func (*VirtualMachine) Get added in v0.14.0

func (vm *VirtualMachine) Get(name string) (object.Object, error)

Get a global variable by name as a Risor Object. Returns an error if the variable can't be found.

func (*VirtualMachine) GlobalNames added in v0.14.0

func (vm *VirtualMachine) GlobalNames() []string

GlobalNames returns the names of all global variables in the active code.

func (*VirtualMachine) Run

func (vm *VirtualMachine) Run(ctx context.Context) (err error)

func (*VirtualMachine) TOS

func (vm *VirtualMachine) TOS() (object.Object, bool)

Jump to

Keyboard shortcuts

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