C++20 + Lua = Flexibility

By James Pascoe

This talk describes an approach for combining C++20 with Lua. A key benefit of this combination is its flexibility i.e. performance critical features can be implemented in C++, whereas, behavioural aspects can be expressed in Lua. As Lua is interpreted, the program’s behaviour can be changed on-the-fly or in the field without a compilation environment. Thus, hypotheses can be tested, workarounds can be explored and previously unknown requirements can be accommodated without requiring a full release cycle.

This session presents the technical details for how to implement such an architecture. The aim of the session is to provide the audience with enough knowledge to be able to implement these ideas in their own projects. In particular, the session will show how to use SWIG to generate bindings and type mappings between C++20 and Lua, how to combine Lua coroutines (which are stateful) with stateless C++20 coroutines and how to integrate SWIG, Lua and C++20 into a CMake build flow. SWIG type mappings (with code available on GitHub) are provided for: std::span, std::any, std::optional and std::variant.

This presentation is a significantly updated and expanded follow-on to a talk given at CppOnSea 2020. Audience members do not need to have seen the CppOnSea talk i.e. the session is completely standalone. However, those that have seen the CppOnSea presentation will benefit from the new content, in particular, the update to C++20, the new SWIG type mappings and a more comprehensive treatment of the interplay between C++ and Lua coroutines.

A further benefit is that these ideas have been tested in a large commercial deployment. As a running exemplar, the talk will describe how Blu Wireless (the author’s employer) has migrated a mission-critical application from a monolithic C++98 code-base to the C++20/Lua architecture described here. In addition to the talk, the author will provide a C++20/Lua application called 'LuaChat' (available on GitHub) to consolidate the concepts and to provide a basis for audience experimentation.