LuaNativeObjects icon indicating copy to clipboard operation
LuaNativeObjects copied to clipboard

Can this be used to generate method signatures automatically from a C/C++ library header?

Open alerque opened this issue 2 years ago • 4 comments

I'm trying to workup ICU bindings for Lua but my initial attempt to use LuaNativeObjects didn't turn out like I expected. Somehow I thought the various subfiles src/*.noobj.lua that have object/method/function signatures could be auto-generated given the C/C++ source code and header files for the library I want to wrap (in this case icu4c). It turns out all I was able to accomplish was generating c code and a loadable Lua module that included any C functions I manually provided signatures with input/output types for.

Is there some way to automatically generate those signatures from a header file? At least as a starting point?

alerque avatar Jan 28 '23 18:01 alerque

Sorry there isn't a parser for C/C++ headers. The .nobj.lua files are hand written. The main benefit of using LuaNativeObjects is that it can generate most of the C code needed for Lua bindings (and LuaJIT FFI bindings).

There are some C++ bindings generators for Lua that you can try: https://stackoverflow.com/questions/52564364/automated-lua-binding-using-c

I looked over the ICU C API docs a little bit and it doesn't seem like an API that will be easy to created Lua bindings for. Not sure how Lua strings can be converted safely to UChar (16-bit chars) or how to allocate and initialize a dynamic UChar * string.

I didn't look at the ICU C++ API, maybe it has an easier to use Unicode string object.

I think a lot of the ICU functions will need some custom C code in the bindings functions. NObjects supports wrapping C code in the bindings functions See an example here from lua-llthreads.

Here are some example Lua bindings that use it: https://github.com/Neopallium/lua-buf https://github.com/Neopallium/lua-llnet https://github.com/Neopallium/lua-zmq https://github.com/Neopallium/lua-llthreads - This Lua bindings for pthreads is a good example of a complex binding.

Neopallium avatar Mar 11 '23 12:03 Neopallium

Thanks @Neopallium, that's very helpful information.

alerque avatar Mar 11 '23 14:03 alerque