feat: implement bait loading

pull/314/merge^2
sammyette 2024-07-20 10:50:04 -04:00
parent af67bbd625
commit 56045ed236
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
4 changed files with 21 additions and 23 deletions

View File

@ -28,10 +28,10 @@ package bait
import (
"errors"
"hilbish/moonlight"
"hilbish/util"
rt "github.com/arnodel/golua/runtime"
"github.com/arnodel/golua/lib/packagelib"
)
type listenerType int
@ -48,26 +48,21 @@ type Listener struct{
typ listenerType
once bool
caller func(...interface{})
luaCaller *rt.Closure
luaCaller *moonlight.Closure
}
type Bait struct{
Loader packagelib.Loader
recoverer Recoverer
handlers map[string][]*Listener
rtm *rt.Runtime
rtm *moonlight.Runtime
}
// New creates a new Bait instance.
func New(rtm *rt.Runtime) *Bait {
func New(rtm *moonlight.Runtime) *Bait {
b := &Bait{
handlers: make(map[string][]*Listener),
rtm: rtm,
}
b.Loader = packagelib.Loader{
Load: b.loaderFunc,
Name: "bait",
}
return b
}
@ -88,16 +83,16 @@ func (b *Bait) Emit(event string, args ...interface{}) {
if handle.typ == luaListener {
funcVal := rt.FunctionValue(handle.luaCaller)
var luaArgs []rt.Value
var luaArgs []moonlight.Value
for _, arg := range args {
var luarg rt.Value
var luarg moonlight.Value
switch arg.(type) {
case rt.Value: luarg = arg.(rt.Value)
default: luarg = rt.AsValue(arg)
}
luaArgs = append(luaArgs, luarg)
}
_, err := rt.Call1(b.rtm.MainThread(), funcVal, luaArgs...)
_, err := b.rtm.Call1(funcVal, luaArgs...)
if err != nil {
if event != "error" {
b.Emit("error", event, handle.luaCaller, err.Error())
@ -212,18 +207,20 @@ func (b *Bait) callRecoverer(event string, handler *Listener, err interface{}) {
b.recoverer(event, handler, err)
}
func (b *Bait) loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
exports := map[string]util.LuaExport{
func (b *Bait) Loader(rtm *moonlight.Runtime) moonlight.Value {
exports := map[string]moonlight.Export{
/*
"catch": util.LuaExport{b.bcatch, 2, false},
"catchOnce": util.LuaExport{b.bcatchOnce, 2, false},
"throw": util.LuaExport{b.bthrow, 1, true},
"release": util.LuaExport{b.brelease, 2, false},
"hooks": util.LuaExport{b.bhooks, 1, false},
*/
}
mod := rt.NewTable()
util.SetExports(rtm, mod, exports)
mod := moonlight.NewTable()
rtm.SetExports(mod, exports)
return rt.TableValue(mod), nil
return moonlight.TableValue(mod)
}
func handleHook(t *rt.Thread, c *rt.GoCont, name string, catcher *rt.Closure, args ...interface{}) {

View File

@ -33,6 +33,7 @@ This sink is for writing errors, as the name would suggest.
package commander
import (
"hilbish/moonlight"
"hilbish/util"
"hilbish/golibs/bait"
@ -46,7 +47,7 @@ type Commander struct{
Commands map[string]*rt.Closure
}
func New(rtm *rt.Runtime) *Commander {
func New(rtm *moonlight.Runtime) *Commander {
c := &Commander{
Events: bait.New(rtm),
Commands: make(map[string]*rt.Closure),

9
lua.go
View File

@ -5,7 +5,7 @@ import (
"os"
//"hilbish/util"
//"hilbish/golibs/bait"
"hilbish/golibs/bait"
//"hilbish/golibs/commander"
//"hilbish/golibs/fs"
//"hilbish/golibs/terminal"
@ -30,15 +30,14 @@ func luaInit() {
cmds = commander.New(l)
lib.LoadLibs(l, cmds.Loader)
*/
hooks = bait.New(l)
hooks.SetRecoverer(func(event string, handler *bait.Listener, err interface{}) {
fmt.Println("Error in `error` hook handler:", err)
hooks.Off(event, handler)
})
lib.LoadLibs(l, hooks.Loader)
l.LoadLibrary(hooks.Loader, "bait")
/*
// Add Ctrl-C handler
hooks.On("signal.sigint", func(...interface{}) {
if !interactive {

View File

@ -8,6 +8,7 @@ type GoFunctionFunc = rt.GoFunctionFunc
type GoCont = rt.GoCont
type Cont = rt.Cont
type Closure = rt.Closure
func (mlr *Runtime) CheckNArgs(c *GoCont, num int) error {
return c.CheckNArgs(num)