本文整理匯總了Golang中C.lua_tolstring函數的典型用法代碼示例。如果您正苦於以下問題:Golang lua_tolstring函數的具體用法?Golang lua_tolstring怎麽用?Golang lua_tolstring使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了lua_tolstring函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Peval
// Peval evaluates a piece of lua code. no panic when error occur.
func (l *Lua) Peval(code string, envs ...interface{}) (returns []interface{}, err error) {
defer C.lua_settop(l.State, 0)
C.push_errfunc(l.State)
curTop := C.lua_gettop(l.State)
// parse
cCode := C.CString(code)
defer C.free(unsafe.Pointer(cCode))
if ret := C.luaL_loadstring(l.State, cCode); ret != 0 { // load error
return nil, fmt.Errorf("LOAD ERROR: %s", C.GoString(C.lua_tolstring(l.State, -1, nil)))
}
// env
if len(envs) > 0 {
if len(envs)%2 != 0 {
return nil, fmt.Errorf("number of arguments not match")
}
C.lua_createtable(l.State, 0, 0)
for i := 0; i < len(envs); i += 2 {
name, ok := envs[i].(string)
if !ok {
return nil, fmt.Errorf("name must be string, not %v", envs[i])
}
C.lua_pushstring(l.State, cstr(name))
err := l.pushGoValue(envs[i+1], name)
if err != nil {
return nil, err
}
C.lua_rawset(l.State, -3)
}
// set env
C.set_eval_env(l.State)
}
// call
l.err = nil
if ret := C.lua_pcall(l.State, 0, C.LUA_MULTRET, -2); ret != 0 {
// error occured
return nil, fmt.Errorf("CALL ERROR: %s", C.GoString(C.lua_tolstring(l.State, -1, nil)))
} else if l.err != nil { // error raise by invokeGoFunc
return nil, l.err
} else {
// return values
nReturn := C.lua_gettop(l.State) - curTop
returns = make([]interface{}, int(nReturn))
for i := C.int(0); i < nReturn; i++ {
value, err := l.toGoValue(-1-i, interfaceType)
if err != nil {
return nil, err
}
if value != nil {
returns[int(nReturn-1-i)] = value.Interface()
} else {
returns[int(nReturn-1-i)] = nil
}
}
}
return
}
示例2: Tostring
func Tostring(s *State, index int) string {
var l C.size_t
cs := C.lua_tolstring((*C.lua_State)(s),
C.int(index), &l)
// Remake this into a string!
return C.GoStringN(cs, C.int(l))
}
示例3: Tostring
// Converts the Lua value at the given valid index to a Go
// string. The Lua value must be a string or a number; otherwise,
// the function returns an empty string. If the value is a number, then
// Tostring also changes the actual value in the stack to a string. (This
// change confuses Next when Tostring is applied to keys during a table
// traversal). The string always has a zero ('\0') after its last
// character (as in C), but can contain other zeros in its body.
func (this *State) Tostring(index int) string {
str := C.lua_tolstring(this.luastate, C.int(index), nil)
if str == nil {
return ""
}
return C.GoString(str)
}
示例4: getStructField
func (state State) getStructField(structPtr reflect.Value, lkey C.int) (ret int, err error) {
L := state.L
vm := state.VM
structValue := structPtr.Elem()
t := structValue.Type()
info := vm.findStruct(t)
if info == nil {
return -1, fmt.Errorf("can not index a solid struct")
}
ltype := int(C.lua_type(L, lkey))
if ltype != C.LUA_TSTRING {
return -1, fmt.Errorf("field key of struct must be a string")
}
//
//key := stringFromLua(L, lkey)
//fld, ok := info.fields[key]
//
// <hack> using string pstr cache
pstr := C.lua_tolstring(L, lkey, nil)
// </hack>
fld, ok := info.cache[pstr]
if !ok {
key := stringFromLua(L, lkey)
return -1, fmt.Errorf("not such field `%v'", key)
}
value := getStructFieldValue(structValue, fld)
state.goToLuaValue(value)
return 1, nil
}
示例5: init
// Initializes the Lua context and compiles the source code.
func (e *ExecutionEngine) init() error {
if e.state != nil {
return nil
}
// Initialize the state and open the libraries.
e.state = C.luaL_newstate()
if e.state == nil {
return errors.New("Unable to initialize Lua context.")
}
C.luaL_openlibs(e.state)
// Generate the header file.
err := e.generateHeader()
if err != nil {
e.Destroy()
return err
}
// Compile the script.
e.fullSource = fmt.Sprintf("%v\n%v", e.header, e.source)
source := C.CString(e.fullSource)
defer C.free(unsafe.Pointer(source))
ret := C.luaL_loadstring(e.state, source)
if ret != 0 {
defer e.Destroy()
errstring := C.GoString(C.lua_tolstring(e.state, -1, nil))
return fmt.Errorf("skyd.ExecutionEngine: Syntax Error: %v", errstring)
}
// Run script once to initialize.
ret = C.lua_pcall(e.state, 0, 0, 0)
if ret != 0 {
defer e.Destroy()
errstring := C.GoString(C.lua_tolstring(e.state, -1, nil))
return fmt.Errorf("skyd.ExecutionEngine: Init Error: %v", errstring)
}
// Setup cursor.
err = e.initCursor()
if err != nil {
e.Destroy()
return err
}
return nil
}
示例6: makeFieldsIndexCache
func (sinfo *structInfo) makeFieldsIndexCache(vm *VM) {
L := vm.globalL
sinfo.cache = make(map[*C.char]*structField, len(sinfo.fields))
sinfo.lref = make(map[string]int, len(sinfo.fields))
for key, field := range sinfo.fields {
pushStringToLua(L, key)
pstr := C.lua_tolstring(L, -1, nil)
sinfo.cache[pstr] = field
sinfo.lref[key] = int(C.luaL_ref(L, C.LUA_REGISTRYINDEX))
}
}
示例7: RunString
func (self *Lua) RunString(code string) {
defer func() {
if r := recover(); r != nil {
if self.PrintTraceback { //NOCOVER
print("============ start lua traceback ============\n")
self.RunString(`print(debug.traceback())`)
print("============ end lua traceback ==============\n")
}
panic(r)
}
}()
cCode := cstr(code)
C.setup_message_handler(self.State)
if ret := C.luaL_loadstring(self.State, cCode); ret != C.int(0) {
self.Panic("%s", C.GoString(C.lua_tolstring(self.State, -1, nil)))
}
ret := C.lua_pcallk(self.State, 0, 0, C.lua_gettop(self.State)-C.int(1), 0, nil)
if ret != C.int(0) {
self.Panic("%s", C.GoString(C.lua_tolstring(self.State, -1, nil)))
}
C.lua_settop(self.State, 0)
}
示例8: packLuaString
func packLuaString(out io.Writer, state State, object int) (n int, err error) {
var cslen C.size_t
cs := C.lua_tolstring(state.L, C.int(object), &cslen)
// <HACK> pretend a []byte slice to avoid intermediate buffer
slhead := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(cs)),
Len: int(cslen), Cap: int(cslen),
}
pslice := (*[]byte)(unsafe.Pointer(&slhead))
// </HACK>
return P.PackRaw(out, *pslice)
}
示例9: Aggregate
// Executes an aggregation over the iterator.
func (e *ExecutionEngine) Aggregate() (interface{}, error) {
functionName := C.CString("sky_aggregate")
defer C.free(unsafe.Pointer(functionName))
C.lua_getfield(e.state, -10002, functionName)
C.lua_pushlightuserdata(e.state, unsafe.Pointer(e.cursor))
rc := C.lua_pcall(e.state, 1, 1, 0)
if rc != 0 {
luaErrString := C.GoString(C.lua_tolstring(e.state, -1, nil))
fmt.Println(e.FullAnnotatedSource())
return nil, fmt.Errorf("skyd.ExecutionEngine: Unable to aggregate: %s", luaErrString)
}
return e.decodeResult()
}
示例10: luaUnpackFromString
func luaUnpackFromString(state State) int {
// arg 1 is func udata itself
var cslen C.size_t
cs := C.lua_tolstring(state.L, C.int(2), &cslen)
// <HACK> pretend a []byte slice to avoid intermediate buffer
slhead := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(cs)),
Len: int(cslen), Cap: int(cslen),
}
pslice := (*[]byte)(unsafe.Pointer(&slhead))
// </HACK>
reader := bytes.NewReader(*pslice)
return UnpackToLua(reader, state)
}
示例11: Pcall
// Pcall calls a lua function. no panic
func (l *Lua) Pcall(fullname string, args ...interface{}) (returns []interface{}, err error) {
C.push_errfunc(l.State)
curTop := C.lua_gettop(l.State)
// get function
path := strings.Split(fullname, ".")
for i, name := range path {
if i == 0 {
C.lua_getfield(l.State, C.LUA_GLOBALSINDEX, cstr(name))
} else {
if C.lua_type(l.State, -1) != C.LUA_TTABLE {
return nil, fmt.Errorf("%s is not a function", fullname)
}
C.lua_pushstring(l.State, cstr(name))
C.lua_gettable(l.State, -2)
C.lua_remove(l.State, -2) // remove table
}
}
if C.lua_type(l.State, -1) != C.LUA_TFUNCTION {
return nil, fmt.Errorf("%s is not a function", fullname)
}
// args
for _, arg := range args {
l.pushGoValue(arg, "")
}
// call
l.err = nil
if ret := C.lua_pcall(l.State, C.int(len(args)), C.LUA_MULTRET, C.int(-(len(args))-2)); ret != 0 {
// error occured
return nil, fmt.Errorf("CALL ERROR: %s", C.GoString(C.lua_tolstring(l.State, -1, nil)))
} else if l.err != nil { // error raise by invokeGoFunc
return nil, l.err
} else {
// return values
nReturn := C.lua_gettop(l.State) - curTop
returns = make([]interface{}, int(nReturn))
for i := C.int(0); i < nReturn; i++ {
value, err := l.toGoValue(-1-i, interfaceType)
if err != nil {
return nil, err
}
returns[int(nReturn-1-i)] = value.Interface()
}
}
return
}
示例12: decodeResult
// Decodes the result from a function into a Go object.
func (e *ExecutionEngine) decodeResult() (interface{}, error) {
// Encode Lua object into msgpack.
rc := C.mp_pack(e.state)
if rc != 1 {
return nil, errors.New("skyd.ExecutionEngine: Unable to msgpack decode Lua result")
}
sz := C.size_t(0)
ptr := C.lua_tolstring(e.state, -1, (*C.size_t)(&sz))
str := C.GoStringN(ptr, (C.int)(sz))
C.lua_settop(e.state, -(1)-1) // lua_pop()
// Decode msgpack into a Go object.
var ret interface{}
decoder := msgpack.NewDecoder(bytes.NewBufferString(str), nil)
err := decoder.Decode(&ret)
if err != nil {
return nil, err
}
return ret, nil
}
示例13: Merge
// Executes an merge over the iterator.
func (e *ExecutionEngine) Merge(results interface{}, data interface{}) (interface{}, error) {
functionName := C.CString("sky_merge")
defer C.free(unsafe.Pointer(functionName))
C.lua_getfield(e.state, -10002, functionName)
err := e.encodeArgument(results)
if err != nil {
return results, err
}
err = e.encodeArgument(data)
if err != nil {
return results, err
}
rc := C.lua_pcall(e.state, 2, 1, 0)
if rc != 0 {
luaErrString := C.GoString(C.lua_tolstring(e.state, -1, nil))
fmt.Println(e.FullAnnotatedSource())
return results, fmt.Errorf("skyd.ExecutionEngine: Unable to merge: %s", luaErrString)
}
return e.decodeResult()
}
示例14: initCursor
// Initializes the cursor used by the script.
func (e *ExecutionEngine) initCursor() error {
// Create the cursor.
minPropertyId, maxPropertyId := e.propertyFile.NextIdentifiers()
e.cursor = C.sky_cursor_new((C.int32_t)(minPropertyId), (C.int32_t)(maxPropertyId))
e.cursor.context = unsafe.Pointer(e)
C.executionEngine_setNextObjectFunc(unsafe.Pointer(e.cursor))
// Initialize the cursor from within Lua.
functionName := C.CString("sky_init_cursor")
defer C.free(unsafe.Pointer(functionName))
C.lua_getfield(e.state, -10002, functionName)
C.lua_pushlightuserdata(e.state, unsafe.Pointer(e.cursor))
//fmt.Printf("%s\n\n", e.FullAnnotatedSource())
rc := C.lua_pcall(e.state, 1, 0, 0)
if rc != 0 {
luaErrString := C.GoString(C.lua_tolstring(e.state, -1, nil))
return fmt.Errorf("Unable to init cursor: %s", luaErrString)
}
return nil
}
示例15: CallFunction
func (self *Lua) CallFunction(name string, args ...interface{}) {
defer func() {
if r := recover(); r != nil {
if self.PrintTraceback { //NOCOVER
print("============ start lua traceback ============\n")
self.RunString(`print(debug.traceback())`)
print("============ end lua traceback ==============\n")
}
panic(r)
}
}()
cName := cstr(name)
C.setup_message_handler(self.State)
C.lua_getglobal(self.State, cName)
for _, arg := range args {
self.PushGoValue(reflect.ValueOf(arg))
}
ret := C.lua_pcallk(self.State, C.int(len(args)), 0, C.lua_gettop(self.State)-C.int(len(args)+2), 0, nil)
if ret != C.int(0) {
self.Panic("%s", C.GoString(C.lua_tolstring(self.State, -1, nil)))
}
}