lua

package
v0.0.0-...-e552112 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2024 License: Unlicense Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	JitVersion   = "LuaJIT 2.1.1724232689"
	JitCopyright = "Copyright (C) 2005-2023 Mike Pall"
)
View Source
const (
	ModeOff   = JitMode(0x0000)
	ModeOn    = JitMode(0x0100)
	ModeFlush = JitMode(0x0200)
	ModeMask  = JitMode(0x00ff)
)
View Source
const (
	CoroutineLibName = "coroutine"
	MathLibName      = "math"
	StringLibName    = "string"
	TableLibName     = "table"
	IoLibName        = "io"
	OsLibName        = "os"
	LoadLibName      = "package"
	DbLibName        = "debug"
	BitLibName       = "bit"
	JitLibName       = "jit"
	FfiLibName       = "ffi"
)
View Source
const (
	Version    = "Lua 5.1"
	Release    = "Lua 5.1.4"
	VersionNum = 501
	Copyright  = "Copyright (C) 1994-2008 Lua.org, PUC-Rio"
	Authors    = "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
)
View Source
const (
	MultRet       = -1
	RegistryIndex = -10000
	EnvironIndex  = -10001
	GlobalsIndex  = -10002
)
View Source
const (
	StatusOk    = 0
	StatusYield = 1
)
View Source
const (
	ErrRun    = 2 // A runtime error
	ErrSyntax = 3 // Syntax error during pre-compilation
	ErrMem    = 4 // Memory allocation error
	ErrErr    = 5 // Error while running the error handler function
)
View Source
const (
	TNone          = T(-1)
	TNil           = T(0)
	TBoolean       = T(1)
	TLightUserdata = T(2)
	TNumber        = T(3)
	TString        = T(4)
	TTable         = T(5)
	TFunction      = T(6)
	TUserdata      = T(7)
	TThread        = T(8)
)
View Source
const (
	GCStop       = GCMode(0) // Stops the garbage collector
	GCRestart    = GCMode(1) // Restarts the garbage collector
	GCCollect    = GCMode(2) // Performs a full garbage collection cycle
	GCCount      = GCMode(3) // Returns the current amount of memory (in Kbytes) in use
	GCCountB     = GCMode(4) // Returns the remainder of dividing the current memory in use by 1024
	GCStep       = GCMode(5) // Performs an incremental step of garbage collection
	GCSetPause   = GCMode(6) // Sets data as the new value for the pause of the collector
	GCSetStepMul = GCMode(7) // Sets data as the new value for the step multiplier of the collector
	GCIsRunning  = GCMode(9) // Returns if the garbage collector is running
)
View Source
const (
	ErrFile = ErrErr + 1 // A file cannot be open/read
)
View Source
const (
	MinStack = 20
)
View Source
const Signature = "\033Lua"

Mark for precompiled code.

Variables

This section is empty.

Functions

func ArgCheck

func ArgCheck(L State, cond bool, numarg int, extramsg string) bool

ArgCheck checks whether cond is true. If not, raises an error with the following message, where func is retrieved from the call stack:

bad argument #<narg> to <func> (<extramsg>)

func ArgError

func ArgError(L State, numarg int, extramsg string) int

ArgError raises an error with the following message:

bad argument #<narg> to <func> (<extramsg>)

Where func is retrieved from the call stack. This function never returns, but it is an idiom to use it in [CFunction]s as a return.

func Call

func Call(L State, nargs, nresults int)

Call calls a function unprotected.

func CallMeta

func CallMeta(L State, obj int, e string) int

CallMeta calls a metamethod.

func CheckStack

func CheckStack(L State, sz int) int

CheckStack ensures that there are at least extra free stack slots in the stack. It returns false if it cannot grow the stack to that size. This function never shrinks the stack; if the stack is already larger than the new size, it is left unchanged.

func CheckString

func CheckString(L State, numarg int) string

CheckString checks whether the function argument numarg is a string and returns its string.

func Close

func Close(L State)

Close destroys all objects in the given Lua state (calling the corresponding garbage-collection metamethods, if any) and frees all dynamic memory used by this state

func Concat

func Concat(L State, n int)

Concat concatenates the n values at the top of the stack, pops them, and leaves the result at the top. If n is 1, the result is the single value on the stack (that is, the function does nothing); if n is 0, the result is the empty string.

Concatenation is performed following the usual semantics of Lua.

