lua

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: May 5, 2023 License: MIT Imports: 12 Imported by: 1

Documentation

Overview

This package provides access to the excellent lua language interpreter from go code.

Access to most of the functions in lua.h and lauxlib.h is provided as well as additional convenience functions to publish Go objects and functions to lua code.

The documentation of this package is no substitute for the official lua documentation and in many instances methods are described only with the name of their C equivalent

Index

Constants

View Source
const (
	LUA_TNIL           = LuaValType(C.LUA_TNIL)
	LUA_TNUMBER        = LuaValType(C.LUA_TNUMBER)
	LUA_TBOOLEAN       = LuaValType(C.LUA_TBOOLEAN)
	LUA_TSTRING        = LuaValType(C.LUA_TSTRING)
	LUA_TTABLE         = LuaValType(C.LUA_TTABLE)
	LUA_TFUNCTION      = LuaValType(C.LUA_TFUNCTION)
	LUA_TUSERDATA      = LuaValType(C.LUA_TUSERDATA)
	LUA_TTHREAD        = LuaValType(C.LUA_TTHREAD)
	LUA_TLIGHTUSERDATA = LuaValType(C.LUA_TLIGHTUSERDATA)
)
View Source
const (
	LUA_VERSION       = C.LUA_VERSION
	LUA_RELEASE       = C.LUA_RELEASE
	LUA_VERSION_NUM   = C.LUA_VERSION_NUM
	LUA_COPYRIGHT     = C.LUA_COPYRIGHT
	LUA_AUTHORS       = C.LUA_AUTHORS
	LUA_MULTRET       = C.LUA_MULTRET
	LUA_REGISTRYINDEX = C.LUA_REGISTRYINDEX
	LUA_ENVIRONINDEX  = C.LUA_ENVIRONINDEX
	LUA_GLOBALSINDEX  = C.LUA_GLOBALSINDEX
	LUA_YIELD         = C.LUA_YIELD
	LUA_ERRRUN        = C.LUA_ERRRUN
	LUA_ERRSYNTAX     = C.LUA_ERRSYNTAX
	LUA_ERRMEM        = C.LUA_ERRMEM
	LUA_ERRERR        = C.LUA_ERRERR
	LUA_TNONE         = C.LUA_TNONE
	LUA_MINSTACK      = C.LUA_MINSTACK
	LUA_GCSTOP        = C.LUA_GCSTOP
	LUA_GCRESTART     = C.LUA_GCRESTART
	LUA_GCCOLLECT     = C.LUA_GCCOLLECT
	LUA_GCCOUNT       = C.LUA_GCCOUNT
	LUA_GCCOUNTB      = C.LUA_GCCOUNTB
	LUA_GCSTEP        = C.LUA_GCSTEP
	LUA_GCSETPAUSE    = C.LUA_GCSETPAUSE
	LUA_GCSETSTEPMUL  = C.LUA_GCSETSTEPMUL
	LUA_HOOKCALL      = C.LUA_HOOKCALL
	LUA_HOOKRET       = C.LUA_HOOKRET
	LUA_HOOKLINE      = C.LUA_HOOKLINE
	LUA_HOOKCOUNT     = C.LUA_HOOKCOUNT
	LUA_HOOKTAILRET   = C.LUA_HOOKTAILRET
	LUA_MASKCALL      = C.LUA_MASKCALL
	LUA_MASKRET       = C.LUA_MASKRET
	LUA_MASKLINE      = C.LUA_MASKLINE
	LUA_MASKCOUNT     = C.LUA_MASKCOUNT
	LUA_ERRFILE       = C.LUA_ERRFILE
	LUA_NOREF         = C.LUA_NOREF
	LUA_REFNIL        = C.LUA_REFNIL
	LUA_FILEHANDLE    = C.LUA_FILEHANDLE
	LUA_COLIBNAME     = C.LUA_COLIBNAME
	LUA_TABLIBNAME    = C.LUA_TABLIBNAME
	LUA_IOLIBNAME     = C.LUA_IOLIBNAME
	LUA_OSLIBNAME     = C.LUA_OSLIBNAME
	LUA_STRLIBNAME    = C.LUA_STRLIBNAME
	LUA_MATHLIBNAME   = C.LUA_MATHLIBNAME
	LUA_DBLIBNAME     = C.LUA_DBLIBNAME
	LUA_LOADLIBNAME   = C.LUA_LOADLIBNAME
)
View Source
const ExecutionQuantumExceeded = "Lua execution quantum exceeded"

