nexuslua
|
The import function in nexuslua offers the ability to bridge C++ shared libraries with nexuslua's scripting interface. By using import, one can utilize functions from shared libraries (*.dll, *.so, *.dylib) directly within nexuslua scripts.
The total number of arguments allowed is 3 (configurable via max_arguments) and the sequence must follow the list above. Given that the type LuaTable can encapsulate an indeterminate number of values, these constraints primarily cater to shared libraries not linking the [https://github.com/acrion/Cbeam](Cbeam) library, a prerequisite for unlocking the type LuaTable. Note that a table is automatically serialized to make use of the advantages of the extern "C" interface. The order and limits can be tweaked within the code generator lua_code_generator.lua.
During interactions between Lua and C++, it's crucial to understand the intricacies of memory management. While shared libraries can allocate their memory as they typically would, there are specific requirements for memory that is exchanged between the shared library and the Lua script.
Any memory area passed back to the caller – be it the Lua script which imported the shared library or a C++ application running this Lua script as an nexuslua agent – must be mandatorily allocated using nexuslua::Memory. This ensures proper memory management and safeguards against premature deallocations, especially when their sole reference exists in pure Lua context.
For developers creating shared libraries, it's imperative to consider memory management when their functions are imported and utilized within nexuslua. Utilizing nexuslua::Memory ensures proper and seamless memory handling, including automatic deallocation, even if the memory is only referenced from within Lua code.
A real-world implementation can be observed in the acrion image tools agent. Here, the import function is used to integrate the OpenImageFile function from the shared library ( acrion_image_tools.dll / acrion_image_tools.dylib / libacrion_image_tools.so)
The associated C++ function looks as follows:
Cbeam offers template based serialization functions. In this example, the type of image inherits from cbeam::container::nested_map, which is one of the various types that enables serialization. Of course Cbeam allows to declare your own serializable types, see Cbeam’s serialization namespace for example on how to add serialization functionality to existing types (you don't need to change your actual types to do this).
To fully harness the power of import and ensure smooth interoperability between Lua scripts and C++ functions, understanding memory management and the role of cbeam::container::xpod::type as a std::variant is essential.