Integrate Lua with C++: Seamlessly integrate Lua scripting to enhance application flexibility 9781805128618

Discover the perfect synergy between C++ and Lua to create powerful, adaptable, and high-performing software solutions

339 37 2MB

English Pages 216 Year 2023

Report DMCA / Copyright

DOWNLOAD FILE

Integrate Lua with C++: Seamlessly integrate Lua scripting to enhance application flexibility
 9781805128618

Table of contents :
Integrate Lua with C++
Contributors
About the author
About the reviewer
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Conventions used
Get in touch
Share Your Thoughts
Download a free PDF copy of this book
Part 1 – Lua Basics
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
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++
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
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
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
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
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
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
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
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
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
Why subscribe?
Other Books You May Enjoy
Packt is searching for authors like you
Share Your Thoughts
Download a free PDF copy of this book

Polecaj historie