The errorstring used by State.SetExecutionLimit

Variables

This section is empty.

Functions

func XMove

func XMove(from *State, to *State, n int)

lua_xmove

Types

type Alloc

type Alloc func(ptr unsafe.Pointer, osize uint, nsize uint) unsafe.Pointer

Type of allocation functions to use with NewStateAlloc

type GoStackEntry added in v1.1.0

type GoStackEntry struct {
	Name        string `json:"func"`
	Source      string `json:"file"`
	CurrentLine int    `json:"line"`
}

type HookFunction added in v1.1.0

type HookFunction func(L *State)

This is the type of a go function that can be used as a lua_Hook

type LuaError

type LuaError struct {
	Code  int             `json:"code"`
	Msg   string          `json:"message"`
	LuaS  []string        `json:"lua_stack"`
	LuaST []LuaStackEntry `json:"lua_stacktrace"`
	GoST  []GoStackEntry  `json:"go_stacktrace"`
}

func (*LuaError) Error

func (le *LuaError) Error() string

func (*LuaError) New added in v1.1.0

func (le *LuaError) New(state *State, code int, msg string) *LuaError

func (*LuaError) Parse added in v1.1.0

func (le *LuaError) Parse(data string) error

func (*LuaError) String added in v1.1.0

func (le *LuaError) String() string

type LuaGoFunction

type LuaGoFunction func(L *State) int

This is the type of go function that can be registered as lua functions

type LuaStackEntry

type LuaStackEntry struct {
	Name        string `json:"name"`
	Source      string `json:"source"`
	ShortSource string `json:"short_source"`
	CurrentLine int    `json:"line"`
}

type LuaValType

type LuaValType int

type SharedByAllCoroutines

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

type State

type State struct {

	// index of this object inside the goStates array
	Index int

	Shared *SharedByAllCoroutines

	IsMainCoro bool // if true, then will be registered

	MainCo  *State       // always points to the main coroutine.
	CmainCo *C.lua_State // always points to the main coroutine's C state.

	// Upos is position in uniqArray. Upos must be 1 for
	// a main state because code in c-golua.c counts on this
	// to lookup the main coroutine from a non-main
	// coroutine. As happens naturally, that means the main
	// coroutine must be registered first, before any
	// other coroutines in that main state are
	// generated/registered.
	//
	Upos int

	// Upos -> all coroutines within a main state.
	// For non-main coroutines, AllCoro is a nil map.
	//
	// TODO: currently no hooks for garbage collection
	//  from the Lua side back to Go. So when Lua
	//  deletes a coroutine, we don't notice, and
	// it stays in our maps (uniqArray, revUniq, Lmap)
	// and on the Go side (AllCoro) forever, at the moment.
	AllCoro map[int]*State
	// contains filtered or unexported fields
}

Wrapper to keep cgo from complaining about incomplete ptr type

func NewState

func NewState() *State

luaL_newstate

func NewStateAlloc

func NewStateAlloc(f Alloc) *State

Creates a new lua interpreter state with the given allocation function

func (*State) ArgError

func (L *State) ArgError(narg int, extramsg string) int

luaL_argerror is dangerous on windows system

func (*State) Argcheck

func (L *State) Argcheck(cond bool, narg int, extramsg string)

