mirror of https://github.com/Hilbis/Hilbish
refactor: move arg handle function to util
parent
d2d423a4ef
commit
e466085d24
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
16
util/util.go
16
util/util.go
|
@ -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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue