Integrate Lua with C++

Book description

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.

Table of contents

  1. Integrate Lua with C++
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Share Your Thoughts
    8. Download a free PDF copy of this book
  6. Part 1 – Lua Basics
  7. Chapter 1: Getting Your C++ Project Lua-Ready
    1. Technical requirements
    2. Compiling the Lua source code
      1. Introducing shell
      2. Building Lua
    3. Building a C++ project with the Lua library
      1. Creating a project to work with the Lua library
      2. Writing the C++ code
      3. Writing the Makefile
      4. Testing the project
    4. Building a C++ project with the Lua source code
      1. Creating a project to work with the Lua source code
      2. Writing the C++ code
      3. Writing the Makefile
      4. Testing the project
      5. Testing the clean target
    5. Executing a simple Lua script
      1. Creating a project
      2. Writing the Makefile
      3. Writing the C++ code
      4. Testing the project
      5. Writing the Lua script
    6. Other toolchain options
      1. Using Visual Studio or Xcode
      2. Using Cygwin
    7. Summary
  8. Chapter 2: Lua Fundamentals
    1. Technical requirements
    2. Variables and types
      1. Nil
      2. Booleans
      3. Numbers
      4. Strings
      5. Tables
      6. Functions
      7. Local variables and scopes
    3. Control structures
      1. if then else
      2. while
      3. repeat
      4. for, numerical
      5. for, generic
    4. Summary
    5. Exercises
    6. References
  9. Part 2 – Calling Lua from C++
  10. Chapter 3: How to Call Lua from C++
    1. Technical requirements
    2. Implementing a Lua executor
      1. How to include the Lua library in C++ code
      2. Getting a Lua instance
      3. What is Lua state?
    3. Executing a Lua file
      1. What is a chunk?
      2. What is a Lua stack?
      3. Testing the Lua executor so far
    4. Executing a Lua script
    5. Understanding the Lua stack
      1. Pushing elements
      2. Querying elements
      3. Popping elements
    6. Operating on global variables
      1. Getting global variables
      2. Setting global variables
      3. Testing it out
    7. Calling Lua functions
      1. Implementing function invocation
      2. Testing it out
    8. Summary
    9. Exercises
  11. Chapter 4: Mapping Lua Types to C++
    1. Technical requirements
    2. Mapping Lua types
      1. Exploring different mapping options
      2. Introducing some new Makefile tricks
      3. Defining Lua types in C++
      4. Implementing Lua types in C++
      5. Implementing a union type
      6. Working with the union type
    3. Supporting different argument types
      1. Pushing onto the stack
      2. Popping from the stack
      3. Putting it together
      4. Testing it out
    4. Supporting a variable number of arguments
      1. Implementing the C++ function
      2. Testing it out
      3. Some more words on our mechanism
    5. Supporting multiple return values
      1. Implementing the C++ function
      2. Testing it out
    6. Summary
    7. Exercises
  12. Chapter 5: Working with Lua Tables
    1. Technical requirements
    2. Working with Lua table entries
      1. Getting a table entry value
      2. Setting a table entry value
      3. Testing table operations with string keys
    3. Working with Lua arrays
      1. Using array index optimization
      2. Testing the array index optimization
      3. Revisiting string keys
    4. OOP in Lua
      1. Using Lua metatables to achieve inheritance
      2. Implementing Lua class member functions
      3. Testing it out
    5. Working with Lua table functions
      1. Implementing table function support
      2. Testing it out
    6. Summary
    7. Exercises
  13. Part 3 – Calling C++ from Lua
  14. Chapter 6: How to Call C++ from Lua
    1. Technical requirements
    2. How to register C++ functions
      1. How to declare C++ functions for Lua
      2. Implementing your first C++ function for Lua
      3. How to register C++ functions to Lua
      4. Testing it out
    3. How to override Lua library functions
      1. Reimplementing the Lua print function
      2. Overriding the Lua print function
      3. Testing it out
    4. How to register C++ modules
      1. Implementing a C++ class
      2. What to export to Lua
      3. Designing a reusable exporting mechanism
      4. Testing our mechanism
      5. Accessing the C++ class instance
      6. Completing our stubs
      7. Testing it out
    5. Summary
    6. Exercises
  15. Chapter 7: Working with C++ Types
    1. Technical requirements
    2. How to use the Lua registry
      1. Supporting the registry
      2. Testing the registry
    3. How to use userdata
      1. Preparing the C++ type
      2. What is userdata?
      3. Designing the userdata
      4. Preparing the Lua script
    4. Exporting C++ types to Lua
      1. Setting a metatable for the userdata
      2. Creating a metatable for the userdata
      3. Getting the object in C++
      4. Making wrappers work again
      5. Testing it out
      6. Providing a finalizer
    5. Summary
    6. Exercise
  16. Chapter 8: Abstracting a C++ Type Exporter
    1. Technical requirements
    2. Reviewing the factory implementation
      1. Defining the factory
      2. Implementing the factory
    3. Designing a type exporter
      1. Choosing a design pattern
    4. Stubbing the exporter
      1. Preparing the C++ test code
      2. Preparing the Lua test script
    5. Defining LuaModuleDef
      1. Using LuaModuleDef
    6. Re-implementing luaNew
    7. Are you flexible enough?
    8. Summary
    9. Exercise
  17. Part 4 – Advanced Topics
  18. Chapter 9: Recapping Lua-C++ Communication Mechanisms
    1. Technical requirements
    2. The stack
      1. Pushing onto the stack
      2. Querying the stack
      3. Other stack operations
    3. Calling Lua from C++
    4. Calling C++ from Lua
      1. Exporting C++ modules
    5. Implementing standalone C++ modules
      1. Compiling the standalone module
      2. Testing the standalone module
    6. Storing state in Lua
      1. Upvalues
      2. The registry
    7. Userdata
      1. Light userdata
      2. Full userdata
    8. Summary
  19. Chapter 10: Managing Resources
    1. Technical requirements
    2. Customizing Lua memory allocation
      1. What is the Lua memory allocation function?
      2. Implementing a memory allocation function
      3. Testing it out
    3. Delegating C++ object memory allocation to Lua
      1. Using C++ placement new
      2. Extending LuaModuleDef
      3. Extending LuaModuleExporter
      4. Testing with the Destinations.cc module
    4. What is RAII?
    5. Summary
  20. Chapter 11: Multithreading with Lua
    1. Technical requirements
    2. Multithreading in C++
      1. How does C++ support multithreading?
      2. Using multiple Lua instances
      3. Testing it out
    3. Multithreading in Lua
      1. How does Lua support multithreading?
    4. Using coroutine with C++
    5. Summary
  21. Index
    1. Why subscribe?
  22. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Integrate Lua with C++
  • Author(s): Wenhuan Li
  • Release date: October 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781805128618