mirror of
https://github.com/Hilbis/Hilbish
synced 2025-07-01 16:52:03 +00:00
fix: set any value as table key, not only strings
This commit is contained in:
parent
0e1e964948
commit
4f4a836f05
@ -3,6 +3,8 @@
|
|||||||
package moonlight
|
package moonlight
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/aarzilli/golua/lua"
|
"github.com/aarzilli/golua/lua"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -49,6 +51,8 @@ func (mlr *Runtime) pushToState(v Value) {
|
|||||||
mlr.state.PushNil()
|
mlr.state.PushNil()
|
||||||
case StringType:
|
case StringType:
|
||||||
mlr.state.PushString(v.AsString())
|
mlr.state.PushString(v.AsString())
|
||||||
|
case IntType:
|
||||||
|
mlr.state.PushInteger(v.AsInt())
|
||||||
case BoolType:
|
case BoolType:
|
||||||
mlr.state.PushBoolean(v.AsBool())
|
mlr.state.PushBoolean(v.AsBool())
|
||||||
case TableType:
|
case TableType:
|
||||||
@ -58,6 +62,7 @@ func (mlr *Runtime) pushToState(v Value) {
|
|||||||
case FunctionType:
|
case FunctionType:
|
||||||
mlr.state.PushGoClosure(v.AsLuaFunction())
|
mlr.state.PushGoClosure(v.AsLuaFunction())
|
||||||
default:
|
default:
|
||||||
|
fmt.Println("PUSHING UNIMPLEMENTED TYPE", v.TypeName())
|
||||||
mlr.state.PushNil()
|
mlr.state.PushNil()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,19 +41,21 @@ func (t *Table) Push() {
|
|||||||
|
|
||||||
func (t *Table) SetField(key string, value Value) {
|
func (t *Table) SetField(key string, value Value) {
|
||||||
if t.refIdx != -1 {
|
if t.refIdx != -1 {
|
||||||
t.setInLua(key, value)
|
t.setInLua(StringValue(key), value)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
t.setInGo(key, value)
|
t.setInGo(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Table) setInLua(key string, value Value) {
|
func (t *Table) setInLua(key Value, value Value) {
|
||||||
t.Push()
|
t.Push()
|
||||||
defer t.mlr.state.Pop(1)
|
defer t.mlr.state.Pop(1)
|
||||||
|
|
||||||
t.mlr.pushToState(value)
|
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) {
|
func (t *Table) setInGo(key string, value Value) {
|
||||||
@ -66,7 +68,7 @@ func (t *Table) Set(key Value, value Value) {
|
|||||||
|
|
||||||
func (t *Table) syncToLua() {
|
func (t *Table) syncToLua() {
|
||||||
for k, v := range t.nativeFields {
|
for k, v := range t.nativeFields {
|
||||||
t.SetField(k.AsString(), v)
|
t.setInLua(k, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,12 @@
|
|||||||
|
|
||||||
package moonlight
|
package moonlight
|
||||||
|
|
||||||
import "github.com/aarzilli/golua/lua"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/aarzilli/golua/lua"
|
||||||
|
)
|
||||||
|
|
||||||
type Value struct {
|
type Value struct {
|
||||||
iface interface{}
|
iface interface{}
|
||||||
@ -122,6 +127,26 @@ func ToString(v Value) string {
|
|||||||
return v.AsString()
|
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 {
|
func (v Value) TypeName() string {
|
||||||
switch v.iface.(type) {
|
switch v.iface.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user