Using exceptions with FFI

  • From: Simon Cooke <sjcfwd@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 16 Aug 2012 15:24:59 -0400

I'm using the FFI library to call C++ code that may throw exceptions -
often just literal character strings. By default these are converted
to the generic Lua error "C++ exception", but I'd like to report
something more informative.

My initial solution creates a Lua callback function that takes the
error message as its argument and calls error(ffi.string(message)). I
register this callback in a C++ function pointer (a global static
variable), and use it with try...catch(...) in each C++ function to
intercept exceptions and forward the error message to Lua via the
callback. This works, but doesn't seem particularly elegant.

I wondered if there might be a better solution, or if something
equivalent to the existing "C call wrapper" feature might be a
possibility. Would native support for intercepting throw "..." be
feasible, for example?

Thanks,
Simon

Other related posts: