2024-07-21 15:37:08 +00:00
|
|
|
//go:build !midnight
|
2024-07-19 20:54:15 +00:00
|
|
|
package moonlight
|
|
|
|
|
|
|
|
import (
|
|
|
|
rt "github.com/arnodel/golua/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Table struct{
|
|
|
|
lt *rt.Table
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTable() *Table {
|
|
|
|
return &Table{
|
|
|
|
lt: rt.NewTable(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) Get(val Value) Value {
|
|
|
|
return t.lt.Get(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) SetField(key string, value Value) {
|
|
|
|
t.lt.Set(rt.StringValue(key), value)
|
|
|
|
}
|
|
|
|
|
2024-07-20 15:34:39 +00:00
|
|
|
func (t *Table) Set(key Value, value Value) {
|
|
|
|
t.lt.Set(key, value)
|
|
|
|
}
|
|
|
|
|
2024-07-20 19:11:45 +00:00
|
|
|
func ForEach(tbl *Table, cb func(key Value, val Value)) {
|
|
|
|
nextVal := rt.NilValue
|
|
|
|
for {
|
|
|
|
key, val, _ := tbl.lt.Next(nextVal)
|
|
|
|
if key == rt.NilValue {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
nextVal = key
|
|
|
|
|
|
|
|
cb(Value(key), Value(val))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-19 20:54:15 +00:00
|
|
|
func (mlr *Runtime) GlobalTable() *Table {
|
|
|
|
return &Table{
|
|
|
|
lt: mlr.rt.GlobalEnv(),
|
|
|
|
}
|
|
|
|
}
|
2024-07-20 19:11:45 +00:00
|
|
|
|
|
|
|
func convertToMoonlightTable(t *rt.Table) *Table {
|
|
|
|
return &Table{
|
|
|
|
lt: t,
|
|
|
|
}
|
|
|
|
}
|
2024-07-21 15:37:08 +00:00
|
|
|
|
|
|
|
func TryTable(v Value) (*Table, bool) {
|
|
|
|
t, ok := v.TryTable()
|
|
|
|
|
|
|
|
return convertToMoonlightTable(t), ok
|
|
|
|
}
|