runtime

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: May 23, 2022 License: Apache-2.0, MIT Imports: 18 Imported by: 30

Documentation

Index

Constants

View Source
const (

	// ConstTypeMaj is bigger than any const type
	ConstTypeMaj
)

Variables

This section is empty.

Functions

func Call

func Call(t *Thread, f Value, args []Value, next Cont) error

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

func CompileLuaChunk(name string, source []byte) (*code.Unit, error)

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 IsNil

func IsNil(v Value) bool

IsNil returns true if v is a nil value.

func Lt

func Lt(t *Thread, x, y Value) (bool, error)

Lt returns whether x < y is true (and an error if it's not possible to compare them).

func Metacall

func Metacall(t *Thread, obj Value, method string, args []Value, next Cont) (error, bool)

Metacall calls the metamethod called method on obj with the given arguments args, pushing the result to the continuation next.

func ParseLuaChunk

func ParseLuaChunk(name string, source []byte) (*ast.BlockStat, error)

ParseLuaChunk parses a string as a Lua statement and returns the AST.

func Push

func Push(c Cont, vals ...Value)

Push is a convenience method that pushes a number of values to the continuation c.

func RawEqual

func RawEqual(x, y Value) (bool, bool)

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 SetEnv

func SetEnv(t *Table, name string, v Value)

SetEnv sets the item in the table t for a string key. Useful when writing libraries

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

func SetIndex(t *Thread, coll Value, idx Value, val Value) error

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

func Traceback(err error) string

Traceback returns a string that represents the traceback of the error using its context.

func Truth

func Truth(v Value) bool

Truth returns true if v is neither nil nor a false boolean.

func WrapWithContext added in v0.1.4

func WrapWithContext(err error, c Cont) error

WrapWithContext wraps the given error up as a new error with context c.

func WriteConst

func WriteConst(w io.Writer, c Konst) (err error)

WriteConst serializes a const value to the writer w.

Types

type Bool

type Bool bool

Bool is a runtime boolean value.

type Callable

type Callable interface {
	Continuation(Cont) Cont
}

Callable is the interface for callable values.

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.

func NewCell

func NewCell(v Value) Cell

NewCell returns a new Cell instance containing the given value.

func (Cell) Get

func (c Cell) Get() Value

Get returns the value that the cell c contains

func (Cell) Set

func (c Cell) Set(v Value)

Set sets the the value contained by c to v.

type Closure

type Closure struct {
	*Code
	Upvalues []Cell
	// contains filtered or unexported fields
}

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

func CompileAndLoadLuaChunk(name string, source []byte, env Value) (*Closure, error)

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

func LoadLuaUnit(unit *code.Unit, env Value) *Closure

LoadLuaUnit turns a code unit into a closure given an environment env.

func NewClosure

func NewClosure(c *Code) *Closure

NewClosure returns a pointer to a new Closure instance for the given code.

func (*Closure) AddUpvalue

func (c *Closure) AddUpvalue(cell Cell)

AddUpvalue append a new upvalue to the closure.

func (*Closure) Continuation

func (c *Closure) Continuation(next Cont) Cont

Continuation implements Callable.Continuation

func (*Closure) GetUpvalue

func (c *Closure) GetUpvalue(n int) Value

GetUpvalue returns the upvalue for c at index n.

func (*Closure) SetUpvalue

func (c *Closure) SetUpvalue(n int, val Value)

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

func (c *Code) Name() string

Name returns the name.

func (*Code) RefactorConsts

func (c *Code) RefactorConsts() *Code

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.

func (*Code) Source

func (c *Code) Source() string

Source returns the source.

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

func ContWithArgs(c Callable, args []Value, next Cont) Cont

ContWithArgs is a convenience function that returns a new continuation from a callable, some arguments and a next continuation.

func Continue

func Continue(t *Thread, f Value, next Cont) (Cont, error)

Continue tries tried to continue the value f or else use its '__call' metamethod and returns the continuations that needs to be run to get the results.

type Contexter added in v0.1.4

type Contexter interface {
	Context() Cont
}

Contexter is the interface satisfied by the Context method.

type DebugInfo

type DebugInfo struct {
	Source      string
	Name        string
	CurrentLine int32
}

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 ToFloat

func ToFloat(v Value) (Float, bool)

ToFloat returns v as a FLoat and true if v is a valid float.

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

func (c *GoCont) Arg(n int) Value

Arg returns the n-th arg of the continuation. It doesn't do any range check!

func (*GoCont) Args

func (c *GoCont) Args() []Value

Args returns the slice of args pushed to the continuation.

func (*GoCont) CallableArg

func (c *GoCont) CallableArg(n int) (Callable, error)

CallableArg returns the n-th argument as a callable if possible, otherwise a non-nil error. No range check!

func (*GoCont) Check1Arg

func (c *GoCont) Check1Arg() error

Check1Arg returns a non-nil error if the continuation doesn't have at least one arg.

func (*GoCont) CheckNArgs

func (c *GoCont) CheckNArgs(n int) error

CheckNArgs returns a non-nil error if the continuation doesn't have at least n args.

func (*GoCont) ClosureArg

func (c *GoCont) ClosureArg(n int) (*Closure, error)

ClosureArg returns the n-th argument as a closure if possible, otherwise a non-nil error. No range check!

func (*GoCont) DebugInfo

func (c *GoCont) DebugInfo() *DebugInfo

DebugInfo implements Cont.DebugInfo.

func (*GoCont) Etc

func (c *GoCont) Etc() []Value

Etc returns the etc args pushed to the continuation they exist.

func (*GoCont) FloatArg

func (c *GoCont) FloatArg(n int) (Float, error)

FloatArg returns the n-th argument as a Float if possible, otherwise a non-nil error. No range check!

func (*GoCont) IntArg

func (c *GoCont) IntArg(n int) (Int, error)

IntArg returns the n-th argument as an Int if possible, otherwise a non-nil error. No range check!

func (*GoCont) NArgs

func (c *GoCont) NArgs() int

NArgs returns the number of args pushed to the continuation.

func (*GoCont) Next

func (c *GoCont) Next() Cont

Next implements Cont.Next.

func (*GoCont) Push

func (c *GoCont) Push(v Value)

Push implements Cont.Push.

func (*GoCont) PushEtc

func (c *GoCont) PushEtc(etc []Value)

PushEtc pushes a slice of values to the continutation. TODO: find why this is not used.

func (*GoCont) PushingNext

func (c *GoCont) PushingNext(vals ...Value) Cont

PushingNext is convenient when implementing go functions. It pushes the given values to c.Next() and returns it.

func (*GoCont) RunInThread

func (c *GoCont) RunInThread(t *Thread) (Cont, error)

RunInThread implements Cont.RunInThread

func (*GoCont) StringArg

func (c *GoCont) StringArg(n int) (String, error)

StringArg returns the n-th argument as a string if possible, otherwise a non-nil error. No range check!

func (*GoCont) TableArg

func (c *GoCont) TableArg(n int) (*Table, error)

TableArg returns the n-th argument as a table if possible, otherwise a non-nil error. No range check!

func (*GoCont) ThreadArg

func (c *GoCont) ThreadArg(n int) (*Thread, error)

ThreadArg returns the n-th argument as a thread 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.

func IntLen

func IntLen(t *Thread, v Value) (Int, error)

IntLen returns the length of v as an Int, possibly calling the '__len' metamethod. This is an optimization of Len for an integer output.

func ToInt

func ToInt(v Value) (Int, bool)

ToInt returns v as an Int and true if v is actually a valid integer.

type Konst

type Konst interface {
	Value
	// contains filtered or unexported methods
}

Konst is a runtime value that is a constant

func LoadConst

func LoadConst(r io.Reader) (Konst, error)

LoadConst reads from r to deserialize a const value.

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

func NewLuaCont(clos *Closure, next Cont) *LuaCont

NewLuaCont returns a new LuaCont from a closure and next, a continuation to push results into.

func (*LuaCont) DebugInfo

func (c *LuaCont) DebugInfo() *DebugInfo

DebugInfo implements Cont.DebugInfo.

func (*LuaCont) Next

func (c *LuaCont) Next() Cont

Next implements Cont.Next.

func (*LuaCont) Push

func (c *LuaCont) Push(val Value)

Push implements Cont.Push.

func (*LuaCont) PushEtc

func (c *LuaCont) PushEtc(vals []Value)

PushEtc implements Cont.PushEtc. TODO: optimise.

func (*LuaCont) RunInThread

func (c *LuaCont) RunInThread(t *Thread) (Cont, error)

RunInThread implements Cont.RunInThread.

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

type Runtime struct {
	Stdout io.Writer
	// contains filtered or unexported fields
}

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 New

func New(stdout io.Writer) *Runtime

New returns a new pointer to a Runtime with the given stdout.

func (*Runtime) GlobalEnv

func (r *Runtime) GlobalEnv() *Table

GlobalEnv returns the global environment of the runtime.

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

func (r *Runtime) MainThread() *Thread

MainThread returns the runtime's main thread.

func (*Runtime) Metatable

func (r *Runtime) Metatable(v Value) Value

Metatable returns the metatalbe of v (looking for '__metatable' in the raw metatable).

func (*Runtime) RawMetatable

func (r *Runtime) RawMetatable(v Value) *Table

RawMetatable returns the raw metatable for a value (that is, not looking at the metatable's '__metatable' key).

func (*Runtime) Registry

func (r *Runtime) Registry(key Value) Value

Registry returns the Value associated with key in the runtime's registry.

func (*Runtime) SetRawMetatable

func (r *Runtime) SetRawMetatable(v Value, meta *Table)

SetRawMetatable sets the metatable for value v to meta.

func (*Runtime) SetRegistry

func (r *Runtime) SetRegistry(k, v Value)

SetRegistry sets the value associated with the key k to v in the registry.

func (*Runtime) SetStringMeta

func (r *Runtime) SetStringMeta(meta *Table)

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

func AsString(x Value) (String, bool)

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 Type

func Type(v Value) String

Type returns a String describing the Lua type of v.

func (String) NormPos

func (s String) NormPos(n Int) int

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

type SyntaxError struct {
	File string
	Err  parsing.Error
}

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 NewTable

func NewTable() *Table

NewTable returns a new Table.

func (*Table) Get

func (t *Table) Get(k Value) Value

Get returns t[k].

func (*Table) Len

func (t *Table) Len() Int

Len returns a length for t (see lua docs for details).

func (*Table) MarshalJSON

func (t *Table) MarshalJSON() ([]byte, error)

MarshalJSON marshals the table to JSON.

func (*Table) Metatable

func (t *Table) Metatable() *Table

Metatable returns the table's metatable.

func (*Table) Next

func (t *Table) Next(k Value) (next Value, val Value, ok bool)

Next returns the key-value pair that comes after k in the table t.

func (*Table) Set

func (t *Table) Set(k, v Value)

Set implements t[k] = v (doesn't check if k is nil).

func (*Table) SetCheck

func (t *Table) SetCheck(k, v Value) error

SetCheck implements t[k] = v, returns an error if k is nil.

func (*Table) SetMetatable

func (t *Table) SetMetatable(m *Table)

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

func (c *Termination) Next() Cont

Next implmements Cont.Next.

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

func NewThread(rt *Runtime) *Thread

NewThread creates a new thread. Its initial status is suspended. Call Resume to run it.

func (*Thread) CurrentCont

func (t *Thread) CurrentCont() Cont

CurrentCont returns the continuation currently running (or suspended) in the thread.

func (*Thread) IsMain

func (t *Thread) IsMain() bool

IsMain returns true if the thread is the runtime's main thread.

func (*Thread) Resume

func (t *Thread) Resume(caller *Thread, args []Value) ([]Value, error)

Resume execution of a suspended thread. Its status switches to running while its caller's status switches to suspended.

func (*Thread) RunContinuation

func (t *Thread) RunContinuation(c Cont) (err error)

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

func (t *Thread) Start(c Callable)

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

func (*Thread) Yield

func (t *Thread) Yield(args []Value) ([]Value, error)

Yield to the caller thread. The yielding thread's status switches to suspended while the caller's status switches back to running.

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

func NewUserData(v interface{}, meta *Table) *UserData

NewUserData returns a new UserData pointer for the value v, giving it meta as a metatable.

func (*UserData) MarshalJSON

func (d *UserData) MarshalJSON() ([]byte, error)

MarshalJSON marshals the userdata to JSON.

func (*UserData) Metatable

func (d *UserData) Metatable() *Table

Metatable returns the userdata's metatable.

func (*UserData) SetMetatable

func (d *UserData) SetMetatable(m *Table)

SetMetatable sets d's metatable to m.

func (*UserData) Value

func (d *UserData) Value() interface{}

Value returns the userdata's value.

type Value

type Value interface{}

Value is a runtime value.

func Call1

func Call1(t *Thread, f Value, args ...Value) (Value, error)

Call1 is a convenience method that calls f with arguments args and returns exactly one value.

func Concat

func Concat(t *Thread, x, y Value) (Value, error)

Concat returns x .. y, possibly calling the '__concat' metamethod.

func Index

func Index(t *Thread, coll Value, k Value) (Value, error)

Index returns the item in a collection for the given key k, using the '__index' metamethod if appropriate.

func Len

func Len(t *Thread, v Value) (Value, error)

Len returns the length of v, possibly calling the '__len' metamethod.

func Mod

func Mod(t *Thread, x Value, y Value) (Value, error)

Mod returns x % y.

func RawGet

func RawGet(t *Table, k Value) Value

RawGet returns the item in a table for the given key, or nil if t is nil. It doesn't check the metatable of t.

type Valuer added in v0.1.4

type Valuer interface {
	Value() Value
}

Valuer is the interface satisfied by the Value method.

Jump to

Keyboard shortcuts

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