luaL_argcheck WARNING: before b30b2c62c6712c6683a9d22ff0abfa54c8267863 the function ArgCheck had the opposite behaviour

func (*State) AtPanic

func (L *State) AtPanic(panicf LuaGoFunction) (oldpanicf LuaGoFunction)

Sets the AtPanic function, returns the old one

BUG(everyone_involved): passing nil causes serious problems

func (*State) Call

func (L *State) Call(nargs, nresults int) error

lua_call

func (*State) CallMeta

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

luaL_callmeta

func (*State) CdataToInt32

func (L *State) CdataToInt32(index int) int32

lua_cdata_to_int32

func (*State) CdataToInt64

func (L *State) CdataToInt64(index int) int64

lua_cdata_to_int64

func (*State) CdataToUint64

func (L *State) CdataToUint64(index int) uint64

lua_cdata_to_uint64

func (*State) CheckAny

func (L *State) CheckAny(narg int)

luaL_checkany isn't work on windows due lua_error call

func (*State) CheckInteger

func (L *State) CheckInteger(narg int) int

luaL_checkinteger isn't work on windows due lua_error call

func (*State) CheckNumber

func (L *State) CheckNumber(narg int) float64

luaL_checknumber isn't work on windows due lua_error call

func (*State) CheckOption

func (L *State) CheckOption(narg int, def string, lst []string) int

luaL_checkoption

BUG(everyone_involved): not implemented

func (*State) CheckStack

func (L *State) CheckStack(extra int) bool

lua_checkstack

func (*State) CheckStackArg

func (L *State) CheckStackArg(narg int)

func (*State) CheckString

func (L *State) CheckString(narg int) string

luaL_checkstring isn't work on windows due lua_error call

func (*State) CheckType

func (L *State) CheckType(narg int, t LuaValType)

luaL_checktype isn't work on windows due lua_error call

func (*State) CheckUdata

func (L *State) CheckUdata(narg int, tname string) unsafe.Pointer

luaL_checkudata isn't work on windows due lua_error call

func (*State) Close

func (L *State) Close()

lua_close

func (*State) Concat

func (L *State) Concat(n int)

lua_concat

func (*State) CreateTable

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

lua_createtable

func (*State) DoFile

func (L *State) DoFile(filename string) error

Executes file, returns nil for no errors or the lua error string on failure

func (*State) DoString

func (L *State) DoString(str string) error

Executes the string, returns nil for no errors or the lua error string on failure

func (*State) Dump added in v1.1.0

func (L *State) Dump() int

lua_dump

func (*State) DumpLuaStack added in v1.1.0

func (L *State) DumpLuaStack()

func (*State) DumpLuaStackAsString added in v1.1.0

func (L *State) DumpLuaStackAsString() (s string)

func (*State) Equal

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

lua_equal

func (*State) GC

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

lua_gc

func (*State) GSub

func (L *State) GSub(s string, p string, r string) string

luaL_gsub

func (*State) GetField

func (L *State) GetField(index int, k string)

lua_getfield

func (*State) GetGlobal

func (L *State) GetGlobal(name string)

Pushes on the stack the value of a global variable (lua_getglobal)

func (*State) GetMetaField

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

luaL_getmetafield

func (*State) GetMetaTable

func (L *State) GetMetaTable(index int) bool

lua_getmetatable

func (*State) GetState added in v1.1.0

func (L *State) GetState() *C.lua_State

func (*State) GetTable

func (L *State) GetTable(index int)

lua_gettable

func (*State) GetTop

func (L *State) GetTop() int

lua_gettop

func (*State) GetfEnv

func (L *State) GetfEnv(index int)

lua_getfenv

func (*State) GoStackTrace added in v1.1.0

func (L *State) GoStackTrace() []GoStackEntry

Returns the current go runtime stack trace

func (*State) Insert

func (L *State) Insert(index int)

lua_insert

