From efc956a04cd4192fe1a10fa0bdb5e5a97b6fd8f4 Mon Sep 17 00:00:00 2001 From: sammyette <38820196+TorchedSammy@users.noreply.github.com> Date: Fri, 11 Jun 2021 21:37:10 -0400 Subject: [PATCH] fix: cleanup, store lua commands in map --- lua.go | 13 +++---------- main.go | 2 +- shell.go | 16 ++++------------ 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/lua.go b/lua.go index 58bee46..f8ab36f 100644 --- a/lua.go +++ b/lua.go @@ -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) diff --git a/main.go b/main.go index 2c2f9b3..4850b4d 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/shell.go b/shell.go index 8b2bcf0..707ce1a 100644 --- a/shell.go +++ b/shell.go @@ -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:]))