func CreateTable

func CreateTable(L State, narr int, nrec int)

CreateTable creates a new empty table and pushes it onto the stack. The new table has space pre-allocated for narr array elements and nrec non-array elements.

This pre-allocation is useful when you know exactly how many elements the table will have. Otherwise you can use the function NewTable.

func Equal

func Equal(L State, index1, index2 int) bool

Equals returns true if the two values in acceptable indices index1 and index2 are equal, following the semantics of the Lua == operator (that is, may call metamethods). Otherwise returns false.

Also returns false if any of the indices is non valid.

func Error

func Error(L State) int

Error generates a Lua error.

The error message (which can actually be a Lua value of any type) must be on the stack top.

func GC

func GC(L State, what GCMode, data int) int

GC controls the garbage collector.

func GetFEnv

func GetFEnv(L State, idx int)

GetFEnv pushes onto the stack the environment table of the value at the given index.

func GetField

func GetField(L State, idx int, k string)

GetField pushes onto the stack the value t[k], where t is the value at the given valid index.

func GetGCCount

func GetGCCount(L State) int

GetGCCount returns the current amount of memory (in Kbytes) in use by Lua.

func GetGlobal

func GetGlobal(L State, s string)

GetGlobal pushes onto the stack the value of the global name.

func GetMetaField

func GetMetaField(L State, obj int, e string) bool

GetMetaField pushes onto the stack the field e from the metatable of the object at index obj. If the object does not have a metatable, or if the metatable does not have this field, returns false and pushes nothing.

func GetMetaTableFor

func GetMetaTableFor(L State, name string)

GetMetaTableFor pushes onto the stack the metatable associated with name in the registry.

func GetMetatable

func GetMetatable(L State, objindex int) bool

GetMetatable pushes onto the stack the metatable of the value at the given acceptable index. If the index is not valid, or if the value does not have a metatable, the function returns false and pushes nothing on the stack.

func GetRegistry

func GetRegistry(L State)

func GetTable

func GetTable(L State, idx int)

GetTable pushes onto the stack the value t[k], where t is the value at the given valid index and k is the value at the top of the stack.

func GetTop

func GetTop(L State) int

GetTop returns the index of the top element in the stack.

Because indices start at 1, this result is equal to the number of elements in the stack (and so 0 means an empty stack).

func Insert

func Insert(L State, idx int)

Insert moves the top element into the given valid index, shifting up the elements above this index to open space. Cannot be called with a pseudo-index, because a pseudo-index is not an actual stack position.

func IsBoolean

func IsBoolean(L State, n int) bool

IsBoolean returns if the given acceptable index is a boolean.

func IsCFunction

func IsCFunction(L State, idx int) bool

IsCFunction returns true if the value at the given acceptable index is a C function, and false otherwise.

func IsFunction

func IsFunction(L State, n int) bool

IsTable returns if the value at the given acceptable index is a function.

func IsLightUserdata

func IsLightUserdata(L State, n int) bool

IsLightUserdata returns if the value at the given acceptable index is a userdata (either full or light).

func IsNil

func IsNil(L State, n int) bool

IsNil returns if the given acceptable index is nil.

func IsNone

func IsNone(L State, n int) bool

IsNone returns if the given acceptable index is not valid (that is, it refers to an element outside the current stack).

func IsNoneOrNil

func IsNoneOrNil(L State, n int) bool

IsNoneOrNil returns if the given acceptable index is not valid (that is, it refers to an element outside the current stack) or if the value at this index is nil.

func IsNumber

func IsNumber(L State, idx int) bool

IsNumber returns true if the value at the given acceptable index is a number or a string convertible to a number, and false otherwise.

func IsString

func IsString(L State, idx int) bool

IsString returns true if the value at the given acceptable index is a string or a number (which is always convertible to a string), and false otherwise.

func IsTable

func IsTable(L State, n int) bool

IsTable returns if the value at the given acceptable index is a table.

func IsThread

func IsThread(L State, n int) bool

IsThread returns if the given acceptable index is a thread.

func IsUserdata

func IsUserdata(L State, idx int) bool

IsUserdata returns true if the value at the given acceptable index is a userdata (either full or light), and false otherwise.

func LessThan

func LessThan(L State, index1, index2 int) bool

LessThan returns true if the value at acceptable index index1 is smaller than the value at acceptable index index2, following the semantics of the Lua < operator (that is, may call metamethods). Otherwise returns false.

