如何在lua函数中获取函数的调用者?

Gre*_*reg 3 lua coronasdk

如何在lua函数中获取函数的调用者?

具体来说,我正在寻找(在命令输出中用于调试目的,例如print)在调用公共函数时记录的能力,并指示从何处调用它.

这可能只是例如调用它的位置的文件名

File 1 - Has commonly used function 

File 2 - Calls of the the file one functions
Run Code Online (Sandbox Code Playgroud)

PS Mud - 我这样做的时候实际上是一个零 - 这是正常的吗?在这种情况下无法获得更多信息:

被叫文件;

SceneBase = {}
  function SceneBase:new(options)
  end
return SceneBase
Run Code Online (Sandbox Code Playgroud)

调用文件:

require("views.scenes.scene_base")
local scene = SceneBase.new()
Run Code Online (Sandbox Code Playgroud)

Mud*_*Mud 5

debug.getinfo(2).name会给你调用函数的名称,如果它有一个,它是一个字符串.如果它是一个匿名函数,你会得到nil,如果它使用字符串键以外的东西存储在表中,你就会得到?.

function foo() print(debug.getinfo(2).name) end

-- _G["foo"] => function name is a string
function bar() foo() end
bar() --> 'bar'

-- _G[123] => function name is a number
_G[123] = function() foo() end
_G[123]() --> '?'

-- function has no name
(function() foo() end)()  --> 'nil'
Run Code Online (Sandbox Code Playgroud)