github.com/dfcfw/lua@v0.0.0-20230325031207-0cc7ffb7b8b9/luar/util.go (about)

     1  package luar
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"unicode"
     7  	"unicode/utf8"
     8  
     9  	"github.com/dfcfw/lua"
    10  )
    11  
    12  func check(L *lua.LState, idx int) (ref reflect.Value, mt *Metatable) {
    13  	ud := L.CheckUserData(idx)
    14  	ref = reflect.ValueOf(ud.Value)
    15  	mt = &Metatable{LTable: ud.Metatable.(*lua.LTable)}
    16  	return
    17  }
    18  
    19  func tostring(L *lua.LState) int {
    20  	ud := L.CheckUserData(1)
    21  	if stringer, ok := ud.Value.(fmt.Stringer); ok {
    22  		L.Push(lua.LString(stringer.String()))
    23  	} else {
    24  		L.Push(lua.LString(ud.String()))
    25  	}
    26  	return 1
    27  }
    28  
    29  func getUnexportedName(name string) string {
    30  	first, n := utf8.DecodeRuneInString(name)
    31  	if n == 0 {
    32  		return name
    33  	}
    34  	return string(unicode.ToLower(first)) + name[n:]
    35  }