Also returns false if any of the indices is non valid.

func LoadString

func LoadString(L State, s string) int

LoadString loads a string as a Lua chunk.

This function returns the same results as Load.

func NewTable

func NewTable(L State)

NewTable creates a new empty table and pushes it onto the stack.

func NewUserdata

func NewUserdata(L State, sz int) uintptr

NewUserdata allocates a new block of memory with the given size, pushes onto the stack a new full userdata with the block address, and returns this address.

func Next

func Next(L State, idx int) bool

Next pops a key from the stack, and pushes a key-value pair from the table at the given index (the "next" pair after the given key).

If there are no more elements in the table, then Next returns false (and pushes nothing).

func ObjLen

func ObjLen(L State, idx int) int

ObjLen returns the "length" of the value at the given acceptable index:

  • For strings, this is the string length
  • For tables, this is the result of the length operator ('#');
  • For userdata, this is the size of the block of memory allocated for the userdata;
  • For other values, it is 0.

func OpenBase

func OpenBase(L State) int

OpenBase opens the base library into the given state.

func OpenBit

func OpenBit(L State) int

OpenBit opens the bit library into the given state.

func OpenDebug

func OpenDebug(L State) int

OpenDebug opens the debug library into the given state.

func OpenFfi

func OpenFfi(L State) int

OpenFfi opens the ffi library into the given state.

func OpenIo

func OpenIo(L State) int

OpenIo opens the io library into the given state.

func OpenJit

func OpenJit(L State) int

OpenJit opens the jit library into the given state.

func OpenLibs

func OpenLibs(L State)

OpenLibs opens all standard Lua libraries into the given state.

func OpenMath

func OpenMath(L State) int

OpenMath opens the math library into the given state.

func OpenOs

func OpenOs(L State) int

OpenOs opens the os library into the given state.

func OpenPacakge

func OpenPacakge(L State) int

OpenPackage opens the package library into the given state.

func OpenString

func OpenString(L State) int

OpenString opens the string library into the given state.

func OpenStringBuffer

func OpenStringBuffer(L State) int

OpenStringBuilder opens the string builder library into the given state.

func OpenTable

func OpenTable(L State) int

OpenTable opens the table library into the given state.

func OptString

func OptString(L State, numarg int, d string) string

func PCall

func PCall(L State, nargs, nresults, errfunc int) int

PCall calls a function in protected mode.

If errfunc is 0, then the error message returned on the stack is exactly the original error message.

Otherwise, errfunc is the stack index of an error handler function. (In the current implementation, this index cannot be a pseudo-index.) In case of runtime errors, this function will be called with the error message and its return value will be the message returned on the stack by PCall.

func Pop

func Pop(L State, n int)

Pop pops n elements from the stack.

func ProfileDumpStack

func ProfileDumpStack(L State, fmt string, depth int, len *uint) string

ProfileDumpStack allows taking stack dumps in an effecient manner.

func ProfileStart

func ProfileStart(L State, mode string, cb ProfileCallback, data uintptr)

ProfileStart starts the profiler.

func ProfileStop

func ProfileStop(L State)

ProfileStop stops the profiler.

func PushBoolean

func PushBoolean(L State, b bool)

PushBoolean pushes a boolean value with value b onto the stack.

func PushClosure

func PushClosure(L State, fn CFunction, n int)

PushClosure pushes a new closure onto the stack.

The maximum value for n is 255.

func PushFString

func PushFString(L State, fmt string, args ...any) string

PushFString pushes onto the stack a formatted string and returns the string.

The conversion specifiers are quite restricted. There are no flags, widths, or precisions. The conversion specifiers can only be:

  • '%%' (inserts a '%' in the string)
  • '%s' (inserts a zero-terminated string, with no size restrictions)
  • '%f' (inserts a Number)
  • '%p' (inserts a pointer as a hexadecimal numeral)
  • '%d' (inserts an Integer)
  • '%c' (inserts an Integer as a character)

func PushInteger

func PushInteger(L State, n Integer)

PushInteger pushes a number with value n onto the stack.

func PushLString

func PushLString(L State, s string, l int)

PushLString pushes a string with the value s and the size len onto the stack.

Lua makes (or reuses) an internal copy of the given string.

func PushLightUserdata

func PushLightUserdata(L State, p uintptr)

PushLightUserdata pushes a light userdata onto the stack.

