name: lua-c-integration description: Lua C API for native extensions — stack operations, calling C from Lua, calling Lua from C, userdata, metatables, creating loadable C modules. license: MIT
Lua C Integration
Introduction
Lua's C API enables seamless integration with C code, allowing developers to extend Lua with high-performance native functionality or embed Lua as a scripting engine within C applications.
The C API operates through a virtual stack for passing values between Lua and C.
Lua Stack Fundamentals
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
// Push values onto stack
lua_pushinteger(L, 42); // Stack: 42
lua_pushnumber(L, 3.14); // Stack: 42 | 3.14
lua_pushstring(L, "hello"); // Stack: 42 | 3.14 | "hello"
int top = lua_gettop(L); // Get stack size
// Access by index (1-based)
lua_Integer i = lua_tointeger(L, 1);
// Negative indices (from top)
lua_Number n = lua_tonumber(L, -1);
// Type checking
if (lua_isnumber(L, 1)) { }
// Remove elements
lua_pop(L, 1);
// Replace element
lua_pushstring(L, "world");
lua_replace(L, 3);
Calling C Functions from Lua
static int add(lua_State *L) {
lua_Number a = luaL_checknumber(L, 1);
lua_Number b = luaL_checknumber(L, 2);
lua_pushnumber(L, a + b);
return 1;
}
static const luaL_Reg mylib[] = {
{"add", add},
{NULL, NULL}
};
int luaopen_mylib(lua_State *L) {
luaL_newlib(L, mylib);
return 1;
}
Calling Lua from C
void call_lua_function(lua_State *L, const char *func_name, int arg) {
lua_getglobal(L, func_name);
lua_pushinteger(L, arg);
if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
return;
}
lua_Integer result = lua_tointeger(L, -1);
lua_pop(L, 1);
}
Creating C Modules
static int vector_new(lua_State *L) {
lua_Number x = luaL_checknumber(L, 1);
lua_Number y = luaL_checknumber(L, 2);
lua_newtable(L);
lua_pushnumber(L, x);
lua_setfield(L, -2, "x");
lua_pushnumber(L, y);
lua_setfield(L, -2, "y");
return 1;
}
static const luaL_Reg vector_funcs[] = {
{"new", vector_new},
{NULL, NULL}
};
int luaopen_vector(lua_State *L) {
luaL_newlib(L, vector_funcs);
return 1;
}
Userdata and Metatables
typedef struct {
double x;
double y;
} Point;
#define POINT_METATABLE "Point"
static int point_new(lua_State *L) {
double x = luaL_checknumber(L, 1);
double y = luaL_checknumber(L, 2);
Point *p = (Point *)lua_newuserdata(L, sizeof(Point));
p->x = x;
p->y = y;
luaL_getmetatable(L, POINT_METATABLE);
lua_setmetatable(L, -2);
return 1;
}
static int point_distance(lua_State *L) {
Point *p1 = (Point *)luaL_checkudata(L, 1, POINT_METATABLE);
Point *p2 = (Point *)luaL_checkudata(L, 2, POINT_METATABLE);
double dx = p2->x - p1->x;
double dy = p2->y - p1->y;
lua_pushnumber(L, sqrt(dx*dx + dy*dy));
return 1;
}
static const luaL_Reg point_metamethods[] = {
{"__add", point_add},
{"__eq", point_eq},
{"__gc", point_gc},
{NULL, NULL}
};
Best Practices
- Always check
lua_pcallresults - Use
luaL_checkfunctions for argument validation - Balance push and pop operations
- Create metatables for userdata
- Use
luaL_newlibfor modules - Handle errors with
luaL_error
When to Use
- Performance-critical operations exceeding pure Lua
- Wrapping existing C libraries for Lua
- Complex C structures managed from Lua
- Embedding Lua as scripting in C applications