feat: implement bait loading

midnight-edition
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 ( import (
"errors" "errors"
"hilbish/moonlight"
"hilbish/util" "hilbish/util"
rt "github.com/arnodel/golua/runtime" rt "github.com/arnodel/golua/runtime"
"github.com/arnodel/golua/lib/packagelib"
) )
type listenerType int type listenerType int
@ -48,26 +48,21 @@ type Listener struct{
typ listenerType typ listenerType
once bool once bool
caller func(...interface{}) caller func(...interface{})
luaCaller *rt.Closure luaCaller *moonlight.Closure
} }
type Bait struct{ type Bait struct{
Loader packagelib.Loader
recoverer Recoverer recoverer Recoverer
handlers map[string][]*Listener handlers map[string][]*Listener
rtm *rt.Runtime rtm *moonlight.Runtime
} }
// New creates a new Bait instance. // New creates a new Bait instance.
func New(rtm *rt.Runtime) *Bait { func New(rtm *moonlight.Runtime) *Bait {
b := &Bait{ b := &Bait{
handlers: make(map[string][]*Listener), handlers: make(map[string][]*Listener),
rtm: rtm, rtm: rtm,
} }
b.Loader = packagelib.Loader{
Load: b.loaderFunc,
Name: "bait",
}
return b return b
} }
@ -88,16 +83,16 @@ func (b *Bait) Emit(event string, args ...interface{}) {
if handle.typ == luaListener { if handle.typ == luaListener {
funcVal := rt.FunctionValue(handle.luaCaller) funcVal := rt.FunctionValue(handle.luaCaller)
var luaArgs []rt.Value var luaArgs []moonlight.Value
for _, arg := range args { for _, arg := range args {
var luarg rt.Value var luarg moonlight.Value
switch arg.(type) { switch arg.(type) {
case rt.Value: luarg = arg.(rt.Value) case rt.Value: luarg = arg.(rt.Value)
default: luarg = rt.AsValue(arg) default: luarg = rt.AsValue(arg)
} }
luaArgs = append(luaArgs, luarg) luaArgs = append(luaArgs, luarg)
} }
_, err := rt.Call1(b.rtm.MainThread(), funcVal, luaArgs...) _, err := b.rtm.Call1(funcVal, luaArgs...)
if err != nil { if err != nil {
if event != "error" { if event != "error" {
b.Emit("error", event, handle.luaCaller, err.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) b.recoverer(event, handler, err)
} }
func (b *Bait) loaderFunc(rtm *rt.Runtime) (rt.Value, func()) { func (b *Bait) Loader(rtm *moonlight.Runtime) moonlight.Value {
exports := map[string]util.LuaExport{ exports := map[string]moonlight.Export{
/*
"catch": util.LuaExport{b.bcatch, 2, false}, "catch": util.LuaExport{b.bcatch, 2, false},
"catchOnce": util.LuaExport{b.bcatchOnce, 2, false}, "catchOnce": util.LuaExport{b.bcatchOnce, 2, false},
"throw": util.LuaExport{b.bthrow, 1, true}, "throw": util.LuaExport{b.bthrow, 1, true},
"release": util.LuaExport{b.brelease, 2, false}, "release": util.LuaExport{b.brelease, 2, false},
"hooks": util.LuaExport{b.bhooks, 1, false}, "hooks": util.LuaExport{b.bhooks, 1, false},
*/
} }
mod := rt.NewTable() mod := moonlight.NewTable()
util.SetExports(rtm, mod, exports) 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{}) { 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 package commander
import ( import (
"hilbish/moonlight"
"hilbish/util" "hilbish/util"
"hilbish/golibs/bait" "hilbish/golibs/bait"
@ -46,7 +47,7 @@ type Commander struct{
Commands map[string]*rt.Closure Commands map[string]*rt.Closure
} }
func New(rtm *rt.Runtime) *Commander { func New(rtm *moonlight.Runtime) *Commander {
c := &Commander{ c := &Commander{
Events: bait.New(rtm), Events: bait.New(rtm),
Commands: make(map[string]*rt.Closure), Commands: make(map[string]*rt.Closure),

9
lua.go
View File

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

View File

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