lua

package
v0.0.0-...-182b3b8 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package lua is a dependency-free Lua 5.1 implementation written in Go.

Index

Constants

This section is empty.

Variables

View Source
var False = Value{/* contains filtered or unexported fields */}
View Source
var Nil = Value{}
View Source
var True = Value{/* contains filtered or unexported fields */}

Functions

func LoadScript

func LoadScript(source, name string, globals map[string]Value) (*Script, []Value, error)

Types

type Error

type Error struct {
	Source string
	Line   int
	Msg    string
}

func (*Error) Error

func (e *Error) Error() string

type Function

type Function struct {
	Proto         *Prototype
	Native        NativeFunction
	NativeNumber1 func(float64) float64
	NativeNumber2 func(float64, float64) float64
	Env           *Table
	Up            []*cell
	// contains filtered or unexported fields
}

type Instruction

type Instruction uint32

Instruction is a compact fixed-width register instruction. ABC operands cover hot arithmetic/table operations; Bx is used for constants and jumps.

type Kind

type Kind uint8

Kind is deliberately byte-sized: Value is copied in every VM instruction.

const (
	NilKind Kind = iota
	BoolKind
	NumberKind
	StringKind
	TableKind
	FunctionKind
	UserdataKind
	ThreadKind
)

type LocalVariableInfo

type LocalVariableInfo struct {
	Name     string
	Register int
	StartPC  int
	EndPC    int
}

type NativeFunction

type NativeFunction func(*State, []Value) ([]Value, error)

type Prototype

type Prototype struct {
	Source           string
	DefinedLine      int
	LastDefinedLine  int
	EndLine          int
	Code             []Instruction
	Constants        []Value
	Children         []*Prototype
	Upvalues         []UpvalueDescriptor
	UpvalueNames     []string
	RegisterNames    map[int]string
	LocalVariables   []LocalVariableInfo
	Lines            []int
	LiveRegisters    []uint8
	Parameters       uint8
	MaxRegisters     uint8
	Vararg           bool
	UsesDots         bool
	Captured         bool
	Fast             bool
	NumericPure      bool
	NumericCode      []Instruction
	NumericRegisters uint8
	NumericFormula   *numericFormula
	FieldCaches      []fieldCache
	ExtraConstants   map[int]int
	ErrorContexts    map[int][]string
}

func Compile

func Compile(source, name string) (*Prototype, error)

type Script

type Script struct {
	State  *State
	Module Value
	// contains filtered or unexported fields
}

Script owns a persistent VM and an optional module table returned by its chunk. Calls are serialized because a State intentionally reuses stacks.

func (*Script) Call

func (s *Script) Call(name string, args ...Value) ([]Value, error)

func (*Script) Get

func (s *Script) Get(name string) Value

func (*Script) Inspect

func (s *Script) Inspect() string

func (*Script) Set

func (s *Script) Set(name string, value Value)

type State

type State struct {
	Output io.Writer
	// contains filtered or unexported fields
}

func NewState

func NewState() *State

func (*State) Call

func (s *State) Call(function Value, args ...Value) ([]Value, error)

Call invokes a Lua function with any number of arguments and returns an owned slice of every result. This generality has a cost: dispatch can route to the general VM, which stores a callee's varargs slice directly in its frame, so Go's escape analysis must (correctly) treat args as escaping for every call through this function, not just vararg ones — escape analysis is a static property of the function body, not of a particular call's runtime arguments. The variadic argument slice and the returned result slice therefore both allocate. For a hot loop, use CallInto (reused argument/result buffers, zero allocation) or CallNumber2 (a specialized allocation-free path for two-argument numeric functions) instead.

func (*State) CallInto

func (s *State) CallInto(function Value, args, results []Value) (int, error)

CallInto invokes a Lua function without allocating a result slice. It returns the total result count and copies as many values as fit in results. Callers that need every result should provide sufficient destination space.

func (*State) CallNumber2

func (s *State) CallNumber2(function Value, a, b float64) (float64, error)

