Creating Solid APIs with Lua

Book description

Learn how you can build APIs by integrating the Lua and C programming languages. These APIs can be used to speed up development within your company, allow people to customize your app or game, and serve as the foundation of a domain-specific language within a platform you’re developing.

Author Tyler Neylon—a game designer with a PhD in applied math—shows you how C and Lua interface with each other, so you can create a set of Lua classes and functions that work programmatically with local applications. You’ll also learn how to guard against shady scripts written by users who either make mistakes or purposefully attempt to break your system.

  • Build Lua from source, then link the Lua library to a C program that can execute a Lua script
  • Write Lua-visible functions with the power of C by calling C functions from Lua
  • Work with Lua data types from C
  • Add Lua classes to your API, implemented in either Lua or C
  • Manage Lua exceptions with error-handling functions, and add custom hook functions that can protect you from common programming errors
  • Give users the freedom to write their own scripts that can be run in a C-created sandboxed environment

Lua is easy to learn, especially if you know JavaScript. Neylon’s free 3-part series on oreilly.com, Learn Lua from JavaScript, will help you get started.

Table of contents

  1. Preface
    1. Acknowledgments
  2. I. Making a Lua API
  3. 1. Running a Lua Script from C
    1. An Overview of the C/Lua Interface
    2. An Example: EatyGuy
    3. Getting the Lua Source
      1. If You’re Using macOS or Linux…
      2. If You’re Using Windows…
    4. This Book’s GitHub Repo
    5. Running a Lua Script from C
    6. Lua Modules and Loading Them from C
    7. Drawing Mazes
    8. One Way to Call a Lua Function from C
      1. Stack Indexes
    9. Giving Lua the Power of C
  4. 2. Calling C from Lua
    1. EatyGuy Version 1: A Lua-Callable C Function
      1. The tput Shell Command
      2. The Core Mechanics of Lua-Callable C Functions
    2. EatyGuy Version 2: Adding an Animated Player
      1. Strings and Multiple Arguments with set_color() and set_pos()
      2. Sending a Return Value from C to Lua by Using timestamp()
      3. Fancier Maze Drawing
      4. Starting, Stopping, and Sharing Functions with Lua
      5. Game State and Lua’s Event Loop
      6. Updating Player State
      7. Drawing the Player State
    3. Calling Lua Functions from C More Effectively
  5. 3. Using Lua Values in C
    1. EatyGuy Version 3: Passing Data to a Lua Function
      1. Receiving Arrow Key Presses
    2. EatyGuy Version 4: Implementing API Functions Within Lua
      1. Fixing the tput Bottleneck
    3. EatyGuy Version 5: Working with Lua Tables from C
      1. Where Tables Live
      2. The Primary Table Functions
      3. Using a Table to Send Game State to Lua
    4. Adding Some Class to Your API
  6. 4. Making Your API Classy
    1. EatyGuy Version 6: Writing a Class in Lua
      1. A Common Code Pattern for Lua Classes
      2. The Character Class Example
    2. EatyGuy Version 7: Class Inheritance
      1. Subclasses in Lua
      2. The Baddy Class
    3. EatyGuy Version 8: Introducing the userdata Type
    4. Writing a Lua-Loadable Module in C
      1. Implementing a Class-Like userdata Type
      2. Integrating a C Module into Your API
    5. Making Your Code Debug-Friendly
  7. II. Script Safety
  8. 5. Detecting Errors
    1. Throwing and Catching Errors in Lua
    2. When Should You Throw an Exception?
    3. Don’t Panic!
      1. Lua Errors in C
    4. How lua_pcall() Handles Errors
    5. Turtles All the Way Down
    6. Getting Strict About Globals and Typos
    7. The Environment of Freshly Loaded Chunks
    8. A Version of strict.lua
    9. EatyGuy Version 9: Death, Strictly
    10. A Brief but Quickly Dismissed Question of Morality
  9. 6. Sandboxing User Scripts
    1. EatyGuy Version 10: The Baddy Construction Kit
      1. The Code of Scum and Villainy
    2. Scripts Gone Wild
    3. Controlling Library Access
    4. Managing Memory Use
    5. Managing CPU Use
    6. End == Nigh

Product information

  • Title: Creating Solid APIs with Lua
  • Author(s): Tyler Neylon
  • Release date: February 2017
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781491963760