lua-users home
lua-l archive

Re: Strange way to determine whether it is Lua or C function

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sun, Aug 23, 2020 at 5:26 PM Viacheslav Usov <via.usov@gmail.com> wrote:
> The coroutine business ensures that the test subjects never actually run.
As established in an erroneous spin-off bug thread, the yield results
in an error rather than a normal yield. Which suggests that coroutines
are not in fact a necessary means toward this end. Indeed, the
following code seems to achieve the same effect without coroutines:
local debug = require 'debug'
local function is_C_function(func)
 local x
 debug.sethook(
 function()
 if debug.getinfo(2, 'f').func == func then
 x = debug.getlocal(2, 2)
 error(0)
 end
 end,
 'c'
 )
 pcall(func)
 return not x
end
)
Cheers,
V.

AltStyle によって変換されたページ (->オリジナル) /