func MessThingTellallMethod(state *lua.State, thing *Thing) int { state.PushGoFunction(func(state *lua.State) int { place := checkThing(state, 1) text := state.CheckString(2) // If arg 3 is present, it should be a table of Things to exclude. excludes := make(map[ThingId]bool) if 2 < state.GetTop() { if !state.IsTable(3) { state.ArgError(3, "expected `table` for exclude argument if present") } numExcludes := int(state.ObjLen(3)) for i := 0; i < numExcludes; i++ { state.RawGeti(3, i+1) exclude := checkThing(state, -1) excludes[exclude.Id] = true } } for _, content := range place.GetContents() { if excludes[content.Id] { continue } if content.Client != nil { content.Client.Send(text) } } return 0 }) return 1 }
func popMap(L *lua.State) map[string]interface{} { m := make(map[string]interface{}) L.PushNil() for L.Next(-2) != 0 { if L.IsString(-2) { var v interface{} if L.IsBoolean(-1) { v = L.ToBoolean(-1) } else if L.IsNumber(-1) { var t float64 t = L.ToNumber(-1) if t == float64(int64(t)) { v = int(t) } else { v = t } } else if L.IsString(-1) { v = L.ToString(-1) } else if L.IsNil(-1) { v = nil } else if L.IsTable(-1) { v = popMap(L) } m[L.ToString(-2)] = v } L.Pop(1) } return m }
func outputNumbersToStrings(L *lua.State) { L.GetGlobal("output") if !L.IsTable(-1) { L.NewTable() L.SetGlobal("output") } L.GetField(-1, "tags") if L.IsTable(-1) { // First key. L.PushNil() for L.Next(-2) != 0 { // Use 'key' at index -2 and 'value' at index -1. if L.IsString(-2) && L.IsString(-1) { // Convert numbers to strings. L.ToString(-1) L.SetField(-3, L.ToString(-2)) } else { // Remove 'value' and keep 'key' for next iteration. L.Pop(1) } } } L.Pop(1) L.Pop(1) }
func LuaIntTimestamp(L *lua.State) int { luaAssertArgnum(L, 1, "timestamp()") if !L.IsTable(-1) { panic(errors.New("Argoment of timestamp is not a table")) return 0 } L.CheckStack(1) t := time.Date(GetTableInt(L, "year"), time.Month(GetTableInt(L, "month")), GetTableInt(L, "day"), GetTableInt(L, "hour"), GetTableInt(L, "minute"), GetTableInt(L, "second"), 0, time.Local) L.PushInteger(int64(t.Unix())) return 1 }
// Convert a Lua value 'idx' on the stack to the Go value of desired type 't'. Handles // numerical and string types in a straightforward way, and will convert tables to // either map or slice types. func LuaToGo(L *lua.State, t reflect.Type, idx int) interface{} { var value interface{} var kind reflect.Kind if t != nil { // let the Lua type drive the conversion... if t.Kind() == reflect.Ptr { kind = t.Elem().Kind() } else if t.Kind() == reflect.Interface { t = nil } else { kind = t.Kind() } } switch L.Type(idx) { case lua.LUA_TNIL: if t == nil { return nil } switch kind { default: cannotConvert(L, idx, "nil", kind, t) } case lua.LUA_TBOOLEAN: if t == nil { kind = reflect.Bool } switch kind { case reflect.Bool: { ptr := new(bool) *ptr = bool(L.ToBoolean(idx)) value = *ptr } default: cannotConvert(L, idx, "bool", kind, t) } case lua.LUA_TSTRING: if t == nil { kind = reflect.String } switch kind { case reflect.String: { tos := L.ToString(idx) ptr := new(string) *ptr = tos value = *ptr } default: cannotConvert(L, idx, "string", kind, t) } case lua.LUA_TNUMBER: if t == nil { kind = reflect.Float64 } switch kind { case reflect.Float64: { ptr := new(float64) *ptr = L.ToNumber(idx) value = *ptr } case reflect.Float32: { ptr := new(float32) *ptr = float32(L.ToNumber(idx)) value = *ptr } case reflect.Int: { ptr := new(int) *ptr = int(L.ToNumber(idx)) value = *ptr } case reflect.Int8: { ptr := new(int8) *ptr = int8(L.ToNumber(idx)) value = *ptr } case reflect.Int16: { ptr := new(int16) *ptr = int16(L.ToNumber(idx)) value = *ptr } case reflect.Int32: { ptr := new(int32) *ptr = int32(L.ToNumber(idx)) value = *ptr } case reflect.Int64: { ptr := new(int64) *ptr = int64(L.ToNumber(idx)) value = *ptr } case reflect.Uint: { ptr := new(uint) *ptr = uint(L.ToNumber(idx)) value = *ptr } case reflect.Uint8: { ptr := new(uint8) *ptr = uint8(L.ToNumber(idx)) value = *ptr } case reflect.Uint16: { ptr := new(uint16) *ptr = uint16(L.ToNumber(idx)) value = *ptr } case reflect.Uint32: { ptr := new(uint32) *ptr = uint32(L.ToNumber(idx)) value = *ptr } case reflect.Uint64: { ptr := new(uint64) *ptr = uint64(L.ToNumber(idx)) value = *ptr } default: cannotConvert(L, idx, "number", kind, t) } case lua.LUA_TTABLE: if t == nil { kind = reflect.Interface } fallthrough default: istable := L.IsTable(idx) // if we don't know the type and the Lua object is userdata, // then it might be a proxy for a Go object. Otherwise wrap // it up as a LuaObject. if t == nil && !istable { if isValueProxy(L, idx) { return unwrapProxy(L, idx) } else { return NewLuaObject(L, idx) } } switch kind { case reflect.Slice: { // if we get a table, then copy its values to a new slice if istable { value = CopyTableToSlice(L, t, idx) } else { value = unwrapProxyOrComplain(L, idx) } } case reflect.Map: { if istable { value = CopyTableToMap(L, t, idx) } else { value = unwrapProxyOrComplain(L, idx) } } case reflect.Struct: { if istable { value = CopyTableToStruct(L, t, idx) } else { value = unwrapProxyOrComplain(L, idx) } } case reflect.Interface: { if istable { // have to make an executive decision here: tables with non-zero // length are assumed to be slices! if L.ObjLen(idx) > 0 { value = CopyTableToSlice(L, nil, idx) } else { value = CopyTableToMap(L, nil, idx) } } else if L.IsNumber(idx) { value = L.ToNumber(idx) } else if L.IsString(idx) { value = L.ToString(idx) } else if L.IsBoolean(idx) { value = L.ToBoolean(idx) } else if L.IsNil(idx) { return nil } else { value = unwrapProxyOrComplain(L, idx) } } default: value = unwrapProxyOrComplain(L, idx) //cannotConvert(L,idx,"unknown",kind,t) } } return value }
func luaToGo(L *lua.State, t reflect.Type, idx int, visited map[uintptr]interface{}) interface{} { var value interface{} var kind reflect.Kind if t != nil { if t.Kind() == reflect.Ptr { kind = t.Elem().Kind() } else if t.Kind() == reflect.Interface { // Let the Lua type drive the conversion. t = nil } else { kind = t.Kind() } } switch L.Type(idx) { case lua.LUA_TNIL: if t == nil { return nil } switch kind { default: value = reflect.Zero(t).Interface() } case lua.LUA_TBOOLEAN: if t == nil { kind = reflect.Bool } switch kind { case reflect.Bool: ptr := new(bool) *ptr = L.ToBoolean(idx) value = *ptr default: value = reflect.Zero(t).Interface() } case lua.LUA_TSTRING: if t == nil { kind = reflect.String } switch kind { case reflect.String: tos := L.ToString(idx) ptr := new(string) *ptr = tos value = *ptr default: value = reflect.Zero(t).Interface() } case lua.LUA_TNUMBER: if t == nil { // Infering the type here (e.g. int if round value) would break backward // compatibility and drift away from Lua's design: the numeric type is // specified at compile time. kind = reflect.Float64 } switch kind { case reflect.Float64: ptr := new(float64) *ptr = L.ToNumber(idx) value = *ptr case reflect.Float32: ptr := new(float32) *ptr = float32(L.ToNumber(idx)) value = *ptr case reflect.Int: ptr := new(int) *ptr = int(L.ToNumber(idx)) value = *ptr case reflect.Int8: ptr := new(int8) *ptr = int8(L.ToNumber(idx)) value = *ptr case reflect.Int16: ptr := new(int16) *ptr = int16(L.ToNumber(idx)) value = *ptr case reflect.Int32: ptr := new(int32) *ptr = int32(L.ToNumber(idx)) value = *ptr case reflect.Int64: ptr := new(int64) *ptr = int64(L.ToNumber(idx)) value = *ptr case reflect.Uint: ptr := new(uint) *ptr = uint(L.ToNumber(idx)) value = *ptr case reflect.Uint8: ptr := new(uint8) *ptr = uint8(L.ToNumber(idx)) value = *ptr case reflect.Uint16: ptr := new(uint16) *ptr = uint16(L.ToNumber(idx)) value = *ptr case reflect.Uint32: ptr := new(uint32) *ptr = uint32(L.ToNumber(idx)) value = *ptr case reflect.Uint64: ptr := new(uint64) *ptr = uint64(L.ToNumber(idx)) value = *ptr default: value = reflect.Zero(t).Interface() } case lua.LUA_TTABLE: if t == nil { kind = reflect.Interface } fallthrough default: istable := L.IsTable(idx) // If we don't know the type and the Lua object is userdata, // then it might be a proxy for a Go object. Otherwise wrap // it up as a LuaObject. if t == nil && !istable { if isValueProxy(L, idx) { v, _ := valueOfProxy(L, idx) return v.Interface() } return NewLuaObject(L, idx) } switch kind { case reflect.Array: if istable { ptr := L.ToPointer(idx) if val, ok := visited[ptr]; ok { return val } value = copyTableToSlice(L, t, idx, visited) } else { value = mustUnwrapProxy(L, idx) } case reflect.Slice: // if we get a table, then copy its values to a new slice if istable { ptr := L.ToPointer(idx) if val, ok := visited[ptr]; ok { return val } value = copyTableToSlice(L, t, idx, visited) } else { value = mustUnwrapProxy(L, idx) } case reflect.Map: if istable { ptr := L.ToPointer(idx) if val, ok := visited[ptr]; ok { return val } value = copyTableToMap(L, t, idx, visited) } else { value = mustUnwrapProxy(L, idx) } case reflect.Struct: if istable { ptr := L.ToPointer(idx) if val, ok := visited[ptr]; ok { return val } value = copyTableToStruct(L, t, idx, visited) } else { value = mustUnwrapProxy(L, idx) } case reflect.Interface: if istable { ptr := L.ToPointer(idx) if val, ok := visited[ptr]; ok { return val } // We have to make an executive decision here: tables with non-zero // length are assumed to be slices! if L.ObjLen(idx) > 0 { value = copyTableToSlice(L, nil, idx, visited) } else { value = copyTableToMap(L, nil, idx, visited) } } else if L.IsNumber(idx) { value = L.ToNumber(idx) } else if L.IsString(idx) { value = L.ToString(idx) } else if L.IsBoolean(idx) { value = L.ToBoolean(idx) } else if L.IsNil(idx) { return nil } else { value = mustUnwrapProxy(L, idx) } default: value = mustUnwrapProxy(L, idx) } } return value }
// Convert a Lua value 'idx' on the stack to the Go value of desired type 't'. Handles // numerical and string types in a straightforward way, and will convert tables to // either map or slice types. func LuaToGo(L *lua.State, t reflect.Type, idx int) interface{} { var value interface{} var kind reflect.Kind if t == nil { // let the Lua type drive the conversion... switch L.Type(idx) { case lua.LUA_TNIL: return nil // well, d'oh case lua.LUA_TBOOLEAN: kind = reflect.Bool case lua.LUA_TSTRING: kind = reflect.String case lua.LUA_TTABLE: kind = reflect.Interface default: return NewLuaObject(L, idx) } } else if t.Kind() == reflect.Ptr { kind = t.Elem().Kind() } else { kind = t.Kind() } switch kind { // various numerical types are tedious but straightforward case reflect.Float64: { ptr := new(float64) *ptr = L.ToNumber(idx) value = *ptr } case reflect.Float32: { ptr := new(float32) *ptr = float32(L.ToNumber(idx)) value = *ptr } case reflect.Int: { ptr := new(int) *ptr = int(L.ToNumber(idx)) value = *ptr } case reflect.Int8: { ptr := new(byte) *ptr = byte(L.ToNumber(idx)) value = *ptr } case reflect.String: { tos := L.ToString(idx) ptr := new(string) *ptr = tos value = *ptr } case reflect.Bool: { ptr := new(bool) *ptr = bool(L.ToBoolean(idx)) value = *ptr } case reflect.Slice: { // if we get a table, then copy its values to a new slice if L.IsTable(idx) { value = CopyTableToSlice(L, t, idx) } else { value = unwrapProxy(L, idx) } } case reflect.Map: { if L.IsTable(idx) { value = CopyTableToMap(L, t, idx) } else { value = unwrapProxy(L, idx) } } case reflect.Struct: { if L.IsTable(idx) { value = CopyTableToStruct(L, t, idx) } else { value = unwrapProxy(L, idx) } } case reflect.Interface: { if L.IsTable(idx) { // have to make an executive decision here: tables with non-zero // length are assumed to be slices! if L.ObjLen(idx) > 0 { value = CopyTableToSlice(L, nil, idx) } else { value = CopyTableToMap(L, nil, idx) } } else if L.IsNumber(idx) { value = L.ToNumber(idx) } else if L.IsString(idx) { value = L.ToString(idx) } else if L.IsBoolean(idx) { value = L.ToBoolean(idx) } else { value = unwrapProxy(L, idx) } } default: { fmt.Println("unhandled type", t) value = 20 } } return value }
// 'exist' is optional. func run(L *lua.State, registryIndex string, code string, input *inputInfo, output *outputInfo, exist *inputInfo) error { // Restore the sandbox. err := L.DoString(luaRestoreSandbox) if err != nil { log.Fatal("Cannot load function to restore sandbox", err) } L.PushString(registryWhitelist) L.GetTable(lua.LUA_REGISTRYINDEX) err = L.Call(1, 0) if err != nil { log.Fatal("Failed to restore sandbox", err) } goToLua(L, "input", *input) goToLua(L, "output", *output) if exist != nil { goToLua(L, "existinfo", *exist) } // Shortcut (mostly for prescript and postscript). L.GetGlobal("input") L.GetField(-1, "tags") L.SetGlobal("i") L.Pop(1) L.GetGlobal("output") L.GetField(-1, "tags") L.SetGlobal("o") L.Pop(1) // Call the compiled script. L.PushString(registryIndex) L.GetTable(lua.LUA_REGISTRYINDEX) L.PushString(code) if L.IsTable(-2) { L.GetTable(-2) if L.IsFunction(-1) { err := L.Call(0, 0) if err != nil { L.SetTop(0) return fmt.Errorf("%s", err) } } else { L.Pop(1) } } else { L.Pop(1) } L.Pop(1) // Allow tags to be numbers for convenience. outputNumbersToStrings(L) L.GetGlobal("output") r := luar.LuaToGo(L, reflect.TypeOf(*output), -1) L.Pop(1) *output = r.(outputInfo) return nil }