lua-users home
lua-l archive

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

On Tue, Oct 20, 2015 at 7:33 PM, Philipp Janda <> wrote:


You shouldn't throw exceptions from `extern "C"` functions, especially if real C code is involved. So the whole point of compiling Lua as C++ becomes moot.

This is false. Oh, and "real C" is not defined, too. The current C++ standard states, in clause 7.5.1: "Some of the properties associated with an entity with language linkage are specific to each implementation and are not described here". Subsequent clauses on language linkage do not introduce any exception-related restrictions. So exception propagation over C/C++ boundaries is both standard compliant and entirely implementation specific. There are in fact C/C++ implementations that have consistent and documented behaviour with respect to exception propagation over C/C++ boundaries. With such implementations, the try/catch/throw mechanics as used in Lua is perfectly interoperable with C-language libraries that are designed correctly for use in Lua. Designed correctly for use in Lua implies that any call such a library makes into Lua or into a user-provided function is expected to result in a non-local transfer via lua_error().

Bottom line, compiling Lua as C++ with try/catch/throw mechanics and using C-language Lua libraries is both standard compliant and well defined in particular C/C++ implementations, and is a valuable feature.

Also `extern "C" int (*)( lua_State* )` and `extern "C++" int (*)( lua_State* )` are distinct types not convertible to each other, and currently you can only pass one *or* the other to the Lua API functions, depending on how the Lua library was compiled.

That is true, even though at least some C++ implementations, in non-pedantic modes, do not enforce this distinction. I have missed this aspect; so I need to spend some more time to develop a standard compliant solution for this.
So if you have C code *and* C++ code, and both use `lua_CFunction`s, your only standards compliant option is to compile Lua as C and use `extern "C"` in your C++ code for your binding functions.

This is not the only option. One can still compile Lua as C++ with lua_CFunction and other function types defined as extern "C".