func main() { lua := ` fn = function(obj) slice = obj.GetSlice() print(type(slice), #slice) obj = slice[1] -- slice[2] will raise a 'index out of range' error -- obj.Naam = 'howzit' -- will raise a 'no field' error name = obj.GetName() return name end ` L := luar.Init() defer L.Close() L.DoString(lua) luafn := luar.NewLuaObjectFromName(L, "fn") gobj := NewStructWithSlice("string") res, err := luafn.Call(gobj) if err != nil { fmt.Println("error!", err) } else { fmt.Println("result", res) } }
func main() { L := luar.Init() defer L.Close() L.GetGlobal("print") print := luar.NewLuaObject(L, -1) print.Call("one two", 12) L.GetGlobal("package") pack := luar.NewLuaObject(L, -1) fmt.Println(pack.Get("path")) lcopy := luar.NewLuaObjectFromValue gsub := luar.NewLuaObjectFromName(L, "string.gsub") rmap := lcopy(L, luar.Map{ "NAME": "Dolly", "HOME": "where you belong", }) res, err := gsub.Call("hello $NAME go $HOME", "%$(%u+)", rmap) if res == nil { fmt.Println("error", err) } else { fmt.Println("result", res) } }
func main() { L := luar.Init() defer L.Close() L.GetGlobal("print") print := luar.NewLuaObject(L, -1) print.Call("one two", 12) L.GetGlobal("package") pack := luar.NewLuaObject(L, -1) fmt.Println(pack.Get("path")) /* L.GetGlobal("string") strtab := luar.NewLuaObject(L,-1) iter := strtab.Iter() for iter.Next() { fmt.Println(iter.Key,iter.Value) } */ gsub := luar.NewLuaObjectFromName(L, "string.gsub") res, err := gsub.Call("hello $NAME go $HOME", "%$(%u+)", luar.Map{ "NAME": "Dolly", "HOME": "where you belong", }) if res == nil { fmt.Println("error", err) } else { fmt.Println("result", res) } }
func (r Runtime) CallPlugin(name, function string, args []interface{}) (interface{}, error) { luafn := luar.NewLuaObjectFromName(r.plugins[name], function) value, err := luafn.Call(args...) if err != nil { return nil, err } return value, nil }
func (c *Conf) RunMain() { fun := luar.NewLuaObjectFromName(c.l, "Main") _, err := fun.Call() if err != nil { panic(err) } }
func main() { whj := "wang::hai::jun" fmt.Println(strings.Split(whj, "::")) TestCall("111") L := luar.Init() defer L.Close() M := luar.Map{ "one": "ein", "two": "zwei", "three": "drei", } S := []string{"alfred", "alice", "bob", "frodo"} ST := &MyStruct{"Dolly", 46} luar.Register(L, "", luar.Map{ "Print": fmt.Println, "testcall": TestCall, "print": fmt.Println, "MSG": "hello", // can also register constants "M": M, "S": S, "ST": ST, }) //L.DoString(test) L.DoString(code) L.GetGlobal("print") print := luar.NewLuaObject(L, -1) print.Call("one two", 12) L.GetGlobal("package") pack := luar.NewLuaObject(L, -1) fmt.Println(pack.Get("path")) lcopy := luar.NewLuaObjectFromValue gsub := luar.NewLuaObjectFromName(L, "string.gsub") rmap := lcopy(L, luar.Map{ "NAME": "Dolly", "HOME": "where you belong", }) res, err := gsub.Call("hello $NAME go $HOME", "%$(%u+)", rmap) if res == nil { fmt.Println("error", err) } else { fmt.Println("\033[0;31mresult\033[0m", res) } }
// AddLuaHook adds a named hook from lua. func (script *Script) AddLuaHook(verb string, name string) error { function := luar.NewLuaObjectFromName(script.L, name) hook := NewHook(verb, func(args ...interface{}) { script.Trigger <- []interface{}{ INV_NAMHOOK, function, args, } }) script.Hooks = append(script.Hooks, hook) return nil }
// AddLuaProtohook adds a lua function as a protocol hook. func (script *Script) AddLuaProtohook(verb string, name string) error { function := luar.NewLuaObjectFromName(script.L, name) handler, err := AddHandler(verb, func(line *r1459.RawLine) { debugf("sending %s", verb) script.Trigger <- []interface{}{INV_PROHOOK, function, line} }) if err != nil { return err } handler.Script = script script.Handlers[handler.Uuid] = handler return nil }
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 }
// AddLuaCommand adds a new command to a script from a lua context. func (script *Script) AddLuaCommand(verb string, name string) error { function := luar.NewLuaObjectFromName(script.L, name) command, err := script.Client.NewCommand(verb, func(client *Client, target Targeter, args []string) string { reschan := make(chan string) defer close(reschan) script.Trigger <- []interface{}{ INV_COMMAND, function, client, target, args, reschan, } return <-reschan }) if err != nil { return err } command.Script = script script.Commands[command.Uuid] = command return nil }
func ExampleLuaObject_Callf() { L := luar.Init() defer L.Close() returns := luar.Types([]string{}) // []reflect.Type const code = ` function return_strings() return {'one', luar.null, 'three'} end` err := L.DoString(code) if err != nil { log.Fatal(err) } fun := luar.NewLuaObjectFromName(L, "return_strings") // Using `Call` we would get a generic `[]interface{}`, which is awkward to // work with. But the return type can be specified: results, err := fun.Callf(returns) if err != nil { log.Fatal(err) } strs := results[0].([]string) fmt.Println(strs[0]) // We get an empty string corresponding to a luar.null in a table, // since that's the empty 'zero' value for a string. fmt.Println(strs[1]) fmt.Println(strs[2]) // Output: // one // // three }
func main() { history := os.Getenv("HOME") + "/.luar_history" linenoise.LoadHistory(history) defer func() { L.Close() linenoise.SaveHistory(history) if x := recover(); x != nil { fmt.Println("runtime " + x.(error).Error()) } }() luar.Register(L, "", luar.Map{ "__DUMMY__": &Dummy{"me"}, }) // most of this program's code is Lua.... err := L.DoString(lua_code) if err != nil { fmt.Println("initial " + err.Error()) return } // particularly the completion logic complete := luar.NewLuaObjectFromName(L, "lua_candidates") // this function returns a string slice of candidates str_slice := luar.Types([]string{}) linenoise.SetCompletionHandler(func(in string) []string { val, err := complete.Callf(str_slice, in) if err != nil || len(val) == 1 && val[0] == nil { return []string{} } else { return val[0].([]string) } }) register() fmt.Println("luar 1.2 Copyright (C) 2013-2014 Steve Donovan") fmt.Println("Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio") prompt := LUA_PROMPT1 code := "" for { // ctrl-C/ctrl-D handling with latest update of go.linenoise str, err := linenoise.Line(prompt) if err != nil { return } if len(str) > 0 { if str == "exit" { return } linenoise.AddHistory(str) if str[0] == '=' || str[0] == '.' { exprs := str[1:] if str[0] == '=' { str = "pprint(" + exprs + ")" } else { str = "println(" + exprs + ")" } } continuing := false code = code + str //fmt.Println("'"+code+"'") err := L.DoString(code) if err != nil { errs := err.Error() // fmt.Println("err",errs) // strip line nonsense if error occurred on prompt idx := strings.Index(errs, ": ") if idx > -1 && strings.HasPrefix(errs, "[string ") { errs = errs[idx+2:] } // need to keep collecting line? if strings.HasSuffix(errs, "near '<eof>'") { continuing = true } else { fmt.Println(errs) } } if continuing { // prompt must reflect continuing state prompt = LUA_PROMPT2 code = code + "\n" } else { prompt = LUA_PROMPT1 code = "" } } else { fmt.Println("empty line. Use exit to get out") } } }
// Handle Directionals func (gs *GameServer) HandleMovementPacket(cp *ClientPacket) { action := cp.Data.(game.Action) p := cp.Client.Player offset := game.DirTable[action] oldposx, oldposy := p.GetPos() newpos := image.Pt(oldposx+offset.X, oldposy+offset.Y) valid := true // check terrain collision if !gs.Map.CheckCollision(nil, newpos) { valid = false cp.Reply(gnet.NewPacket("Rchat", "Ouch! You bump into a wall.")) } // check gameobject collision for o := range gs.Objects.Chan() { // check if collision with Item and item name is flag px, py := o.GetPos() if px == newpos.X && py == newpos.Y { collfn := luar.NewLuaObjectFromName(gs.Lua, "collide") res, err := collfn.Call(p, o) if err != nil { log.Printf("GameServer: HandleMovementPacket: Lua error: %s", err) return } // only update position if collide returns true if thebool, ok := res.(bool); !ok || !thebool { log.Printf("GameServer: HandleMovementPacket: Lua collision failed") valid = false } else { // tell everyone that the colliders changed gs.SendPacketAll(gnet.NewPacket("Raction", o)) } if o.GetTag("player") { cp.Reply(gnet.NewPacket("Rchat", fmt.Sprintf("Ouch! You bump into %s.", o.GetName()))) // check if other player's got the goods for sub := range o.GetSubObjects().Chan() { if sub.GetTag("item") == true { // swap pop'n'lock // remove item from player swap := o.RemoveSubObject(sub) p.AddSubObject(swap) cp.Reply(gnet.NewPacket("Rchat", fmt.Sprintf("You steal a %s!", swap.GetName()))) } } } if o.GetTag("item") && o.GetTag("gettable") && valid { cp.Reply(gnet.NewPacket("Rchat", fmt.Sprintf("You see a %s here.", o.GetName()))) } } } if valid { cp.Client.Player.SetPos(newpos.X, newpos.Y) //gs.SendPacketAll(gnet.NewPacket("Raction", p)) } }