LUALIB_API void luaL_openlibs (lua_State *L) { const luaL_Reg *lib; /* "require" functions from 'loadedlibs' and set results to global table */ for (lib = loadedlibs; lib->func; lib++) { luaL_requiref(L, lib->name, lib->func, 1); lua_pop(L, 1); /* remove lib */ } ///------------------------------------------------------- ///slua - add preloaded libraries // PRELOAD: preload a library (see luaL_openlibs below) #define PRELOAD(libname) \ int luaopen_##libname (lua_State *L); \ lua_pushcfunction(L, luaopen_##libname); \ lua_setfield(L, -2, #libname); // PRELOAD2 is used for special cases, eg. for mime.core: // PRELOAD2(mime.core, mime_core) #define PRELOAD2(libname, libfuncname) \ int luaopen_##libfuncname (lua_State *L); \ lua_pushcfunction(L, luaopen_##libfuncname); \ lua_setfield(L, -2, #libname); luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); PRELOAD(linenoise) #if !defined(SLUA_DYNLINK) PRELOAD(lfs) //~ PRELOAD(lpeg) PRELOAD(minisock) //~ PRELOAD2(socket.core, socket_core) //~ PRELOAD2(mime.core, mime_core) PRELOAD(luazen) PRELOAD(luatweetnacl) PRELOAD(luaproc) #endif lua_pop(L, 1); /* remove _PRELOAD table */ ///------------------------------------------------------- } // luaL_openlibs()
int CorsixTH_lua_main_no_eval(lua_State *L) { // assert(_VERSION == LUA_VERSION) size_t iLength; lua_getglobal(L, "_VERSION"); const char* sVersion = lua_tolstring(L, -1, &iLength); if(iLength != strlen(LUA_VERSION) || strcmp(sVersion, LUA_VERSION) != 0) { lua_pushliteral(L, "Linked against a version of Lua different to the " "one used when compiling.\nPlease recompile CorsixTH against the " "same Lua version it is linked against."); return lua_error(L); } lua_pop(L, 1); // registry._CLEANUP = {} lua_newtable(L); lua_setfield(L, LUA_REGISTRYINDEX, "_CLEANUP"); // math.random* = Mersenne twister variant luaT_cpcall(L, luaopen_random, NULL); // package.preload["jit.opt"] = load(jit_opt_lua) // package.preload["jit.opt_inline"] = load(jit_opt_inline_lua) lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); luaL_loadbuffer(L, (const char*)jit_opt_lua, sizeof(jit_opt_lua), "jit/opt.luac"); lua_setfield(L, -2, "jit.opt"); luaL_loadbuffer(L, (const char*)jit_opt_inline_lua, sizeof(jit_opt_inline_lua), "jit/opt_inline.luac"); lua_setfield(L, -2, "jit.opt_inline"); lua_pop(L, 2); // if registry._LOADED.jit then // require"jit.opt".start() // else // print "Notice: ..." // end // (this could be done in Lua rather than here, but ideally the optimiser // should be turned on before any Lua code is loaded) lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); lua_getfield(L, -1, "jit"); if(lua_type(L, -1) == LUA_TNIL) { lua_pop(L, 2); lua_getglobal(L, "print"); lua_pushliteral(L, "Notice: LuaJIT not being used.\nConsider replacing" " Lua with LuaJIT to improve performance."); #ifdef CORSIX_TH_64BIT lua_pushliteral(L, " Note that there is not currently a 64 bit version" " of LuaJIT."); lua_concat(L, 2); #endif lua_call(L, 1, 0); } else { lua_pop(L, 2); lua_getglobal(L, "require"); lua_pushliteral(L, "jit.opt"); lua_call(L, 1, 1); lua_getfield(L, -1, "start"); lua_call(L, 0, 0); lua_pop(L, 1); } // Fill in package.preload table so that calls to require("X") from Lua // will call the appropriate luaopen_X function in C. #define PRELOAD(name, fn) \ luaT_execute(L, "package.preload." name " = ...", fn) PRELOAD("lfs", luaopen_lfs_ext); PRELOAD("lpeg", luaopen_lpeg); PRELOAD("rnc", luaopen_rnc); PRELOAD("TH", luaopen_th); PRELOAD("ISO_FS", luaopen_iso_fs); PRELOAD("persist", luaopen_persist); PRELOAD("sdl", luaopen_sdl); #undef PRELOAD // require "debug" (Harmless in Lua 5.1, useful in 5.2 for compatbility) luaT_execute(L, "require \"debug\""); // Check for --interpreter and run that instead of CorsixTH.lua bool bGotScriptFile = false; int iNArgs = lua_gettop(L); for(int i = 1; i <= iNArgs; ++i) { if(lua_type(L, i) == LUA_TSTRING) { size_t iLen; const char* sCmd = lua_tolstring(L, i, &iLen); if(iLen > 14 && memcmp(sCmd, "--interpreter=", 14) == 0) { lua_getglobal(L, "assert"); lua_getglobal(L, "loadfile"); lua_pushlstring(L, sCmd + 14, iLen - 14); bGotScriptFile = true; break; } } } // Code to try several variations on finding CorsixTH.lua: // CorsixTH.lua // CorsixTH/CorsixTH.lua // ../CorsixTH.lua // ../CorsixTH/CorsixTH.lua // ../../CorsixTH.lua // ../../CorsixTH/CorsixTH.lua // ../../../CorsixTH.lua // ../../../CorsixTH/CorsixTH.lua // It is simpler to write this in Lua than in C. const char sLuaCorsixTHLuaOld[] = "local name, sep, code = \"CorsixTH.lua\", package.config:sub(1, 1)\n" "local root = (... or \"\"):match(\"^(.*[\"..sep..\"])\") or \"\"\n" "code = loadfile(\"%s\"..name)\n" "if code then return code end\n" #ifdef __APPLE__ // Darrell: Search inside the bundle first. // There's probably a better way of doing this. #if defined(IS_CORSIXTH_APP) "code = loadfile(\"CorsixTH.app/Contents/Resources/\"..name)\n" "if code then return code end\n" #elif defined(IS_MAPEDIT_APP) "code = loadfile(\"MapEdit.app/Contents/Resources/\"..name)\n" "if code then return code end\n" #endif #endif "for num_dotdot = 0, 3 do\n" " for num_dir = 0, 1 do\n" " code = loadfile(root..(\"..\"..sep):rep(num_dotdot)..\n" " (\"CorsixTH\"..sep):rep(num_dir)..name)\n" " if code then return code end \n" " end \n" "end \n" "return loadfile(name)"; char sLuaCorsixTHLua[sizeof(sLuaCorsixTHLuaOld) + sizeof(path)]; sprintf(sLuaCorsixTHLua, sLuaCorsixTHLuaOld, path); //LOGI(sLuaCorsixTHLua); // return assert(loadfile"CorsixTH.lua")(...) if(!bGotScriptFile) { lua_getglobal(L, "assert"); luaL_loadbuffer(L, sLuaCorsixTHLua, strlen(sLuaCorsixTHLua), "@main.cpp (l_main bootstrap)"); if(lua_gettop(L) == 2) lua_pushnil(L); else lua_pushvalue(L, 1); } lua_call(L, 1, 2); lua_call(L, 2, 1); lua_insert(L, 1); return lua_gettop(L); }