vms

package module
v0.0.0-...-6846fad Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetVMSymbol

func GetVMSymbol[T any](vm VM, name string) (T, error)

GetVMSymbol is a generic helper for VM.GetSymbol. It is suitable for functions mostly, since it returns value directly, not a pointer.

func GetVMSymbolPtr

func GetVMSymbolPtr[T any](vm VM, name string) (*T, error)

GetVMSymbolPtr is a generic helper for VM.GetSymbol. It is similar to GetVMSymbol, but returns a pointer to the value. Useful for variables.

func OnEvent

func OnEvent(vm VM, event string)

OnEvent calls event-related functions in VM.

func OnFrame

func OnFrame(vm VM)

OnFrame calls OnFrame function in VM.

func Register

func Register(r RuntimeInfo)

Register registers a new script VM runtime.

func WithEngine

func WithEngine(ctx context.Context, g Engine) context.Context

WithEngine stores Engine reference in the context.

Types

type BaseVM

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

func NewBaseVM

func NewBaseVM(g Engine) *BaseVM

func (*BaseVM) GetEngine

func (vm *BaseVM) GetEngine() Engine

func (*BaseVM) Stderr

func (vm *BaseVM) Stderr() Writer

func (*BaseVM) Stdout

func (vm *BaseVM) Stdout() Writer

type Engine

type Engine interface {
	// Frame returns current server tick/frame number.
	Frame() int
	// Time returns relative time from server start.
	Time() time.Duration

	// Console returns script console.
	Console(error bool) Writer
}

Engine is an interface to the game engine.

func GetEngine

func GetEngine(ctx context.Context) Engine

GetEngine loads Game from the context.

type ErrIncorrectSymbolType

type ErrIncorrectSymbolType struct {
	Name     string
	Expected reflect.Type
	Actual   reflect.Type
}

func (*ErrIncorrectSymbolType) Error

func (e *ErrIncorrectSymbolType) Error() string

type RuntimeInfo

type RuntimeInfo struct {
	// Name is a short name for the VM runtime. Must be unique.
	Name string
	// Title is a human-friendly name for the VM runtime.
	Title string
	// NewMap creates a new script VM for map scripts.
	// If there's no scripts for the map, function may return nil, nil.
	NewMap func(g Engine, maps string, name string) (VM, error)
}

RuntimeInfo is a type for registering new script runtime implementations.

func VMRuntimes

func VMRuntimes() []RuntimeInfo

VMRuntimes returns all registered VM runtimes.

type VM

type VM interface {

	// GetEngine returns Engine used by this VM.
	GetEngine() Engine
	// Stdout returns standard output used by this VM.
	Stdout() Writer
	// Stderr returns error output used by this VM.
	Stderr() Writer

	// Exec executes text as a script source code.
	Exec(s string) (reflect.Value, error)
	// ExecFile executes a script from the file or directory.
	ExecFile(path string) error
	// GetSymbol tries to find a function or variable with a given name and type.
	// If symbol is found, but type doesn't match, it returns an error.
	// If symbol is not found, it returns reflect.Value{}, false, nil.
	GetSymbol(name string, typ reflect.Type) (reflect.Value, bool, error)
	// Close the VM and free resources.
	Close() error
	// contains filtered or unexported methods
}

VM is an interface for a virtual machine running the script engine.

type Writer

type Writer interface {
	io.Writer
	io.StringWriter
}

Writer is an interface for writing text messages.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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