Integrate Lua with C++ - Wenhuan Li - E-Book

Integrate Lua with C++ E-Book

Wenhuan Li

0,0
29,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Seitenzahl: 237

Veröffentlichungsjahr: 2023

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Integrate Lua with C++

Seamlessly integrate Lua scripting to enhance application flexibility

Wenhuan Li

BIRMINGHAM—MUMBAI

Integrate Lua with C++

Copyright © 2023 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Group Product Manager: Kunal Sawant

Publishing Product Manager: Akash Sharma

Senior Editor: Esha Banerjee

Technical Editor: Maran Fernandes

Copy Editor: Safis Editing

Project Coordinator: Deeksha Thakkar

Proofreader: Safis Editing

Indexer: Manju Arasan

Production Designer: Alishon Mendonca

Business Development Executive: Debadrita Chatterjee

Marketing Coordinator: Sonia Chauhan

Production reference: 1131023

Published by Packt Publishing Ltd.

Grosvenor House

11 St Paul’s Square

Birmingham

B3 1RB, UK

ISBN 978-1-80512-861-8

www.packtpub.com

To my father, mother, and uncle who gifted me an 8086 when I was 8 years old. To all ordinary people who work hard and stay true to their honors. To all the developers who are constantly learning and crafting.

– Wenhuan Li

Contributors

About the author

Wenhuan Li is a software architect and full stack developer. He started his career in the chip design industry, where he worked on embedded software and development tools. Over the past decade, Wenhuan has primarily focused on mobile applications and backend services and was associated with big tech companies such as Meta, Bloomberg, Booking.com, and McAfee.

About the reviewer

Emilio Panighetti brings over twenty years of experience in software development, with the last decade dedicated to working on embedded platforms and cloud-native applications in modern C++ and Lua for Oracle Communications, which are widely utilized globally. Emilio began his career as a researcher and assistant teacher in college. Later, he co-founded an early internet provider, before moving on to collaborate with various unified communications providers and development houses. In his spare time, he engages in home IoT projects.

For inquiries, you can contact him via email at [email protected].

Table of Contents

Preface

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

Other Books You May Enjoy

Part 1 – Lua Basics

This part of the book will guide you through the process of getting and compiling Lua from the source code. Then, you will explore some options to set up your C++ projects to use Lua. This will be followed by a brief introduction to the Lua programming language.

Hence, the aim of this part is to prepare you for the real work of integrating Lua into C++. Feel free to skip through some of the topics if you are already familiar with them.

This part comprises the following chapters:

Chapter 1, Getting Your C++ Project Lua-ReadyChapter 2, Lua Fundamentals