Documentation
¶
Overview ¶
Package vm provides a VirtualMachine that executes compiled Risor code.
Index ¶
- Constants
- Variables
- func Run(ctx context.Context, main *compiler.Code, options ...Option) (object.Object, error)
- type Option
- type VirtualMachine
- func (vm *VirtualMachine) Call(ctx context.Context, fn *object.Function, args []object.Object) (result object.Object, err error)
- func (vm *VirtualMachine) Clone() (*VirtualMachine, error)
- func (vm *VirtualMachine) Get(name string) (object.Object, error)
- func (vm *VirtualMachine) GetIP() int
- func (vm *VirtualMachine) GlobalNames() []string
- func (vm *VirtualMachine) Run(ctx context.Context) (err error)
- func (vm *VirtualMachine) SetIP(value int) error
- func (vm *VirtualMachine) TOS() (object.Object, bool)
Constants ¶
const ( MaxArgs = 256 MaxFrameDepth = 1024 MaxStackDepth = 1024 StopSignal = -1 MB = 1024 * 1024 )
const DefaultFrameLocals = 8
Variables ¶
var ErrGlobalNotFound = errors.New("global not found")
Functions ¶
Types ¶
type Option ¶
type Option func(*VirtualMachine)
Option is a configuration function for a Virtual Machine.
func WithConcurrency ¶ added in v1.4.0
func WithConcurrency() Option
WithConcurrency opts into allowing the spawning of goroutines.
func WithGlobals ¶ added in v0.14.0
WithGlobals provides global variables with the given names.
func WithImporter ¶
WithImporter is used to supply an Importer to the Virtual Machine.
func WithInstructionOffset ¶
WithInstructionOffset sets the initial instruction offset.
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, ) (result object.Object, err error)
Call a function with the given arguments. If isolation between VMs is important to you, do not provide a function that obtained from another VM, since it could be a closure over variables there. If this VM is already running, an error is returned.
func (*VirtualMachine) Clone ¶ added in v0.15.0
func (vm *VirtualMachine) Clone() (*VirtualMachine, error)
Clone the Virtual Machine. The returned clone has its own independent frame stack and data stack, but shares the loaded modules and global variables with the original VM.
Clone is designed to be safe to call from any goroutine.
The caller and the user code that runs are responsible for thread safety when using modules and global variables, since concurrently executing cloned VMs can modify the same objects.
The returned clone has an empty frame stack and data stack, which makes this most useful for cloning a VM then using vm.Call() to call a function, rather than calling vm.Run() on the clone, which would start execution at the beginning of the main entrypoint.
Do not use Clone if you want a strict guarantee of isolation between VMs.
If an OS was provided to the original VM via the WithOS option, it will be copied into the clone. Otherwise, the clone will use the standard OS fallback behavior of using any OS present in the context or NewSimpleOS as a default.
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.
func (*VirtualMachine) GetIP ¶ added in v1.1.0
func (vm *VirtualMachine) GetIP() int
GetIP returns the current instruction pointer.
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) SetIP ¶ added in v1.1.0
func (vm *VirtualMachine) SetIP(value int) error
SetIP sets the instruction pointer on a stopped VM. If the VM is running, an error is returned.
func (*VirtualMachine) TOS ¶
func (vm *VirtualMachine) TOS() (object.Object, bool)
TOS returns the top-of-stack object if there is one, without modifying the stack. The returned bool value indicates whether there was a valid TOS. This only works on a stopped VM. If the VM is running, (nil, false) is returned.