refactor: move arg handle function to util

lua5.4
TorchedSammy 2022-03-28 19:12:13 -04:00
parent d2d423a4ef
commit e466085d24
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 19 additions and 19 deletions

View File

@ -4,9 +4,9 @@ import (
"fmt" "fmt"
"hilbish/util" "hilbish/util"
"github.com/chuckpreslar/emission"
rt "github.com/arnodel/golua/runtime" rt "github.com/arnodel/golua/runtime"
"github.com/arnodel/golua/lib/packagelib" "github.com/arnodel/golua/lib/packagelib"
"github.com/chuckpreslar/emission"
) )
type Bait struct{ type Bait struct{
@ -54,22 +54,6 @@ failed, etc. To find all available hooks, see doc hooks.`)
return rt.TableValue(mod), nil return rt.TableValue(mod), nil
} }
func handleArgs(t *rt.Thread, c *rt.GoCont) (string, *rt.Closure, error) {
if err := c.CheckNArgs(2); err != nil {
return "", nil, err
}
name, err := c.StringArg(0)
if err != nil {
return "", nil, err
}
catcher, err := c.ClosureArg(1)
if err != nil {
return "", nil, err
}
return name, catcher, err
}
func handleHook(t *rt.Thread, c *rt.GoCont, name string, catcher *rt.Closure, args ...interface{}) { func handleHook(t *rt.Thread, c *rt.GoCont, name string, catcher *rt.Closure, args ...interface{}) {
funcVal := rt.FunctionValue(catcher) funcVal := rt.FunctionValue(catcher)
var luaArgs []rt.Value var luaArgs []rt.Value
@ -117,7 +101,7 @@ func (b *Bait) bthrow(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
// --- @param name string // --- @param name string
// --- @param cb function // --- @param cb function
func (b *Bait) bcatch(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func (b *Bait) bcatch(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
name, catcher, err := handleArgs(t, c) name, catcher, err := util.HandleStrCallback(t, c)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -134,7 +118,7 @@ func (b *Bait) bcatch(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
// --- @param name string // --- @param name string
// --- @param cb function // --- @param cb function
func (b *Bait) bcatchOnce(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func (b *Bait) bcatchOnce(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
name, catcher, err := handleArgs(t, c) name, catcher, err := util.HandleStrCallback(t, c)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -55,3 +55,19 @@ func DoFile(rtm *rt.Runtime, filename string) error {
return DoString(rtm, string(data)) return DoString(rtm, string(data))
} }
func HandleStrCallback(t *rt.Thread, c *rt.GoCont) (string, *rt.Closure, error) {
if err := c.CheckNArgs(2); err != nil {
return "", nil, err
}
name, err := c.StringArg(0)
if err != nil {
return "", nil, err
}
cb, err := c.ClosureArg(1)
if err != nil {
return "", nil, err
}
return name, cb, err
}