Documentation
¶
Index ¶
- Constants
- func Call(t *Thread, f Value, args []Value, next Cont) error
- func ClearInterrupt(r *Runtime)
- func CompileLuaChunk(name string, source []byte) (*code.Unit, error)
- func Interrupt(r *Runtime)
- func IsNil(v Value) bool
- func Lt(t *Thread, x, y Value) (bool, error)
- func Metacall(t *Thread, obj Value, method string, args []Value, next Cont) (error, bool)
- func ParseLuaChunk(name string, source []byte) (*ast.BlockStat, error)
- func Push(c Cont, vals ...Value)
- func RawEqual(x, y Value) (bool, bool)
- func SetEnv(t *Table, name string, v Value)
- func SetEnvGoFunc(t *Table, name string, f func(*Thread, *GoCont) (Cont, error), nArgs int, ...)
- func SetEnvGoFuncContext(t *Table, name string, f func(context.Context, *Thread, *GoCont) (Cont, error), ...)
- func SetIndex(t *Thread, coll Value, idx Value, val Value) error
- func ToNumber(x Value) (Value, NumberType)
- func Traceback(err error) string
- func Truth(v Value) bool
- func WrapWithContext(err error, c Cont) error
- func WriteConst(w io.Writer, c Konst) (err error)
- type Bool
- type Callable
- type Cell
- type Closure
- type Code
- type Cont
- type Contexter
- type DebugInfo
- type Float
- type GoCont
- func (c *GoCont) Arg(n int) Value
- func (c *GoCont) Args() []Value
- func (c *GoCont) CallableArg(n int) (Callable, error)
- func (c *GoCont) Check1Arg() error
- func (c *GoCont) CheckNArgs(n int) error
- func (c *GoCont) ClosureArg(n int) (*Closure, error)
- func (c *GoCont) DebugInfo() *DebugInfo
- func (c *GoCont) Etc() []Value
- func (c *GoCont) FloatArg(n int) (Float, error)
- func (c *GoCont) IntArg(n int) (Int, error)
- func (c *GoCont) NArgs() int
- func (c *GoCont) Next() Cont
- func (c *GoCont) Push(v Value)
- func (c *GoCont) PushEtc(etc []Value)
- func (c *GoCont) PushingNext(vals ...Value) Cont
- func (c *GoCont) RunInThread(t *Thread) (Cont, error)
- func (c *GoCont) StringArg(n int) (String, error)
- func (c *GoCont) TableArg(n int) (*Table, error)
- func (c *GoCont) ThreadArg(n int) (*Thread, error)
- type GoFunction
- type Int
- type Konst
- type LightUserData
- type LuaCont
- type NumberType
- type Runtime
- func (r *Runtime) GlobalEnv() *Table
- func (r *Runtime) Interrupt() <-chan struct{}
- func (r *Runtime) MainThread() *Thread
- func (r *Runtime) Metatable(v Value) Value
- func (r *Runtime) RawMetatable(v Value) *Table
- func (r *Runtime) Registry(key Value) Value
- func (r *Runtime) SetRawMetatable(v Value, meta *Table)
- func (r *Runtime) SetRegistry(k, v Value)
- func (r *Runtime) SetStringMeta(meta *Table)
- type String
- type SyntaxError
- type Table
- func (t *Table) Get(k Value) Value
- func (t *Table) Len() Int
- func (t *Table) MarshalJSON() ([]byte, error)
- func (t *Table) Metatable() *Table
- func (t *Table) Next(k Value) (next Value, val Value, ok bool)
- func (t *Table) Set(k, v Value)
- func (t *Table) SetCheck(k, v Value) error
- func (t *Table) SetMetatable(m *Table)
- type Termination
- func (c *Termination) DebugInfo() *DebugInfo
- func (c *Termination) Etc() []Value
- func (c *Termination) Get(n int) Value
- func (c *Termination) Next() Cont
- func (c *Termination) Push(v Value)
- func (c *Termination) PushEtc(etc []Value)
- func (c *Termination) Reset()
- func (c *Termination) RunInThread(_ *Thread) (Cont, error)
- type Thread
- func (t *Thread) CurrentCont() Cont
- func (t *Thread) IsMain() bool
- func (t *Thread) Resume(caller *Thread, args []Value) ([]Value, error)
- func (t *Thread) RunContinuation(c Cont) (err error)
- func (t *Thread) Start(c Callable)
- func (t *Thread) Status() ThreadStatus
- func (t *Thread) Yield(args []Value) ([]Value, error)
- type ThreadStatus
- type UserData
- type Value
- func Call1(t *Thread, f Value, args ...Value) (Value, error)
- func Concat(t *Thread, x, y Value) (Value, error)
- func Index(t *Thread, coll Value, k Value) (Value, error)
- func Len(t *Thread, v Value) (Value, error)
- func Mod(t *Thread, x Value, y Value) (Value, error)
- func RawGet(t *Table, k Value) Value
- type Valuer
Constants ¶
const (
// ConstTypeMaj is bigger than any const type
ConstTypeMaj
)
Variables ¶
This section is empty.
Functions ¶
func Call ¶
Call calls f with arguments args, pushing the results on next. It may use the metamethod '__call' if f is not callable.
func ClearInterrupt ¶
func ClearInterrupt(r *Runtime)
ClearInterrupt clears any pending interrupt in the given runtime r.
func CompileLuaChunk ¶
CompileLuaChunk parses and compiles the source as a Lua Chunk and returns the compile code Unit.
func Interrupt ¶
func Interrupt(r *Runtime)
Interrupt issues an interrupt that will abort any executing continuations in the given runtime r. The caller should call ClearInterrupt before resuming execution in order to ensure that any unconsumed interrupt has been cleared.
func Metacall ¶
Metacall calls the metamethod called method on obj with the given arguments args, pushing the result to the continuation next.
func ParseLuaChunk ¶
ParseLuaChunk parses a string as a Lua statement and returns the AST.
func RawEqual ¶
RawEqual returns two values. The second one is true if raw equality makes sense for x and y. The first one returns whether x and y are raw equal.
func SetEnvGoFunc ¶
func SetEnvGoFunc(t *Table, name string, f func(*Thread, *GoCont) (Cont, error), nArgs int, hasEtc bool)
SetEnvGoFunc sets the item in the table t for a string key to be a GoFunction defined by f. Useful when writing libraries
func SetEnvGoFuncContext ¶
func SetEnvGoFuncContext(t *Table, name string, f func(context.Context, *Thread, *GoCont) (Cont, error), nArgs int, hasEtc bool)
SetEnvGoFuncContext sets the item in the table t for a string key to be a GoFunction defined by f. Useful when writing libraries
func SetIndex ¶
SetIndex sets the item in a collection for the given key, using the '__newindex' metamethod if appropriate.
func ToNumber ¶
func ToNumber(x Value) (Value, NumberType)
ToNumber returns x as a Float or Int, and the type (IsFloat, IsInt or NaN).
func Traceback ¶ added in v0.1.4
Traceback returns a string that represents the traceback of the error using its context.
func WrapWithContext ¶ added in v0.1.4
WrapWithContext wraps the given error up as a new error with context c.
Types ¶
type Cell ¶
type Cell struct {
// contains filtered or unexported fields
}
Cell is the data structure that represents a reference to a value. Whenever a value is put into a register that contains a cell, it is put in the cell rather than the register itself. It is used in order to implement upvalues in lua. Example:
local x local function f() return x + 1 end x = 3 f()
The variable x is an upvalue in f so when x is set to 3 the upvalue of f must be set to 3. This is achieved by setting the register that contains x to a Cell.
type Closure ¶
Closure is the data structure that represents a Lua function. It is simply a reference to a Code instance and a set of upvalues.
func CompileAndLoadLuaChunk ¶
CompileAndLoadLuaChunk pares, compiles and loads a Lua chunk from source and returns the closure that runs the chunk in the given global environment.
func LoadLuaUnit ¶
LoadLuaUnit turns a code unit into a closure given an environment env.
func NewClosure ¶
NewClosure returns a pointer to a new Closure instance for the given code.
func (*Closure) AddUpvalue ¶
AddUpvalue append a new upvalue to the closure.
func (*Closure) Continuation ¶
Continuation implements Callable.Continuation
func (*Closure) GetUpvalue ¶
GetUpvalue returns the upvalue for c at index n.
func (*Closure) SetUpvalue ¶
SetUpvalue sets the upvalue for c at index n to v.
type Code ¶
type Code struct { UpvalueCount int16 UpNames []string RegCount int16 // contains filtered or unexported fields }
Code represents the code for a Lua function together with all the constants that this function uses. It can be turned into a closure by adding upvalues.
func (*Code) RefactorConsts ¶
RefactorConsts returns an equivalent *Code this consts "refactored", which means that the consts are slimmed down to only contains the constants required for the function.
type Cont ¶
type Cont interface { Push(Value) PushEtc([]Value) RunInThread(*Thread) (Cont, error) Next() Cont DebugInfo() *DebugInfo }
Cont is an interface for things that can be run in a Thread. Implementations of Cont a typically an invocation of a Lua function or an invocation of a Go function.
TODO: document the methods.
func ContWithArgs ¶
ContWithArgs is a convenience function that returns a new continuation from a callable, some arguments and a next continuation.
type Contexter ¶ added in v0.1.4
type Contexter interface {
Context() Cont
}
Contexter is the interface satisfied by the Context method.
type DebugInfo ¶
DebugInfo contains info about a continuation that can be looked at for debuggin purposes (and tracebacks)
type Float ¶
type Float float64
Float is a runtime floating point numeric value.
func (Float) ToInt ¶
func (f Float) ToInt() (Int, NumberType)
ToInt turns a Float into an Int if possible.
type GoCont ¶
type GoCont struct {
// contains filtered or unexported fields
}
GoCont implements Cont for functions written in Go.
func NewGoCont ¶
func NewGoCont(f *GoFunction, next Cont) *GoCont
NewGoCont returns a new pointer to GoCont for the given GoFunction and Cont.
func (*GoCont) CallableArg ¶
CallableArg returns the n-th argument as a callable if possible, otherwise a non-nil error. No range check!
func (*GoCont) Check1Arg ¶
Check1Arg returns a non-nil error if the continuation doesn't have at least one arg.
func (*GoCont) CheckNArgs ¶
CheckNArgs returns a non-nil error if the continuation doesn't have at least n args.
func (*GoCont) ClosureArg ¶
ClosureArg returns the n-th argument as a closure if possible, otherwise a non-nil error. No range check!
func (*GoCont) FloatArg ¶
FloatArg returns the n-th argument as a Float if possible, otherwise a non-nil error. No range check!
func (*GoCont) IntArg ¶
IntArg returns the n-th argument as an Int if possible, otherwise a non-nil error. No range check!
func (*GoCont) PushEtc ¶
PushEtc pushes a slice of values to the continutation. TODO: find why this is not used.
func (*GoCont) PushingNext ¶
PushingNext is convenient when implementing go functions. It pushes the given values to c.Next() and returns it.
func (*GoCont) RunInThread ¶
RunInThread implements Cont.RunInThread
func (*GoCont) StringArg ¶
StringArg returns the n-th argument as a string if possible, otherwise a non-nil error. No range check!
type GoFunction ¶
type GoFunction struct {
// contains filtered or unexported fields
}
A GoFunction is a callable value implemented by a native Go function.
func NewGoFunction ¶
func NewGoFunction(f func(*Thread, *GoCont) (Cont, error), name string, nArgs int, hasEtc bool) *GoFunction
NewGoFunction returns a new GoFunction.
func NewGoFunctionContext ¶
func NewGoFunctionContext(f func(context.Context, *Thread, *GoCont) (Cont, error), name string, nArgs int, hasEtc bool) *GoFunction
NewGoFunctionContext returns a new GoFunction.
func (*GoFunction) Continuation ¶
func (f *GoFunction) Continuation(next Cont) Cont
Continuation implements Callable.Continuation.
func (*GoFunction) HasEtc ¶
func (f *GoFunction) HasEtc() bool
HasEtc returns true iff the GoFunction has an Etc.
func (*GoFunction) NArgs ¶
func (f *GoFunction) NArgs() int
NArgs returns the number of arguments the GoFunction tables.
func (*GoFunction) Name ¶
func (f *GoFunction) Name() string
Name returns the name assigned to the GoFunction.
type Int ¶
type Int int64
Int is a runtime integral numeric value.
type Konst ¶
type Konst interface { Value // contains filtered or unexported methods }
Konst is a runtime value that is a constant
type LightUserData ¶
type LightUserData struct {
Data interface{}
}
A LightUserData is some Go value of unspecified type wrapped to be used as a lua Value.
func (*LightUserData) MarshalJSON ¶
func (d *LightUserData) MarshalJSON() ([]byte, error)
MarshalJSON marshals the user data to JSON.
type LuaCont ¶
type LuaCont struct { *Closure // contains filtered or unexported fields }
LuaCont is a Lua continuation, made from a closure, values for registers and some state.
func NewLuaCont ¶
NewLuaCont returns a new LuaCont from a closure and next, a continuation to push results into.
type NumberType ¶
type NumberType uint16
NumberType represents a type of number
const ( // IsFloat is the type of Floats IsFloat NumberType = 1 << iota // IsInt is the type of Ints IsInt // NaN is the type of values which are not numbers NaN // NaI is a type for values which a not Ints NaI )
type Runtime ¶
A Runtime is a Lua runtime. It contains all the global state of the runtime (in particular a reference to the global environment and the main thread).
func (*Runtime) Interrupt ¶
func (r *Runtime) Interrupt() <-chan struct{}
Interrupt returns a channel down which will fire when an interrupt has been requested.
func (*Runtime) MainThread ¶
MainThread returns the runtime's main thread.
func (*Runtime) Metatable ¶
Metatable returns the metatalbe of v (looking for '__metatable' in the raw metatable).
func (*Runtime) RawMetatable ¶
RawMetatable returns the raw metatable for a value (that is, not looking at the metatable's '__metatable' key).
func (*Runtime) Registry ¶
Registry returns the Value associated with key in the runtime's registry.
func (*Runtime) SetRawMetatable ¶
SetRawMetatable sets the metatable for value v to meta.
func (*Runtime) SetRegistry ¶
SetRegistry sets the value associated with the key k to v in the registry.
func (*Runtime) SetStringMeta ¶
SetStringMeta sets the runtime's string metatable (all strings in a runtime have the same metatable).
type String ¶
type String string
String is a runtime string value.
func AsString ¶
AsString returns x as a String and a boolean which is true if this is a 'good' conversion. TODO: refactor or explain the meaning of the boolean better.
func (String) NormPos ¶
NormPos returns a normalised position in the string i.e. -1 -> len(s)
-2 -> len(s) - 1
etc
func (String) ToInt ¶
func (s String) ToInt() (Int, NumberType)
ToInt turns a String into and Int if possible.
func (String) ToNumber ¶
func (s String) ToNumber() (Value, NumberType)
ToNumber turns a String into a numeric value (Int or Float) if possible.
type SyntaxError ¶
A SyntaxError is a lua syntax error.
func NewSyntaxError ¶
func NewSyntaxError(file string, err parsing.Error) *SyntaxError
NewSyntaxError returns a pointer to a SyntaxError for the error err in file.
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string
Error implements the error interface.
func (*SyntaxError) IsUnexpectedEOF ¶
func (e *SyntaxError) IsUnexpectedEOF() bool
IsUnexpectedEOF returns true if the error signals that EOF was encountered when further tokens were required.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table implements a Lua table.
func (*Table) MarshalJSON ¶
MarshalJSON marshals the table to JSON.
func (*Table) SetMetatable ¶
SetMetatable sets the table's metatable.
type Termination ¶
type Termination struct {
// contains filtered or unexported fields
}
Termination is a 'dead-end' continuation: it cannot be run.
func NewTermination ¶
func NewTermination(args []Value, etc *[]Value) *Termination
NewTermination returns a new pointer to Termination where the first len(args) values will be pushed into args and the remaining ones will be added to etc if it is non nil, dropped otherwise.
func NewTerminationWith ¶
func NewTerminationWith(nArgs int, hasEtc bool) *Termination
NewTerminationWith creates a new Termination expecting nArgs args and possibly gathering extra args into an etc if hasEtc is true.
func (*Termination) DebugInfo ¶
func (c *Termination) DebugInfo() *DebugInfo
DebugInfo implements Cont.DebugInfo.
func (*Termination) Etc ¶
func (c *Termination) Etc() []Value
Etc returns all the extra args pushed to the termination.
func (*Termination) Get ¶
func (c *Termination) Get(n int) Value
Get returns the n-th arg pushed to the termination.
func (*Termination) Push ¶
func (c *Termination) Push(v Value)
Push implements Cont.Push. It just accumulates values into a slice.
func (*Termination) PushEtc ¶
func (c *Termination) PushEtc(etc []Value)
PushEtc implements Cont.PushEtc.
func (*Termination) Reset ¶
func (c *Termination) Reset()
Reset erases all the args pushed to the termination.
func (*Termination) RunInThread ¶
func (c *Termination) RunInThread(_ *Thread) (Cont, error)
RunInThread implements Cont.RunInThread. A termination exits immediately so it always returns nil.
type Thread ¶
type Thread struct { *Runtime // contains filtered or unexported fields }
A Thread is a lua thread.
The mutex guarantees that if status == ThreadRunning, then caller is not nil.
func NewThread ¶
NewThread creates a new thread. Its initial status is suspended. Call Resume to run it.
func (*Thread) CurrentCont ¶
CurrentCont returns the continuation currently running (or suspended) in the thread.
func (*Thread) Resume ¶
Resume execution of a suspended thread. Its status switches to running while its caller's status switches to suspended.
func (*Thread) RunContinuation ¶
RunContinuation runs the continuation c in the thread. It keeps running until the next continuation is nil or an error occurs, in which case it returns the error.
func (*Thread) Start ¶
Start starts the thread in a goroutine, giving it the callable c to run. the t.Resume() method needs to be called to provide arguments to the callable.
func (*Thread) Status ¶
func (t *Thread) Status() ThreadStatus
Status returns the status of a thread (suspended, running or dead).
type ThreadStatus ¶
type ThreadStatus uint
ThreadStatus is the type of a thread status
const ( ThreadOK ThreadStatus = 0 ThreadSuspended ThreadStatus = 1 ThreadDead ThreadStatus = 3 )
Available statuses for threads.
func (ThreadStatus) String ¶
func (s ThreadStatus) String() string
String returns a string description of the thread status.
type UserData ¶
type UserData struct {
// contains filtered or unexported fields
}
A UserData is a Go value of any type wrapped to be used as a Lua value. It has a metatable which may allow Lua code to interact with it.
func NewUserData ¶
NewUserData returns a new UserData pointer for the value v, giving it meta as a metatable.
func (*UserData) MarshalJSON ¶
MarshalJSON marshals the userdata to JSON.
func (*UserData) SetMetatable ¶
SetMetatable sets d's metatable to m.
type Value ¶
type Value interface{}
Value is a runtime value.
func Call1 ¶
Call1 is a convenience method that calls f with arguments args and returns exactly one value.
func Index ¶
Index returns the item in a collection for the given key k, using the '__index' metamethod if appropriate.