func (*State) IsBoolean

func (L *State) IsBoolean(index int) bool

Returns true if lua_type == LUA_TBOOLEAN

func (*State) IsFunction

func (L *State) IsFunction(index int) bool

Returns true if the value at index is user data pushed with PushGoFunction

func (*State) IsGoFunction

func (L *State) IsGoFunction(index int) bool

Returns true if the value at index is a LuaGoFunction

func (*State) IsGoStruct

func (L *State) IsGoStruct(index int) bool

Returns true if the value at index is user data pushed with PushGoStruct

func (*State) IsLightUserdata

func (L *State) IsLightUserdata(index int) bool

Returns true if the value at index is light user data

func (*State) IsNil

func (L *State) IsNil(index int) bool

lua_isnil

func (*State) IsNone

func (L *State) IsNone(index int) bool

lua_isnone

func (*State) IsNoneOrNil

func (L *State) IsNoneOrNil(index int) bool

lua_isnoneornil

func (*State) IsNumber

func (L *State) IsNumber(index int) bool

lua_isnumber

func (*State) IsString

func (L *State) IsString(index int) bool

lua_isstring

func (*State) IsTable

func (L *State) IsTable(index int) bool

lua_istable

func (*State) IsThread

func (L *State) IsThread(index int) bool

lua_isthread

func (*State) IsUserdata

func (L *State) IsUserdata(index int) bool

lua_isuserdata

func (*State) LGetMetaTable

func (L *State) LGetMetaTable(tname string)

luaL_getmetatable

func (*State) LTypename

func (L *State) LTypename(index int) string

luaL_typename

func (*State) LessThan

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

lua_lessthan

func (*State) Load added in v1.1.0

func (L *State) Load(bs []byte, name string) int

lua_load

func (*State) LoadBuffer

func (L *State) LoadBuffer(data []byte, size int, name string) int

luaL_loadbuffer

func (*State) LoadFile

func (L *State) LoadFile(filename string) int

luaL_loadfile

func (*State) LoadString

func (L *State) LoadString(s string) int

luaL_loadstring

func (*State) Lock

func (L *State) Lock()

func (*State) LuaError added in v1.1.2

func (L *State) LuaError(msg string) int

luaL_argerror is dangerous on windows system

func (*State) LuaJITctypeID

func (L *State) LuaJITctypeID(idx int) uint32

LuaJIT only: return ctype of the cdata at the top of the stack.

func (*State) LuaStack added in v1.1.0

func (L *State) LuaStack() []string

Returns the current lua stack trace

func (*State) LuaStackPosToString added in v1.1.0

func (L *State) LuaStackPosToString(i int) string

func (*State) LuaStackTrace added in v1.1.0

func (L *State) LuaStackTrace() []LuaStackEntry

Returns the current lua stack trace

func (*State) MustCall

func (L *State) MustCall(nargs, nresults int)

Like lua_call but panics on errors

func (*State) MustDoString

func (L *State) MustDoString(str string)

Like DoString but panics on error

func (*State) NewError

func (L *State) NewError(msg string) *LuaError

func (*State) NewMetaTable

func (L *State) NewMetaTable(tname string) bool

luaL_newmetatable

func (*State) NewTable

func (L *State) NewTable()

lua_newtable

func (*State) NewThread

func (L *State) NewThread() *State

lua_newthread

func (*State) NewUserdata

func (L *State) NewUserdata(size uintptr) unsafe.Pointer

Creates a new user data object of specified size and returns it

func (*State) Next

func (L *State) Next(index int) int

lua_next

func (*State) ObjLen

func (L *State) ObjLen(index int) uint

lua_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. jea note: Despite the misleading description above, in 5.1 or LuaJit, ObjLen does not call the metamethod __len(). In 5.2 it was split into len and rawlen, with len calling the __len metamethod.

func (*State) OpenBase

func (L *State) OpenBase()

