2
2
mirror of https://github.com/Hilbis/Hilbish synced 2025-07-01 08:42:04 +00:00

fix: set any value as table key, not only strings

This commit is contained in:
sammyette 2025-06-14 12:26:23 -04:00
parent 0e1e964948
commit 4f4a836f05
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
3 changed files with 37 additions and 5 deletions

View File

@ -3,6 +3,8 @@
package moonlight
import (
"fmt"
"github.com/aarzilli/golua/lua"
)
@ -49,6 +51,8 @@ func (mlr *Runtime) pushToState(v Value) {
mlr.state.PushNil()
case StringType:
mlr.state.PushString(v.AsString())
case IntType:
mlr.state.PushInteger(v.AsInt())
case BoolType:
mlr.state.PushBoolean(v.AsBool())
case TableType:
@ -58,6 +62,7 @@ func (mlr *Runtime) pushToState(v Value) {
case FunctionType:
mlr.state.PushGoClosure(v.AsLuaFunction())
default:
fmt.Println("PUSHING UNIMPLEMENTED TYPE", v.TypeName())
mlr.state.PushNil()
}
}

View File

@ -41,19 +41,21 @@ func (t *Table) Push() {
func (t *Table) SetField(key string, value Value) {
if t.refIdx != -1 {
t.setInLua(key, value)
t.setInLua(StringValue(key), value)
return
}
t.setInGo(key, value)
}
func (t *Table) setInLua(key string, value Value) {
func (t *Table) setInLua(key Value, value Value) {
t.Push()
defer t.mlr.state.Pop(1)
t.mlr.pushToState(value)
t.mlr.state.SetField(-2, key)
t.mlr.pushToState(key)
t.mlr.state.Insert(-2)
t.mlr.state.SetTable(-3)
}
func (t *Table) setInGo(key string, value Value) {
@ -66,7 +68,7 @@ func (t *Table) Set(key Value, value Value) {
func (t *Table) syncToLua() {
for k, v := range t.nativeFields {
t.SetField(k.AsString(), v)
t.setInLua(k, v)
}
}

View File

@ -2,7 +2,12 @@
package moonlight
import "github.com/aarzilli/golua/lua"
import (
"fmt"
"strconv"
"github.com/aarzilli/golua/lua"
)
type Value struct {
iface interface{}
@ -122,6 +127,26 @@ func ToString(v Value) string {
return v.AsString()
}
func (v Value) ToString() string {
if v.iface == nil {
return "nil"
}
switch v.iface.(type) {
case bool:
return strconv.FormatBool(v.AsBool())
case int64:
return strconv.FormatInt(v.AsInt(), 10)
case string:
return v.AsString()
case *Table:
return "<moonlight table>"
default:
fmt.Println("UNKNOWN in ToString", v.TypeName())
return "<unk>"
}
}
func (v Value) TypeName() string {
switch v.iface.(type) {
case bool: