2024-07-21 15:37:08 +00:00
|
|
|
//go:build midnight
|
2024-12-22 05:08:56 +00:00
|
|
|
|
2024-07-21 15:37:08 +00:00
|
|
|
package moonlight
|
|
|
|
|
2024-12-22 04:03:21 +00:00
|
|
|
import (
|
|
|
|
"github.com/aarzilli/golua/lua"
|
|
|
|
)
|
2024-07-21 15:37:08 +00:00
|
|
|
|
2024-12-22 05:08:56 +00:00
|
|
|
type Table struct {
|
|
|
|
refIdx int
|
|
|
|
mlr *Runtime
|
2024-12-22 04:03:21 +00:00
|
|
|
nativeFields map[Value]Value
|
2024-07-21 15:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTable() *Table {
|
|
|
|
return &Table{
|
2024-12-22 05:08:56 +00:00
|
|
|
refIdx: -1,
|
2024-12-22 04:03:21 +00:00
|
|
|
nativeFields: make(map[Value]Value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) SetRuntime(mlr *Runtime) {
|
|
|
|
t.mlr = mlr
|
|
|
|
|
|
|
|
if t.refIdx == -1 {
|
|
|
|
mlr.state.NewTable()
|
|
|
|
t.refIdx = mlr.state.Ref(lua.LUA_REGISTRYINDEX)
|
2024-12-22 05:08:56 +00:00
|
|
|
t.Push() // because Ref pops off the stack
|
|
|
|
t.syncToLua()
|
2024-12-22 04:03:21 +00:00
|
|
|
mlr.state.Pop(1)
|
2024-07-21 15:37:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) Get(val Value) Value {
|
|
|
|
return NilValue
|
|
|
|
}
|
|
|
|
|
2024-12-22 04:03:21 +00:00
|
|
|
func (t *Table) Push() {
|
|
|
|
t.mlr.state.RawGeti(lua.LUA_REGISTRYINDEX, t.refIdx)
|
|
|
|
}
|
|
|
|
|
2024-07-21 15:37:08 +00:00
|
|
|
func (t *Table) SetField(key string, value Value) {
|
2024-12-22 05:08:56 +00:00
|
|
|
if t.refIdx != -1 {
|
|
|
|
t.setInLua(key, value)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.setInGo(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) setInLua(key string, value Value) {
|
2024-12-22 04:03:21 +00:00
|
|
|
t.Push()
|
|
|
|
defer t.mlr.state.Pop(1)
|
|
|
|
|
|
|
|
t.mlr.pushToState(value)
|
2024-12-22 05:08:56 +00:00
|
|
|
t.mlr.state.SetField(-2, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) setInGo(key string, value Value) {
|
|
|
|
t.nativeFields[StringValue(key)] = value
|
2024-07-21 15:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) Set(key Value, value Value) {
|
2024-12-22 04:03:21 +00:00
|
|
|
t.nativeFields[key] = value
|
2024-07-21 15:37:08 +00:00
|
|
|
}
|
|
|
|
|
2024-12-22 05:08:56 +00:00
|
|
|
func (t *Table) syncToLua() {
|
|
|
|
for k, v := range t.nativeFields {
|
|
|
|
t.SetField(k.AsString(), v)
|
|
|
|
}
|
|
|
|
}
|
2024-07-21 15:37:08 +00:00
|
|
|
func ForEach(tbl *Table, cb func(key Value, val Value)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mlr *Runtime) GlobalTable() *Table {
|
2024-12-22 04:03:21 +00:00
|
|
|
mlr.state.GetGlobal("_G")
|
2024-07-21 15:37:08 +00:00
|
|
|
return &Table{
|
2024-12-22 04:03:21 +00:00
|
|
|
refIdx: mlr.state.Ref(lua.LUA_REGISTRYINDEX),
|
2024-07-21 15:37:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ToTable(v Value) *Table {
|
|
|
|
return &Table{
|
|
|
|
refIdx: -1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TryTable(v Value) (*Table, bool) {
|
|
|
|
return nil, false
|
|
|
|
}
|
2024-07-26 21:58:25 +00:00
|
|
|
|
2024-12-21 22:30:03 +00:00
|
|
|
func (t *Table) setRefIdx(mlr *Runtime, i int) {
|
2024-07-26 21:58:25 +00:00
|
|
|
t.refIdx = mlr.state.Ref(i)
|
|
|
|
}
|