Skip to Content
Lua Quick Start Guide
book

Lua Quick Start Guide

by Gabor Szauer
July 2018
Beginner
202 pages
5h 42m
English
Packt Publishing
Content preview from Lua Quick Start Guide

Calling C functions from Lua

Because functions in C and Lua work so differently, exposing a C function to Lua can get a bit tricky. All C functions that Lua can call must follow the signature of lua_CFunction, which is defined in lua.h as the following:

typedef int (*lua_CFunction) (lua_State *L);

This function takes only one argument, the lua_State. The return value of the function is an integer. This integer is the number of elements that the function pushed onto the stack as return values.

Lua has multiple stacks—each C function called from Lua has its own stack and does not share the global stack.

Let's take for example a simple C function that returns the magnitude of a three-dimensional vector. In C, the code for doing so might look ...

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.
Start your free trial

You might also like

Beginning Lua Programming

Beginning Lua Programming

Kurt Jung, Aaron Brown
Vim Masterclass

Vim Masterclass

Jason Cannon

Publisher Resources

ISBN: 9781789343229Supplemental Content