Documentation ¶
Overview ¶
Package glu support yuin/gopher-lua with easy modular definition and other enchantments. glu.Modular and gua.BaseType will inject mod.Help(name string?) method to output HelpCache information. glu.Get: Pool function to get a lua.LState. glu.Put: Pool function to return a lua.LState. glu.registry: shared module registry. glu.Auto: config for autoload modules in registry into lua.LState.
Example ¶
// fetch an instance vm := Get() err := vm.DoString(`print('hello lua')`) if err != nil { return }
Output:
Index ¶
- Variables
- func Check[T any](s *lua.LState, n int, def T, cast func(v lua.LValue) (val T, ok bool)) (T, bool)
- func CheckBool(s *lua.LState, n int) (bool, bool)
- func CheckFloat32(s *lua.LState, n int) (float32, bool)
- func CheckFloat64(s *lua.LState, n int) (float64, bool)
- func CheckInt(s *lua.LState, n int) (int, bool)
- func CheckInt16(s *lua.LState, n int) (int16, bool)
- func CheckInt32(s *lua.LState, n int) (int32, bool)
- func CheckInt64(s *lua.LState, n int) (int64, bool)
- func CheckRecUserData[T any](s *lua.LState, ud *lua.LUserData, def T, cast func(v any) (val T, ok bool)) (T, bool)
- func CheckString(s *lua.LState, n int) (string, bool)
- func CheckUserData[T any](s *lua.LState, n int, def T, cast func(v any) (val T, ok bool)) (T, bool)
- func CompileChunk(code string, source string) (*FunctionProto, error)
- func ExecuteChunk(code *FunctionProto, argN, retN int, before Operator, after Operator) (err error)
- func ExecuteCode(code string, argsN, retN int, before Operator, after Operator) error
- func Failed(err error)
- func MakePool()
- func Pack(v any, s *LState) LValue
- func Put(s *Vm)
- func Raise(s *lua.LState, act func() int) (ret int)
- func Raw(v LValue) any
- func Recover(act func()) (err error)
- func RecoverErr(act func() error) (err error)
- func Register(m ...Modular) (err error)
- func SafeFunc(fn func(state *LState) int) LGFunction
- func SafeOpt(s *LState, at int, t LValueType) any
- func SafeParam(s *LState, start int, types ...LValueType) (r []any)
- func Success(err error)
- func TableToMap(s *LTable) (r map[LValue]LValue)
- func TableToSlice(s *LTable) (r []LValue)
- func TableUnpack(s *LTable, noLua bool, history map[LValue]any) (r map[any]any, keys []any)
- type BaseType
- func (m *BaseType[T]) AddField(name string, help string, value LValue) Type[T]
- func (m *BaseType[T]) AddFunc(name string, help string, fn LGFunction) Type[T]
- func (m *BaseType[T]) AddMethod(name string, help string, value LGFunction) Type[T]
- func (m *BaseType[T]) AddMethodCast(name string, help string, act func(s *LState, data T) int) Type[T]
- func (m *BaseType[T]) AddMethodUserData(name string, help string, act func(s *LState, data *LUserData) int) Type[T]
- func (m *BaseType[T]) AddModule(mod Modular) Type[T]
- func (m BaseType[T]) CanCast() bool
- func (m BaseType[T]) Cast(s *LState) (T, bool)
- func (m BaseType[T]) CastUserData(ud *LUserData, s *LState) (T, bool)
- func (m BaseType[T]) CastVar(s *LState, n int) (T, bool)
- func (m *BaseType[T]) Caster() func(any) (T, bool)
- func (m *BaseType[T]) GetHelp() string
- func (m *BaseType[T]) GetName() string
- func (m BaseType[T]) New(l *LState, val T) int
- func (m BaseType[T]) NewValue(l *LState, val T) *LUserData
- func (m *BaseType[T]) Override(op Operate, help string, fn LGFunction) Type[T]
- func (m *BaseType[T]) OverrideCast(op Operate, help string, act func(s *LState, data T) int) Type[T]
- func (m *BaseType[T]) OverrideUserData(op Operate, help string, act func(s *LState, data *LUserData) int) Type[T]
- func (m *BaseType[T]) PreLoad(l *LState)
- func (m *BaseType[T]) PreloadSubModule(l *LState, t *LTable)
- func (m *BaseType[T]) SafeFun(name string, help string, fn LGFunction) Type[T]
- func (m *BaseType[T]) SafeMethod(name string, help string, value LGFunction) Type[T]
- func (m *BaseType[T]) SafeOverride(op Operate, help string, fn LGFunction) Type[T]
- func (m *BaseType[T]) TopLevel() bool
- type Chunk
- type Mod
- func (m *Mod) AddField(name string, help string, value LValue) Module
- func (m *Mod) AddFunc(name string, help string, fn LGFunction) Module
- func (m *Mod) AddModule(mod Modular) Module
- func (m *Mod) GetHelp() string
- func (m *Mod) GetName() string
- func (m *Mod) PreLoad(l *LState)
- func (m *Mod) PreloadSubModule(l *LState, t *LTable)
- func (m *Mod) SafeFun(name string, help string, fn LGFunction) Module
- func (m *Mod) TopLevel() bool
- type Modular
- type Module
- type Operate
- type Operator
- type Prepare
- type Type
- type Vm
- func (s *Vm) OpenLibsWithout(names ...string) *Vm
- func (s *Vm) Polluted() (r bool)
- func (s *Vm) Reset() (r *Vm)
- func (s *Vm) Snapshot() *Vm
- func (s *Vm) TabChildEqualTo(t1 *LTable, t2 *LTable, keys ...string) (r bool)
- func (s *Vm) TabCopyChildNew(f *LTable, keys ...string) *LTable
- func (s *Vm) TabCopyNew(f *LTable) *LTable
- func (s *Vm) TabEqualTo(t1 *LTable, t2 *LTable) (r bool)
- type VmPool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( FmtErrMissing = "required value at %d" FmtErrType = "required type not match at %d" )
generic functions
var ( //HelpKey the module HelpCache key HelpKey = "?" //HelpFunc the help function name HelpFunc = "help" //HelpPrompt the prompt for no value supply for HelpCache HelpPrompt = "show Help with those key word: " HelpChunk = `chunk(code,name string)(Chunk?,string?) ==> pre compile string into bytecode` HelpHelp = HelpFunc + `(topic string?)string? => fetch Help of topic,'?' show topics,without topic show loadable modules` HelpTopic = `?,chunk` EagerHelpPrepare = false )
var ( ErrAlreadyExists = errors.New("element already exists") ErrIndexOverrideWithMethods = errors.New("element both have methods and index overrides") ErrIsTop = errors.New("element is top module") )
var ( //Option LState configuration Option = Options{} InitialSize = 4 )
var ( //OpNone operator do nothing, better to use nil OpNone = func(s *Vm) error { return nil } //OpPush operator to push N value OpPush = func(n ...LValue) Operator { return func(s *Vm) error { for _, value := range n { s.Push(value) } return nil } } //OpPushUserData operator to push N UserDate OpPushUserData = func(n ...any) Operator { return func(s *Vm) error { for _, i := range n { ud := s.NewUserData() ud.Value = i s.Push(ud) } return nil } } //OpSafe operator to wrap as none error will happen OpSafe = func(fn func(s *Vm)) Operator { return func(s *Vm) error { fn(s) return nil } } //OpPop operator to pop and consume N value from start,then pop to n OpPop = func(fn func(value ...LValue), start, count int) Operator { if start < 0 || count <= 0 { panic("start must greater than 0 and count must greater than 0") } t := start + count return func(s *Vm) error { v := make([]LValue, 0, count) for i := start; i < t; i++ { v = append(v, s.Get(i)) } s.Pop(t - 1) fn(v...) return nil } } //OpPopN operator to pop n value (Reset stack) OpPopN = func(count int) Operator { if count < 1 { panic("count must greater than 0") } return func(s *Vm) error { s.Pop(count) return nil } } )
var ( //Auto if true, will autoload modules in registry Auto = true )
var ( //BaseMod the global module BaseMod = glu(map[string]string{}) )
var ( //ErrorSuppress not raise error ,use for SafeFunc ErrorSuppress = errors.New("") )
var (
// ExistNode is placeholder for a map set
ExistNode = struct{}{}
)
Functions ¶
func CheckBool ¶
CheckBool return value and true only when value exists and is bool. Otherwise, an error raised.
func CheckFloat32 ¶
CheckFloat32 return value and true only when value exists and is exactly equals to the wanted number type. Returns converted number and false when value is number
func CheckFloat64 ¶
CheckFloat64 return value and true only when value exists and is exactly equals to the wanted number type.
func CheckInt ¶
CheckInt return value and true only when value exists and is exactly equals to the wanted number type. Returns converted number and false when value is number
func CheckInt16 ¶
CheckInt16 return value and true only when value exists and is exactly equals to the wanted number type. Returns converted number and false when value is number
func CheckInt32 ¶
CheckInt32 return value and true only when value exists and is exactly equals to the wanted number type. Returns converted number and false when value is number
func CheckInt64 ¶
CheckInt64 return value and true only when value exists and is exactly equals to the wanted number type. Returns converted number and false when value is number
func CheckRecUserData ¶
func CheckRecUserData[T any](s *lua.LState, ud *lua.LUserData, def T, cast func(v any) (val T, ok bool)) (T, bool)
CheckRecUserData check the receiver as userdata of wanted type.
func CheckString ¶
CheckString return value and true only when value exists and is string. Otherwise, an error raised.
func CheckUserData ¶
CheckUserData return value and true only when value exists and can cast to the wanted type. Otherwise, an error raised.
func CompileChunk ¶
CompileChunk compile code to FunctionProto
func ExecuteChunk ¶
ExecuteChunk execute pre complied FunctionProto
func ExecuteCode ¶
ExecuteCode run code in LState, use before to push args, after to extract return value
func MakePool ¶
func MakePool()
MakePool manual create statePool , when need to change Option, should invoke once before use Get and Put
func Pack ¶
func Pack(v any, s *LState) LValue
Pack any to LValue.
1. nil, bool, numbers and other Lua value packed as normal LValue
2. array, slice,map[string]any packed into LTable (the elements also packed)
3. others are packed into LUserData
func Raw ¶
func Raw(v LValue) any
Raw extract raw LValue: nil bool float64 string *LUserData *LState *LTable *LChannel
func RecoverErr ¶
RecoverErr warp an error supplier func with recover
func TableToMap ¶
func TableToMap(s *LTable) (r map[LValue]LValue)
TableToMap convert LTable to a Map with all key values
func TableToSlice ¶
func TableToSlice(s *LTable) (r []LValue)
TableToSlice convert LTable to a Slice with all Number index values
func TableUnpack ¶
TableUnpack convert LTable to a Map with all key values
All keys and values may be:
LTNumber: float64
LTBool: bool
LTTable: map[any]any
LTString: string
(those type will not output with noLua=true )
LTFunction: *LFunction
LTUserData: *LUserData
LChannel: LChannel
Types ¶
type BaseType ¶
BaseType define a LTable with MetaTable, which mimicry class like action in Lua
func NewSimpleType ¶
NewSimpleType create new BaseType without ctor
func NewType ¶
func NewType[T any](name string, help string, top bool, ctorHelp string, ctor func(*LState) (T, bool)) *BaseType[T]
NewType create new BaseType
func NewTypeCast ¶
func NewTypeCast[T any](caster func(a any) (v T, ok bool), name string, help string, top bool, ctorHelp string, ctor func(s *LState) (v T, ok bool)) *BaseType[T]
NewTypeCast create new BaseType with reflect Signature
func (*BaseType[T]) AddField ¶
AddField add value field to this Modular
@name the field name
@HelpCache HelpCache string, if empty will not generate into HelpCache
@value the field value
func (*BaseType[T]) AddFunc ¶
AddFunc add function to this Modular
@name function name, must match lua limitation
@HelpCache HelpCache string, if empty will not generate into HelpCache
@fn the LGFunction
func (*BaseType[T]) AddMethodCast ¶
func (m *BaseType[T]) AddMethodCast(name string, help string, act func(s *LState, data T) int) Type[T]
AddMethodCast prechecked type (only create with NewTypeCast).
func (*BaseType[T]) AddMethodUserData ¶
func (m *BaseType[T]) AddMethodUserData(name string, help string, act func(s *LState, data *LUserData) int) Type[T]
AddMethodUserData add method to this type which means instance method, with auto extract first argument.
func (*BaseType[T]) AddModule ¶
AddModule add sub-module to this Modular
@mod the Mod **Note** must with TopLevel false.
func (BaseType[T]) CastUserData ¶
func (BaseType[T]) NewValue ¶
func (m BaseType[T]) NewValue(l *LState, val T) *LUserData
NewValue create new LValue
func (*BaseType[T]) OverrideCast ¶
func (m *BaseType[T]) OverrideCast(op Operate, help string, act func(s *LState, data T) int) Type[T]
OverrideCast see Override and AddMethodCast
func (*BaseType[T]) OverrideUserData ¶
func (m *BaseType[T]) OverrideUserData(op Operate, help string, act func(s *LState, data *LUserData) int) Type[T]
OverrideUserData see Override and AddMethodUserData
func (*BaseType[T]) PreloadSubModule ¶
func (m *BaseType[T]) PreloadSubModule(l *LState, t *LTable)
func (*BaseType[T]) SafeMethod ¶
SafeMethod warp with SafeFunc
func (*BaseType[T]) SafeOverride ¶
SafeOverride wrap with SafeFunc
type Mod ¶
type Mod struct { Name string //Name of Modular Top bool //is top level Help string //Help information of this Modular Submodules []Modular //registered sub modules HelpCache map[string]string //exported helps for better use // contains filtered or unexported fields }
Mod define a Mod only contains Functions and value fields,maybe with Submodules
func (*Mod) AddField ¶
AddField add value field to this Modular
@name the field name
@HelpCache HelpCache string, if empty will not generate into HelpCache
@value the field value
func (*Mod) AddFunc ¶
AddFunc add function to this Modular
@name function name, must match lua limitation
@HelpCache HelpCache string, if empty will not generate into HelpCache
@fn the LGFunction
func (*Mod) AddModule ¶
AddModule add sub-module to this Modular
@mod the Mod **Note** must with TopLevel false.
func (*Mod) PreloadSubModule ¶
func (m *Mod) PreloadSubModule(l *LState, t *LTable)
type Modular ¶
type Modular interface { //TopLevel dose this Mod is top level,means should not be submodule TopLevel() bool //PreLoad load as global Mod PreLoad(l *lua.LState) //PreloadSubModule use for submodule loading, Should NOT invoke manually PreloadSubModule(l *lua.LState, t *lua.LTable) //GetName the unique name (if is a Top Level Modular) GetName() string //GetHelp Modular HelpCache info GetHelp() string }
Modular shared methods make it a Modular
type Module ¶
type Module interface { Modular // AddFunc add function to this Module // // @name function name, must match lua limitation // // @HelpCache HelpCache string, if empty will generate just Module.Function as HelpCache // // @fn the LGFunction AddFunc(name string, help string, fn LGFunction) Module //SafeFun warp with SafeFunc,see AddFunc for detail SafeFun(name string, help string, value LGFunction) Module // AddField add value field to this Module (static value) AddField(name string, help string, value LValue) Module // AddModule add submodule to this Module // // @mod the Mod , requires Mod.TopLevel is false. AddModule(mod Modular) Module }
type Operate ¶
type Operate int
const ( OPERATE_INVALID Operate = iota OPERATE_ADD // + OPERATE_SUB // - OPERATE_MUL // * OPERATE_DIV // / OPERATE_UNM // - OPERATE_MOD // % OPERATE_POW // ^ OPERATE_CONCAT // .. OPERATE_EQ // == OPERATE_LT // < OPERATE_LE // <= OPERATE_LEN // # OPERATE_INDEX // [] OPERATE_NEWINDEX // []= OPERATE_TO_STRING // tostring OPERATE_CALL // () )
noinspection GoSnakeCaseUsage,GoUnusedConst
type Type ¶
type Type[T any] interface { Modular // New create new instance and push on stack New(l *LState, val T) int // NewValue create new LValue NewValue(l *LState, val T) *LUserData // CanCast check the type can use cast (when construct with NewTypeCast) CanCast() bool // CastVar cast value on stack (already have error processed) CastVar(s *LState, n int) (T, bool) // Cast receiver on stack (already have error processed) Cast(s *LState) (T, bool) // CastUserData cast UserData (already have error processed) CastUserData(ud *LUserData, s *LState) (T, bool) // Caster cast value Caster() func(any) (T, bool) // AddFunc static function AddFunc(name string, help string, fn LGFunction) Type[T] //SafeFun warp with SafeFunc SafeFun(name string, help string, value LGFunction) Type[T] // AddField static field AddField(name string, help string, value LValue) Type[T] //SafeMethod warp with SafeFunc SafeMethod(name string, help string, value LGFunction) Type[T] // AddMethod add method to this type which means instance method. AddMethod(name string, help string, value LGFunction) Type[T] // AddMethodUserData add method to this type which means instance method, with auto extract first argument. AddMethodUserData(name string, help string, act func(s *LState, u *LUserData) int) Type[T] // AddMethodCast prechecked type (only create with NewTypeCast). AddMethodCast(name string, help string, act func(s *LState, i T) int) Type[T] // Override operators an operator Override(op Operate, help string, fn LGFunction) Type[T] //SafeOverride warp with SafeFunc SafeOverride(op Operate, help string, value LGFunction) Type[T] // OverrideUserData see Override and AddMethodUserData OverrideUserData(op Operate, help string, act func(s *LState, u *LUserData) int) Type[T] // OverrideCast see Override and AddMethodCast OverrideCast(op Operate, help string, act func(s *LState, i T) int) Type[T] }
type Vm ¶
type Vm struct { *LState // contains filtered or unexported fields }
Vm take Env Snapshot to protect from Global pollution
func (*Vm) OpenLibsWithout ¶
OpenLibsWithout open gopher-lua libs but filter some by name @fluent
func (*Vm) TabChildEqualTo ¶
func (*Vm) TabCopyChildNew ¶
func (*Vm) TabCopyNew ¶
func (s *Vm) TabCopyNew(f *LTable) *LTable
func (*Vm) TabEqualTo ¶
type VmPool ¶
type VmPool struct {
// contains filtered or unexported fields
}
VmPool threadsafe LState Pool
func CreatePool ¶
func CreatePool() *VmPool
func CreatePoolWith ¶
func CreatePoolWith(ctor func() *LState) *VmPool
CreatePoolWith create pool with user defined constructor
BaseMod will auto registered