From 7f3947728cf2f72718faf325792c57d01136eaf0 Mon Sep 17 00:00:00 2001 From: actboy168 Date: Fri, 2 Feb 2024 11:53:03 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8D=95=E7=9A=84=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E6=97=A7=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rd/lua/lprefix.h | 2 +- binding/lua_thread.cpp | 33 ++++++++++++++++++++++++++++++++- bootstrap/main.cpp | 22 +++++++++++++++++++++- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/3rd/lua/lprefix.h b/3rd/lua/lprefix.h index 05c9a8ce..47ff87c4 100644 --- a/3rd/lua/lprefix.h +++ b/3rd/lua/lprefix.h @@ -76,7 +76,7 @@ inline void _bee_lua_apicheck(lua_State* L, const char* message, const char* fil # define luai_apicheck(l, expression) (void)((!!(expression)) || (_bee_lua_apicheck(l, #expression, __FILE__, (unsigned)(__LINE__)), 0)) #endif -#define l_randomizePivot() (~0) +#define l_randomizePivot(L) (~0) #if defined(_MSC_VER) && !defined(__SANITIZE_ADDRESS__) diff --git a/binding/lua_thread.cpp b/binding/lua_thread.cpp index 54687c6a..01efc5cb 100644 --- a/binding/lua_thread.cpp +++ b/binding/lua_thread.cpp @@ -248,8 +248,39 @@ namespace bee::lua_thread { return 1; } + 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); + } + + static int panic(lua_State* L) { + const char* msg = (lua_type(L, -1) == LUA_TSTRING) + ? lua_tostring(L, -1) + : "error object is not a string"; + lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", msg); + return 0; /* return to Lua to abort */ + } + static void thread_main(void* ud) noexcept { - lua_State* L = luaL_newstate(); + lua_State* L = lua_newstate(l_alloc, NULL, *(unsigned int*)"Lua\0Lua\0"); + if (L == NULL) { + boxchannel errlog = g_channel.query("errlog"); + if (errlog) { + void* errmsg = seri_pack(L, lua_gettop(L) - 1, NULL); + errlog->push(errmsg); + } + else { + std::println(stdout, "cannot create state: not enough memory"); + } + return; + } + lua_atpanic(L, &panic); lua_pushcfunction(L, msghandler); lua_pushcfunction(L, thread_luamain); lua_pushlightuserdata(L, ud); diff --git a/bootstrap/main.cpp b/bootstrap/main.cpp index 45dca7cc..dc472d16 100644 --- a/bootstrap/main.cpp +++ b/bootstrap/main.cpp @@ -245,17 +245,37 @@ static int pmain(lua_State *L) { return 1; } +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); +} + +static int panic(lua_State *L) { + const char *msg = (lua_type(L, -1) == LUA_TSTRING) + ? lua_tostring(L, -1) + : "error object is not a string"; + lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", msg); + return 0; /* return to Lua to abort */ +} + #if defined(_WIN32) int umain(int argc, char **argv) { #else int main(int argc, char **argv) { #endif int status, result; - lua_State *L = luaL_newstate(); /* create state */ + lua_State *L = lua_newstate(l_alloc, NULL, *(unsigned int *)"Lua\0Lua\0"); if (L == NULL) { l_message(argv[0], "cannot create state: not enough memory"); return EXIT_FAILURE; } + lua_atpanic(L, &panic); lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ lua_pushinteger(L, argc); /* 1st argument */ lua_pushlightuserdata(L, argv); /* 2nd argument */