func PushNil

func PushNil(L State)

PushNil pushes a nil value onto the stack.

func PushNumber

func PushNumber(L State, n Number)

PushNumber pushes a number with value n onto the stack.

func PushString

func PushString(L State, s string)

PushString pushes a string with the value s onto the stack.

Lua makes (or reuses) an internal copy of the given string.

func PushThread

func PushThread(L State) bool

PushThread pushes the thread represented by L onto the stack. Returns true if this thread is the main thread of its state.

func PushValue

func PushValue(L State, idx int)

PushValue pushes a copy of the element at the given valid index onto the stack.

func RawEqual

func RawEqual(L State, index1, index2 int) bool

RawEqual returns true if the two values in acceptable indices index1 and index2 are primitively equal (that is, without calling metamethods). Otherwise returns false.

Also returns false if any of the indices are non valid.

func RawGet

func RawGet(L State, idx int)

RawGet similar to GetTable, but does a raw access (i.e., without metamethods).

func RawGetI

func RawGetI(L State, idx, n int)

RawGetI pushes onto the stack the value t[n], where t is the value at the given valid index.

The access is raw; that is, it does not invoke metamethods.

func RawSet

func RawSet(L State, idx int)

RawSet similar to SetTable, but does a raw assignment (i.e., without metamethods).

func RawSetI

func RawSetI(L State, idx, n int)

RawSetI does the equivalent of t[n] = v, where t is the value at the given valid index, and v is the value at the top of the stack.

This function pops the value from the stack. The assignment is raw; that is, it does not invoke metamethods.

func Register

func Register(L State, libname string, l []LReg)

Register opens a library.

When called with libname equal to nil, it simply registers all functions in the list l into the table on the top of the stack.

func Remove

func Remove(L State, idx int)

Remove removes the element at the given valid index, shifting down the elements above this index to fill the gap. Cannot be called with a pseudo-index, because a pseudo-index is not an actual stack position.

func Replace

func Replace(L State, idx int)

Replace moves the top element into the given position (and pops it), without shifting any element (therefore replacing the value at the given position).

func Resume

func Resume(L State, narg int) int

Resume starts and resumes a coroutine in a given thread.

func SetFEnv

func SetFEnv(L State, idx int) bool

SetFEnv pops a table from the stack and sets it as the new environment for the value at the given index.

If the value at the given index is neither a function nor a thread nor a userdata, SetFEnv returns false. Otherwise it returns true.

func SetField

func SetField(L State, idx int, k string)

SetField does the equivalent to t[k] = v, where t is the value at the given valid index, and v is the value at the top of the stack.

This function pops the value from the stack. As in Lua, this function may trigger a metamethod for the "newindex" event.

func SetGlobal

func SetGlobal(L State, s string)

SetGlobal pops a value from the stack and sets it as the new value of global name.

func SetMetatable

func SetMetatable(L State, objindex int) int

SetMetatable pops a table from the stack and sets it as the new metatable for the value at the given acceptable index.

func SetMode

func SetMode(L State, idx int, mode JitMode) bool

SetMode allows control of the VM.

'idx' is expected to be 0 or a stack index.

'mode' specifies the mode, which is 'or'ed with a flag. The flag can be:

  • ModeOff to turn a feature off
  • ModeOn to turn a feature on
  • ModeFlush to flush cached code.

func SetTable

func SetTable(L State, idx int)

SetTable does the equivalent to t[k] = v, where t is the value at the given valid index, v is the value at the top of the stack, and k is the value just below the top.

This function pops both the key and the value from the stack. As in Lua, this function may trigger a metamethod for the "newindex" event.

func SetTop

func SetTop(L State, idx int)

SetTop accepts any acceptable index, or 0, and sets the stack top to this index. If the new top is larger than the old one, then the new elements are filled with nil. If index is 0, then all stack elements are removed.

func Status

func Status(L State) int

Status returns the status of the thread L.

The status can be StatusOk for a normal thread, an error code if the thread finished its execution with an error, or StatusYield if the thread is suspended.

func Strlen

func Strlen(L State, i int) int

func ToBoolean

func ToBoolean(L State, idx int) bool

ToBoolean converts the Lua value at the given acceptable index to a boolean value.

func ToCString

func ToCString(gostring string) []byte

ToCString creates a null-terminated byte slice from a Go string.

The resulting slice is a new copy.

