本文整理匯總了Golang中github.com/aarzilli/golua/lua.State.ToString方法的典型用法代碼示例。如果您正苦於以下問題:Golang State.ToString方法的具體用法?Golang State.ToString怎麽用?Golang State.ToString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/aarzilli/golua/lua.State
的用法示例。
在下文中一共展示了State.ToString方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: CopyTableToStruct
// Copy matching Lua table entries to a struct, given the struct type
// and the index on the Lua stack.
func CopyTableToStruct(L *lua.State, t reflect.Type, idx int) interface{} {
was_ptr := t.Kind() == reflect.Ptr
if was_ptr {
t = t.Elem()
}
s := reflect.New(t) // T -> *T
ref := s.Elem()
L.PushNil()
if idx < 0 {
idx--
}
for L.Next(idx) != 0 {
key := L.ToString(-2)
f := ref.FieldByName(strings.Title(key))
if f.IsValid() {
val := luaToGoValue(L, f.Type(), -1)
f.Set(val)
}
L.Pop(1)
}
if was_ptr {
return s.Interface()
}
return s.Elem().Interface()
}
示例2: popMap
func popMap(L *lua.State) map[string]interface{} {
m := make(map[string]interface{})
L.PushNil()
for L.Next(-2) != 0 {
if L.IsString(-2) {
var v interface{}
if L.IsBoolean(-1) {
v = L.ToBoolean(-1)
} else if L.IsNumber(-1) {
var t float64
t = L.ToNumber(-1)
if t == float64(int64(t)) {
v = int(t)
} else {
v = t
}
} else if L.IsString(-1) {
v = L.ToString(-1)
} else if L.IsNil(-1) {
v = nil
} else if L.IsTable(-1) {
v = popMap(L)
}
m[L.ToString(-2)] = v
}
L.Pop(1)
}
return m
}
示例3: apiProcessNew
func (p *Plugin) apiProcessNew(l *lua.State) int {
callback := luar.NewLuaObject(l, 1)
command := l.ToString(2)
args := make([]string, l.GetTop()-2)
for i := 3; i <= l.GetTop(); i++ {
args[i-3] = l.ToString(i)
}
proc := &process{
cmd: exec.Command(command, args...),
}
go func() {
var str string
bytes, err := proc.cmd.Output()
if err == nil {
if bytes != nil {
str = string(bytes)
}
p.callValue(callback, proc.cmd.ProcessState.Success(), str)
} else {
p.callValue(callback, false, "")
}
callback.Close()
}()
obj := luar.NewLuaObjectFromValue(l, proc)
obj.Push()
obj.Close()
return 1
}
示例4: outputNumbersToStrings
func outputNumbersToStrings(L *lua.State) {
L.GetGlobal("output")
if !L.IsTable(-1) {
L.NewTable()
L.SetGlobal("output")
}
L.GetField(-1, "tags")
if L.IsTable(-1) {
// First key.
L.PushNil()
for L.Next(-2) != 0 {
// Use 'key' at index -2 and 'value' at index -1.
if L.IsString(-2) && L.IsString(-1) {
// Convert numbers to strings.
L.ToString(-1)
L.SetField(-3, L.ToString(-2))
} else {
// Remove 'value' and keep 'key' for next iteration.
L.Pop(1)
}
}
}
L.Pop(1)
L.Pop(1)
}
示例5: ProxyMethod
// ProxyMethod pushes the proxy method on the stack.
//
// Argument: proxy
//
// Returns: method (function)
func ProxyMethod(L *lua.State) int {
if !isValueProxy(L, 1) {
L.PushNil()
return 1
}
v, _ := valueOfProxy(L, 1)
name := L.ToString(2)
pushGoMethod(L, name, v)
return 1
}
示例6: LuaTableGetString
func LuaTableGetString(L *lua.State, key string) string {
L.PushString(key)
L.GetTable(-2)
r := ""
if !L.IsNil(-1) {
r = L.ToString(-1)
}
L.Pop(1)
return r
}
示例7: GoLuaFunc
// GoLuaFunc converts an arbitrary Go function into a Lua-compatible GoFunction.
// There are special optimized cases for functions that go from strings to strings,
// and doubles to doubles, but otherwise Go
// reflection is used to provide a generic wrapper function
func GoLuaFunc(L *lua.State, fun interface{}) lua.LuaGoFunction {
switch f := fun.(type) {
case func(*lua.State) int:
return f
case func(string) string:
return func(L *lua.State) int {
L.PushString(f(L.ToString(1)))
return 1
}
case func(float64) float64:
return func(L *lua.State) int {
L.PushNumber(f(L.ToNumber(1)))
return 1
}
default:
}
var funv reflect.Value
switch ff := fun.(type) {
case reflect.Value:
funv = ff
default:
funv = valueOf(fun)
}
funt := funv.Type()
targs, tout := functionArgRetTypes(funt)
return func(L *lua.State) int {
var lastT reflect.Type
orig_targs := targs
isVariadic := funt.IsVariadic()
if isVariadic {
n := len(targs)
lastT = targs[n-1].Elem()
targs = targs[0 : n-1]
}
args := make([]reflect.Value, len(targs))
for i, t := range targs {
val := LuaToGo(L, t, i+1)
args[i] = valueOfNil(val)
//println(i,args[i].String())
}
if isVariadic {
n := L.GetTop()
for i := len(targs) + 1; i <= n; i++ {
ival := LuaToGo(L, lastT, i)
args = append(args, valueOfNil(ival))
}
targs = orig_targs
}
resv := callGo(L, funv, args)
for i, val := range resv {
GoToLua(L, tout[i], val, false)
}
return len(resv)
}
}
示例8: struct__newindex
func struct__newindex(L *lua.State) int {
st, t := valueOfProxy(L, 1)
name := L.ToString(2)
if t.Kind() == reflect.Ptr {
st = st.Elem()
}
field := st.FieldByName(name)
val := LuaToGo(L, field.Type(), 3)
field.Set(valueOf(val))
return 0
}
示例9: LuaIntRmColumn
func LuaIntRmColumn(L *lua.State) int {
luaAssertArgnum(L, 1, "rmcolumn()")
name := L.ToString(1)
entry := GetEntryFromLua(L, CURSOR, "rmcolumn()")
entry.RemoveColumn(name)
tl := GetTasklistFromLua(L)
if !tl.luaFlags.cursorCloned {
tl.luaFlags.cursorEdited = true
}
return 0
}
示例10: copyTableToStruct
func copyTableToStruct(L *lua.State, t reflect.Type, idx int, visited map[uintptr]interface{}) interface{} {
if t == nil {
RaiseError(L, "type argument must be non-nill")
}
wasPtr := t.Kind() == reflect.Ptr
if wasPtr {
t = t.Elem()
}
s := reflect.New(t) // T -> *T
ref := s.Elem()
// See copyTableToSlice.
ptr := L.ToPointer(idx)
if !luaIsEmpty(L, idx) {
if wasPtr {
visited[ptr] = s.Interface()
} else {
visited[ptr] = s.Elem().Interface()
}
}
// Associate Lua keys with Go fields: tags have priority over matching field
// name.
fields := map[string]string{}
st := ref.Type()
for i := 0; i < ref.NumField(); i++ {
field := st.Field(i)
tag := field.Tag.Get("lua")
if tag != "" {
fields[tag] = field.Name
continue
}
fields[field.Name] = field.Name
}
L.PushNil()
if idx < 0 {
idx--
}
for L.Next(idx) != 0 {
key := L.ToString(-2)
f := ref.FieldByName(fields[key])
if f.CanSet() && f.IsValid() {
val := reflect.ValueOf(luaToGo(L, f.Type(), -1, visited))
f.Set(val)
}
L.Pop(1)
}
if wasPtr {
return s.Interface()
}
return s.Elem().Interface()
}
示例11: sandboxCompile
func sandboxCompile(L *lua.State, registryIndex string, name, code string) {
L.PushString(registryIndex)
L.GetTable(lua.LUA_REGISTRYINDEX)
L.PushString(name)
err := L.LoadString(code)
if err != 0 {
log.Fatalf("%s: %s", name, L.ToString(-1))
L.Pop(2)
} else {
L.SetTable(-3)
}
}
示例12: LuaIntPriorityQuery
func LuaIntPriorityQuery(L *lua.State) int {
luaAssertArgnum(L, 1, "priorityq()")
priority := L.ToString(1)
L.CheckStack(1)
tl := GetTasklistFromLua(L)
tl.luaState.PushGoStruct(&SimpleExpr{":priority", "=", priority, nil, ParsePriority(priority), ""})
return 1
}
示例13: LuaIntStringFunction
func LuaIntStringFunction(L *lua.State, name string, n int, fn func(tl *Tasklist, argv []string) int) int {
luaAssertArgnum(L, n, name)
argv := make([]string, 0)
for i := 1; i <= n; i++ {
argv = append(argv, L.ToString(i))
}
L.Pop(n)
L.CheckStack(1)
tl := GetTasklistFromLua(L)
return fn(tl, argv)
}
示例14: struct__index
func struct__index(L *lua.State) int {
st, t := valueOfProxy(L, 1)
name := L.ToString(2)
est := st
if t.Kind() == reflect.Ptr {
est = st.Elem()
}
ret := est.FieldByName(name)
if !ret.IsValid() { // no such field, try for method?
callGoMethod(L, name, st)
} else {
GoToLua(L, ret.Type(), ret)
}
return 1
}
示例15: struct__newindex
func struct__newindex(L *lua.State) int {
st, t := valueOfProxy(L, 1)
name := L.ToString(2)
if t.Kind() == reflect.Ptr {
st = st.Elem()
}
field := st.FieldByName(name)
assertValid(L, field, st, name, "field")
val := luaToGoValue(L, field.Type(), 3)
if isPointerToPrimitive(field) {
field.Elem().Set(val)
} else {
field.Set(val)
}
return 0
}