fix: cleanup, store lua commands in map

pull/61/head
sammyette 2021-06-11 21:37:10 -04:00
parent 1e1662a6b2
commit efc956a04c
No known key found for this signature in database
GPG Key ID: 50EE40A2809851F5
3 changed files with 8 additions and 23 deletions

13
lua.go
View File

@ -45,16 +45,9 @@ func LuaInit() {
cmds := commander.New()
// When a command from Lua is added, register it for use
// TODO: maybe dont add command code to a lua table? insstead use a map
cmds.Events.On("commandRegister",
func(cmdName string, cmd *lua.LFunction) {
commands[cmdName] = true
l.SetField(
l.GetTable(l.GetGlobal("commanding"),
lua.LString("__commands")),
cmdName,
cmd)
})
cmds.Events.On("commandRegister", func(cmdName string, cmd *lua.LFunction) {
commands[cmdName] = cmd
})
l.PreloadModule("commander", cmds.Loader)

View File

@ -22,7 +22,7 @@ var (
l *lua.LState
lr *LineReader
commands = map[string]bool{}
commands = map[string]*lua.LFunction{}
aliases = map[string]string{}
homedir string

View File

@ -49,13 +49,9 @@ func RunInput(input string) {
hooks.Em.Emit("command.exit", 0)
return
}
if commands[cmdArgs[0]] {
if commands[cmdArgs[0]] != nil {
err := l.CallByParam(lua.P{
Fn: l.GetField(
l.GetTable(
l.GetGlobal("commanding"),
lua.LString("__commands")),
cmdArgs[0]),
Fn: commands[cmdArgs[0]],
NRet: 1,
Protect: true,
}, luar.New(l, cmdArgs[1:]))
@ -128,13 +124,9 @@ func execCommand(cmd string) error {
}
// If command is defined in Lua then run it
if commands[args[0]] {
if commands[args[0]] != nil {
err := l.CallByParam(lua.P{
Fn: l.GetField(
l.GetTable(
l.GetGlobal("commanding"),
lua.LString("__commands")),
args[0]),
Fn: commands[args[0]],
NRet: 1,
Protect: true,
}, luar.New(l, args[1:]))