Skip to Content
Creating Solid APIs with Lua
book

Creating Solid APIs with Lua

by Tyler Neylon
February 2017
Intermediate to advanced
133 pages
2h 43m
English
O'Reilly Media, Inc.
Content preview from Creating Solid APIs with Lua

Chapter 3. Using Lua Values in C

This chapter covers intermediate techniques of API construction in Lua. We’ll see how to pass function arguments from C to Lua, how to work with Lua tables, and how to implement API functions in Lua instead of C.

EatyGuy Version 3: Passing Data to a Lua Function

In Lua, you can pass as many arguments to a function as you like. Even if the function is defined to accept three parameters, for example, you can pass in one or four, or even no arguments when you call it.

You can achieve this same flexibility from C at runtime. Chapter 2 briefly introduces lua_call(), and because this is such a useful function, it’s worthwhile to take a more detailed look at it here. Following is the general technique for calling a Lua function from C, written with an example call to print(1, 2, ’three’):

// 1. Push the Lua function onto the stack.

lua_getglobal(L, "print");

// 2. Push the arguments, in order, onto the stack.

lua_pushnumber(L, 1);
lua_pushnumber(L, 2);
lua_pushstring(L, "three");

// 3. Call the function.
//    You must also indicate how many return values you'd like.

lua_call(L, 3, 0);  // 3 = #input values; 0 = #output values.

// The result on stdout is:
// 1    2    three

When I play games, I love having the ability to influence the outcome of the game by pressing buttons. Let’s add this feature to EatyGuy. In particular, we can use C’s getchar() function to get key codes, and pass those into the eatyguy.loop() function.

Receiving Arrow Key Presses

Terminal-based ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Create a Vim Plugin

Create a Vim Plugin

Alfredo Deza, Noah Gift
Hands-On Enterprise Java Microservices with Eclipse MicroProfile

Hands-On Enterprise Java Microservices with Eclipse MicroProfile

Cesar Saavedra, Heiko W. Rupp, Jeff Mesnil, Pavol Loffay, Antoine Sabot-Durand, Scott Stark

Publisher Resources

ISBN: 9781491986301