Discover the perfect synergy between C++ and Lua to create powerful, adaptable, and high-performing software solutions
Key Features
Get hands-on experience by integrating Lua with C++
Explore real-life project-ready advanced techniques for your future projects
Learn Lua through practical coding examples and exercises
Purchase of the print or Kindle book includes a free PDF eBook
Book Description
C++ is a popular choice in the developer community for building complex and large-scale performant applications and systems. Often a need arises to extend the system at runtime, without recompiling the whole C++ program. Using a scripting language like Lua can help achieve this goal efficiently.
Integrate Lua to C++ is a comprehensive guide to integrating Lua to C++ and will enable you to achieve the goal of extending C++ programs at runtime. You’ll learn, in sequence, how to get and compile the Lua library, the Lua programming language, calling Lua code from C++, and calling C++ code from Lua. In each topic, you’ll practice with code examples, and learn the in-depth mechanisms for smooth working. Throughout the book, the latter examples build on the earlier ones while also acting as a standalone. You’ll learn to implement Lua executor and Lua binding generator, which you can use in your projects directly with further customizations.
By the end of this book, you’ll have mastered integrating Lua into C++ and using Lua in your C++ project efficiently, gained the skills to extend your applications at runtime, and achieved dynamic and adaptable C++ development.
What you will learn
Explore how to access and compile Lua source code
Call Lua code from C++ for enhanced functionality
Integrate C++ code into Lua for powerful interactions
Deepen your understanding of Lua stack for advanced usage
Implement a project-ready Lua executor and binding generator
Extend C++ projects with customizable and extensible Lua scripting
Who this book is for
This book is for C++ developers seeking to seamlessly integrate Lua, learn the Lua programming language by examples, or enhance their understanding of Lua-C++ interaction. Basic knowledge of C++ is required to fully benefit from this book.
Author(s): WENHUANLI
Publisher: Packt
Year: 2023
Language: English
Pages: 216
Cover
Title Page
Copyright and Credits
Dedication
Contributors
Table of Contents
Preface
Part 1 – Lua Basics
Chapter 1: Getting Your C++ Project Lua-Ready
Technical requirements
Compiling the Lua source code
Introducing shell
Building Lua
Building a C++ project with the Lua library
Creating a project to work with the Lua library
Writing the C++ code
Writing the Makefile
Testing the project
Building a C++ project with the Lua source code
Creating a project to work with the Lua source code
Writing the C++ code
Writing the Makefile
Testing the project
Testing the clean target
Executing a simple Lua script
Creating a project
Writing the Makefile
Writing the C++ code
Testing the project
Writing the Lua script
Other toolchain options
Using Visual Studio or Xcode
Using Cygwin
Summary
Chapter 2: Lua Fundamentals
Technical requirements
Variables and types
Nil
Booleans
Numbers
Strings
Tables
Functions
Local variables and scopes
Control structures
if then else
while
repeat
for, numerical
for, generic
Summary
Exercises
References
Part 2 – Calling Lua from C++
Chapter 3: How to Call Lua from C++
Technical requirements
Implementing a Lua executor
How to include the Lua library in C++ code
Getting a Lua instance
What is Lua state?
Executing a Lua file
What is a chunk?
What is a Lua stack?
Testing the Lua executor so far
Executing a Lua script
Understanding the Lua stack
Pushing elements
Querying elements
Popping elements
Operating on global variables
Getting global variables
Setting global variables
Testing it out
Calling Lua functions
Implementing function invocation
Testing it out
Summary
Exercises
Chapter 4: Mapping Lua Types to C++
Technical requirements
Mapping Lua types
Exploring different mapping options
Introducing some new Makefile tricks
Defining Lua types in C++
Implementing Lua types in C++
Implementing a union type
Working with the union type
Supporting different argument types
Pushing onto the stack
Popping from the stack
Putting it together
Testing it out
Supporting a variable number of arguments
Implementing the C++ function
Testing it out
Some more words on our mechanism
Supporting multiple return values
Implementing the C++ function
Testing it out
Summary
Exercises
Chapter 5: Working with Lua Tables
Technical requirements
Working with Lua table entries
Getting a table entry value
Setting a table entry value
Testing table operations with string keys
Working with Lua arrays
Using array index optimization
Testing the array index optimization
Revisiting string keys
OOP in Lua
Using Lua metatables to achieve inheritance
Implementing Lua class member functions
Testing it out
Working with Lua table functions
Implementing table function support
Testing it out
Summary
Exercises
Part 3 – Calling C++ from Lua
Chapter 6: How to Call C++ from Lua
Technical requirements
How to register C++ functions
How to declare C++ functions for Lua
Implementing your first C++ function for Lua
How to register C++ functions to Lua
Testing it out
How to override Lua library functions
Reimplementing the Lua print function
Overriding the Lua print function
Testing it out
How to register C++ modules
Implementing a C++ class
What to export to Lua
Designing a reusable exporting mechanism
Testing our mechanism
Accessing the C++ class instance
Completing our stubs
Testing it out
Summary
Exercises
Chapter 7: Working with C++ Types
Technical requirements
How to use the Lua registry
Supporting the registry
Testing the registry
How to use userdata
Preparing the C++ type
What is userdata?
Designing the userdata
Preparing the Lua script
Exporting C++ types to Lua
Setting a metatable for the userdata
Creating a metatable for the userdata
Getting the object in C++
Making wrappers work again
Testing it out
Providing a finalizer
Summary
Exercise
Chapter 8: Abstracting a C++ Type Exporter
Technical requirements
Reviewing the factory implementation
Defining the factory
Implementing the factory
Designing a type exporter
Choosing a design pattern
Stubbing the exporter
Preparing the C++ test code
Preparing the Lua test script
Defining LuaModuleDef
Using LuaModuleDef
Re-implementing luaNew
Are you flexible enough?
Summary
Exercise
Part 4 – Advanced Topics
Chapter 9: Recapping Lua-C++ Communication Mechanisms
Technical requirements
The stack
Pushing onto the stack
Querying the stack
Other stack operations
Calling Lua from C++
Calling C++ from Lua
Exporting C++ modules
Implementing standalone C++ modules
Compiling the standalone module
Testing the standalone module
Storing state in Lua
Upvalues
The registry
Userdata
Light userdata
Full userdata
Summary
Chapter 10: Managing Resources
Technical requirements
Customizing Lua memory allocation
What is the Lua memory allocation function?
Implementing a memory allocation function
Testing it out
Delegating C++ object memory allocation to Lua
Using C++ placement new
Extending LuaModuleDef
Extending LuaModuleExporter
Testing with the Destinations.cc module
What is RAII?
Summary
Chapter 11: Multithreading with Lua
Technical requirements
Multithreading in C++
How does C++ support multithreading?
Using multiple Lua instances
Testing it out
Multithreading in Lua
How does Lua support multithreading?
Using coroutine with C++
Summary
Index
About Packt
Other Books You May Enjoy