Documentation ¶
Overview ¶
Go Lua Compiler and VM
This is a Lua 5.3 VM and compiler written in Go. This is intended to allow easy embedding into Go programs, with minimal fuss and bother.
Index ¶
- Constants
- type STypeID
- type State
- func (l *State) AbsIndex(i int) int
- func (l *State) Arith(op opCode)
- func (l *State) Call(args, rtns int)
- func (l *State) Compare(i1, i2 int, op opCode) bool
- func (l *State) CompareRaw(i1, i2 int, op opCode) bool
- func (l *State) Count(i int) int
- func (l *State) Dump(i int, strip bool) []byte
- func (l *State) Error()
- func (l *State) ForEach(t int, f func() bool)
- func (l *State) ForEachRaw(t int, f func() bool)
- func (l *State) GetIter(i int)
- func (l *State) GetMetaField(i int, name string) TypeID
- func (l *State) GetMetaTable(i int) bool
- func (l *State) GetRaw(i int) interface{}
- func (l *State) GetTable(i int) TypeID
- func (l *State) GetTableRaw(i int) TypeID
- func (l *State) Insert(i int)
- func (l *State) IsNil(i int) bool
- func (l *State) Length(i int) int
- func (l *State) LengthRaw(i int) int
- func (l *State) LoadBinary(in io.Reader, name string, env int) error
- func (l *State) LoadString(src, name string, env int) error
- func (l *State) LoadText(in io.Reader, name string, env int) error
- func (l *State) NewTable(as, hs int)
- func (l *State) OptFloat(i int, d float64) float64
- func (l *State) OptInteger(i int, d int64) int64
- func (l *State) OptString(i int, d string) string
- func (l *State) PCall(args, rtns int, trace bool) (msg interface{})
- func (l *State) Pop(n int)
- func (l *State) Preload(name string, loader func(*State) int)
- func (l *State) PrintStack()
- func (l *State) Push(v interface{})
- func (l *State) PushClosure(f func(*State) int, v ...int)
- func (l *State) PushIndex(i int)
- func (l *State) STypeOf(i int) STypeID
- func (l *State) Set(d, s int)
- func (l *State) SetGlobal(name string)
- func (l *State) SetMetaTable(i int)
- func (l *State) SetTable(i int)
- func (l *State) SetTableRaw(i int)
- func (l *State) SetUpValue(f, i, v int) bool
- func (l *State) ToBoolean(i int) bool
- func (l *State) ToFloat(i int) float64
- func (l *State) ToInteger(i int) int64
- func (l *State) ToString(i int) string
- func (l *State) TryFloat(i int) (float64, error)
- func (l *State) TryInteger(i int) (int64, error)
- func (l *State) TypeOf(i int) TypeID
- type TypeID
Constants ¶
const ( // These exported values are used in calls to Arith OpAdd opCode OpSub OpMul OpMod OpPow OpDiv OpIDiv OpBinAND OpBinOR OpBinXOR OpBinShiftL OpBinShiftR OpUMinus OpBinNot OpEqual OpLessThan OpLessOrEqual )
const ( // Registry table index RegistryIndex = -1000000 - iota // Globals table index GlobalsIndex // To get a specific upvalue use "FirstUpVal-<upvalue index>" FirstUpVal )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type State ¶
type State struct { // Add a native stack trace to errors that have attached stack traces. NativeTrace bool // contains filtered or unexported fields }
State is the central arbitrator of all Lua operations.
func (*State) AbsIndex ¶
AbsIndex converts the given index into an absolute index. Use -1 as the index to get the number of items currently on the stack.
func (*State) Arith ¶
func (l *State) Arith(op opCode)
Arith performs the specified the arithmetic operator with the top two items on the stack (or just the top item for OpUMinus and OpBinNot). The result is pushed onto the stack. This may raise an error if they values are not appropriate for the given operator.
func (*State) Call ¶
Call runs a function with the given number of arguments and results. The function must be on the stack just before the first argument. If this raises an error the stack is NOT unwound! Call this only from code that is below a call to PCall unless you want your State to be permanently trashed!
func (*State) Compare ¶
Compare performs the specified the comparison operator with the items at the given stack indexes. This may raise an error if they values are not appropriate for the given operator.
func (*State) CompareRaw ¶
CompareRaw is exactly like Compare, but without meta-methods.
func (*State) Dump ¶
Dump converts the Lua function at the given index to a binary chunk. The returned value may be used with LoadBinary to get a function equivalent to the dumped function (but without the original function's up values). Currently the "strip" argument does nothing. This (obviously) only works with Lua functions, trying to dump a native function or a non-function value will raise an error.
func (*State) Error ¶
func (l *State) Error()
Error pops a value off the top of the stack and raises it as an error.
func (*State) ForEach ¶
ForEach is a fancy version of ForEachRaw that respects metamethods (to be specific, __pairs).
func (*State) ForEachRaw ¶
ForEachRaw is a simple wrapper around GetIter and is provided as a convenience. The given function is called once for every item in the table at t. For each call of the function the value is at -1 and the key at -2. You MUST keep the stack balanced inside the function! Do not pop the key and value off the stack before returning! The value returned by the iteration function determines if ForEach should return early. Return false to break, return true to continue to the next iteration.
func (*State) GetIter ¶
GetIter pushes a table iterator onto the stack. If the given value is not a table this will raise an error.
func (*State) GetMetaField ¶
GetMetaField pushes the meta method with the given name for the item at the given index onto the stack, then returns the type of the pushed item. If the item does not have a meta table or does not have the specified method this does nothing and returns TypNil.
func (*State) GetMetaTable ¶
GetMetaTable gets the meta table for the value at the given index and pushes it onto the stack. If the value does not have a meta table then this returns false and pushes nothing.
func (*State) GetRaw ¶
GetRaw gets the raw data for a Lua value. Lua types use the following mapping:
nil -> nil number -> int64 or float64 string -> string bool -> bool table -> string: "table: <pointer as hexadecimal>" function -> string: "function: <pointer as hexadecimal>" userdata -> The raw user data value
func (*State) GetTable ¶
GetTable reads from the table at the given index, popping the key from the stack and pushing the result. The type of the pushed object is returned. This may raise an error if the value is not a table or is lacking the __index meta method.
func (*State) GetTableRaw ¶
GetTableRaw is like GetTable except it ignores meta methods. This may raise an error if the value is not a table.
func (*State) IsNil ¶
IsNil check if the value at the given index is nil. Nonexistent values are always nil.
func (*State) Length ¶
Returns the "length" of the item at the given index, exactly like the "#" operator would. If this calls a meta method it may raise an error if the length is not an integer.
func (*State) LengthRaw ¶
Returns the length of the table or string at the given index. This does not call meta methods. If the value is not a table or string this will raise an error.
func (*State) LoadBinary ¶
LoadBinary loads a binary chunk into memory and pushes the result onto the stack. If there is an error it is returned and nothing is pushed. Set env to 0 to use the default environment.
func (*State) LoadText ¶
LoadText loads a text chunk into memory and pushes the result onto the stack. If there is an error it is returned and nothing is pushed. Set env to 0 to use the default environment.
func (*State) NewTable ¶
NewTable creates a new table with "as" preallocated array elements and "hs" preallocated hash elements.
func (*State) OptFloat ¶
OptFloat is the same as ToFloat, except the given default is returned if the value is nil or non-existent.
func (*State) OptInteger ¶
OptInteger is the same as ToInt, except the given default is returned if the value is nil or non-existent.
func (*State) OptString ¶
OptString is the same as ToString, except the given default is returned if the value is nil or non-existent.
func (*State) PCall ¶
PCall is exactly like Call, except instead of panicking when it encounters an error the error is cleanly recovered and returned. On error the stack is reset to the way it was before the call minus the function and it's arguments, the State may then be reused.
func (*State) PrintStack ¶
func (l *State) PrintStack()
PrintStack prints some stack information for sanity checking during test runs.
func (*State) Push ¶
func (l *State) Push(v interface{})
Push pushes the given value onto the stack. If the value is not one of nil, float32, float64, int, int32, int64, string or bool, it is converted to a userdata value before being pushed.
func (*State) PushClosure ¶
PushClosure pushes a native function as a closure.
func (*State) Set ¶
Set sets the value at index d to the value at index s (d = s). Trying to set the registry or an invalid index will do nothing. Setting an absolute index will never fail, the stack will be extended as needed. Be careful not to waste stack space or you could run out of memory! This function is mostly for setting up-values and things like that.
func (*State) SetGlobal ¶
SetGlobal pops a value from the stack and sets it as the new value of global name.
func (*State) SetMetaTable ¶
SetMetaTable pops a table from the stack and sets it as the meta table of the value at the given index. If the value is not a userdata or table then the meta table is set for ALL values of that type! If you try to set a metatable that is not a table or try to pass an invalid type this will raise an error.
func (*State) SetTable ¶
SetTable writes to the table at the given index, popping the key and value from the stack. This may raise an error if the value is not a table or is lacking the __newindex meta method. The value must be on TOS, the key TOS-1.
func (*State) SetTableRaw ¶
SetTableRaw is like SetTable except it ignores meta methods. This may raise an error if the value is not a table.
func (*State) SetUpValue ¶
SetUpVal sets upvalue "i" in the function at "f" to the value at "v". If the upvalue index is out of range, "f" is not a function, or the upvalue is not closed, false is returned and nothing is done, else returns true and sets the upvalue. Any other functions that share this upvalue will also be affected!
func (*State) ToBoolean ¶
ToBoolean reads a value from the stack at the given index and interprets it as a boolean.
func (*State) ToFloat ¶
ToFloat reads a floating point value from the stack at the given index. If the value is not an float and cannot be converted to one this may panic.
func (*State) ToInteger ¶
ToInteger reads an integer value from the stack at the given index. If the value is not an integer and cannot be converted to one this may panic.
func (*State) ToString ¶
ToString reads a value from the stack at the given index and formats it as a string. This will call a __tostring metamethod if provided. This is safe if no metamethods are called, but may panic if the metamethod errors out.
func (*State) TryFloat ¶
TryFloat attempts to read the value at the given index as a floating point number.
func (*State) TryInteger ¶
TryInteger attempts to read the value at the given index as a integer number.