luajit

package
v0.0.0-...-17c0c07 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2015 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
const (
	LUA_REGISTRYINDEX = int(C.LUA_REGISTRYINDEX)
	LUA_ENVIRONINDEX  = int(C.LUA_ENVIRONINDEX)
	LUA_GLOBALSINDEX  = int(C.LUA_GLOBALSINDEX)
)

Index constants

View Source
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

View Source
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

View Source
var GlobalMutex *sync.Mutex = &sync.Mutex{}

Functions

This section is empty.

Types

type Gofunction

type Gofunction func(*State) int

A Gofunction is a Go function that may be registered with the Lua interpreter and called by Lua programs.

In order to communicate properly with Lua, a Go function must use the following protocol, which defines the way parameters and results are passed: a Go function receives its arguments from Lua in its stack in direct order (the first argument is pushed first). So, when the function starts, s.Gettop returns the number of arguments received by the function. The first argument (if any) is at index 1 and its last argument is at index s.Gettop. To return values to Lua, a Go function just pushes them onto the stack, in direct order (the first result is pushed first), and returns the number of results. Any other value in the stack below the results will be properly discarded by Lua. Like a Lua function, a Go function called by Lua can also return many results.

As an example, the following function receives a variable number of numerical arguments and returns their average and sum:

func foo(s *luajit.State) int {
	n := s.Gettop()		// number of arguments
	sum := 0.0
	for i := 1; i <= n; i++ {
		if !s.Isnumber(i) {
			s.Pushstring("incorrect argument")
			s.Error()
		}
		sum += s.Tonumber(i)
	}
	s.Pushnumber(sum/n)	// first result
	s.Pushnumber(sum)	// second result
	return 2		// number of results
}

type Gometatable

type Gometatable struct {
	IndexFunction    Gofunction
	NewindexFunction Gofunction
	TostringFunction Gofunction
	GCFunction       Gofunction
}

func (*Gometatable) GC

func (this *Gometatable) GC() Gofunction

func (*Gometatable) Index

func (this *Gometatable) Index() Gofunction

func (*Gometatable) Newindex

func (this *Gometatable) Newindex() Gofunction

func (*Gometatable) Tostring

func (this *Gometatable) Tostring() Gofunction

type GovalueRegistry

type GovalueRegistry struct {
	// contains filtered or unexported fields
}
var Gvregistry *GovalueRegistry = NewGovalueRegistry()

gvregistry holds golang values in which their registry indexes are to be passed to the C runtime. upon return from the C runtime, we can use the Registry Indexes sent back from C to obtain our real values. NEVER EVER SEND GOLANG POINTERS TO C. golang may change pointer values during scheduling and GC, so references to go pointers in C may age-out

func NewGovalueRegistry

func NewGovalueRegistry() *GovalueRegistry

func (*GovalueRegistry) AddValue

func (this *GovalueRegistry) AddValue(govalue interface{}) int

func (*GovalueRegistry) GetValue

func (this *GovalueRegistry) GetValue(INDEX int) (interface{}, error)

func (*GovalueRegistry) RemoveValue

func (this *GovalueRegistry) RemoveValue(INDEX int) error

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

func (this *State) Call(nargs, nresults int)

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

func (this *State) Checkstack(extra int) bool

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

func (this *State) Concat(n int)

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

func (this *State) Createtable(narr, nrec int)

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

func (this *State) Dofile(path string) int

Dofile Loads and runs the given file. It returns 0 if there are no errors or 1 in case of errors.

func (*State) Dostring

func (this *State) Dostring(str string) int

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

func (this *State) Getfield(index int, k string)

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

func (*State) Getglobal

func (this *State) Getglobal(name string)

Pushes onto the stack the value of the global name.

func (*State) Gettable

func (this *State) Gettable(index int)

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

func (this *State) Gettop() int

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) Init

func (this *State) Init()

Init configures internal values of the luajit.State object. This is called automatically by luajit.Newstate() and should only be called if the State struct was instantiated manually.

func (*State) Insert

func (this *State) Insert(index int)

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

func (this *State) Isboolean(index int) bool

Returns true if the value at the given valid index has type boolean, and false otherwise.

func (*State) Isfunction

func (this *State) Isfunction(index int) bool

Returns true if the value at the given valid index is a function (either Go or Lua), and false otherwise.

func (*State) Isgofunction

