vm

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package api defines the interface for the Lua VM execution engine.

This package merges the responsibilities of C's lvm.c (execution loop) and ldo.c (call/return/error handling) because they are mutually recursive. In Go, this avoids circular imports while matching the C reality.

All shared types (LuaError, Status, CIST_* flags, CallInfo, LuaState) are defined in state/api. This package only defines VM-specific signatures.

Reference: .analysis/05-vm-execution-loop.md, .analysis/04-call-return-error.md

callerror.go — Function name resolution and type error messages. Mirrors: funcnamefromcode, funcnamefromcall, luaG_callerror, luaG_typeerror from ldebug.c

Call, return, error handling, and stack management for the Lua VM.

This is the Go equivalent of C's ldo.c. It handles: - Error handling via panic/recover (Go's equivalent of setjmp/longjmp) - Stack reallocation and growth - Function call preparation and post-call cleanup - Protected calls (pcall) - Parser integration

Reference: lua-master/ldo.c, .analysis/04-call-return-error.md

VM execution loop — the heart of the Lua interpreter.

This is the Go equivalent of C's lvm.c. The core is execute(), a giant switch on opcodes that runs Lua bytecode.

Reference: lua-master/lvm.c, .analysis/05-vm-execution-loop.md

vmhelpers.go — VM helper functions (arithmetic, comparison, coercion, table access).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func APIGetTable

func APIGetTable(L *state.LuaState, t, key object.TValue, ra int)

APIGetTable performs result = t[key] with __index metamethod support. Used by the C API's lua_gettable. Writes result to L.Stack[ra].

func APISetTable

func APISetTable(L *state.LuaState, t, key, val object.TValue)

APISetTable performs t[key] = val with __newindex metamethod support. Used by the C API's lua_settable.

func Call

func Call(L *state.LuaState, funcIdx int, nResults int)

Call performs a function call. For Lua functions, calls execute. Mirrors: luaD_call / ccall in ldo.c

func CallNoYield

func CallNoYield(L *state.LuaState, funcIdx int, nResults int)

CallNoYield performs a non-yieldable function call. Mirrors: luaD_callnoyield in ldo.c

func CheckStack

func CheckStack(L *state.LuaState, n int)

CheckStack ensures at least n free stack slots, growing if needed.

func CloseTBC

func CloseTBC(L *state.LuaState, level int)

CloseTBC is the exported wrapper for closeTBC. Closes all TBC variables from L.TBCList down to (but not including) level. Uses StatusOK semantics: resets L.Top to level+1 before calling __close. Used by the API layer (lua_settop).

func CloseTBCKeepTop

func CloseTBCKeepTop(L *state.LuaState, level int)

CloseTBCKeepTop closes TBC variables with CLOSEKTOP semantics: does NOT reset L.Top, preserving values above the closed slot. Used by lua_closeslot where return values sit above TBC vars.

func CloseThread

func CloseThread(L *state.LuaState, from *state.LuaState) int

CloseThread closes all TBC variables in a coroutine and resets it. Mirrors: lua_closethread → luaE_resetthread in lstate.c:315

func Concat

func Concat(L *state.LuaState, total int)

Concat concatenates 'total' values on the stack from L.Top-total to L.Top-1.

func DumpProto

func DumpProto(p *object.Proto, strip bool) []byte

DumpProto serializes a Proto to Lua 5.5 binary chunk format. If strip is true, debug information is omitted. Mirrors: luaU_dump in ldump.c

func EqualObj

func EqualObj(L *state.LuaState, t1, t2 object.TValue) bool

EqualObj performs t1 == t2 with metamethods.

func ErrorMsg

func ErrorMsg(L *state.LuaState)

ErrorMsg calls the error handler (if set) then throws a runtime error. Mirrors: luaG_errormsg in ldebug.c The error object must already be on the stack at L.Top-1.

func FinishGet

func FinishGet(L *state.LuaState, t, key object.TValue, ra int)

FinishGet completes a table get with __index metamethod chain.

func FinishSet

func FinishSet(L *state.LuaState, t, key, val object.TValue)

FinishSet completes a table set with __newindex metamethod chain.

func FuncNameFromCode

func FuncNameFromCode(L *state.LuaState, p *object.Proto, pc int) (string, string)

funcNameFromCode examines the instruction at pc to determine the function name. Returns (kind, name) or ("", "") if unknown. Mirrors: funcnamefromcode in ldebug.c FuncNameFromCode examines the instruction at pc to determine the function name. Returns (kind, name) or ("", "") if unknown. Mirrors: funcnamefromcode in ldebug.c

func GetFuncLine

func GetFuncLine(f *object.Proto, pc int) int

GetFuncLine returns the source line corresponding to instruction 'pc' in proto 'f'. Returns -1 if no debug info is available. Mirrors: luaG_getfuncline in ldebug.c

func GetGlobalTable

func GetGlobalTable(L *state.LuaState) *table.Table

GetGlobalTable returns the global table from the registry.

func LessEqual

func LessEqual(L *state.LuaState, l, r object.TValue) bool

LessEqual performs l <= r with metamethods.

func LessThan

func LessThan(L *state.LuaState, l, r object.TValue) bool

LessThan performs l < r with metamethods.

func Load

func Load(L *state.LuaState, reader lex.LexReader, source string) int

Load compiles Lua source and pushes the resulting closure. Returns StatusOK on success, StatusErrSyntax on parse error.

func MarkTBC

func MarkTBC(L *state.LuaState, level int)

MarkTBC is the exported wrapper for markTBC. Used by the API layer (lua_toclose) to mark a stack slot as to-be-closed.

func ObjLen

func ObjLen(L *state.LuaState, ra int, rb object.TValue)

ObjLen computes #rb and stores in L.Stack[ra].

func PCall

func PCall(L *state.LuaState, funcIdx int, nResults int, errFunc int) int

func Resume

func Resume(L *state.LuaState, from *state.LuaState, nArgs int) (int, int)

Resume resumes a coroutine. Mirrors: lua_resume in ldo.c (simplified)

func RunError

func RunError(L *state.LuaState, msg string)

RunError raises a runtime error with a string message. Mirrors: luaG_runerror in ldebug.c — adds source:line: prefix for Lua frames.

func ShortSrc

func ShortSrc(source string) string

ShortSrc creates a short source name for error messages. Mirrors: luaO_chunkid in lobject.c

func UndumpProto

func UndumpProto(L *state.LuaState, data []byte, name string) (*closure.LClosure, error)

UndumpProto deserializes a Lua 5.5 binary chunk into a Proto + LClosure. Returns the LClosure ready to execute. Mirrors: luaU_undump in lundump.c

func Yield

func Yield(L *state.LuaState, nResults int)

Yield yields a coroutine. Mirrors: lua_yieldk in ldo.c (simplified)

Types

This section is empty.

Jump to

Keyboard shortcuts

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