Calls luaopen_base

func (*State) OpenIO

func (L *State) OpenIO()

Calls luaopen_io

func (*State) OpenLibs

func (L *State) OpenLibs()

luaL_openlibs

func (*State) OpenMath

func (L *State) OpenMath()

Calls luaopen_math

func (*State) OpenOS

func (L *State) OpenOS()

Calls luaopen_os

func (*State) OpenPackage

func (L *State) OpenPackage()

Calls luaopen_package

func (*State) OpenString

func (L *State) OpenString()

Calls luaopen_string

func (*State) OpenTable

func (L *State) OpenTable()

Calls luaopen_table

func (*State) OptInteger

func (L *State) OptInteger(narg int, d int) int

luaL_optinteger

func (*State) OptNumber

func (L *State) OptNumber(narg int, d float64) float64

luaL_optnumber

func (*State) OptString

func (L *State) OptString(narg int, d string) string

luaL_optstring

func (*State) Pop

func (L *State) Pop(n int)

lua_pop

func (*State) PushBoolean

func (L *State) PushBoolean(b bool)

lua_pushboolean

func (*State) PushBytes

func (L *State) PushBytes(b []byte)

func (*State) PushGoClosure

func (L *State) PushGoClosure(f LuaGoFunction)

PushGoClosure pushes a lua.LuaGoFunction to the stack wrapped in a Closure. this permits the go function to reflect lua type 'function' when checking with type() this implements behaviour akin to lua_pushcfunction() in lua C API.

func (*State) PushGoFunction

func (L *State) PushGoFunction(f LuaGoFunction)

Like lua_pushcfunction pushes onto the stack a go function as user data

func (*State) PushGoStruct

func (L *State) PushGoStruct(iface interface{})

Pushes a Go struct onto the stack as user data.

The user data will be rigged so that lua code can access and change the public members of simple types directly

func (*State) PushInt64

func (L *State) PushInt64(n int64)

func (*State) PushInteger

func (L *State) PushInteger(n int64)

lua_pushinteger

func (*State) PushLightUserdata

func (L *State) PushLightUserdata(ud *interface{})

Push a pointer onto the stack as user data.

This function doesn't save a reference to the interface, it is the responsibility of the caller of this function to insure that the interface outlasts the lifetime of the lua object that this function creates.

func (*State) PushNil

func (L *State) PushNil()

lua_pushnil

func (*State) PushNumber

func (L *State) PushNumber(n float64)

lua_pushnumber

func (*State) PushString

func (L *State) PushString(str string)

lua_pushstring

func (*State) PushThread

func (L *State) PushThread() (isMain bool)

lua_pushthread

func (*State) PushUint64

func (L *State) PushUint64(u uint64)

func (*State) PushValue

func (L *State) PushValue(index int)

lua_pushvalue

func (*State) RaiseError

func (L *State) RaiseError(msg string)

correctly catching lua_error can't support on windows so there using go panic method

func (*State) RawEqual

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

lua_rawequal

func (*State) RawGet

func (L *State) RawGet(index int)

lua_rawget

func (*State) RawGeti

func (L *State) RawGeti(index int, n int)

lua_rawgeti

func (*State) RawSet

func (L *State) RawSet(index int)

lua_rawset

func (*State) RawSeti

func (L *State) RawSeti(index int, n int)

lua_rawseti

func (*State) Ref

func (L *State) Ref(t int) int

luaL_ref

func (*State) Register

func (L *State) Register(name string, f LuaGoFunction)

Registers a Go function as a global variable

func (*State) Remove

func (L *State) Remove(index int)

lua_remove

func (*State) Replace

func (L *State) Replace(index int)

lua_replace

func (*State) Resume

func (L *State) Resume(narg int) int

lua_resume

func (*State) SetAllocf

func (L *State) SetAllocf(f Alloc)

lua_setallocf

func (*State) SetExecutionLimit

