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)
```
### 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'")

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 → Lua
stuff := map[string]interface{}{
stuff := map[string]any{
"name": "Arthur Dent",
"age": 30,
"items": []float64{1, 2, 3},

View File

@ -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},

View File

@ -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
}

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
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