lua-users home
lua-l archive

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


Hi!

I have seen Lua 5.2+ code which uses the following approach
to distinguish C functions from Lua functions:

local function is_C_function(func)
  local x = coroutine.create(func)
  debug.sethook(x, function() x=debug.getlocal(2,2) coroutine.yield() end, "c")
  coroutine.resume(x)
  return not x
end

print(is_C_function(print))             -->  true
print(is_C_function(function() end))    -->  false

Why does this code work?