为什么对于debug.getinfo(1),“名称"为零 [英] Why is 'name' nil for debug.getinfo(1)

查看:319
本文介绍了为什么对于debug.getinfo(1),“名称"为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个lua测试框架,该框架可以让您知道出现问题的函数,但是当我从loadstring切换到_G时,(我进行了切换,因此我的测试工具可以看到该函数的结果调用),我的函数开始使用"nil"作为函数名

为什么在以下代码中_G无法检测到当前函数的名称?另外,当使用_G时,如何从加载字符串中获得返回结果(即从blah调用中返回"false")或设置函数名称(即,告诉lua解释器函数名称应为什么)?

function run_test(one, two)
    if one ~= two then
        print(debug.getinfo(2).name..' Failed')
    end
end

function blah()
    run_test(false, true)
    return false
end

local fname = 'blah'
local status, result = pcall(_G[fname])  -- Outputs 'nil'; result is 'false'
local status, result = pcall(loadstring(fname..'()'))  -- Outputs 'blah', result is 'nil'

我需要的主要是一种使用函数名称字符串的方法来调用函数,能够在调用内看到函数名称(用于测试失败以指向失败的函数,例如fname ='blah '(在上面的代码中),并能够获取返回值

local fname = 'blah'
status, result = pcall(??Call fname somehow??)
assert(status)
assert(not result)

--stdout should be "blah Failed"

解决方案

这是Lua用于提供函数名称的试探法的局限性.

在Lua中,所有功能都是匿名的.给定的函数可以是几个变量的值:全局,局部和表字段. Lua调试系统会通过查看正在执行的字节码来尝试根据值的来源找到一个合理的名称.

考虑更简单的示例

blah()
pcall(blah)

在第一次调用中,调试系统会看到正在调用的函数来自全局blah,而debug.getinfo(1).name给出了预期的结果blah.

在第二次调用中,调试系统看到正在调用的函数来自pcall的第一个参数,但不再看该参数的来源,而debug.getinfo(1).name给出了nil. /p>

当您呼叫_G[name]()时,也会发生同样的事情.调试系统只看到一个表的字段,并且该字段的名称相距太远.

尝试将print(debug.traceback())添加为blah的第一行,以查看对该解释的另一种理解.

I'm trying to put together a lua testing framework that lets you know the function that had the problem, but when I switched from loadstring to _G, (I switched so my test harness could see the results of the function call) my functions started using 'nil' for the function name

Why can _G not detect the name of the current function in the following code? Also, how can I get the return results from loadstring (ie the 'false' from the blah call) or set the function name when using _G (ie. Tell the lua interpreter what the function name should be)?

function run_test(one, two)
    if one ~= two then
        print(debug.getinfo(2).name..' Failed')
    end
end

function blah()
    run_test(false, true)
    return false
end

local fname = 'blah'
local status, result = pcall(_G[fname])  -- Outputs 'nil'; result is 'false'
local status, result = pcall(loadstring(fname..'()'))  -- Outputs 'blah', result is 'nil'

The main thing I need is a way to call a function using a string of the function name, be able to see the function name inside the call (for test failures to point to the function that failed, like fname = 'blah' in the code above) and be able to get the return value

local fname = 'blah'
status, result = pcall(??Call fname somehow??)
assert(status)
assert(not result)

--stdout should be "blah Failed"

解决方案

This is a limitation of the heuristics used by Lua to provide names for functions.

In Lua, all functions are anonymous. A given function can be the value of several variables: global, local, and table fields. The Lua debug system tries to find a reasonable name for a value based on where it came from by looking into the bytecode being executed.

Consider the simpler example

blah()
pcall(blah)

In the first call, the debug system sees that the function being called comes from the global blah and debug.getinfo(1).name gives the expected result, blah.

In the second call, the debug system sees that the function being called comes from the first argument to pcall but it does not look further to see where that argument came from, and debug.getinfo(1).name gives nil.

The same thing happens when you call _G[name](). All the debug system sees is a field of a table and the name of the field is too far off.

Try adding print(debug.traceback()) as the first line of blah to see another take on this explanation.

这篇关于为什么对于debug.getinfo(1),“名称"为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