interface{} to any

This commit is contained in:
Sky Johnson 2025-03-07 07:25:34 -06:00
parent 143b9333c6
commit 98ca857d73
5 changed files with 22 additions and 22 deletions

12
DOCS.md
View File

@ -110,7 +110,7 @@ Converts the value to a boolean.
bool := L.ToBoolean(-1) bool := L.ToBoolean(-1)
``` ```
### ToValue(index int) (interface{}, error) ### ToValue(index int) (any, error)
Converts any Lua value to its Go equivalent. Converts any Lua value to its Go equivalent.
```go ```go
val, err := L.ToValue(-1) 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. Converts a Lua table to a Go map.
```go ```go
table, err := L.ToTable(-1) table, err := L.ToTable(-1)
@ -148,16 +148,16 @@ L.PushBoolean(true)
L.PushNil() L.PushNil()
``` ```
### PushValue(v interface{}) error ### PushValue(v any) error
Pushes any Go value onto the stack. Pushes any Go value onto the stack.
```go ```go
err := L.PushValue(myValue) err := L.PushValue(myValue)
``` ```
### PushTable(table map[string]interface{}) error ### PushTable(table map[string]any) error
Pushes a Go map as a Lua table. Pushes a Go map as a Lua table.
```go ```go
data := map[string]interface{}{ data := map[string]any{
"key": "value", "key": "value",
"numbers": []float64{1, 2, 3}, "numbers": []float64{1, 2, 3},
} }
@ -310,7 +310,7 @@ nresults, err := L.Execute("return 1, 2, 3")
// nresults would be 3 // nresults would be 3
``` ```
### ExecuteWithResult(code string) (interface{}, error) ### ExecuteWithResult(code string) (any, error)
Executes a Lua string and returns the first result. Executes a Lua string and returns the first result.
```go ```go
result, err := L.ExecuteWithResult("return 'hello'") result, err := L.ExecuteWithResult("return 'hello'")

View File

@ -101,7 +101,7 @@ Lua tables are pretty powerful - they're like a mix of Go's maps and slices. We
```go ```go
// Go → Lua // Go → Lua
stuff := map[string]interface{}{ stuff := map[string]any{
"name": "Arthur Dent", "name": "Arthur Dent",
"age": 30, "age": 30,
"items": []float64{1, 2, 3}, "items": []float64{1, 2, 3},

View File

@ -35,7 +35,7 @@ func main() {
}) })
// Add some values to the Lua environment // Add some values to the Lua environment
L.PushValue(map[string]interface{}{ L.PushValue(map[string]any{
"appName": "LuaJIT Example", "appName": "LuaJIT Example",
"version": 1.0, "version": 1.0,
"features": []float64{1, 2, 3}, "features": []float64{1, 2, 3},

View File

@ -19,8 +19,8 @@ import (
// Use a pool to reduce GC pressure when handling many tables // Use a pool to reduce GC pressure when handling many tables
var tablePool = sync.Pool{ var tablePool = sync.Pool{
New: func() interface{} { New: func() any {
return make(map[string]interface{}) 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 // getTableFromPool gets a map from the pool and ensures it's empty
func getTableFromPool() map[string]interface{} { func getTableFromPool() map[string]any {
table := tablePool.Get().(map[string]interface{}) table := tablePool.Get().(map[string]any)
// Clear any existing entries // Clear any existing entries
for k := range table { for k := range table {
delete(table, k) delete(table, k)
@ -40,12 +40,12 @@ func getTableFromPool() map[string]interface{} {
} }
// putTableToPool returns a map to the pool // putTableToPool returns a map to the pool
func putTableToPool(table map[string]interface{}) { func putTableToPool(table map[string]any) {
tablePool.Put(table) tablePool.Put(table)
} }
// PushTable pushes a Go map onto the Lua stack as a 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 // Create table with appropriate capacity hints
s.CreateTable(0, len(table)) 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 // 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 // Check for our special marker - used for array tables in the pool
_, hasEmptyKey := table[""] _, hasEmptyKey := table[""]
return len(table) == 1 && hasEmptyKey return len(table) == 1 && hasEmptyKey
} }
// ToTable converts a Lua table at the given index to a Go map // 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) absIdx := s.absIndex(index)
if !s.IsTable(absIdx) { if !s.IsTable(absIdx) {
return nil, fmt.Errorf("value at index %d is not a table", index) 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 // 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 { if arr, ok := m[""]; ok {
value = arr value = arr
} }

View File

@ -230,7 +230,7 @@ func (s *State) Next(index int) bool {
} }
// PushValue pushes a Go value onto the stack with proper type conversion // 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) { switch v := v.(type) {
case nil: case nil:
s.PushNil() s.PushNil()
@ -242,7 +242,7 @@ func (s *State) PushValue(v interface{}) error {
s.PushNumber(v) s.PushNumber(v)
case string: case string:
s.PushString(v) s.PushString(v)
case map[string]interface{}: case map[string]any:
// Special case: handle array stored in map // Special case: handle array stored in map
if arr, ok := v[""].([]float64); ok { if arr, ok := v[""].([]float64); ok {
s.CreateTable(len(arr), 0) s.CreateTable(len(arr), 0)
@ -261,7 +261,7 @@ func (s *State) PushValue(v interface{}) error {
s.PushNumber(elem) s.PushNumber(elem)
s.SetTable(-3) s.SetTable(-3)
} }
case []interface{}: case []any:
s.CreateTable(len(v), 0) s.CreateTable(len(v), 0)
for i, elem := range v { for i, elem := range v {
s.PushNumber(float64(i + 1)) 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 // 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) luaType := s.GetType(index)
switch luaType { switch luaType {
case TypeNil: case TypeNil:
@ -385,7 +385,7 @@ func (s *State) Execute(code string) (int, error) {
} }
// ExecuteWithResult executes a Lua string and returns the first result // 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() top := s.GetTop()
defer s.SetTop(top) // Restore stack when done defer s.SetTop(top) // Restore stack when done