2024-07-21 15:37:08 +00:00
|
|
|
//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())
|
2024-12-22 04:03:21 +00:00
|
|
|
case TableType:
|
|
|
|
tbl := v.AsTable()
|
|
|
|
tbl.SetRuntime(mlr)
|
2024-12-22 05:08:56 +00:00
|
|
|
tbl.Push()
|
2024-12-22 04:03:21 +00:00
|
|
|
default: mlr.state.PushNil()
|
2024-07-21 15:37:08 +00:00
|
|
|
}
|
|
|
|
}
|