Lua C Integration
БесплатноБез исполняемых скриптовНе проверенUse when lua C API for extending Lua with native code including stack operations, calling C from Lua, calling Lua from C, creating C modules, userdata types…
от TheBushidoCollective★ 202 звезды на GitHubОб этом скилле
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. This bidirectional integration makes Lua ideal for performance-critical applications requiring scripting capabilities.
The C API operates through a virtual stack for passing values between Lua and C, with functions for manipulating Lua values, calling functions, and managing memory. Understanding stack operations and Lua's data model is essential for safe, efficient C integration.
This skill covers the Lua stack, calling C from Lua, calling Lua from C, creating C modules, userdata and metatables, error handling, memory management, and performance optimization patterns.
Lua Stack Fundamentals
The Lua-C API uses a virtual stack for all value exchange between Lua and C, requiring understanding of push/pop operations.
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
// Basic stack operations
void demonstrate_stack(lua_State *L) {
// 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"
lua_pushboolean(L, 1); // Stack: 42 | 3.14 | "hello" | true
lua_pushnil(L); // Stack: 42 | 3.14 | "hello" | true | nil
// Get stack size
int top = lua_gettop(L); // Returns 5
// Access values by index (1-based)
lua_Integer i = lua_tointeger(L, 1); // 42 (index from bottom)
lua_Number n = lua_tonumber(L, 2); // 3.14
const char *s = lua_tostring(L, 3); // "hello"
int b = lua_toboolean(L, 4); // 1 (true)
// Negative indices (from top)
lua_Number n2 = lua_tonumber(L, -4); // 3.14 (4th from top)
const char *s2 = lua_tostring(L, -3); // "hello" (3rd from top)
// Type checking
if (lua_isnumber(L, 1)) {
// Handle number
}
if (lua_isstring(L, 3)) {
// Handle string
}
// Remove elements
lua_pop(L, 1); // Remove top element (nil)
lua_remove(L, 2); // Remove element at index 2 (3.14)
// Replace element
lua_pushstring(L, "world");
lua_replace(L, 3); // Replace index 3 with "world"
// Clear stack
lua_settop(L, 0); // Empty the stack
}
// Stack manipulation patterns
void stack_patterns(lua_State *L) {
// Insert at specific position
lua_pushstring(L, "new value");
lua_insert(L, 1); // Insert at bottom
// Copy element
lua_pushvalue(L, 1); // Duplicate element at index 1
// Rotate elements
lua_rotate(L, 1, 2); // Rotate 2 elements starting from index 1
// Check stack space
if (!lua_checkstack(L, 100)) {
// Failed to allocate stack space
}
// Absolute index (doesn't change with stack modifications)
int abs_idx = lua_absindex(L, -1);
}
// Type checking helper
int check_arguments(lua_State *L) {
int argc = lua_gettop(L);
if (argc < 2) {
return luaL_error(L, "Expected at least 2 arguments");
}
if (!lua_isnumber(L, 1)) {
return luaL_error(L, "Argument 1 must be a number");
}
if (!lua_isstring(L, 2)) {
return luaL_error(L, "Argument 2 must be a string");
}
return 0;
}
// Table operations
void table_operations(lua_State *L) {
// Create table
lua_newtable(L); // Stack: {}
// Set field: table["key"] = "value"
lua_pushstring(L, "value");
lua_setfield(L, -2, "key");
// Get field: value = table["key"]
lua_getfield(L, -1, "key");
const char *value = lua_tostring(L, -1);
lua_pop(L, 1);
// Set with arbitrary key
lua_pushstring(L, "key2");
lua_pushinteger(L, 42);
lua_settable(L, -3); // table[key2] = 42
// Array-style: table[1] = "first"
lua_pushinteger(L, 1);
lua_pushstring(L, "first");
lua_settable(L, -3);
// Rawset/rawget (bypass metamethods)
lua_pushstring(L, "rawkey");
lua_pushstring(L, "rawvalue");
lua_rawset(L, -3);
// Table length
lua_len(L, -1);
lua_Integer len = lua_tointeger(L, -1);
lua_pop(L, 1);
}
// Global variables
void global_operations(lua_State *L) {
// Set global: my_global = 42
lua_pushinteger(L, 42);
lua_setglobal(L, "my_global");
// Get global: value = my_global
lua_getglobal(L, "my_global");
lua_Integer value = lua_tointeger(L, -1);
lua_pop(L, 1);
}
Master stack operations for efficient C-Lua value exchange and avoid stack overflow through proper cleanup.
Calling C Functions from Lua
C functions follow specific signatures and conventions to be callable from Lua scripts.
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
// Basic C functio
Установить Lua C Integration в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл Lua C Integration?
Use when lua C API for extending Lua with native code including stack operations, calling C from Lua, calling Lua from C, creating C modules, userdata types, metatables in C, and performance optimization techniques.
Как установить скилл Lua C Integration?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл Lua C Integration запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
mcp-builder
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when…
от Anthropicclaude-api
Reference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER…
от Anthropicalgorithmic-art
Creating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code…
от Anthropicmcp-integration
This skill should be used when the user asks to "add MCP server", "integrate MCP", "configure MCP in plugin", "use .mcp.json", "set up Model Context…
от Anthropic