本文整理汇总了Golang中github.com/aarzilli/golua/lua.State类的典型用法代码示例。如果您正苦于以下问题:Golang State类的具体用法?Golang State怎么用?Golang State使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了State类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: apiTimerNew
func (p *Plugin) apiTimerNew(l *lua.State) int {
callback := luar.NewLuaObject(l, 1)
timeout := l.ToInteger(2)
t := &Timer{
cancel: make(chan bool),
}
go func() {
defer func() {
close(t.cancel)
t.cancel = nil
}()
select {
case <-time.After(time.Millisecond * time.Duration(timeout)):
p.callValue(callback)
callback.Close()
case <-t.cancel:
}
}()
obj := luar.NewLuaObjectFromValue(l, t)
obj.Push()
obj.Close()
return 1
}
示例2: prepContext
func (p *LuaPlugin) prepContext(L *lua.State, req *http.Request, ctx *apiplexy.APIContext) {
var clientIP string
if req.Header.Get("X-Forwarded-For") != "" {
clientIP = req.Header.Get("X-Forwarded-For")
} else {
clientIP, _, _ = net.SplitHostPort(req.RemoteAddr)
}
headers := make(map[string]interface{}, len(req.Header))
for k, vs := range req.Header {
headers[k] = strings.Join(vs, " ")
}
request := map[string]interface{}{
"path": req.URL.Path,
"method": req.Method,
"ip": clientIP,
"referrer": req.Referer(),
"browser": req.UserAgent(),
"headers": headers,
}
pushMap(L, request, false)
L.SetGlobal("request")
pushMap(L, structs.Map(ctx), true)
L.SetGlobal("context")
}
示例3: sliceSub
func sliceSub(L *lua.State) int {
slice, _ := valueOfProxy(L, 1)
i1, i2 := L.ToInteger(2), L.ToInteger(3)
newslice := slice.Slice(i1-1, i2)
makeValueProxy(L, newslice, cSLICE_META)
return 1
}
示例4: callGoMethod
func callGoMethod(L *lua.State, name string, st reflect.Value) {
ret := st.MethodByName(name)
if !ret.IsValid() {
fmt.Println("whoops")
}
L.PushGoFunction(GoLuaFunc(L, ret))
}
示例5: GoLua
func GoLua(L *lua.State) int {
go func() {
LT := L.NewThread()
L.PushValue(1)
lua.XMove(L, LT, 1)
res := LT.Resume(0)
for res != 0 {
if res == 2 {
emsg := LT.ToString(-1)
RaiseError(LT, emsg)
}
ch, t := valueOfProxy(LT, -2)
if LT.ToBoolean(-1) { // send on a channel
val := luaToGoValue(LT, t.Elem(), -3)
ch.Send(val)
res = LT.Resume(0)
} else { // receive on a channel
val, ok := ch.Recv()
GoToLua(LT, t.Elem(), val, false)
LT.PushBoolean(ok)
res = LT.Resume(2)
}
}
}()
return 0
}
示例6: MessThingIndex
func MessThingIndex(state *lua.State) int {
log.Println("HEY WE MADE IT")
printStackTypes(state)
state.GetMetaTable(1)
state.LGetMetaTable(ThingMetaTableName)
isThing := state.RawEqual(-1, -2)
state.Pop(2)
if !isThing {
log.Println("!!! OMG ARG #1 IS NOT A MESS.THING !!!")
}
fieldName := state.CheckString(2)
log.Println("Arg #2 checks out, it's a string")
thing := checkThing(state, 1)
log.Println("So we're tryin'a look up", fieldName, "on thing", thing.Id)
if member, ok := MessThingMembers[fieldName]; ok {
return member(state, thing)
}
// That wasn't one of our members, so look it up in our Table.
if data, ok := thing.Table[fieldName]; ok {
// TODO: instead of pushing a whole map if the script asks for one, maybe we should use another kind of userdata that tracks the name & can access its submembers until the script asks for the leaf (or a non-existent branch)?
pushValue(state, data)
return 1
}
// uh... I guess we didn't do anything, so...?
return 0
}
示例7: apiAudioSetBitrate
func (p *Plugin) apiAudioSetBitrate(l *lua.State) int {
bitrate := l.ToInteger(1)
if enc, ok := p.instance.Client.AudioEncoder.(*opus.Encoder); ok {
enc.SetBitrate(bitrate)
}
return 0
}
示例8: apiAudioPlay
func (p *Plugin) apiAudioPlay(l *lua.State) int {
if p.instance.Audio.IsPlaying() {
l.PushBoolean(false)
return 1
}
obj := luar.NewLuaObject(l, 1)
filename := obj.Get("filename").(string)
callback := obj.GetObject("callback")
obj.Close()
if enc, ok := p.instance.Client.AudioEncoder.(*opus.Encoder); ok {
enc.SetApplication(gopus.Audio)
}
p.instance.Audio.Source = gumble_ffmpeg.SourceFile(filename)
p.instance.Audio.Play()
go func() {
p.instance.Audio.Wait()
if callback.Type != "nil" {
p.callValue(callback)
}
callback.Close()
}()
return 0
}
示例9: slice__newindex
func slice__newindex(L *lua.State) int {
slice, t := valueOfProxy(L, 1)
idx := L.ToInteger(2)
val := LuaToGo(L, t.Elem(), 3)
slice.Index(idx - 1).Set(valueOf(val))
return 0
}
示例10: copySliceToTable
// Also for arrays.
func copySliceToTable(L *lua.State, vslice reflect.Value, visited visitor) int {
ref := vslice
for vslice.Kind() == reflect.Ptr {
// For arrays.
vslice = vslice.Elem()
}
if vslice.IsValid() && (vslice.Kind() == reflect.Slice || vslice.Kind() == reflect.Array) {
n := vslice.Len()
L.CreateTable(n, 0)
if vslice.Kind() == reflect.Slice {
visited.mark(vslice)
} else if ref.Kind() == reflect.Ptr {
visited.mark(ref)
}
for i := 0; i < n; i++ {
L.PushInteger(int64(i + 1))
v := vslice.Index(i)
if isNil(v) {
v = nullv
}
goToLua(L, nil, v, true, visited)
L.SetTable(-3)
}
return 1
}
L.PushNil()
L.PushString("not a slice/array")
return 2
}
示例11: proxy__gc
func proxy__gc(L *lua.State) int {
vp := (*valueProxy)(L.ToUserdata(1))
mu.Lock()
delete(proxyMap, vp)
mu.Unlock()
return 0
}
示例12: LuaIntUTCTime
func LuaIntUTCTime(L *lua.State) int {
luaAssertArgnum(L, 1, "utctime()")
timestamp := L.ToInteger(1)
PushTime(L, time.Unix(int64(timestamp), 0))
return 1
}
示例13: PushStringVec
func PushStringVec(L *lua.State, v []string) {
L.CheckStack(3)
L.CreateTable(len(v), 0)
for idx, val := range v {
SetTableIntString(L, int64(idx+1), val)
}
}
示例14: apiAudioBitrate
func (p *Plugin) apiAudioBitrate(l *lua.State) int {
if enc, ok := p.instance.Client.AudioEncoder.(*opus.Encoder); ok {
l.PushInteger(int64(enc.Bitrate()))
} else {
l.PushInteger(-1)
}
return 1
}
示例15: LuaIntShowRet
func LuaIntShowRet(L *lua.State) int {
L.CheckStack(2)
tl := GetTasklistFromLua(L)
tl.luaFlags.showReturnValue = true
return 0
}