diff --git a/moonlight/runtime_clua.go b/moonlight/runtime_clua.go index 943dc0bd..c251cc05 100644 --- a/moonlight/runtime_clua.go +++ b/moonlight/runtime_clua.go @@ -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() } } diff --git a/moonlight/table_clua.go b/moonlight/table_clua.go index 17d8341e..d34cdd78 100644 --- a/moonlight/table_clua.go +++ b/moonlight/table_clua.go @@ -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) } } diff --git a/moonlight/value_clua.go b/moonlight/value_clua.go index 4e8578c3..1c1a2a17 100644 --- a/moonlight/value_clua.go +++ b/moonlight/value_clua.go @@ -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 "" + default: + fmt.Println("UNKNOWN in ToString", v.TypeName()) + return "" + } +} + func (v Value) TypeName() string { switch v.iface.(type) { case bool: