2
2
mirror of https://github.com/Hilbis/Hilbish synced 2025-07-01 08:42:04 +00:00
Hilbish/moonlight/runtime_clua.go
sammy-ette 9e77f0ba32
refactor: change go function types
this is the only way i could think of to be able
to push go functions to lua on the clua side.

this may or may not need adjustments on golua side
though...
2025-06-14 10:06:20 -04:00

61 lines
961 B
Go

//go:build midnight
package moonlight
import (
"github.com/aarzilli/golua/lua"
)
type Runtime struct {
state *lua.State
}
func NewRuntime() *Runtime {
L := lua.NewState()
L.OpenLibs()
return &Runtime{
state: L,
}
}
func (mlr *Runtime) PushNext1(c *GoCont, v Value) Cont {
c.vals = []Value{v}
return c
}
func (mlr *Runtime) Call1(f Value, args ...Value) (Value, error) {
for _, arg := range args {
mlr.pushToState(arg)
}
if f.refIdx > 0 {
mlr.state.RawGeti(lua.LUA_REGISTRYINDEX, f.refIdx)
mlr.state.Call(len(args), 1)
}
if mlr.state.GetTop() == 0 {
return NilValue, nil
}
return NilValue, nil
}
func (mlr *Runtime) pushToState(v Value) {
switch v.Type() {
case NilType:
mlr.state.PushNil()
case StringType:
mlr.state.PushString(v.AsString())
case TableType:
tbl := v.AsTable()
tbl.SetRuntime(mlr)
tbl.Push()
case FunctionType:
mlr.state.PushGoClosure(v.AsLuaFunction())
default:
mlr.state.PushNil()
}
}