Documentation
¶
Overview ¶
Package opruntime is glue: a registry mapping an opcodes.Op to the Go function that implements it, plus dispatch.
It is not a virtual machine. It owns no registers, no operand stack, and no execution state; all state lives in closures the caller supplies as Handlers.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
Types ¶
type DispatchError ¶
DispatchError identifies an instruction that failed to dispatch or whose handler returned an error. Index is -1 when produced directly by Table.Dispatch outside of Run.
func (*DispatchError) Error ¶
func (e *DispatchError) Error() string
func (*DispatchError) Unwrap ¶
func (e *DispatchError) Unwrap() error
type Handler ¶
type Handler func(opcodes.Instruction) error
Handler executes a single decoded instruction. A Handler must return normally; it must not panic or call runtime.Goexit.
type RegisterError ¶
RegisterError identifies a Table.Register call that could not be applied.
func (*RegisterError) Error ¶
func (e *RegisterError) Error() string
func (*RegisterError) Unwrap ¶
func (e *RegisterError) Unwrap() error
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table maps an opcodes.Op to the Handler that implements it. opcodes.Op is a uint8, so lookup is a direct array index rather than a hash lookup.
func (*Table) Dispatch ¶
func (t *Table) Dispatch(inst opcodes.Instruction) error
Dispatch looks up the handler registered for inst.Op and calls it. It returns a DispatchError if no handler is registered or if the handler itself returns an error.
Example ¶
package main
import (
"fmt"
"github.com/apsis-io/velocity/opcodes"
"github.com/apsis-io/velocity/opruntime"
)
func main() {
const opPrint opcodes.Op = 1
table := opruntime.NewTable()
_ = table.Register(opPrint, func(inst opcodes.Instruction) error {
fmt.Println("printed", inst.A)
return nil
})
_ = table.Dispatch(opcodes.Instruction{Op: opPrint, A: 42})
}
Output: printed 42