예제 #1
0
파일: glua.go 프로젝트: gooops/glua
// 调用lua中的函数,但是只能返回bool, float, string,以及其他go特殊类型,int型被转换为float返回。
func (L *State) Call(fname string, args ...interface{}) (out []interface{}, ok bool) {
	fn := C.CString(fname)
	defer C.free(unsafe.Pointer(fn))

	top := int(C.lua_gettop(L.s))

	if C.int(1) != C.FindFuncs(L.s, fn) {
		ok = false
		out = append(out, errors.New(fmt.Sprintf("not find the function(%s).\n", fname)))
		return
	}

	num := len(args)
	for _, arg := range args {
		argt := rf.TypeOf(arg)
		argv := rf.ValueOf(arg)
		L.pushValueByType(argt.Kind(), &argv)
	}

	C.lua_call(L.s, C.int(num), C.LUA_MULTRET)

	for i := top; i < int(C.lua_gettop(L.s)); i++ {
		ret := L.getValueByLuaType(i)
		if ret.IsValid() {
			out = append(out, ret.Interface())
		} else {
			out = append(out, nil)
		}
	}
	C.lua_settop(L.s, C.int(top))
	ok = true
	return
}
예제 #2
0
파일: State.go 프로젝트: rdlaitila/leaf
// Calls a function.
//
// To call a function you must use the following protocol: first,
// the function to be called is pushed onto the stack; then, the
// arguments to the function are pushed in direct order; that is, the
// first argument is pushed first. Finally you call Call; nargs is the
// number of arguments that you pushed onto the stack. All arguments
// and the function value are popped from the stack when the function
// is called. The function results are pushed onto the stack when the
// function returns. The number of results is adjusted to nresults, unless
// nresults is luajit.Multret. In this case, all results from the function
// are pushed. Lua takes care that the returned values fit into the stack
// space. The function results are pushed onto the stack in direct order
// (the first result is pushed first), so that after the call the last
// result is on the top of the stack.
//
// Any error inside the called function is propagated upwards (with
// a longjmp).
func (this *State) Call(nargs, nresults int) {
	C.lua_call(this.luastate, C.int(nargs), C.int(nresults))
}
예제 #3
0
파일: lua.go 프로젝트: matthewtidd/golua
func (L *State) Call(nargs int, nresults int) {
	C.lua_call(L.s, C.int(nargs), C.int(nresults))
}
예제 #4
0
파일: state.go 프로젝트: halturin/luajit
// Calls a function.
//
// To call a function you must use the following protocol: first,
// the function to be called is pushed onto the stack; then, the
// arguments to the function are pushed in direct order; that is, the
// first argument is pushed first. Finally you call Call; nargs is the
// number of arguments that you pushed onto the stack. All arguments
// and the function value are popped from the stack when the function
// is called. The function results are pushed onto the stack when the
// function returns. The number of results is adjusted to nresults, unless
// nresults is luajit.Multret. In this case, all results from the function
// are pushed. Lua takes care that the returned values fit into the stack
// space. The function results are pushed onto the stack in direct order
// (the first result is pushed first), so that after the call the last
// result is on the top of the stack.
//
// Any error inside the called function is propagated upwards (with
// a longjmp).
func (s *State) Call(nargs, nresults int) {
	C.lua_call(s.l, C.int(nargs), C.int(nresults))
}
예제 #5
0
파일: lua.go 프로젝트: reusee/lua
func (l *Lua) getStackTraceback() string {
	C.lua_getfield(l.State, C.LUA_GLOBALSINDEX, cstr("debug"))
	C.lua_getfield(l.State, -1, cstr("traceback"))
	C.lua_call(l.State, 0, 1)
	return C.GoString(C.lua_tolstring(l.State, -1, nil))
}