Example #1
0
// Returns information about a specific function or function invocation.
//
// To get information about a function invocation, the parameter ar must be
// a valid activation record that was filled by a previous call to Getstack
// or given as argument to a hook.
//
// To get information about a function you push it onto the stack and start
// the what string with the character '>'. (In that case, Getinfo pops the
// function in the top of the stack.) For instance, to know in which line
// a function f was defined, you can write the following code:
//
// 	d := luajit.Newdebug(s)
// 	s.Getfield(luajit.Globalsindex, "f")  // get global 'f'
// 	d.Getinfo(">S")
// 	fmt.Printf("%d\n", d.Linedefined);
//
// Each character in the string what selects some fields of the structure
// ar to be filled or a value to be pushed on the stack:
//
// 	'n'	fills in the field Name and Namewhat
// 	'S'	fills in the fields Source, Shortsrc, Linedefined,
// 		Lastlinedefined, and What
// 	'l'	fills in the field Currentline
// 	'u'	fills in the field Nups
// 	'f'	pushes onto the stack the function that is running at the
// 		given level
// 	'L'	pushes onto the stack a table whose indices are the numbers of
// 		the lines that are valid on the function. (A valid line is a line
// 		with some associated code, that is, a line where you can put a break
// 		point. Invalid lines include empty lines and comments.)
//
func (d *Debug) Getinfo(what string) error {
	cs := C.CString(what)
	defer C.free(unsafe.Pointer(cs))
	if int(C.lua_getinfo(d.l, cs, d.d)) == 0 {
		return errors.New("The significant owl hoots in the night.")
	}
	d.update()
	return nil
}
Example #2
0
File: lua.go Project: szll/golua
// Returns the current stack trace
func (L *State) StackTrace() []LuaStackEntry {
	r := []LuaStackEntry{}
	var d C.lua_Debug
	Sln := C.CString("Sln")
	defer C.free(unsafe.Pointer(Sln))

	for depth := 0; C.lua_getstack(L.s, C.int(depth), &d) > 0; depth++ {
		C.lua_getinfo(L.s, Sln, &d)
		ssb := make([]byte, C.LUA_IDSIZE)
		for i := 0; i < C.LUA_IDSIZE; i++ {
			ssb[i] = byte(d.short_src[i])
			if ssb[i] == 0 {
				ssb = ssb[:i]
				break
			}
		}
		ss := string(ssb)

		r = append(r, LuaStackEntry{C.GoString(d.name), C.GoString(d.source), ss, int(d.currentline)})
	}

	return r
}