/apps/twnlua: fix for windows, parametrize newstate for alloc func
This commit is contained in:
		@@ -1,7 +1,8 @@
 | 
			
		||||
cmake_minimum_required(VERSION 3.21)
 | 
			
		||||
cmake_policy(SET CMP0171 NEW)
 | 
			
		||||
cmake_minimum_required(VERSION 3.30)
 | 
			
		||||
project(twnlua LANGUAGES C)
 | 
			
		||||
 | 
			
		||||
find_package(Python3 COMPONENTS Interpreter)
 | 
			
		||||
 | 
			
		||||
if(NOT CMAKE_BUILD_TYPE)
 | 
			
		||||
        set(CMAKE_BUILD_TYPE Debug)
 | 
			
		||||
endif()
 | 
			
		||||
@@ -14,14 +15,13 @@ set(FLAGS
 | 
			
		||||
 | 
			
		||||
add_custom_command(
 | 
			
		||||
   OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/game.c
 | 
			
		||||
   COMMAND ${PYTHON3} ${CMAKE_CURRENT_SOURCE_DIR}/bindgen.py $ENV{TWNROOT}/share/twn_api.json ${FLAGS} > ${CMAKE_CURRENT_SOURCE_DIR}/luabind.c
 | 
			
		||||
   COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/bindgen.py $ENV{TWNROOT}/share/twn_api.json ${FLAGS} > ${CMAKE_CURRENT_SOURCE_DIR}/luabind.c
 | 
			
		||||
   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/bindgen.py $ENV{TWNROOT}/share/twn_api.json
 | 
			
		||||
   CODEGEN
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
add_custom_command(
 | 
			
		||||
   OUTPUT ${TWN_OUT_DIR}/data/scripts/twnapi.lua
 | 
			
		||||
   COMMAND ${PYTHON3} ${CMAKE_CURRENT_SOURCE_DIR}/docgen.py $ENV{TWNROOT}/share/twn_api.json > ${TWN_OUT_DIR}/data/scripts/twnapi.lua
 | 
			
		||||
   COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/docgen.py $ENV{TWNROOT}/share/twn_api.json > ${TWN_OUT_DIR}/data/scripts/twnapi.lua
 | 
			
		||||
   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/docgen.py $ENV{TWNROOT}/share/twn_api.json
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -147,12 +147,12 @@ static void exchange_lua_states(lua_State *from, lua_State *to, int level, int i
 | 
			
		||||
void game_tick(void) {
 | 
			
		||||
    if (ctx.initialization_needed) {
 | 
			
		||||
        if (!ctx.udata)
 | 
			
		||||
            ctx.udata = calloc(1, sizeof (State));
 | 
			
		||||
            ctx.udata = SDL_calloc(1, sizeof (State));
 | 
			
		||||
 | 
			
		||||
        State *state = ctx.udata;
 | 
			
		||||
 | 
			
		||||
        /* let's init lua */
 | 
			
		||||
        lua_State *new_state = luaL_newstate();
 | 
			
		||||
        lua_State *new_state = luaL_newstate(custom_alloc);
 | 
			
		||||
        lua_setallocf(new_state, custom_alloc, NULL);
 | 
			
		||||
 | 
			
		||||
        /* state existed already, copy its udata over */
 | 
			
		||||
@@ -161,7 +161,6 @@ void game_tick(void) {
 | 
			
		||||
            lua_getfield(state->L, -1, "udata");
 | 
			
		||||
            SDL_assert(!lua_isnoneornil(state->L, -1));
 | 
			
		||||
            SDL_assert(!lua_isnoneornil(state->L, -2));
 | 
			
		||||
            // SDL_TriggerBreakpoint();
 | 
			
		||||
            if (!lua_isnoneornil(state->L, -1)) {
 | 
			
		||||
                log_info("Exchanging lua states...");
 | 
			
		||||
                lua_newtable(new_state);
 | 
			
		||||
@@ -228,7 +227,7 @@ void game_tick(void) {
 | 
			
		||||
            lua_pop(state->L, 1);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        free(game_buf);
 | 
			
		||||
        SDL_free(game_buf);
 | 
			
		||||
 | 
			
		||||
        /* from this point we have access to everything defined in lua */
 | 
			
		||||
    }
 | 
			
		||||
@@ -261,5 +260,5 @@ void game_end(void) {
 | 
			
		||||
    State *state = ctx.udata;
 | 
			
		||||
    bindgen_unload_twn(state->L);
 | 
			
		||||
    lua_close(state->L);
 | 
			
		||||
    free(state);
 | 
			
		||||
    SDL_free(state);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1513,7 +1513,7 @@ LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz,
 | 
			
		||||
                                   const char *name, const char *mode);
 | 
			
		||||
LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
 | 
			
		||||
 | 
			
		||||
LUALIB_API lua_State *(luaL_newstate) (void);
 | 
			
		||||
LUALIB_API lua_State *(luaL_newstate) (lua_Alloc alloc);
 | 
			
		||||
 | 
			
		||||
LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx);
 | 
			
		||||
 | 
			
		||||
@@ -21898,8 +21898,8 @@ static void warnfon (void *ud, const char *message, int tocont) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
LUALIB_API lua_State *luaL_newstate (void) {
 | 
			
		||||
  lua_State *L = lua_newstate(l_alloc, NULL);
 | 
			
		||||
LUALIB_API lua_State *luaL_newstate (lua_Alloc alloc) {
 | 
			
		||||
  lua_State *L = lua_newstate(alloc ? alloc : l_alloc, NULL);
 | 
			
		||||
  if (l_likely(L)) {
 | 
			
		||||
    lua_atpanic(L, &panic);
 | 
			
		||||
    lua_setwarnf(L, warnfoff, L);  /* default is warnings off */
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,6 @@
 | 
			
		||||
#ifndef STATE_H
 | 
			
		||||
#define STATE_H
 | 
			
		||||
 | 
			
		||||
#include <lua.h>
 | 
			
		||||
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
 | 
			
		||||
typedef struct State {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user