Skip to content

Commit

Permalink
简单的兼容旧版本
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Feb 2, 2024
1 parent c2a522a commit 7f39477
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
2 changes: 1 addition & 1 deletion 3rd/lua/lprefix.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down
33 changes: 32 additions & 1 deletion binding/lua_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 21 additions & 1 deletion bootstrap/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 7f39477

Please sign in to comment.