CallNumber2 is the allocation-free embedding path for the common numeric binary-function case measured by the embedding benchmark.

func (*State) DoString

func (s *State) DoString(source string) ([]Value, error)

func (*State) GetGlobal

func (s *State) GetGlobal(name string) Value

func (*State) Globals

func (s *State) Globals() *Table

func (*State) Load

func (s *State) Load(source, name string) (Value, error)

func (*State) SetGlobal

func (s *State) SetGlobal(name string, value Value)

func (*State) String

func (s *State) String() string

type Table

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

Table is a hybrid array/hash table. Dense positive integer keys avoid hashing; strings have a dedicated map because they dominate object fields. The generic map handles the uncommon boolean/reference/non-dense keys.

func NewTable

func NewTable(arrayHint, hashHint int) *Table

func (*Table) ForEach

func (t *Table) ForEach(fn func(Value, Value) bool)

ForEach visits live entries. Iteration order is intentionally unspecified, matching Lua's next/pairs contract.

func (*Table) Get

func (t *Table) Get(key Value) Value

func (*Table) GetString

func (t *Table) GetString(name string) Value

func (*Table) Len

func (t *Table) Len() int

func (*Table) Metatable

func (t *Table) Metatable() *Table

func (*Table) Next

func (t *Table) Next(previous Value) (Value, Value, bool)

func (*Table) Set

func (t *Table) Set(key, value Value) error

func (*Table) SetMetatable

func (t *Table) SetMetatable(meta *Table)

func (*Table) SetString

func (t *Table) SetString(name string, value Value)

type Thread

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

Thread is a Lua coroutine. A suspended coroutine retains its ordinary VM call stack in a parked Go goroutine; resume and yield rendezvous directly, so no C stack, cgo shim, or copied continuation is involved.

func (*Thread) Resume

func (t *Thread) Resume(args []Value) ([]Value, error)

func (*Thread) Yield

func (t *Thread) Yield(values []Value) ([]Value, error)

type UpvalueDescriptor

type UpvalueDescriptor struct {
	Index uint8
	Local bool
}

type Value

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

Value is a compact tagged pair. bits stores numbers, booleans, or string length; ptr stores reference objects or string data. Keeping every Lua value to three machine words materially reduces register and table traffic.

func Bool

func Bool(v bool) Value

func FunctionValue

func FunctionValue(v *Function) Value

func Native

func Native(fn NativeFunction) Value

func NativeBinary

func NativeBinary(fn func(float64, float64) float64) Value

NativeBinary registers the allocation-free typed path for a Go numeric callback called from Lua.

func Number

func Number(v float64) Value

func String

func String(v string) Value

func TableValue

func TableValue(v *Table) Value

func ThreadValue

func ThreadValue(v *Thread) Value

func Userdata

func Userdata(v any) Value

func UserdataWithMetatable

func UserdataWithMetatable(v any, meta *Table) Value

func (Value) Bool

func (v Value) Bool() bool

func (Value) Function

func (v Value) Function() *Function

func (Value) Interface

func (v Value) Interface() any

func (Value) Kind

func (v Value) Kind() Kind

func (Value) Number

func (v Value) Number() float64

func (Value) Repr

func (v Value) Repr() string

func (Value) StringValue

func (v Value) StringValue() string

func (Value) Table

func (v Value) Table() *Table

func (Value) Thread

func (v Value) Thread() *Thread

func (Value) Truthy

func (v Value) Truthy() bool

func (Value) TypeName

func (v Value) TypeName() string

func (Value) UserdataValue

func (v Value) UserdataValue() any

Directories

Path Synopsis
Command examples demonstrates embedding the native Go Lua plugin both directly (github.com/oarkflow/interpreter/plugins/lua) and through the host interpreter's SPL layer.
Command examples demonstrates embedding the native Go Lua plugin both directly (github.com/oarkflow/interpreter/plugins/lua) and through the host interpreter's SPL layer.

Jump to

Keyboard shortcuts

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