func (this *State) Isgofunction(index int) bool

Returns true if the value at the given valid index is a Go function, and false otherwise.

func (*State) Islightuserdata

func (this *State) Islightuserdata(index int) bool

Returns true if the value at the given valid index is light userdata, and false otherwise.

func (*State) Isnil

func (this *State) Isnil(index int) bool

Returns true if the value at the given valid index is nil, and false otherwise.

func (*State) Isnone

func (this *State) Isnone(index int) bool

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

func (this *State) Isnoneornil(index int) bool

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

func (this *State) Isnumber(index int) bool

Returns true if the value at the given valid index is a number, and false otherwise.

func (*State) Isstring

func (this *State) Isstring(index int) bool

Returns true if the value at the given valid index is a string, and false otherwise.

func (*State) Istable

func (this *State) Istable(index int) bool

Returns true if the value at the given valid index is a table, and false otherwise.

func (*State) Isthread

func (this *State) Isthread(index int) bool

Returns true if the value at the given valid index is a thread, and false otherwise.

func (*State) Isuserdata

func (this *State) Isuserdata(index int) bool

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

func (*State) Loadfile

func (this *State) Loadfile(filename string) error

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.

http://www.lua.org/manual/5.1/manual.html#luaL_loadfile

func (*State) Loadstring

func (this *State) Loadstring(str string) error

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() *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

func (this *State) Pcall(nargs, nresults, errfunc int) error

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) Pop

func (this *State) Pop(index int)

Pops n elements from the stack.

func (*State) Pushclosure

func (this *State) Pushclosure(fn Gofunction, n int)

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 Gofunction)

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 Gofunction).

func (*State) Pushmetatable

func (this *State) Pushmetatable(mt *Gometatable)

Pushmetatable pushes a Gometatable struct to the stack and conducts the appropriate mapping of metatable keys to Gofunctions.

you must still use Setmetatable after this function returns to assign your metatable to some other table on the stack.

func (*State) Pushmodule

func (this *State) Pushmodule(name string, fn Gofunction)

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

func (this *State) Pushnumber(n float64)

Pushes a number with value n onto the stack.

func (*State) Pushstring

func (this *State) Pushstring(str string)

Pushes the string str onto the stack.

func (*State) Pushthread

func (this *State) Pushthread() int

Pushes the thread represented by s onto the stack. Returns 1 if this thread is the main thread of its state.

func (*State) Pushvalue

func (this *State) Pushvalue(index int)

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

func (*State) Rawequal

func (this *State) Rawequal(i1, i2 int) bool

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) Rawget

func (this *State) Rawget(index int)

Similar to Gettable, but does a raw access (i.e., without metamethods).

func (*State) Rawgeti

func (this *State) Rawgeti(index, n int)

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) Rawset

func (this *State) Rawset(index int)

Similar to Settable, but does a raw assignment (i.e., without metamethods).

func (*State) Rawseti

func (this *State) Rawseti(index, n int)

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) Register

func (this *State) Register(fn Gofunction, name string)

Sets the Go function fn as the new value of global name.

func (*State) Remove

func (this *State) Remove(index int)

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

func (this *State) Replace(index int)

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

func (this *State) Resume(narg int) (yield bool, e error)

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

func (this *State) Setfield(index int, k string)

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) Setglobal

func (this *State) Setglobal(name string)

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

func (*State) Setmetatable

func (this *State) Setmetatable(index int) int

Pops a table from the stack and sets it as the new metatable for the value at the given valid index.

func (*State) Settable

func (this *State) Settable(index int)

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

func (this *State) Settop(index int)

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

func (this *State) Setupvalue(funcindex, n int) (string, error)

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

func (this *State) Status() int

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

func (this *State) Toboolean(index int) bool

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

func (this *State) Tointeger(index int) int

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

func (this *State) Tonumber(index int) float64

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

func (this *State) Topointer(index int) unsafe.Pointer

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

func (this *State) Tostring(index int) string

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

func (this *State) Tothread(index int) *State

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

func (this *State) Touserdata(index int) unsafe.Pointer

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

func (this *State) Type(index int) int

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

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

Returns the name of the type encoded by the value tp, which must be one the values returned by Type.

func (*State) Xmove

func (this *State) Xmove(from *State, n int)

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

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

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.

Jump to

Keyboard shortcuts

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