From c83e5d2713334d39bf0718f626cab387f7331a98 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Thu, 17 Jul 2025 17:13:45 +0200 Subject: Added exposed C functions to Lua embed --- c-embed-lua/main.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'c-embed-lua/main.c') diff --git a/c-embed-lua/main.c b/c-embed-lua/main.c index 7ab678a..662ea70 100644 --- a/c-embed-lua/main.c +++ b/c-embed-lua/main.c @@ -4,10 +4,21 @@ #include "hi.h" +static int l_sum(lua_State *L) { + double a = luaL_checknumber(L, 1); + double b = luaL_checknumber(L, 2); + lua_pushnumber(L, a + b); + return 1; +} + int main(void) { lua_State *L = luaL_newstate(); luaL_openlibs(L); + // You do not need to prefix name of the function with `c_`. I did it to + // make it clear in the lua script code that this is a custom function. + lua_register(L, "c_sum", l_sum); + // Not wise since this is not a null-terminated string. /* luaL_dostring(L, (const char*)hi); */ -- cgit v1.2.3