I've got a lot of c++ code which contains a lot of functions and classes in namespaces (boost, for example).
Now I'm trying to embed LuaJiT2 as script engine, but I cannot find anything about calling functions and using other stuff from namespaces.
So, Is it possible to pass the functions from c++ namespaces to LuaJIT with the FFI?

- 262
- 4
- 13
-
2I'd assume you'd need to export them with C style decoration via extern "C", but i doubt you'll be able to get anything like class methods/templates or non-exported namespaced functions – Necrolis Jul 14 '11 at 10:32
-
Exposing boost to Lua is probably not a fantastic idea either. You're better off exposing a limited functional API based on your actual use-case. – jsimmons Jul 15 '11 at 04:15
-
Thanks, @jsimmons, but boost was an example and "a limited functional API based on your actual use-case" is much bigger than boost library functional. – Vladimir Protasov Jul 15 '11 at 13:36
2 Answers
You may use the standard Lua API to expose namespace-scope functions, as well as class static functions, to Lua. This is done exactly as you would with the regular Lua interpreter, since LuaJIT is drop-in compatible with it.
But you can't use FFI, because FFI is based on a C-based parsing of the header files. And you're using C++ syntax. FFI is not the only way to use LuaJIT; it's just one that is based on C.
Any of the C++-specific binding APIs that use Lua (Luabind, SWIG, etc) should work just fine with LuaJIT as well.

- 449,505
- 63
- 781
- 982
It is possible to use different name mangling other than C. The reason why its not "common" is because the C++ name mangling is very compiler/platform specific: http://lua-users.org/lists/lua-l/2011-07/msg00502.html
So this sort of declaration is valid:
ffi.cdef[[
void Test1_Method1(void) asm("_ZN5Test17Method1Ev");
]]
And you can then call Test1_Method1. Mike Pall has done an amazing job with luajit. So many great features.