func (L *State) SetExecutionLimit(instrNumber int)

Sets the maximum number of operations to execute at instrNumber, after this the execution ends This and SetHook are mutual exclusive

func (*State) SetField

func (L *State) SetField(index int, k string)

lua_setfield

func (*State) SetGlobal

func (L *State) SetGlobal(name string)

lua_setglobal

func (*State) SetHook added in v1.1.0

func (L *State) SetHook(f HookFunction, instrNumber int)

Sets the lua hook (lua_sethook). This and SetExecutionLimit are mutual exclusive

func (*State) SetMetaMethod

func (L *State) SetMetaMethod(methodName string, f LuaGoFunction)

Sets a metamethod to execute a go function

The code:

L.LGetMetaTable(tableName)
L.SetMetaMethod(methodName, function)

is the logical equivalent of:

L.LGetMetaTable(tableName)
L.PushGoFunction(function)
L.SetField(-2, methodName)

except this wouldn't work because pushing a go function results in user data not a cfunction

func (*State) SetMetaTable

func (L *State) SetMetaTable(index int)

lua_setmetatable

func (*State) SetTable

func (L *State) SetTable(index int)

lua_settable

func (*State) SetTop

func (L *State) SetTop(index int)

lua_settop

func (*State) SetfEnv

func (L *State) SetfEnv(index int)

lua_setfenv

func (*State) Status

func (L *State) Status() int

lua_status

func (*State) ToBoolean

func (L *State) ToBoolean(index int) bool

lua_toboolean

func (*State) ToBytes

func (L *State) ToBytes(index int) []byte

func (*State) ToFloat32

func (L *State) ToFloat32(index int) float32

lua_tointeger

func (*State) ToFloat64

func (L *State) ToFloat64(index int) float64

lua_tointeger

func (*State) ToGoFunction

func (L *State) ToGoFunction(index int) (f LuaGoFunction)

Returns the value at index as a Go function (it must be something pushed with PushGoFunction)

func (*State) ToGoStruct

func (L *State) ToGoStruct(index int) (f interface{})

Returns the value at index as a Go Struct (it must be something pushed with PushGoStruct)

func (*State) ToInteger

func (L *State) ToInteger(index int) int

lua_tointeger

func (*State) ToInteger32

func (L *State) ToInteger32(index int) int32

lua_tointeger

func (*State) ToInteger64

func (L *State) ToInteger64(index int) int64

lua_tointeger

func (*State) ToNumber

func (L *State) ToNumber(index int) float64

lua_tonumber

func (*State) ToPointer

func (L *State) ToPointer(index int) uintptr

lua_topointer

func (*State) ToString

func (L *State) ToString(index int) string

lua_tostring

func (*State) ToThread

func (L *State) ToThread(index int) *State

lua_tothread

func (*State) ToThreadHelper

func (L *State) ToThreadHelper(ptr *C.lua_State) *State

func (*State) ToUInteger

func (L *State) ToUInteger(index int) uint

lua_tointeger

func (*State) ToUInteger32

func (L *State) ToUInteger32(index int) uint32

lua_tointeger

func (*State) ToUInteger64

func (L *State) ToUInteger64(index int) uint64

lua_tointeger

func (*State) ToUserdata

func (L *State) ToUserdata(index int) unsafe.Pointer

lua_touserdata

func (*State) Type

func (L *State) Type(index int) LuaValType

lua_type

func (*State) Typename

func (L *State) Typename(tp int) string

lua_typename

func (*State) Unlock

func (L *State) Unlock() error

func (*State) Unref

func (L *State) Unref(t int, ref int)

luaL_unref

func (*State) Where

func (L *State) Where(lvl int)

luaL_where

func (*State) Yield

func (L *State) Yield(nresults int) int

lua_yield

Notes

Bugs

  • not implemented

  • passing nil causes serious problems

Jump to

Keyboard shortcuts

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