diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2025-07-17 17:04:08 +0200 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2025-07-17 17:04:08 +0200 |
| commit | 22840a3551d6564ea271d4be6ef7b539d4eba409 (patch) | |
| tree | afc8b697bd5ea5aecd3617233a547db10a09c92e /c-embed-lua/main.c | |
| parent | fccba39aa0c0060e7e17c4075963bf8a428536b1 (diff) | |
| download | probe-22840a3551d6564ea271d4be6ef7b539d4eba409.tar.gz | |
Added embedding Lua
Diffstat (limited to 'c-embed-lua/main.c')
| -rw-r--r-- | c-embed-lua/main.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/c-embed-lua/main.c b/c-embed-lua/main.c new file mode 100644 index 0000000..7ab678a --- /dev/null +++ b/c-embed-lua/main.c @@ -0,0 +1,29 @@ +#include "lua-5.4.8/src/lua.h" +#include "lua-5.4.8/src/lualib.h" +#include "lua-5.4.8/src/lauxlib.h" + +#include "hi.h" + +int main(void) { + lua_State *L = luaL_newstate(); + luaL_openlibs(L); + + // Not wise since this is not a null-terminated string. + /* luaL_dostring(L, (const char*)hi); */ + + // https://www.lua.org/manual/5.4/manual.html#luaL_loadbuffer + // Load Lua chunk from `hi` buffer. + if (luaL_loadbuffer(L, (const char*)hi, hi_len, "hi_chunk") != LUA_OK) { + fprintf(stderr, "Load error: %s\n", lua_tostring(L, -1)); + lua_pop(L, 1); + } else { + // Execute the chunk. + if (lua_pcall(L, 0, 0, 0) != LUA_OK) { + fprintf(stderr, "Runtime error: %s\n", lua_tostring(L, -1)); + lua_pop(L, 1); + } + } + + lua_close(L); + return 0; +} |
