From 98ca857d73956bf69a07641710b678c11681319f Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Fri, 7 Mar 2025 07:25:34 -0600 Subject: [PATCH] interface{} to any --- DOCS.md | 12 ++++++------ README.md | 2 +- example/main.go | 2 +- table.go | 18 +++++++++--------- wrapper.go | 10 +++++----- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/DOCS.md b/DOCS.md index 5f07f5d..b611cfa 100644 --- a/DOCS.md +++ b/DOCS.md @@ -110,7 +110,7 @@ Converts the value to a boolean. bool := L.ToBoolean(-1) ``` -### ToValue(index int) (interface{}, error) +### ToValue(index int) (any, error) Converts any Lua value to its Go equivalent. ```go val, err := L.ToValue(-1) @@ -119,7 +119,7 @@ if err != nil { } ``` -### ToTable(index int) (map[string]interface{}, error) +### ToTable(index int) (map[string]any, error) Converts a Lua table to a Go map. ```go table, err := L.ToTable(-1) @@ -148,16 +148,16 @@ L.PushBoolean(true) L.PushNil() ``` -### PushValue(v interface{}) error +### PushValue(v any) error Pushes any Go value onto the stack. ```go err := L.PushValue(myValue) ``` -### PushTable(table map[string]interface{}) error +### PushTable(table map[string]any) error Pushes a Go map as a Lua table. ```go -data := map[string]interface{}{ +data := map[string]any{ "key": "value", "numbers": []float64{1, 2, 3}, } @@ -310,7 +310,7 @@ nresults, err := L.Execute("return 1, 2, 3") // nresults would be 3 ``` -### ExecuteWithResult(code string) (interface{}, error) +### ExecuteWithResult(code string) (any, error) Executes a Lua string and returns the first result. ```go result, err := L.ExecuteWithResult("return 'hello'") diff --git a/README.md b/README.md index 2c49de0..113b046 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ Lua tables are pretty powerful - they're like a mix of Go's maps and slices. We ```go // Go → Lua -stuff := map[string]interface{}{ +stuff := map[string]any{ "name": "Arthur Dent", "age": 30, "items": []float64{1, 2, 3}, diff --git a/example/main.go b/example/main.go index 2287deb..745889f 100644 --- a/example/main.go +++ b/example/main.go @@ -35,7 +35,7 @@ func main() { }) // Add some values to the Lua environment - L.PushValue(map[string]interface{}{ + L.PushValue(map[string]any{ "appName": "LuaJIT Example", "version": 1.0, "features": []float64{1, 2, 3}, diff --git a/table.go b/table.go index 7316ddc..d13e14d 100644 --- a/table.go +++ b/table.go @@ -19,8 +19,8 @@ import ( // Use a pool to reduce GC pressure when handling many tables var tablePool = sync.Pool{ - New: func() interface{} { - return make(map[string]interface{}) + New: func() any { + return make(map[string]any) }, } @@ -30,8 +30,8 @@ func (s *State) GetTableLength(index int) int { } // getTableFromPool gets a map from the pool and ensures it's empty -func getTableFromPool() map[string]interface{} { - table := tablePool.Get().(map[string]interface{}) +func getTableFromPool() map[string]any { + table := tablePool.Get().(map[string]any) // Clear any existing entries for k := range table { delete(table, k) @@ -40,12 +40,12 @@ func getTableFromPool() map[string]interface{} { } // putTableToPool returns a map to the pool -func putTableToPool(table map[string]interface{}) { +func putTableToPool(table map[string]any) { tablePool.Put(table) } // PushTable pushes a Go map onto the Lua stack as a table -func (s *State) PushTable(table map[string]interface{}) error { +func (s *State) PushTable(table map[string]any) error { // Create table with appropriate capacity hints s.CreateTable(0, len(table)) @@ -72,14 +72,14 @@ func (s *State) PushTable(table map[string]interface{}) error { } // isPooledTable detects if a table came from our pool -func isPooledTable(table map[string]interface{}) bool { +func isPooledTable(table map[string]any) bool { // Check for our special marker - used for array tables in the pool _, hasEmptyKey := table[""] return len(table) == 1 && hasEmptyKey } // ToTable converts a Lua table at the given index to a Go map -func (s *State) ToTable(index int) (map[string]interface{}, error) { +func (s *State) ToTable(index int) (map[string]any, error) { absIdx := s.absIndex(index) if !s.IsTable(absIdx) { return nil, fmt.Errorf("value at index %d is not a table", index) @@ -145,7 +145,7 @@ func (s *State) ToTable(index int) (map[string]interface{}, error) { } // Handle nested array tables - if m, ok := value.(map[string]interface{}); ok { + if m, ok := value.(map[string]any); ok { if arr, ok := m[""]; ok { value = arr } diff --git a/wrapper.go b/wrapper.go index d37a12d..9a4949a 100644 --- a/wrapper.go +++ b/wrapper.go @@ -230,7 +230,7 @@ func (s *State) Next(index int) bool { } // PushValue pushes a Go value onto the stack with proper type conversion -func (s *State) PushValue(v interface{}) error { +func (s *State) PushValue(v any) error { switch v := v.(type) { case nil: s.PushNil() @@ -242,7 +242,7 @@ func (s *State) PushValue(v interface{}) error { s.PushNumber(v) case string: s.PushString(v) - case map[string]interface{}: + case map[string]any: // Special case: handle array stored in map if arr, ok := v[""].([]float64); ok { s.CreateTable(len(arr), 0) @@ -261,7 +261,7 @@ func (s *State) PushValue(v interface{}) error { s.PushNumber(elem) s.SetTable(-3) } - case []interface{}: + case []any: s.CreateTable(len(v), 0) for i, elem := range v { s.PushNumber(float64(i + 1)) @@ -277,7 +277,7 @@ func (s *State) PushValue(v interface{}) error { } // ToValue converts a Lua value at the given index to a Go value -func (s *State) ToValue(index int) (interface{}, error) { +func (s *State) ToValue(index int) (any, error) { luaType := s.GetType(index) switch luaType { case TypeNil: @@ -385,7 +385,7 @@ func (s *State) Execute(code string) (int, error) { } // ExecuteWithResult executes a Lua string and returns the first result -func (s *State) ExecuteWithResult(code string) (interface{}, error) { +func (s *State) ExecuteWithResult(code string) (any, error) { top := s.GetTop() defer s.SetTop(top) // Restore stack when done