lua-users home
lua-l archive

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


Luiz Henrique de Figueiredo wrote:
> > I was surprised at finding that (unless I'm mistaken) it's not possible to catch a luaL_error thrown from a lua_sethook function.
> 
> Sure is. The code below works fine. Could you please show us your code?

Err ... append another print() to your example and the 2nd time
through it aborts the script. :-)

Just like you, he probably forgot to turn off the hook. Either
before throwing the error or after the pcall fails. The latter
only works with lua_pcall from C, but not reliably for pcall()
since the hook may be called again before the next Lua
instruction (*).

[
(*) Actually it should be considered an implementation detail or a
bug that Lua sometimes fails to call the line hook. Check the
output of this:

  local write = io.write
  debug.sethook(function(w, l) write(l) end, "l")
  local function f()
    write"a" write"b"
    write"c" write"d"
  end
  write"A"
  write"B" f() f() write"C"
  write"D\n"

Lua 5.1.4 prints:               67A8B4ab5cd64ab5cd6C9D
But I would have expected this: 67A8B4ab5cd684ab5cd68C9D

Note that the changes back to line 8 after the return are missing.
]

--Mike