// Also for arrays. func copySliceToTable(L *lua.State, vslice reflect.Value, visited visitor) int { ref := vslice for vslice.Kind() == reflect.Ptr { // For arrays. vslice = vslice.Elem() } if vslice.IsValid() && (vslice.Kind() == reflect.Slice || vslice.Kind() == reflect.Array) { n := vslice.Len() L.CreateTable(n, 0) if vslice.Kind() == reflect.Slice { visited.mark(vslice) } else if ref.Kind() == reflect.Ptr { visited.mark(ref) } for i := 0; i < n; i++ { L.PushInteger(int64(i + 1)) v := vslice.Index(i) if isNil(v) { v = nullv } goToLua(L, nil, v, true, visited) L.SetTable(-3) } return 1 } L.PushNil() L.PushString("not a slice/array") return 2 }
func copyStructToTable(L *lua.State, vstruct reflect.Value, visited visitor) int { // If 'vstruct' is a pointer to struct, use the pointer to mark as visited. ref := vstruct for vstruct.Kind() == reflect.Ptr { vstruct = vstruct.Elem() } if vstruct.IsValid() && vstruct.Type().Kind() == reflect.Struct { n := vstruct.NumField() L.CreateTable(n, 0) if ref.Kind() == reflect.Ptr { visited.mark(ref) } for i := 0; i < n; i++ { st := vstruct.Type() field := st.Field(i) key := field.Name tag := field.Tag.Get("lua") if tag != "" { key = tag } goToLua(L, nil, reflect.ValueOf(key), true, visited) v := vstruct.Field(i) goToLua(L, nil, v, true, visited) L.SetTable(-3) } return 1 } L.PushNil() L.PushString("not a struct") return 2 }
func pushMap(L *lua.State, m map[string]interface{}, lower bool) { L.CreateTable(0, len(m)) for k, v := range m { if lower { L.PushString(strings.ToLower(k)) } else { L.PushString(k) } switch t := v.(type) { case string: L.PushString(t) case int64: L.PushInteger(t) case int: L.PushInteger(int64(t)) case float64: L.PushNumber(t) case bool: L.PushBoolean(t) case map[string]interface{}: pushMap(L, t, false) default: L.PushNil() } L.SetTable(-3) } }
func MessThingContents(state *lua.State, thing *Thing) int { // make a new table state.CreateTable(len(thing.Contents), 0) // ( -- tbl ) // for each content add a new lua-space Thing for i, contentId := range thing.Contents { state.PushInteger(int64(i)) pushValue(state, contentId) state.SetTable(-3) // ( tbl key val -- tbl ) } // ( tbl -- tbl ) return 1 }
func sandboxCompile(L *lua.State, registryIndex string, name, code string) { L.PushString(registryIndex) L.GetTable(lua.LUA_REGISTRYINDEX) L.PushString(name) err := L.LoadString(code) if err != 0 { log.Fatalf("%s: %s", name, L.ToString(-1)) L.Pop(2) } else { L.SetTable(-3) } }
// copy a Go slice to a Lua table func CopySliceToTable(L *lua.State, vslice reflect.Value) int { if vslice.IsValid() && vslice.Type().Kind() == reflect.Slice { n := vslice.Len() L.CreateTable(n, 0) for i := 0; i < n; i++ { L.PushInteger(int64(i + 1)) GoToLua(L, nil, vslice.Index(i)) L.SetTable(-3) } return 1 } else { L.PushNil() L.PushString("not a slice!") } return 2 }
// copy a Go map to a Lua table func CopyMapToTable(L *lua.State, vmap reflect.Value) int { if vmap.IsValid() && vmap.Type().Kind() == reflect.Map { n := vmap.Len() L.CreateTable(0, n) for _, key := range vmap.MapKeys() { val := vmap.MapIndex(key) GoToLua(L, nil, key) GoToLua(L, nil, val) L.SetTable(-3) } return 1 } else { L.PushNil() L.PushString("not a map!") } return 2 }
func copyMapToTable(L *lua.State, vmap reflect.Value, visited visitor) int { if vmap.IsValid() && vmap.Type().Kind() == reflect.Map { n := vmap.Len() L.CreateTable(0, n) visited.mark(vmap) for _, key := range vmap.MapKeys() { v := vmap.MapIndex(key) goToLua(L, nil, key, false, visited) if isNil(v) { v = nullv } goToLua(L, nil, v, true, visited) L.SetTable(-3) } return 1 } L.PushNil() L.PushString("not a map!") return 2 }
// Copy a Go struct to a Lua table. nils in both slices and structs // are represented as luar.null. Defines luar.struct2table // Use tags to set field names. func CopyStructToTable(L *lua.State, vstruct reflect.Value) int { if vstruct.IsValid() && vstruct.Type().Kind() == reflect.Struct { n := vstruct.NumField() L.CreateTable(n, 0) for i := 0; i < n; i++ { st := vstruct.Type() field := st.Field(i) key := field.Name tag := field.Tag.Get("lua") if tag != "" { key = tag } GoToLua(L, nil, reflect.ValueOf(key), true) v := vstruct.Field(i) GoToLua(L, nil, v, true) L.SetTable(-3) } return 1 } else { L.PushNil() L.PushString("not a struct!") } return 2 }
func SetTableIntString(L *lua.State, idx int64, value string) { L.PushInteger(idx) L.PushString(value) L.SetTable(-3) }
func SetTableInt(L *lua.State, name string, value int64) { // Remember to check stack for 2 extra locations L.PushString(name) L.PushInteger(value) L.SetTable(-3) }