Compare commits
No commits in common. "master" and "v0.5.0" have entirely different histories.
2
DOCS.md
2
DOCS.md
@ -109,7 +109,7 @@ if err != nil {
|
||||
```
|
||||
|
||||
### ToTable(index int) (any, error)
|
||||
Converts a Lua table to optimal Go type; arrays or `map[string]any`.
|
||||
Converts a Lua table to optimal Go type ([]int, []string, map[string]any, etc.).
|
||||
```go
|
||||
table, err := L.ToTable(-1)
|
||||
if err != nil {
|
||||
|
@ -116,7 +116,7 @@ L.PushValue(stuff) // Handles all Go types automatically
|
||||
|
||||
// Lua → Go with automatic type detection
|
||||
L.GetGlobal("some_table")
|
||||
result, err := L.ToTable(-1) // Returns optimal Go type (typed array, or map[string]any)
|
||||
result, err := L.ToTable(-1) // Returns optimal Go type ([]int, map[string]string, etc.)
|
||||
```
|
||||
|
||||
### Table Builder
|
||||
|
115
wrapper.go
115
wrapper.go
@ -71,6 +71,39 @@ static int sample_array_type(lua_State *L, int index, int count) {
|
||||
if (all_bools) return 4;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sample_map_type(lua_State *L, int index) {
|
||||
int all_string_vals = 1;
|
||||
int all_int_vals = 1;
|
||||
int all_int_keys = 1;
|
||||
int count = 0;
|
||||
|
||||
lua_pushnil(L);
|
||||
while (lua_next(L, index) && count < 5) {
|
||||
if (lua_type(L, -2) != LUA_TSTRING) {
|
||||
all_int_keys = 0;
|
||||
} else {
|
||||
const char *key = lua_tostring(L, -2);
|
||||
char *endptr;
|
||||
strtol(key, &endptr, 10);
|
||||
if (*endptr != '\0') all_int_keys = 0;
|
||||
}
|
||||
|
||||
int val_type = lua_type(L, -1);
|
||||
if (val_type != LUA_TSTRING) all_string_vals = 0;
|
||||
if (val_type != LUA_TNUMBER || !is_integer(L, -1)) all_int_vals = 0;
|
||||
|
||||
lua_pop(L, 1);
|
||||
count++;
|
||||
|
||||
if (!all_string_vals && !all_int_vals && !all_int_keys) break;
|
||||
}
|
||||
|
||||
if (all_int_keys) return 4;
|
||||
if (all_string_vals) return 1;
|
||||
if (all_int_vals) return 2;
|
||||
return 3;
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
@ -206,8 +239,6 @@ func (s *State) PushValue(v any) error {
|
||||
s.PushNumber(val)
|
||||
case string:
|
||||
s.PushString(val)
|
||||
case []byte:
|
||||
s.PushString(string(val))
|
||||
case []int:
|
||||
return s.pushIntSlice(val)
|
||||
case []string:
|
||||
@ -218,8 +249,6 @@ func (s *State) PushValue(v any) error {
|
||||
return s.pushFloatSlice(val)
|
||||
case []any:
|
||||
return s.pushAnySlice(val)
|
||||
case []map[string]any:
|
||||
return s.pushMapSlice(val)
|
||||
case map[string]string:
|
||||
return s.pushStringMap(val)
|
||||
case map[string]int:
|
||||
@ -377,8 +406,18 @@ func (s *State) ToTable(index int) (any, error) {
|
||||
}
|
||||
}
|
||||
|
||||
mapType := int(C.sample_map_type(s.L, C.int(absIdx)))
|
||||
switch mapType {
|
||||
case 1: // map[string]string
|
||||
return s.extractStringMap(absIdx)
|
||||
case 2: // map[string]int
|
||||
return s.extractIntMap(absIdx)
|
||||
case 4: // map[int]any
|
||||
return s.extractIntKeyMap(absIdx)
|
||||
default: // map[string]any
|
||||
return s.extractAnyMap(absIdx)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *State) extractIntArray(index, length int) []int {
|
||||
result := make([]int, length)
|
||||
@ -437,6 +476,62 @@ func (s *State) extractAnyArray(index, length int) []any {
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *State) extractStringMap(index int) (map[string]string, error) {
|
||||
result := make(map[string]string)
|
||||
s.PushNil()
|
||||
for s.Next(index) {
|
||||
if s.GetType(-2) == TypeString {
|
||||
key := s.ToString(-2)
|
||||
value := s.ToString(-1)
|
||||
result[key] = value
|
||||
}
|
||||
s.Pop(1)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *State) extractIntMap(index int) (map[string]int, error) {
|
||||
result := make(map[string]int)
|
||||
s.PushNil()
|
||||
for s.Next(index) {
|
||||
if s.GetType(-2) == TypeString {
|
||||
key := s.ToString(-2)
|
||||
value := int(s.ToNumber(-1))
|
||||
result[key] = value
|
||||
}
|
||||
s.Pop(1)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *State) extractIntKeyMap(index int) (map[int]any, error) {
|
||||
result := make(map[int]any)
|
||||
s.PushNil()
|
||||
for s.Next(index) {
|
||||
var key int
|
||||
switch s.GetType(-2) {
|
||||
case TypeString:
|
||||
if k, err := strconv.Atoi(s.ToString(-2)); err == nil {
|
||||
key = k
|
||||
} else {
|
||||
s.Pop(1)
|
||||
continue
|
||||
}
|
||||
case TypeNumber:
|
||||
key = int(s.ToNumber(-2))
|
||||
default:
|
||||
s.Pop(1)
|
||||
continue
|
||||
}
|
||||
|
||||
if value, err := s.ToValue(-1); err == nil {
|
||||
result[key] = value
|
||||
}
|
||||
s.Pop(1)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *State) extractAnyMap(index int) (map[string]any, error) {
|
||||
result := make(map[string]any)
|
||||
s.PushNil()
|
||||
@ -715,15 +810,3 @@ func (s *State) CallGlobal(name string, args ...any) ([]any, error) {
|
||||
s.SetTop(baseTop)
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (s *State) pushMapSlice(arr []map[string]any) error {
|
||||
s.CreateTable(len(arr), 0)
|
||||
for i, m := range arr {
|
||||
s.PushNumber(float64(i + 1))
|
||||
if err := s.PushValue(m); err != nil {
|
||||
return err
|
||||
}
|
||||
s.SetTable(-3)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user