From 3b39ff4301921d025f95d41a1e3e9ecf02d455e4 Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Tue, 8 Apr 2025 11:48:10 +0200 Subject: [PATCH] Lua fonctionel --- src/lua_inter.c | 37 +++++-------------------------------- 1 file changed, 5 insertions(+), 32 deletions(-) diff --git a/src/lua_inter.c b/src/lua_inter.c index 6fc2efc..ebe9183 100644 --- a/src/lua_inter.c +++ b/src/lua_inter.c @@ -5,6 +5,7 @@ #include #include +#include #include "types.h" @@ -22,7 +23,7 @@ static int lua_add_machine(lua_State *L){ } static int lua_mtprint(lua_State *L){ - printf("(lua) %s\n", lua_tostring(L, 0)); + printf("(lua) %s\n", luaL_checkstring(L, 1)); return 0; } @@ -36,50 +37,22 @@ struct LuaFn exported_funcs[] = { {"mtprint", lua_mtprint} }; -// Taken from the Lua manual -static void *l_alloc (void *ud, void *ptr, size_t osize,size_t nsize){ - (void)ud; (void)osize; /* not used */ - if (nsize == 0) { - free(ptr); - return NULL; - } - else - return realloc(ptr, nsize); -} - struct load_dat { char *str; int pos; int len; }; -const char *file_reader(lua_State *L, void *data, size_t *size){ - (void)L; - struct load_dat *dat = data; - if(dat->pos >= dat->len){ - *size = 0; - return NULL; - } - else{ - *size = dat->len; - dat->pos += *size; - return dat->str; - } -} - lua_State *gl_L; void script_init(){ - gl_L = lua_newstate(l_alloc, NULL); + gl_L = luaL_newstate(); for(int i = 0; i < sizeof(exported_funcs)/sizeof(*exported_funcs); i++) lua_register(gl_L, exported_funcs[i].name, exported_funcs[i].fn); - struct load_dat dat = {"mtprint(\"hello\")", 0}; - dat.len = strlen(dat.str); - lua_load(gl_L, file_reader, &dat, "test", "t"); - int results = 0; - lua_resume(gl_L, NULL, 0, &results); + luaL_loadstring(gl_L, "mtprint(\"hello world\")\n"); + lua_pcall(gl_L, 0, 0, 0); } void script_clean(){