func (p *Plugin) apiProcessNew(l *lua.State) int { callback := luar.NewLuaObject(l, 1) command := l.ToString(2) args := make([]string, l.GetTop()-2) for i := 3; i <= l.GetTop(); i++ { args[i-3] = l.ToString(i) } proc := &process{ cmd: exec.Command(command, args...), } go func() { var str string bytes, err := proc.cmd.Output() if err == nil { if bytes != nil { str = string(bytes) } p.callValue(callback, proc.cmd.ProcessState.Success(), str) } else { p.callValue(callback, false, "") } callback.Close() }() obj := luar.NewLuaObjectFromValue(l, proc) obj.Push() obj.Close() return 1 }
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 }
func (p *Plugin) apiAudioNewTarget(l *lua.State) int { id := l.ToInteger(1) target := &gumble.VoiceTarget{} target.ID = uint32(id) obj := luar.NewLuaObjectFromValue(l, target) obj.Push() obj.Close() return 1 }
func ExampleNewLuaObjectFromValue() { L := luar.Init() defer L.Close() gsub := luar.NewLuaObjectFromName(L, "string.gsub") // We do have to explicitly copy the map to a Lua table, because `gsub` // will not handle userdata types. gmap := luar.NewLuaObjectFromValue(L, luar.Map{ "NAME": "Dolly", "HOME": "where you belong", }) res, err := gsub.Call("hello $NAME go $HOME", "%$(%u+)", gmap) if err != nil { log.Fatal(err) } fmt.Println(res) // Output: // hello Dolly go where you belong }
func (luaipt *LuaIpt) Exec(name string, data interface{}) error { name = strings.Replace(name, ":", "/", -1) f := path.Join(luaipt.path, name+".lua") luaipt.Bind("Data", luar.NewLuaObjectFromValue(luaipt.state, data)) return luaipt.state.DoFile(f) }