func ToGoString

func ToGoString(cstr []byte) string

ToGoString creates a Go string from a null-terminated byte slice.

The resulting string is a new copy.

func ToGoStringPtr

func ToGoStringPtr(cstr *byte, len int) string

ToGoStringPtr creates a Go string from a byte pointer and length.

The resulting string is a new copy.

func ToLString

func ToLString(L State, idx int, len *uint) string

ToLString converts the Lua value at the given acceptable index to a string. If len is not nil, it also sets len with the string length.

func ToPointer

func ToPointer(L State, idx int) uintptr

ToPointer converts the value at the given acceptable index to a generic pointer. The value can be a userdata, a table, a thread, or a function.

Different objects will give different pointers. There is no way to convert the pointer back to its original value.

func ToString

func ToString(L State, idx int) string

ToString converts the Lua value at the given acceptable index to a string value.

func ToUserdata

func ToUserdata(L State, idx int) uintptr

ToUserdata returns the block address of the value at the given acceptable index if it's a full userdata. If the value is a light userdata, ToUserdata returns its pointer.

func TypeError

func TypeError(L State, narg int, tname string) int

TypeError generates an error with a message like the following:

location: bad argument narg to 'func' (tname expected, got rt)

Where location is produced by Where, func is the name of the current function, and rt is the type name of the actual argument

func TypeName

func TypeName(L State, tp T) string

TypeName returns the name of the type encoded by the value tp.

func TypeNameOf

func TypeNameOf(L State, idx int) string

TypeNameOf returns the name of the type of the value at the given index.

func UpvalueIndex

func UpvalueIndex(i int) int

func XMove

func XMove(from, to State, n int)

XMove exchanges values between different threads of the same global state.

This function pops n values from the stack from, and pushes them onto the stack to.

func Yield

func Yield(L State, nresults int) int

Yield yields a coroutine.

This function should only be called as the return expression of a CFunction, as follows:

return Yield(L, nresults)

Types

type CFunction

type CFunction func(L State) (nresults int32)

CFunction represents a function used to interact with the Lua VM.

func ToCFunction

func ToCFunction(L State, idx int) CFunction

ToCFunction converts a value at the given acceptable index to a CFunction.

type GCMode

type GCMode int32

GCMode represents a mode used to interact with the Lua garbage collector.

type Integer

type Integer int64 // Integer represents a Lua integer.

func CheckInt

func CheckInt(L State, numarg int) Integer

CheckInt checks whether the function argument numarg is an integer and returns its number cast to an int.

func OptInt

func OptInt(L State, numarg int, def Integer) Integer

func ToInteger

func ToInteger(L State, idx int) Integer

ToInteger converts the Lua value at the given acceptable index to the type Integer.

type JitMode

type JitMode int32

JitMode represents a mode used to interact with the jit compiler.

const (
	ModeEngine JitMode = iota
	ModeDebug
	ModeFunc
	ModeAllFunc
	ModeAllSubFunc
	ModeTrace
	ModeWrapCFunc = JitMode(0x10)
	ModeMax       = ModeWrapCFunc + 1
)

type LReg

type LReg struct {
	Name string
	Func CFunction
}

LReg is the type used to register external functions.

type Number

type Number float64 // Number represents a Lua number.

func ToNumber

func ToNumber(L State, idx int) Number

ToNumber converts the Lua value at the given acceptable index to the type Number.

type ProfileCallback

type ProfileCallback func(data uintptr, L State, samples, vmstate int32)

ProfileCallback represents a function used for profiling.

type State

type State uintptr

State represents a pointer to a Lua state.

func NewState

func NewState() State

NewState creates a new Lua state.

func NewThread

func NewThread(L State) State

NewThread creates a new thread, pushes it on the stack, and returns a new Lua state that represents this new thread. The new state returned by this function shares with the original state all global objects (such as tables), but has an independent execution stack.

There is no explicit function to close or to destroy a thread. Threads are subject to garbage collection, like any Lua object.

func Open

func Open() State

Open creates a new Lua state.

func ToThread

func ToThread(L State, idx int) State

ToThread converts the value at the given acceptable index to a Lua thread (represented as a State).

type T

type T int32

func Type

func Type(L State, idx int) T

Type returns the type of the value in the given acceptable index, or TNone for a non-valid index (that is, an index to an "empty" stack position).

Jump to

Keyboard shortcuts

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