Documentation ¶
Index ¶
- Constants
- type State
- func (this *State) Call(nargs, nresults int)
- func (this *State) Checkstack(extra int) bool
- func (this *State) Close()
- func (this *State) Concat(n int)
- func (this *State) Createtable(narr, nrec int)
- func (this *State) Dofile(path string) int
- func (this *State) Dostring(str string) int
- func (this *State) Error()
- func (this *State) Getfield(index int, k string)
- func (this *State) Getglobal(name string)
- func (this *State) Gettable(index int)
- func (this *State) Gettop() int
- func (this *State) Insert(index int)
- func (this *State) Isboolean(index int) bool
- func (this *State) Isfunction(index int) bool
- func (this *State) Isgofunction(index int) bool
- func (this *State) Islightuserdata(index int) bool
- func (this *State) Isnil(index int) bool
- func (this *State) Isnone(index int) bool
- func (this *State) Isnoneornil(index int) bool
- func (this *State) Isnumber(index int) bool
- func (this *State) Isstring(index int) bool
- func (this *State) Istable(index int) bool
- func (this *State) Isthread(index int) bool
- func (this *State) Isuserdata(index int) bool
- func (this *State) Loadfile(filename string) error
- func (this *State) Loadstring(str string) error
- func (this *State) Newtable()
- func (this *State) Newthread() lua.State
- func (this *State) Newuserdata()
- func (this *State) Openlibs()
- func (this *State) Pcall(nargs, nresults, errfunc int) error
- func (this *State) Pop(index int)
- func (this *State) Pushclosure(fn lua.Function, n int)
- func (this *State) Pushfunction(fn lua.Function)
- func (this *State) Pushmetatable(mt *lua.Metatable)
- func (this *State) Pushmodule(name string, fn lua.Function)
- func (this *State) Pushnumber(n float64)
- func (this *State) Pushstring(str string)
- func (this *State) Pushthread() int
- func (this *State) Pushvalue(index int)
- func (this *State) Rawequal(i1, i2 int) bool
- func (this *State) Rawget(index int)
- func (this *State) Rawgeti(index, n int)
- func (this *State) Rawset(index int)
- func (this *State) Rawseti(index, n int)
- func (this *State) Register(fn lua.Function, name string)
- func (this *State) Remove(index int)
- func (this *State) Replace(index int)
- func (this *State) Resume(narg int) (yield bool, e error)
- func (this *State) Setfield(index int, k string)
- func (this *State) Setglobal(name string)
- func (this *State) Setmetatable(index int) int
- func (this *State) Settable(index int)
- func (this *State) Settop(index int)
- func (this *State) Setupvalue(funcindex, n int) (string, error)
- func (this *State) Status() int
- func (this *State) Toboolean(index int) bool
- func (this *State) Tointeger(index int) int
- func (this *State) Tonumber(index int) float64
- func (this *State) Topointer(index int) unsafe.Pointer
- func (this *State) Tostring(index int) string
- func (this *State) Tothread(index int) *State
- func (this *State) Touserdata(index int) unsafe.Pointer
- func (this *State) Type(index int) int
- func (this *State) Typename(tp int) string
- func (this *State) Xmove(from lua.State, n int)
- func (this *State) Yield(nresults int) int
Constants ¶
const ( LUA_MINSTACK = int(C.LUA_MINSTACK) LUA_MULTRET = int(C.LUA_MULTRET) LUA_YIELD = int(C.LUA_YIELD) LUA_OK = 0 LUA_SIGNATURE = string(C.LUA_SIGNATURE) )
Top Lua Constants
const ( LUA_REGISTRYINDEX = int(C.LUA_REGISTRYINDEX) LUA_ENVIRONINDEX = int(C.LUA_ENVIRONINDEX) LUA_GLOBALSINDEX = int(C.LUA_GLOBALSINDEX) )
Index constants
const ( LUA_ERRERR = int(C.LUA_ERRERR) LUA_ERRMEM = int(C.LUA_ERRMEM) LUA_ERRRUN = int(C.LUA_ERRRUN) LUA_ERRSYNTAX = int(C.LUA_ERRSYNTAX) LUA_ERRUNK_STR = "UNDEFINED ERROR: " LUA_ERRERR_STR = "ERROR IN ERROR HANDLING: " LUA_ERRMEM_STR = "OUT OF MEMORY ERROR: " LUA_ERRRUN_STR = "RUNTIME ERROR: " LUA_ERRSYNTAX_STR = "SYNTAX ERROR: " )
Error constants
const ( LUA_TNONE = int(C.LUA_TNONE) LUA_TNIL = int(C.LUA_TNIL) LUA_TBOOLEAN = int(C.LUA_TBOOLEAN) LUA_TLIGHTUSERDATA = int(C.LUA_TLIGHTUSERDATA) LUA_TNUMBER = int(C.LUA_TNUMBER) LUA_TSTRING = int(C.LUA_TSTRING) LUA_TTABLE = int(C.LUA_TTABLE) LUA_TFUNCTION = int(C.LUA_TFUNCTION) LUA_TUSERDATA = int(C.LUA_TUSERDATA) LUA_TTHREAD = int(C.LUA_TTHREAD) )
Type Constants
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type State ¶
type State struct {
// contains filtered or unexported fields
}
State is a golang struct wrapping our C lua_State
func Newstate ¶
func Newstate() *State
NewState Creates a new Lua state. It calls luaL_newstate which calls lua_newstate with an allocator based on the standard C realloc function and then sets a panic function (see lua_atpanic) that prints an error message to the standard error output in case of fatal errors. TODO: Handle NULL return of luaL_newstate and error appropriately TODO: Set panic function for lua_atpanic
func (*State) Call ¶
Calls a function.
To call a function you must use the following protocol: first, the function to be called is pushed onto the stack; then, the arguments to the function are pushed in direct order; that is, the first argument is pushed first. Finally you call Call; nargs is the number of arguments that you pushed onto the stack. All arguments and the function value are popped from the stack when the function is called. The function results are pushed onto the stack when the function returns. The number of results is adjusted to nresults, unless nresults is luajit.Multret. In this case, all results from the function are pushed. Lua takes care that the returned values fit into the stack space. The function results are pushed onto the stack in direct order (the first result is pushed first), so that after the call the last result is on the top of the stack.
Any error inside the called function is propagated upwards (with a longjmp).
func (*State) 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 (*State) Close ¶
func (this *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. On several platforms, you may not need to call this function, because all resources are naturally released when the host program ends. On the other hand, long-running programs, such as a daemon or a web server, might need to release states as soon as they are not needed, to avoid growing too large.
func (*State) 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 (*State) 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 (*State) Dofile ¶
Dofile Loads and runs the given file. It returns 0 if there are no errors or 1 in case of errors.
func (*State) Dostring ¶
Dostring Loads and runs the given string. It returns 0 if there are no errors or 1 in case of errors.
func (*State) Error ¶
func (this *State) Error()
Generates a Lua error. The error message (which can actually be a Lua value of any type) must be on the stack top. This function does a long jump, and therefore never returns.
func (*State) Getfield ¶
Pushes onto the stack the value t[k], where t is the value at the given valid index.
func (*State) 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.
This function pops the key from the stack (putting the resulting value in its place). As in Lua, this function may trigger a metamethod for the "index" event
func (*State) 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 (*State) 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 (*State) Isboolean ¶
Returns true if the value at the given valid index has type boolean, and false otherwise.
func (*State) Isfunction ¶
Returns true if the value at the given valid index is a function (either Go or Lua), and false otherwise.
func (*State) Isgofunction ¶
Returns true if the value at the given valid index is a Go function, and false otherwise.
func (*State) Islightuserdata ¶
Returns true if the value at the given valid index is light userdata, and false otherwise.
func (*State) Isnil ¶
Returns true if the value at the given valid index is nil, and false otherwise.
func (*State) Isnone ¶
Returns true if the given valid index is not valid (that is, it refers to an element outside the current stack), and false otherwise.
func (*State) Isnoneornil ¶
Returns true if the given valid index is not valid (that is, it refers to an element outside the current stack) or if the value at this index is nil, and false otherwise.
func (*State) Isnumber ¶
Returns true if the value at the given valid index is a number, and false otherwise.
func (*State) Isstring ¶
Returns true if the value at the given valid index is a string, and false otherwise.
func (*State) Istable ¶
Returns true if the value at the given valid index is a table, and false otherwise.
func (*State) Isthread ¶
Returns true if the value at the given valid index is a thread, and false otherwise.
func (*State) Isuserdata ¶
Returns true if the value at the given acceptable index is a userdata (either full or light), and false otherwise.
func (*State) Loadfile ¶
Loadfile Loads a file as a Lua chunk. This function uses lua_load to load the chunk in the file named filename. The first line in the file is ignored if it starts with a #
As lua_load, this function only loads the chunk; it does not run it.
func (*State) Loadstring ¶
Loads a string as a Lua chunk.
This function only loads the chunk; it does not run it.
func (*State) Newtable ¶
func (this *State) Newtable()
Creates a new empty table and pushes it onto the stack. It is equivalent to Createtable(0, 0).
func (*State) Newthread ¶
func (this *State) Newthread() lua.State
Creates a new thread, pushes it on the stack, and returns a pointer to a 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 (*State) Newuserdata ¶
func (this *State) Newuserdata()
Newuserdata. This function 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.
Userdata represent C values in Lua. A full userdata represents a block of memory. It is an object (like a table): you must create it, it can have its own metatable, and you can detect when it is being collected. A full userdata is only equal to itself (under raw equality).
When Lua collects a full userdata with a gc metamethod, Lua calls the metamethod and marks the userdata as finalized. When this userdata is collected again then Lua frees its corresponding memory.
func (*State) Openlibs ¶
func (this *State) Openlibs()
Openlibs Opens all standard Lua libraries into the given state. http://www.lua.org/manual/5.1/manual.html#luaL_openlibs
func (*State) Pcall ¶
Calls a function in protected mode.
Both nargs and nresults have the same meaning as in Call. If there are no errors during the call, Pcall behaves exactly like Call. However, if there is any error, Pcall catches it, pushes a single value on the stack (the error message), and returns an error code. Like Call, Pcall always removes the function and its arguments from the stack.
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.
Typically, the error handler function is used to add more debug information to the error message, such as a stack traceback. Such information cannot be gathered after the return of Pcall, since by then the stack has unwound.
func (*State) Pushclosure ¶
Pushes a new Go closure onto the stack.
When a Go function is created, it is possible to associate some values with it, thus creating a Go closure; these values are then accessible to the function whenever it is called. To associate values with a Go function, first these values should be pushed onto the stack (when there are multiple values, the first value is pushed first). Then Pushclosure is called to create and push the Go function onto the stack, with the argument n telling how many values should be associated with the function. Pushclosure also pops these values from the stack.
The maximum value for n is 254.
func (*State) Pushfunction ¶
func (this *State) Pushfunction(fn lua.Function)
Pushes a Go function onto the stack. This function receives a pointer to a Go function and pushes onto the stack a Lua value of type function that, when called, invokes the corresponding Go function.
Any function to be registered in Lua must follow the correct protocol to receive its parameters and return its results (see Function).
func (*State) Pushmetatable ¶
func (this *State) Pushmetatable(mt *lua.Metatable)
Pushmetatable pushes a Metatable struct to the stack and conducts the appropriate mapping of metatable keys to Functions.
you must still use Setmetatable after this function returns to assign your metatable to some other table on the stack.
func (*State) Pushmodule ¶
Pushmodule pushes a module loader function into package.preload, permitting you to invoke a go function when conducting lua requires ex: "require('your_module_name')"
func (*State) Pushnumber ¶
Pushes a number with value n onto the stack.
func (*State) Pushstring ¶
Pushes the string str onto the stack.
func (*State) Pushthread ¶
Pushes the thread represented by s onto the stack. Returns 1 if this thread is the main thread of its state.
func (*State) Rawequal ¶
Returns true if the two values at valid indices i1 and i2 are primitively equal (that is, without calling metamethods). Otherwise returns false. Also returns false if any of the indices are invalid.
func (*State) 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 (*State) 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 (*State) 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 (*State) 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 (*State) Resume ¶
Starts and resumes a coroutine in a given thread.
To start a coroutine, you first create a new thread (see Newthread); then you push onto its stack the main function plus any arguments; then you call Resume, with narg being the number of arguments. This call returns when the coroutine suspends or finishes its execution. When it returns, the stack contains all values passed to Yield, or all values returned by the body function. Resume returns (true, nil) if the coroutine yields, (false, nil) if the coroutine finishes its execution without errors, or (false, error) in case of errors (see Pcall).
In case of errors, the stack is not unwound, so you can use the debug API over it. The error message is on the top of the stack.
To resume a coroutine, you remove any results from the last Yield, put on its stack only the values to be passed as results from the yield, and then call Resume.
func (*State) 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 (*State) Setmetatable ¶
Pops a table from the stack and sets it as the new metatable for the value at the given valid index.
func (*State) 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 (*State) Settop ¶
Accepts any valid 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 (*State) Setupvalue ¶
Sets the value of a closure's upvalue. It assigns the value at the top of the stack to the upvalue and returns its name. It also pops the value from the stack. Parameters funcindex and n are as in the Getupvalue.
Returns an error (and pops nothing) when the index is greater than the number of upvalues.
func (*State) Status ¶
Returns the status of the thread s.
The status can be 0 for a normal thread, an error code if the thread finished its execution with an error, or luajit.Yield if the thread is suspended.
func (*State) Toboolean ¶
Converts the Lua value at the given valid index to a Go boolean value. Like all tests in Lua, Toboolean returns true for any Lua value different from false and nil; otherwise it returns false. It also returns false when called with a non-valid index. (If you want to accept only actual boolean values, use Isboolean to test the value's type.)
func (*State) Tointeger ¶
Converts the Lua value at the given valid index to a Go int. The Lua value must be a number or a string convertible to a number; otherwise, Tointeger returns 0.
If the number is not an integer, it is truncated in some non-specified way.
func (*State) Tonumber ¶
Converts the Lua value at the given valid index to a float64. The Lua value must be a number or a string convertible to a number; otherwise, Tonumber returns 0.
func (*State) Topointer ¶
Converts the value at the given acceptable index to a uintptr. The value can be a userdata, a table, a thread, or a function; otherwise, Topointer returns nil. Different objects will give different pointers. There is no way to convert the pointer back to its original value.
Typically this function is used only for debug information.
func (*State) Tostring ¶
Converts the Lua value at the given valid index to a Go string. The Lua value must be a string or a number; otherwise, the function returns an empty string. If the value is a number, then Tostring also changes the actual value in the stack to a string. (This change confuses Next when Tostring is applied to keys during a table traversal). The string always has a zero ('\0') after its last character (as in C), but can contain other zeros in its body.
func (*State) Tothread ¶
Converts the value at the given valid index to a Lua thread (represented as a *State). This value must be a thread; otherwise, the function returns nil.
func (*State) Touserdata ¶
If the value at the given valid index is a full userdata, returns its block address. If the value is a light userdata, returns its pointer. Otherwise, returns unsafe.Pointer(nil).
func (*State) Type ¶
Returns the type of the value in the given valid index, or luajit.LUA_TNONE for a non-valid index (that is, an index to an "empty" stack position). The types returned by lua_type are coded by the following constants defined in const.go: LUA_TNIL, LUA_TNUMBER, LUA_TBOOLEAN, LUA_TSTRING, LUA_TTABLE, LUA_TFUNCTION, LUA_TUSERDATA, LUA_TTHREAD, and LUA_TLIGHTUSERDATA.
func (*State) Typename ¶
Returns the name of the type encoded by the value tp, which must be one the values returned by Type.
func (*State) Xmove ¶
Exchange 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 (*State) Yield ¶
Yields a coroutine.
This function should only be called as the return expression of a Go function, as follows:
return s.Yield(nresults)
When a Go function calls Yield in that way, the running coroutine suspends its execution, and the call to Resume that started this coroutine returns. The parameter nresults is the number of values from the stack that are passed as results to Resume.