//------------------------------------------------------------------------------
void CvDllScriptSystemUtility::PushReplayFromStream(lua_State* L, FDataStream& stream)
{
	CvReplayInfo* pkReplay = FNEW(CvReplayInfo(), c_eMPoolTypeGame, 0);
	if(pkReplay)
	{
		if(!pkReplay->read(stream))
		{
			delete pkReplay;
			pkReplay = NULL;
		}
	}

	if(pkReplay)
	{
		lua_createtable(L, 0, 0);
		const int t = lua_gettop(L);

		lua_pushstring(L, pkReplay->getMapScriptName());
		lua_setfield(L, t, "MapScriptName");

		lua_pushinteger(L, pkReplay->getActivePlayer());
		lua_setfield(L, t, "ActivePlayer");

		lua_pushinteger(L, pkReplay->getWorldSize());
		lua_setfield(L, t, "WorldSize");

		lua_pushinteger(L, pkReplay->getClimate());
		lua_setfield(L, t, "Climate");

		lua_pushinteger(L, pkReplay->getSeaLevel());
		lua_setfield(L, t, "SeaLevel");

		lua_pushinteger(L, pkReplay->getEra());
		lua_setfield(L, t, "Era");

		lua_pushinteger(L, pkReplay->getGameSpeed());
		lua_setfield(L, t, "GameSpeed");

		lua_pushinteger(L, pkReplay->getVictoryType());
		lua_setfield(L, t, "VictoryType");

		lua_pushinteger(L, pkReplay->getGameType());
		lua_setfield(L, t, "GameType");

		lua_pushinteger(L, pkReplay->getInitialTurn());
		lua_setfield(L, t, "InitialTurn");

		lua_pushinteger(L, pkReplay->getFinalTurn());
		lua_setfield(L, t, "FinalTurn");

		lua_pushinteger(L, pkReplay->getStartYear());
		lua_setfield(L, t, "StartYear");

		lua_pushstring(L, pkReplay->getFinalDate());
		lua_setfield(L, t, "FinalDate");

		lua_pushinteger(L, pkReplay->getCalendar());
		lua_setfield(L, t, "Calendar");

		lua_pushinteger(L, pkReplay->getNormalizedScore());
		lua_setfield(L, t, "NormalizedScore");

		lua_pushinteger(L, pkReplay->getMapHeight());
		lua_setfield(L, t, "MapHeight");

		lua_pushinteger(L, pkReplay->getMapWidth());
		lua_setfield(L, t, "MapWidth");

		const int numPlayers = pkReplay->getNumPlayers();
		lua_createtable(L, numPlayers, 0);

		const unsigned int uiNumDataSets = pkReplay->getNumPlayerDataSets();

		for(int iPlayer = 0; iPlayer < numPlayers; iPlayer++)
		{
			lua_createtable(L, 0, 0);
			const int playerIdx = lua_gettop(L);

			lua_pushinteger(L, pkReplay->getPlayerCivilization(iPlayer));
			lua_setfield(L, playerIdx, "Civilization");

			lua_pushinteger(L, pkReplay->getPlayerLeader(iPlayer));
			lua_setfield(L, playerIdx, "Leader");

			lua_pushinteger(L, pkReplay->getPlayerColor(iPlayer));
			lua_setfield(L, playerIdx, "PlayerColor");

			lua_pushinteger(L, pkReplay->getPlayerDifficulty(iPlayer));
			lua_setfield(L, playerIdx, "Difficulty");

			lua_pushstring(L , pkReplay->getPlayerLeaderName(iPlayer));
			lua_setfield(L, playerIdx, "LeaderName");

			lua_pushstring(L, pkReplay->getPlayerCivDescription(iPlayer));
			lua_setfield(L, playerIdx, "CivDescription");

			lua_pushstring(L, pkReplay->getPlayerShortCivDescription(iPlayer));
			lua_setfield(L, playerIdx, "CivShortDescription");

			lua_pushstring(L, pkReplay->getPlayerCivAdjective(iPlayer));
			lua_setfield(L, playerIdx, "CivAdjective");

			lua_createtable(L, 0, 0);
			int iFinalTurn = pkReplay->getFinalTurn();

			for(int iTurn = pkReplay->getInitialTurn(); iTurn <= iFinalTurn; ++iTurn)
			{
				lua_createtable(L, 0, 0);

				for(unsigned int uiDataSet = 0; uiDataSet < uiNumDataSets; ++uiDataSet)
				{
					int iValue = 0;
					if(pkReplay->getPlayerDataSetValue(iPlayer, uiDataSet, iTurn, iValue))
					{
						lua_pushinteger(L, iValue);
						lua_setfield(L, -2, pkReplay->getPlayerDataSetName(uiDataSet));
					}
				}

				lua_rawseti(L, -2, iTurn);
			}
			lua_setfield(L, playerIdx, "Scores");

			lua_rawseti(L, -2, iPlayer + 1);	//Adjust iPlayer to be 1-based instead of 0-based.
		}
		lua_setfield(L, t, "PlayerInfo");

		const int numReplayMessages = pkReplay->getNumReplayMessages();
		lua_createtable(L, numReplayMessages, 0);
		for(int i = 0; i < numReplayMessages; i++)
		{
			const CvReplayMessage* pkMessage = pkReplay->getReplayMessage(i);
			lua_createtable(L, 0, 0);

			lua_pushinteger(L, pkMessage->getTurn());
			lua_setfield(L, -2, "Turn");

			lua_pushinteger(L, pkMessage->getType());
			lua_setfield(L, -2, "Type");

			int iPlayer = pkMessage->getPlayer();
			lua_pushinteger(L, (iPlayer > -1)? iPlayer + 1 : iPlayer);	//Adjust iPlayer to match the 1-based index scheme.
			lua_setfield(L, -2, "Player");

			lua_pushstring(L, pkMessage->getText());
			lua_setfield(L, -2, "Text");

			const int numPlots = pkMessage->getNumPlots();
			lua_createtable(L, numPlots, 0);
			for(int iPlot = 0; iPlot < numPlots; ++iPlot)
			{
				int iPlotX = 0;
				int iPlotY = 0;
				pkMessage->getPlot(iPlot, iPlotX, iPlotY);

				lua_createtable(L, 0, 2);
				lua_pushinteger(L, iPlotX);
				lua_setfield(L, -2, "X");

				lua_pushinteger(L, iPlotY);
				lua_setfield(L, -2, "Y");

				lua_rawseti(L, -2, iPlot + 1);
			}
			lua_setfield(L, -2, "Plots");

			lua_rawseti(L, -2, i);
		}
		lua_setfield(L, t, "Messages");

		const int uiHeight = pkReplay->getMapHeight();
		const int uiWidth = pkReplay->getMapWidth();

		lua_createtable(L, uiHeight * uiWidth, 0);
		int idx = 1;
		for(int uiY = 0; uiY < uiHeight; ++uiY)
		{
			for(int uiX = 0; uiX < uiWidth; ++uiX)
			{
				lua_createtable(L, 0, 0);
				for(int i = pkReplay->getInitialTurn(); i <= pkReplay->getFinalTurn(); ++i)
				{
					CvReplayInfo::PlotState plotState;
					if(pkReplay->getPlotState(uiX, uiY, i, plotState))
					{
						lua_createtable(L, 6, 0);

						lua_pushinteger(L, plotState.m_ePlotType);
						lua_setfield(L, -2, "PlotType");

						lua_pushinteger(L, plotState.m_eTerrain);
						lua_setfield(L, -2, "TerrainType");

						lua_pushinteger(L, plotState.m_eFeature);
						lua_setfield(L, -2, "FeatureType");

						lua_pushinteger(L, plotState.m_bNEOfRiver);
						lua_setfield(L, -2, "NEOfRiver");

						lua_pushinteger(L, plotState.m_bWOfRiver);
						lua_setfield(L, -2, "WOfRiver");

						lua_pushinteger(L, plotState.m_bNWOfRiver);
						lua_setfield(L, -2, "NWOfRiver");

						lua_rawseti(L, -2, i);
					}
				}

				lua_rawseti(L, -2, idx++);
			}
		}
		lua_setfield(L, t, "Plots");

		delete pkReplay;
	}
}
template<> void ExtraInit<ElementInstancer>(lua_State* L, int metatable_index)
{
    lua_pushcfunction(L,ElementInstancernew);
    lua_setfield(L,metatable_index-1,"new");
}
示例#3
0
int luaopen_ex_core(lua_State *L)
{
  const char *name = lua_tostring(L, 1);
  int ex;
  const luaL_reg ex_iolib[] = {
    {"pipe",       ex_pipe},
#define ex_iofile_methods (ex_iolib + 1)
    {"lock",       ex_lock},
    {"unlock",     ex_lock},
    {0,0} };
  const luaL_reg ex_oslib[] = {
    /* environment */
    {"getenv",     ex_getenv},
    {"setenv",     ex_setenv},
    {"environ",    ex_environ},
    /* file system */
    {"access",     ex_access},
    {"getcwd",     ex_getcwd},
    {"chdir",      ex_chdir},
    {"chmod",      ex_chmod},
    {"mkdir",      ex_mkdir},
    {"remove",     ex_remove},
    {"dir",        ex_dir},
    {"dirent",     ex_dirent},
    {"copyfile",   ex_copyfile},
    {"movefile",   ex_movefile},
    {"touch",      ex_touch},
    /* process control */
    {"sleep",      ex_sleep},
    {"spawn",      ex_spawn},
    {0,0} };
  const luaL_reg ex_diriter_methods[] = {
    {"__gc",       diriter_close},
    {0,0} };
  const luaL_reg ex_process_methods[] = {
    {"__tostring", process_tostring},
#define ex_process_functions (ex_process_methods + 1)
    {"wait",       process_wait},
    {0,0} };
  /* diriter metatable */
  luaL_newmetatable(L, DIR_HANDLE);           /* . D */
  luaL_register(L, 0, ex_diriter_methods);    /* . D */
  /* proc metatable */
  luaL_newmetatable(L, PROCESS_HANDLE);       /* . P */
  luaL_register(L, 0, ex_process_methods);    /* . P */
  lua_pushvalue(L, -1);                       /* . P P */
  lua_setfield(L, -2, "__index");             /* . P */
  /* make all functions available via ex. namespace */
  luaL_register(L, name, ex_oslib);           /* . P ex */
  luaL_register(L, 0, ex_iolib);
  copyfields(L, ex_process_functions, -2, -1);
  ex = lua_gettop(L);
  /* extend the os table */
  lua_getglobal(L, "os");                     /* . os */
  if (lua_isnil(L, -1)) return luaL_error(L, "os not loaded");
  copyfields(L, ex_oslib, ex, -1);
  luaopen_os_path(L);
  /* extend the io table */
  lua_getglobal(L, "io");                     /* . io */
  if (lua_isnil(L, -1)) return luaL_error(L, "io not loaded");
  copyfields(L, ex_iolib, ex, -1);
  lua_getfield(L, ex, "pipe");                /* . io ex_pipe */
  newfenv(L, pipe_close);  /* create environment for 'popen' */
  lua_setfenv(L, -2);  /* set fenv for 'popen' */
  lua_pop(L, 1);  /* pop 'popen' */
  /* extend the io.file metatable */
  luaL_getmetatable(L, LUA_FILEHANDLE);       /* . F */
  if (lua_isnil(L, -1)) return luaL_error(L, "can't find FILE* metatable");
  copyfields(L, ex_iofile_methods, ex, -1);
  return 1;
}
示例#4
0
static void setfield (lua_State *L, const char *key, int value) {
  lua_pushinteger(L, value);
  lua_setfield(L, -2, key);
}
示例#5
0
int
main(void)
{
	lua_State * const L = luaL_newstate(); 

	luaL_openlibs(L);

	lua_newtable(L); /* magnet. */
	lua_newtable(L); /* magnet.cache. */
	lua_setfield(L, -2, "cache");
	lua_setfield(L, LUA_GLOBALSINDEX, "magnet");

	while (FCGI_Accept() >= 0)
	{
		assert(lua_gettop(L) == 0);

		if (magnet_get_script(L, getenv("SCRIPT_FILENAME")))
		{
			fputs("Status: 404\r\n\r\n", stdout);
			assert(lua_gettop(L) == 0);
			continue;
		}
		/**
		 * We want to create empty environment for our script. 
		 * 
		 * setmetatable({}, {__index = _G})
		 * 
		 * If a function symbol is not defined in our env,
		 * __index will look it up in the global env. 
		 *
		 * All variables created in the script-env will be thrown 
		 * away at the end of the script run. */

		/* Empty environment; will become parent to _G._G */
		lua_newtable(L);

		/* We have to overwrite the print function */
		lua_pushcfunction(L, magnet_print);
		lua_setfield(L, -2, "print");

		lua_newtable(L);                    /* The meta-table for the new env.          */
		lua_pushvalue(L, LUA_GLOBALSINDEX);

		lua_setfield(L,     -2, "__index"); /* { __index = _G }                         */
		lua_setmetatable(L, -2);            /* setmetatable({}, { __index = _G })       */
		lua_setfenv(L,      -2);            /* On the stack should be the modified env. */

		/* The top of the stack is the function from magnet_get_script() again. */
		if (lua_pcall(L, 0, 1, 0))
		{
			/* Unsure about this. */
			/* fprintf(stderr, "%s\n", lua_tostring(L, -1)); */
			fputs("Status: 503\r\n\r\n", stdout);
			fputs(lua_tostring(L, -1)  , stdout);

			/* Remove the error message. */
			lua_pop(L, 1);

			continue;
		}

		/* Remove the function copy from the stack. */
		lua_pop(L, 1);
		assert(lua_gettop(L) == 0);
	}

	lua_close(L);
	return EXIT_SUCCESS;
}	
示例#6
0
文件: playlist.c 项目: AsamQi/vlc
void luaopen_playlist( lua_State *L )
{
    lua_newtable( L );
    luaL_register( L, NULL, vlclua_playlist_reg );
    lua_setfield( L, -2, "playlist" );
}
示例#7
0
	std::string register_attacks_metatables(lua_State* L)
	{
		std::ostringstream cmd_out;

		// Create the unit attacks metatable.
		cmd_out << "Adding unit attacks metatable...\n";

		luaL_newmetatable(L, uattacksKey);
		lua_pushcfunction(L, impl_unit_attacks_get);
		lua_setfield(L, -2, "__index");
		lua_pushcfunction(L, impl_unit_attacks_set);
		lua_setfield(L, -2, "__newindex");
		lua_pushcfunction(L, impl_unit_attacks_len);
		lua_setfield(L, -2, "__len");
		lua_pushcfunction(L, impl_unit_attacks_iter);
		lua_setfield(L, -2, "__ipairs");
		lua_pushstring(L, uattacksKey);
		lua_setfield(L, -2, "__metatable");

		// Create the unit attack metatable
		luaL_newmetatable(L, uattackKey);
		lua_pushcfunction(L, impl_unit_attack_get);
		lua_setfield(L, -2, "__index");
		lua_pushcfunction(L, impl_unit_attack_set);
		lua_setfield(L, -2, "__newindex");
		lua_pushcfunction(L, impl_unit_attack_equal);
		lua_setfield(L, -2, "__eq");
		lua_pushcfunction(L, impl_unit_attack_collect);
		lua_setfield(L, -2, "__gc");
		lua_pushstring(L, uattackKey);
		lua_setfield(L, -2, "__metatable");
		lua_pushcfunction(L, impl_unit_attack_match);
		lua_setfield(L, -2, "matches");

		return cmd_out.str();
	}
示例#8
0
文件: utils.c 项目: ishikawawa/lev
void luv_set_loop(lua_State *L, uv_loop_t *loop) {
  lua_pushlightuserdata(L, loop);
  lua_setfield(L, LUA_REGISTRYINDEX, "loop");
}
示例#9
0
文件: utils.c 项目: ishikawawa/lev
void luv_set_ares_channel(lua_State *L, ares_channel channel) {
  lua_pushlightuserdata(L, channel);
  lua_setfield(L, LUA_REGISTRYINDEX, "ares_channel");
}
示例#10
0
void push_object_properties(lua_State *L, ObjectProperties *prop)
{
	lua_newtable(L);
	lua_pushnumber(L, prop->hp_max);
	lua_setfield(L, -2, "hp_max");
	lua_pushboolean(L, prop->physical);
	lua_setfield(L, -2, "physical");
	lua_pushboolean(L, prop->collideWithObjects);
	lua_setfield(L, -2, "collide_with_objects");
	lua_pushnumber(L, prop->weight);
	lua_setfield(L, -2, "weight");
	push_aabb3f(L, prop->collisionbox);
	lua_setfield(L, -2, "collisionbox");
	lua_pushlstring(L, prop->visual.c_str(), prop->visual.size());
	lua_setfield(L, -2, "visual");
	lua_pushlstring(L, prop->mesh.c_str(), prop->mesh.size());
	lua_setfield(L, -2, "mesh");
	push_v2f(L, prop->visual_size);
	lua_setfield(L, -2, "visual_size");

	lua_newtable(L);
	u16 i = 1;
	for (std::vector<std::string>::iterator it = prop->textures.begin();
			it != prop->textures.end(); ++it) {
		lua_pushlstring(L, it->c_str(), it->size());
		lua_rawseti(L, -2, i);
	}
	lua_setfield(L, -2, "textures");

	lua_newtable(L);
	i = 1;
	for (std::vector<video::SColor>::iterator it = prop->colors.begin();
			it != prop->colors.end(); ++it) {
		push_ARGB8(L, *it);
		lua_rawseti(L, -2, i);
	}
	lua_setfield(L, -2, "colors");

	push_v2s16(L, prop->spritediv);
	lua_setfield(L, -2, "spritediv");
	push_v2s16(L, prop->initial_sprite_basepos);
	lua_setfield(L, -2, "initial_sprite_basepos");
	lua_pushboolean(L, prop->is_visible);
	lua_setfield(L, -2, "is_visible");
	lua_pushboolean(L, prop->makes_footstep_sound);
	lua_setfield(L, -2, "makes_footstep_sound");
	lua_pushnumber(L, prop->automatic_rotate);
	lua_setfield(L, -2, "automatic_rotate");
	lua_pushnumber(L, prop->stepheight / BS);
	lua_setfield(L, -2, "stepheight");
	if (prop->automatic_face_movement_dir)
		lua_pushnumber(L, prop->automatic_face_movement_dir_offset);
	else
		lua_pushboolean(L, false);
	lua_setfield(L, -2, "automatic_face_movement_dir");
}
void
ngx_http_lua_inject_coroutine_api(ngx_log_t *log, lua_State *L)
{
    int         rc;

    /* new coroutine table */
    lua_createtable(L, 0 /* narr */, 14 /* nrec */);

    /* get old coroutine table */
    lua_getglobal(L, "coroutine");

    /* set running to the old one */
    lua_getfield(L, -1, "running");
    lua_setfield(L, -3, "running");

    lua_getfield(L, -1, "create");
    lua_setfield(L, -3, "_create");

    lua_getfield(L, -1, "resume");
    lua_setfield(L, -3, "_resume");

    lua_getfield(L, -1, "yield");
    lua_setfield(L, -3, "_yield");

    lua_getfield(L, -1, "status");
    lua_setfield(L, -3, "_status");

    /* pop the old coroutine */
    lua_pop(L, 1);

    lua_pushcfunction(L, ngx_http_lua_coroutine_create);
    lua_setfield(L, -2, "__create");

    lua_pushcfunction(L, ngx_http_lua_coroutine_resume);
    lua_setfield(L, -2, "__resume");

    lua_pushcfunction(L, ngx_http_lua_coroutine_yield);
    lua_setfield(L, -2, "__yield");

    lua_pushcfunction(L, ngx_http_lua_coroutine_status);
    lua_setfield(L, -2, "__status");

    lua_setglobal(L, "coroutine");

    /* inject coroutine APIs */
    {
        const char buf[] =
            "local keys = {'create', 'yield', 'resume', 'status'}\n"
            "local getfenv = getfenv\n"
            "for _, key in ipairs(keys) do\n"
               "local std = coroutine['_' .. key]\n"
               "local ours = coroutine['__' .. key]\n"
               "local raw_ctx = ngx._phase_ctx\n"
               "coroutine[key] = function (...)\n"
                    "local r = getfenv(0).__ngx_req\n"
                    "if r then\n"
                        "local ctx = raw_ctx(r)\n"
                        /* ignore header and body filters */
                        "if ctx ~= 0x020 and ctx ~= 0x040 then\n"
                            "return ours(...)\n"
                        "end\n"
                    "end\n"
                    "return std(...)\n"
                "end\n"
            "end\n"
            "local create, resume = coroutine.create, coroutine.resume\n"
            "coroutine.wrap = function(f)\n"
               "local co = create(f)\n"
               "return function(...) return select(2, resume(co, ...)) end\n"
            "end\n"
            "package.loaded.coroutine = coroutine";

#if 0
            "debug.sethook(function () collectgarbage() end, 'rl', 1)"
#endif
            ;

        rc = luaL_loadbuffer(L, buf, sizeof(buf) - 1, "=coroutine.wrap");
    }

    if (rc != 0) {
        ngx_log_error(NGX_LOG_ERR, log, 0,
                      "failed to load Lua code for coroutine.wrap(): %i: %s",
                      rc, lua_tostring(L, -1));

        lua_pop(L, 1);
        return;
    }

    rc = lua_pcall(L, 0, 0, 0);
    if (rc != 0) {
        ngx_log_error(NGX_LOG_ERR, log, 0,
                      "failed to run the Lua code for coroutine.wrap(): %i: %s",
                      rc, lua_tostring(L, -1));
        lua_pop(L, 1);
    }
}
示例#12
0
void dbg_lua_handle_command(struct dbg_state* state, void* ud, freed_bstring name, list_t* parameters)
{
    struct lua_debugst* ds;
    struct customarg_entry* carg;
    char* cstr;
    unsigned int i, k;
    int paramtbl;

    // Convert the name to lowercase.
    btolower(name.ref);
    cstr = bstr2cstr(name.ref, '0');

    // Loop through all of the modules.
    for (k = 0; k < list_size(&modules); k++)
    {
        ds = list_get_at(&modules, k);

        // Set stack top (I don't know why the top of the
        // stack is negative!)
        lua_settop(ds->state, 0);

        // Search handler table for entries.
        lua_getglobal(ds->state, HANDLER_TABLE_COMMANDS_NAME);
        lua_getfield(ds->state, -1, cstr);
        if (!lua_istable(ds->state, -1))
        {
            // No such entry.
            lua_pop(ds->state, 2);
            continue;
        }

        // Call the handler function.
        lua_getfield(ds->state, -1, HANDLER_FIELD_FUNCTION_NAME);
        dbg_lua_push_state(ds, state, ud);
        lua_newtable(ds->state);
        paramtbl = lua_gettop(ds->state);
        for (i = 0; i < list_size(parameters); i++)
        {
            carg = list_get_at(parameters, i);

            lua_newtable(ds->state);
            if (carg->type == DBG_CUSTOMARG_TYPE_PATH)
                lua_pushstring(ds->state, "PATH");
            else if (carg->type == DBG_CUSTOMARG_TYPE_PARAM)
                lua_pushstring(ds->state, "PARAM");
            else if (carg->type == DBG_CUSTOMARG_TYPE_STRING)
                lua_pushstring(ds->state, "STRING");
            else
                lua_pushstring(ds->state, "NUMBER");
            lua_setfield(ds->state, -2, "type");
            if (carg->type == DBG_CUSTOMARG_TYPE_PATH)
                lua_pushstring(ds->state, carg->path->data);
            else if (carg->type == DBG_CUSTOMARG_TYPE_PARAM)
                lua_pushstring(ds->state, carg->param->data);
            else if (carg->type == DBG_CUSTOMARG_TYPE_STRING)
                lua_pushstring(ds->state, carg->string->data);
            else
                lua_pushnumber(ds->state, carg->number);
            lua_setfield(ds->state, -2, "value");
            lua_rawseti(ds->state, paramtbl, i + 1);
        }
        if (lua_pcall(ds->state, 2, 0, 0) != 0)
        {
            printd(LEVEL_ERROR, "error: unable to call debugger command handler for '%s'.\n", name.ref->data);
            printd(LEVEL_ERROR, "%s\n", lua_tostring(ds->state, -1));
            bautodestroy(name);
            bcstrfree(cstr);
            lua_pop(ds->state, 2);
            list_iterator_stop(&modules);
            list_destroy(parameters);
            return;
        }

        bautodestroy(name);
        bcstrfree(cstr);
        lua_pop(ds->state, 2);
        list_iterator_stop(&modules);
        list_destroy(parameters);

        // The command may have started the virtual machine, check the
        // status of the VM and execute if needed.
        if (state->get_vm()->halted == false)
            vm_execute(state->get_vm(), NULL);

        return;
    }

    // There is no command to handle this.
    printd(LEVEL_ERROR, "no such command found.\n");

    // Clean up.
    list_destroy(parameters);
    bautodestroy(name);
    bcstrfree(cstr);
}
示例#13
0
	int Read(lua_State* L)
	{
		byte* ptr = Util::CheckHeader(L, "EQGS", ".mds");

		Header* header = (Header*)ptr;
		uint32 pos = sizeof(Header);

		lua_createtable(L, 0, 5); //to return

		const char* string_block = (const char*)&ptr[pos];
		pos += header->strings_len;

		//materials and their properties
		lua_createtable(L, header->material_count, 0);

		for (uint32 i = 1; i <= header->material_count; ++i)
		{
			Material* mat = (Material*)&ptr[pos];
			pos += sizeof(Material);

			lua_pushinteger(L, i);
			lua_createtable(L, mat->property_count, 2); //one table per material

			lua_pushstring(L, &string_block[mat->name_index]);
			lua_setfield(L, -2, "name");
			lua_pushstring(L, &string_block[mat->shader_index]);
			lua_setfield(L, -2, "shader");

			for (uint32 j = 1; j <= mat->property_count; ++j)
			{
				Property* prop = (Property*)&ptr[pos];
				pos += sizeof(Property);

				lua_pushinteger(L, j);
				lua_createtable(L, 0, 3);

				lua_pushstring(L, &string_block[prop->name_index]);
				lua_setfield(L, -2, "name");
				lua_pushinteger(L, prop->type);
				lua_setfield(L, -2, "type");
				if (prop->type == 0)
					lua_pushnumber(L, Util::FloatToDouble(prop->value.f));
				else
					lua_pushstring(L, &string_block[prop->value.i]);
				lua_setfield(L, -2, "value");

				lua_settable(L, -3);
			}

			lua_settable(L, -3);
		}

		lua_setfield(L, -2, "materials");

		//bones
		lua_createtable(L, header->bone_count, 0);

		for (uint32 i = 1; i <= header->bone_count; ++i)
		{
			Bone* bone = (Bone*)&ptr[pos];
			pos += sizeof(Bone);

			lua_pushinteger(L, i);
			lua_createtable(L, 0, 12);

			lua_pushstring(L, &string_block[bone->name_index]);
			lua_setfield(L, -2, "name");
			lua_pushinteger(L, bone->flag);
			lua_setfield(L, -2, "flag");

			lua_pushnumber(L, Util::FloatToDouble(bone->x));
			lua_setfield(L, -2, "x");
			lua_pushnumber(L, Util::FloatToDouble(bone->y));
			lua_setfield(L, -2, "y");
			lua_pushnumber(L, Util::FloatToDouble(bone->z));
			lua_setfield(L, -2, "z");
			lua_pushnumber(L, Util::FloatToDouble(bone->rot_x));
			lua_setfield(L, -2, "rot_x");
			lua_pushnumber(L, Util::FloatToDouble(bone->rot_y));
			lua_setfield(L, -2, "rot_y");
			lua_pushnumber(L, Util::FloatToDouble(bone->rot_z));
			lua_setfield(L, -2, "rot_z");
			lua_pushnumber(L, Util::FloatToDouble(bone->rot_w));
			lua_setfield(L, -2, "rot_w");
			lua_pushnumber(L, Util::FloatToDouble(bone->scale_x));
			lua_setfield(L, -2, "scale_x");
			lua_pushnumber(L, Util::FloatToDouble(bone->scale_y));
			lua_setfield(L, -2, "scale_y");
			lua_pushnumber(L, Util::FloatToDouble(bone->scale_z));
			lua_setfield(L, -2, "scale_z");

			if (bone->base_bone != 0xFFFFFFFF)
			{
				lua_pushinteger(L, bone->base_bone);
				lua_setfield(L, -2, "base_bone");
			}
			if (bone->next_bone != 0xFFFFFFFF)
			{
				lua_pushinteger(L, bone->next_bone);
				lua_setfield(L, -2, "next_bone");
			}

			lua_settable(L, -3);
		}

		lua_setfield(L, -2, "bones");

		/*int vert_count = 0;
		int tri_count = 0;
		int ba_count = 0;
		lua_newtable(L); //vertices, -3
		lua_newtable(L); //triangles, -2
		lua_newtable(L); //bone assignments, -1

		const int n = header->sub_count;
		for (int i = -1; i < n && i < 1; ++i)
		{
			SubHeader* subheader = (SubHeader*)&ptr[pos];
			pos += sizeof(SubHeader);

			for (uint32 j = 0; j < subheader->vertex_count; ++j)
			{
				Vertex* vert = (Vertex*)&ptr[pos];
				pos += sizeof(Vertex);

				lua_pushinteger(L, ++vert_count);
				lua_createtable(L, 0, 8);

				lua_pushnumber(L, Util::FloatToDouble(vert->x));
				lua_setfield(L, -2, "x");
				lua_pushnumber(L, Util::FloatToDouble(vert->y));
				lua_setfield(L, -2, "y");
				lua_pushnumber(L, Util::FloatToDouble(vert->z));
				lua_setfield(L, -2, "z");
				lua_pushnumber(L, Util::FloatToDouble(vert->i));
				lua_setfield(L, -2, "i");
				lua_pushnumber(L, Util::FloatToDouble(vert->j));
				lua_setfield(L, -2, "j");
				lua_pushnumber(L, Util::FloatToDouble(vert->k));
				lua_setfield(L, -2, "k");
				lua_pushnumber(L, Util::FloatToDouble(vert->u));
				lua_setfield(L, -2, "u");
				lua_pushnumber(L, Util::FloatToDouble(vert->v));
				lua_setfield(L, -2, "v");

				lua_settable(L, -5);
			}

			for (uint32 j = 0; j < subheader->triangle_count; ++j)
			{
				Triangle* tri = (Triangle*)&ptr[pos];
				pos += sizeof(Triangle);

				lua_pushinteger(L, ++tri_count);
				lua_createtable(L, 3, 2);

				lua_pushinteger(L, 1);
				lua_pushinteger(L, tri->index[0]);
				lua_settable(L, -3);
				lua_pushinteger(L, 2);
				lua_pushinteger(L, tri->index[1]);
				lua_settable(L, -3);
				lua_pushinteger(L, 3);
				lua_pushinteger(L, tri->index[2]);
				lua_settable(L, -3);
				lua_pushinteger(L, tri->material);
				lua_setfield(L, -2, "material");
				lua_pushinteger(L, tri->flag);
				lua_setfield(L, -2, "flag");

				lua_settable(L, -4);
			}

			for (uint32 j = 0; j < subheader->bone_assignment_count; ++j)
			{
				BoneAssignment* ba = (BoneAssignment*)&ptr[pos];
				pos += sizeof(BoneAssignment);

				lua_pushinteger(L, ++ba_count);
				lua_createtable(L, ba->count * 2, 1);

				lua_pushinteger(L, ba->count);
				lua_setfield(L, -2, "count");

				for (uint32 j = 0; j < ba->count; ++j)
				{
					lua_pushinteger(L, j * 2 + 1);
					lua_pushinteger(L, ba->weights[j].bone);
					lua_settable(L, -3);
					lua_pushinteger(L, j * 2 + 2);
					lua_pushnumber(L, Util::FloatToDouble(ba->weights[j].weight));
					lua_settable(L, -3);
				}

				lua_settable(L, -3);
			}
		}

		lua_setfield(L, -4, "bone_assignments");
		lua_setfield(L, -3, "triangles");
		lua_setfield(L, -2, "vertices");

		return 1;*/
		
		//sub sections -- we only care about the first one (for now?)
		SubHeader* subheader = (SubHeader*)&ptr[pos];
		pos += sizeof(SubHeader);

		//vertices -- always the smaller type?
		lua_createtable(L, subheader->vertex_count, 0);

		for (uint32 i = 1; i <= subheader->vertex_count; ++i)
		{
			Vertex* vert = (Vertex*)&ptr[pos];
			pos += sizeof(Vertex);

			lua_pushinteger(L, i);
			lua_createtable(L, 0, 8);

			lua_pushnumber(L, Util::FloatToDouble(vert->x));
			lua_setfield(L, -2, "x");
			lua_pushnumber(L, Util::FloatToDouble(vert->y));
			lua_setfield(L, -2, "y");
			lua_pushnumber(L, Util::FloatToDouble(vert->z));
			lua_setfield(L, -2, "z");
			lua_pushnumber(L, Util::FloatToDouble(vert->i));
			lua_setfield(L, -2, "i");
			lua_pushnumber(L, Util::FloatToDouble(vert->j));
			lua_setfield(L, -2, "j");
			lua_pushnumber(L, Util::FloatToDouble(vert->k));
			lua_setfield(L, -2, "k");
			lua_pushnumber(L, Util::FloatToDouble(vert->u));
			lua_setfield(L, -2, "u");
			lua_pushnumber(L, Util::FloatToDouble(vert->v));
			lua_setfield(L, -2, "v");

			lua_settable(L, -3);
		}

		lua_setfield(L, -2, "vertices");

		//triangles
		lua_createtable(L, subheader->triangle_count, 0);

		for (uint32 i = 1; i <= subheader->triangle_count; ++i)
		{
			Triangle* tri = (Triangle*)&ptr[pos];
			pos += sizeof(Triangle);

			lua_pushinteger(L, i);
			lua_createtable(L, 3, 2);

			lua_pushinteger(L, 1);
			lua_pushinteger(L, tri->index[0]);
			lua_settable(L, -3);
			lua_pushinteger(L, 2);
			lua_pushinteger(L, tri->index[1]);
			lua_settable(L, -3);
			lua_pushinteger(L, 3);
			lua_pushinteger(L, tri->index[2]);
			lua_settable(L, -3);
			lua_pushinteger(L, tri->material);
			lua_setfield(L, -2, "material");
			lua_pushinteger(L, tri->flag);
			lua_setfield(L, -2, "flag");

			lua_settable(L, -3);
		}

		lua_setfield(L, -2, "triangles");

		//bone assignments
		lua_createtable(L, subheader->bone_assignment_count, 0);

		for (uint32 i = 1; i<= subheader->bone_assignment_count; ++i)
		{
			BoneAssignment* ba = (BoneAssignment*)&ptr[pos];
			pos += sizeof(BoneAssignment);

			lua_pushinteger(L, i);
			lua_createtable(L, ba->count * 2, 1);

			lua_pushinteger(L, ba->count);
			lua_setfield(L, -2, "count");

			for (uint32 j = 0; j < ba->count; ++j)
			{
				lua_pushinteger(L, j * 2 + 1);
				lua_pushinteger(L, ba->weights[j].bone);
				lua_settable(L, -3);
				lua_pushinteger(L, j * 2 + 2);
				lua_pushnumber(L, Util::FloatToDouble(ba->weights[j].weight));
				lua_settable(L, -3);
			}

			lua_settable(L, -3);
		}
		
		lua_setfield(L, -2, "bone_assignments");

		return 1;
	}
示例#14
0
文件: worker.c 项目: butland/alilua
int worker_process(epdata_t *epd, int thread_at)
{
    //printf("worker_process\n");
    working_at_fd = epd->fd;
    //network_send_error(epd, 503, "Lua Error: main function not found !!!");return 0;
    //network_send(epd, "aaa", 3);network_be_end(epd);return 0;

    if(epd->ssl && !epd->ssl_verify) {
        network_send_error(epd, 400, "No required SSL certificate was send");
        return 0;
    }

    lua_State *L = epd->L;

    if(!L) {
        epd->L = new_lua_thread(_L);

        if(!epd->L) {
            network_send_error(epd, 503, "Lua Error: Thread pool full !!!");
            LOGF(ERR, "Lua Error: Thread pool full !!!");
            return 0;
        }

        lua_pushlightuserdata(epd->L, epd);
        lua_setglobal(epd->L, "__epd__");

        L = epd->L;
    }

    lua_getglobal(L, "process");

    update_timeout(epd->timeout_ptr, STEP_PROCESS_TIMEOUT + 100);

    int init_tables = 0;
    char *pt1 = NULL, *pt2 = NULL, *t1 = NULL, *t2 = NULL, *t3 = NULL;

    int is_form_post = 0;
    char *cookies = NULL;
    pt1 = epd->headers;
    int i = 0;

    epd->uri = NULL;
    epd->host = NULL;
    epd->query = NULL;
    epd->http_ver = NULL;
    epd->referer = NULL;
    epd->user_agent = NULL;
    epd->if_modified_since = NULL;

    //epd->start_time = longtime();

    while(t1 = strtok_r(pt1, "\n", &pt1)) {
        if(++i == 1) { /// first line
            t2 = strtok_r(t1, " ", &t1);
            t3 = strtok_r(t1, " ", &t1);
            epd->http_ver = strtok_r(t1, " ", &t1);

            if(!epd->http_ver) {
                return 1;

            } else {
                if(init_tables == 0) {
                    lua_createtable(L, 0, 20); //headers
                }
            }

            int len = strlen(epd->http_ver);

            if(epd->http_ver[len - 1] == 13) { // CR == 13
                epd->http_ver[len - 1] = '\0';
            }

            if(t2 && t3) {
                for(t1 = t2 ; *t1 ; *t1 = toupper(*t1), t1++);

                epd->method = t2;
                lua_pushstring(L, t2);
                lua_setfield(L, -2, "method");
                t1 = strtok_r(t3, "?", &t3);
                t2 = strtok_r(t3, "?", &t3);
                epd->uri = t1;
                lua_pushstring(L, t1);
                lua_setfield(L, -2, "uri");

                if(t2) {
                    epd->query = (t2 - 1);
                    epd->query[0] = '?';
                    lua_pushstring(L, epd->query);
                    lua_setfield(L, -2, "query");
                }
            }

            continue;
        }

        t2 = strtok_r(t1, ":", &t1);

        if(t2) {
            if(t2[0] == '\r') {
                break;
            }

            for(t3 = t2; *t3; ++t3) {
                *t3 = *t3 >= 'A' && *t3 <= 'Z' ? *t3 | 0x60 : *t3;
            }

            t3 = t2 + strlen(t2) + 1; //strtok_r ( t1, ":", &t1 )

            if(t3) {
                int len = strlen(t3);

                if(t3[len - 1] == 13) { /// 13 == CR
                    t3[len - 1] = '\0';
                    len -= 1;
                }

                if(len < 1) {
                    break;
                }

                lua_pushstring(L, t3 + (t3[0] == ' ' ? 1 : 0));
                lua_setfield(L, -2, t2);

                /// check content-type
                if(t2[0] == 'h' && epd->host == NULL && strcmp(t2, "host") == 0) {
                    char *_t = strstr(t3, ":");

                    if(_t) {
                        _t[0] = '\0';
                    }

                    epd->host = t3 + (t3[0] == ' ' ? 1 : 0);

                } else if(t2[1] == 'o' && strcmp(t2, "content-type") == 0) {
                    if(stristr(t3, "x-www-form-urlencoded", len)) {
                        is_form_post = 1;

                    } else if(stristr(t3, "multipart/form-data", len)) {
                        epd->boundary = (char *)stristr(t3, "boundary=", len - 2);

                        if(epd->boundary) {
                            epd->boundary += 9;
                        }
                    }

                } else if(!cookies && t2[1] == 'o' && strcmp(t2, "cookie") == 0) {
                    cookies = t3 + (t3[0] == ' ' ? 1 : 0);

                } else if(!epd->user_agent && t2[1] == 's' && strcmp(t2, "user-agent") == 0) {
                    epd->user_agent = t3 + (t3[0] == ' ' ? 1 : 0);

                } else if(!epd->referer && t2[1] == 'e' && strcmp(t2, "referer") == 0) {
                    epd->referer = t3 + (t3[0] == ' ' ? 1 : 0);

                } else if(!epd->if_modified_since && t2[1] == 'f' && strcmp(t2, "if-modified-since") == 0) {
                    epd->if_modified_since = t3 + (t3[0] == ' ' ? 1 : 0);
                }
            }
        }
    }

    const char *client_ip = cached_ntoa(epd->client_addr);
    lua_pushstring(L, client_ip);
    lua_setfield(L, -2, "remote-addr");
    int l = sizeof(struct sockaddr);
    struct sockaddr_in addr;
    getsockname(epd->fd, (struct sockaddr *) &addr, &l);
    lua_pushstring(L, cached_ntoa(addr.sin_addr));
    lua_setfield(L, -2, "server-addr");

    lua_setglobal(L, "headers");

    lua_createtable(L, 0, 20); /// _GET

    if(epd->query) { /// parse query string /?a=1&b=2
        char *last = NULL;
        int plen = 0;
        int qlen = strlen(epd->query) - 1;

        t1 = (char *)strsplit(epd->query + 1, qlen, "&", &last, &plen);
        char kk[256] = {0};

        while(t1) {
            char *last2 = NULL;
            int plen2 = 0;
            int plen3 = 0;

            t2 = (char *)strsplit(t1, plen, "=", &last2, &plen2);
            t3 = (char *)strsplit(t1, plen, "=", &last2, &plen3);

            if(t2 && plen2 > 0 && plen3 > 0 && plen2 <= 4096 && plen3 <= 4096) {
                size_t dlen;
                u_char *p;
                u_char *src, *dst;

                p = (u_char *)&buf_4096;
                p[0] = '\0';
                dst = p;
                dlen = urldecode(&p, (u_char **)&t3, plen3, RAW_UNESCAPE_URL);
                lua_pushlstring(L, (char *) p, dlen);

                p[0] = '\0';
                dst = p;

                dlen = urldecode(&dst, (u_char **)&t2, plen2, RAW_UNESCAPE_URL);
                p[dlen] = '\0';
                lua_setfield(L, -2, p);
            }

            t1 = (char *)strsplit(epd->query + 1, qlen, "&", &last, &plen);
        }
    }

    lua_setglobal(L, "_GET");

    lua_createtable(L, 0, 20); /// _COOKIE

    if(cookies) {
        while(t1 = strtok_r(cookies, ";", &cookies)) {
            t2 = strtok_r(t1, "=", &t1);
            t3 = strtok_r(t1, "=", &t1);

            if(t2 && t3 && strlen(t2) > 0 && strlen(t3) > 0) {
                size_t len, dlen;
                u_char *p;
                u_char *src, *dst;
                len = strlen(t3);
                p = malloc(len);
                p[0] = '\0';
                dst = p;
                dlen = urldecode(&dst, (u_char **)&t3, len, RAW_UNESCAPE_URL);
                lua_pushlstring(L, (char *) p, dlen);

                len = strlen(t2);

                if(len > 4096) {
                    free(p);
                    p = malloc(len);
                }

                p[0] = '\0';
                dst = p;

                dlen = urldecode(&dst, (u_char **)&t2, len, RAW_UNESCAPE_URL);
                p[dlen] = '\0';
                lua_setfield(L, -2, p + (p[0] == ' ' ? 1 : 0));
                free(p);
            }
        }
    }

    lua_setglobal(L, "_COOKIE");

    lua_pushnil(L);
    lua_setglobal(L, "__body_buf");

    epd->vhost_root = get_vhost_root(epd->host, &epd->vhost_root_len);

    memcpy(buf_4096, epd->vhost_root, epd->vhost_root_len + 1);
    sprintf(buf_4096 + epd->vhost_root_len + 1, "?.lua;%s/lua-libs/?.lua;", process_chdir);

    lua_pushstring(L, buf_4096);
    lua_getglobal(L, "package");
    lua_insert(L, -2); //-1 bufres -2 package
    lua_setfield(L, -2, "path"); //-1: path -2: package
    lua_pop(L, 1); //void

    lua_pushlstring(L, epd->vhost_root, epd->vhost_root_len); /// host root

    lua_setglobal(L, "__root");

    lua_pushstring(L, epd->vhost_root + epd->vhost_root_len); /// index-route.lua file

    epd->iov[0].iov_base = NULL;
    epd->iov[0].iov_len = 0;
    epd->iov[1].iov_base = NULL;
    epd->iov[1].iov_len = 0;

    lua_routed = 0;

    if(lua_resume(L, 1) == LUA_ERRRUN && lua_isstring(L, -1)) {
        LOGF(ERR, "Lua:error %s", lua_tostring(L, -1));
        network_send_error(epd, 503, lua_tostring(L, -1));
        lua_pop(L, 1);
    }

    return 0;
}
示例#15
0
static int
ngx_http_lua_ngx_timer_at(lua_State *L)
{
    int                      nargs, co_ref;
    u_char                  *p;
    lua_State               *vm;  /* the main thread */
    lua_State               *co;
    ngx_msec_t               delay;
    ngx_event_t             *ev = NULL;
    ngx_http_request_t      *r;
    ngx_connection_t        *saved_c = NULL;
    ngx_http_lua_ctx_t      *ctx;
#if 0
    ngx_http_connection_t   *hc;
#endif

    ngx_http_lua_timer_ctx_t      *tctx = NULL;
    ngx_http_lua_main_conf_t      *lmcf;
#if 0
    ngx_http_core_main_conf_t     *cmcf;
#endif

    nargs = lua_gettop(L);
    if (nargs < 2) {
        return luaL_error(L, "expecting at least 2 arguments but got %d",
                          nargs);
    }

    delay = (ngx_msec_t) (luaL_checknumber(L, 1) * 1000);

    luaL_argcheck(L, lua_isfunction(L, 2) && !lua_iscfunction(L, 2), 2,
                 "Lua function expected");

    r = ngx_http_lua_get_req(L);
    if (r == NULL) {
        return luaL_error(L, "no request");
    }

    ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);

    if (ngx_exiting && delay > 0) {
        lua_pushnil(L);
        lua_pushliteral(L, "process exiting");
        return 2;
    }

    lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);

    if (lmcf->pending_timers >= lmcf->max_pending_timers) {
        lua_pushnil(L);
        lua_pushliteral(L, "too many pending timers");
        return 2;
    }

    if (lmcf->watcher == NULL) {
        /* create the watcher fake connection */

        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
                       "lua creating fake watcher connection");

        if (ngx_cycle->files) {
            saved_c = ngx_cycle->files[0];
        }

        lmcf->watcher = ngx_get_connection(0, ngx_cycle->log);

        if (ngx_cycle->files) {
            ngx_cycle->files[0] = saved_c;
        }

        if (lmcf->watcher == NULL) {
            return luaL_error(L, "no memory");
        }

        /* to work around the -1 check in ngx_worker_process_cycle: */
        lmcf->watcher->fd = (ngx_socket_t) -2;

        lmcf->watcher->idle = 1;
        lmcf->watcher->read->handler = ngx_http_lua_abort_pending_timers;
        lmcf->watcher->data = lmcf;
    }

    vm = ngx_http_lua_get_lua_vm(r, ctx);

    co = lua_newthread(vm);

    /* L stack: time func [args] thread */

    ngx_http_lua_probe_user_coroutine_create(r, L, co);

    lua_createtable(co, 0, 0);  /* the new globals table */

    /* co stack: global_tb */

    lua_createtable(co, 0, 1);  /* the metatable */
    ngx_http_lua_get_globals_table(co);
    lua_setfield(co, -2, "__index");
    lua_setmetatable(co, -2);

    /* co stack: global_tb */

    ngx_http_lua_set_globals_table(co);

    /* co stack: <empty> */

    dd("stack top: %d", lua_gettop(L));

    lua_xmove(vm, L, 1);    /* move coroutine from main thread to L */

    /* L stack: time func [args] thread */
    /* vm stack: empty */

    lua_pushvalue(L, 2);    /* copy entry function to top of L*/

    /* L stack: time func [args] thread func */

    lua_xmove(L, co, 1);    /* move entry function from L to co */

    /* L stack: time func [args] thread */
    /* co stack: func */

    ngx_http_lua_get_globals_table(co);
    lua_setfenv(co, -2);

    /* co stack: func */

    lua_pushlightuserdata(L, &ngx_http_lua_coroutines_key);
    lua_rawget(L, LUA_REGISTRYINDEX);

    /* L stack: time func [args] thread corountines */

    lua_pushvalue(L, -2);

    /* L stack: time func [args] thread coroutines thread */

    co_ref = luaL_ref(L, -2);
    lua_pop(L, 1);

    /* L stack: time func [args] thread */

    if (nargs > 2) {
        lua_pop(L, 1);  /* L stack: time func [args] */
        lua_xmove(L, co, nargs - 2);  /* L stack: time func */

        /* co stack: func [args] */
    }

    p = ngx_alloc(sizeof(ngx_event_t) + sizeof(ngx_http_lua_timer_ctx_t),
                  r->connection->log);
    if (p == NULL) {
        goto nomem;
    }

    ev = (ngx_event_t *) p;

    ngx_memzero(ev, sizeof(ngx_event_t));

    p += sizeof(ngx_event_t);

    tctx = (ngx_http_lua_timer_ctx_t *) p;

    tctx->premature = 0;
    tctx->co_ref = co_ref;
    tctx->co = co;
    tctx->main_conf = r->main_conf;
    tctx->srv_conf = r->srv_conf;
    tctx->loc_conf = r->loc_conf;
    tctx->lmcf = lmcf;

    tctx->pool = ngx_create_pool(128, ngx_cycle->log);
    if (tctx->pool == NULL) {
        goto nomem;
    }

    if (r->connection) {
        tctx->listening = r->connection->listening;

    } else {
        tctx->listening = NULL;
    }

    if (r->connection->addr_text.len) {
        tctx->client_addr_text.data = ngx_palloc(tctx->pool,
                                                 r->connection->addr_text.len);
        if (tctx->client_addr_text.data == NULL) {
            goto nomem;
        }

        ngx_memcpy(tctx->client_addr_text.data, r->connection->addr_text.data,
                   r->connection->addr_text.len);
        tctx->client_addr_text.len = r->connection->addr_text.len;

    } else {
        tctx->client_addr_text.len = 0;
        tctx->client_addr_text.data = NULL;
    }

    if (ctx && ctx->vm_state) {
        tctx->vm_state = ctx->vm_state;
        tctx->vm_state->count++;

    } else {
        tctx->vm_state = NULL;
    }

    ev->handler = ngx_http_lua_timer_handler;
    ev->data = tctx;
    ev->log = ngx_cycle->log;

    lmcf->pending_timers++;

    ngx_add_timer(ev, delay);

    lua_pushinteger(L, 1);
    return 1;

nomem:

    if (tctx && tctx->pool) {
        ngx_destroy_pool(tctx->pool);
    }

    if (ev) {
        ngx_free(ev);
    }

    lua_pushlightuserdata(L, &ngx_http_lua_coroutines_key);
    lua_rawget(L, LUA_REGISTRYINDEX);
    luaL_unref(L, -1, co_ref);

    return luaL_error(L, "no memory");
}
static void
ts_lua_inject_client_response_get_status_api(lua_State *L)
{
    lua_pushcfunction(L, ts_lua_client_response_header_get_status);
    lua_setfield(L, -2, "get_status");
}
示例#17
0
TMODENTRY int luaopen_tek_lib_exec(lua_State *L)
{
	struct TExecBase *TExecBase;
	struct LuaExecTask *lexec;
	TTAGITEM tags[2];
	
	luaL_newmetatable(L, TEK_LIB_EXEC_CLASSNAME);
	tek_lua_register(L, NULL, tek_lib_exec_methods, 0);
	/* execmeta */

#if defined(ENABLE_TASKS)
	luaL_newmetatable(L, TEK_LIB_TASK_CLASSNAME);
	/* execmeta, taskmeta */
	lua_pushvalue(L, -2);
	/* execmeta, taskmeta, execmeta */
	tek_lua_register(L, NULL, tek_lib_exec_child_methods, 1);
	lua_pushvalue(L, -1);
	/* execmeta, taskmeta, taskmeta */
	lua_setfield(L, -2, "__index");
	lua_pop(L, 1);
	/* execmeta */
#endif
	
	lexec = (struct LuaExecTask *)lua_newuserdata(L, sizeof(struct LuaExecTask));
	/* execmeta, luaexec */
	lexec->exec = TNULL;
	
	lua_pushvalue(L, -1);
	/* execmeta, luaexec, luaexec */
	lua_pushvalue(L, -3);
	/* execmeta, luaexec, luaexec, execmeta */
	tek_lua_register(L, "tek.lib.exec", tek_lib_exec_funcs, 2);
	/* execmeta, luaexec, libtab */

	lua_pushvalue(L, -2);
	/* execmeta, luaexec, libtab, libtab */
	lua_pushvalue(L, -4);
	/* execmeta, luaexec, libtab, libtab, execmeta */
	lua_remove(L, -4);
	lua_remove(L, -4);
	/* libtab, libtab, execmeta */

	lua_setmetatable(L, -2);
	/* libtab, libtab */
	lua_setfield(L, -2, "base");
	/* libtab */

	tags[0].tti_Tag = TExecBase_ModInit;
	tags[0].tti_Value = (TTAG) tek_lib_exec_initmodules;
	tags[1].tti_Tag = TTAG_DONE;

	lexec->task = TEKCreate(tags);
	if (lexec->task == TNULL)
		luaL_error(L, "Failed to initialize TEKlib");
	lexec->exec = TExecBase = (struct TExecBase *)TGetExecBase(lexec->task);

#if defined(ENABLE_TASKS)
	if (!tek_lib_exec_init_link_to_parent(L, lexec))
	{
		lua_pop(L, 1);
		return 0;
	}
#endif

	return 1;
}
示例#18
0
LUA_API int luaopen_openssl(lua_State*L)
{
    char * config_filename;
#ifdef ENABLE_CRYPTO_THREAD 
	CRYPTO_thread_setup();
	CRYPTO_lock(CRYPTO_LOCK,CRYPTO_LOCK_ERR,__FILE__,__LINE__);
#endif
    if(g_init==0)
    {
        g_init =  1;
		

        OpenSSL_add_all_ciphers();
        OpenSSL_add_all_digests();
		SSL_library_init();

        ERR_load_ERR_strings();
        ERR_load_crypto_strings();
        ERR_load_EVP_strings();
		ERR_load_SSL_strings();

		ENGINE_load_dynamic();
		ENGINE_load_openssl();
    }
#ifdef ENABLE_CRYPTO_THREAD 
	CRYPTO_lock(CRYPTO_UNLOCK,CRYPTO_LOCK_ERR,__FILE__,__LINE__);
#endif
    /* Determine default SSL configuration file */
    config_filename = getenv("OPENSSL_CONF");
    if (config_filename == NULL) {
        config_filename = getenv("SSLEAY_CONF");
    }

    /* default to 'openssl.cnf' if no environment variable is set */
    if (config_filename == NULL) {
        snprintf(default_ssl_conf_filename, sizeof(default_ssl_conf_filename), "%s/%s",
                 X509_get_default_cert_area(),
                 "openssl.cnf");
    } else {
        strncpy(default_ssl_conf_filename, config_filename, sizeof(default_ssl_conf_filename));
    }

    openssl_register_pkey(L);
    openssl_register_x509(L);
    openssl_register_csr(L);
    openssl_register_digest(L);
    openssl_register_cipher(L);
    openssl_register_sk_x509(L);
    openssl_register_bio(L);
    openssl_register_crl(L);
#ifdef OPENSSL_HAVE_TS
    openssl_register_ts(L);
#endif
    openssl_register_conf(L);
    openssl_register_pkcs7(L);
    openssl_register_misc(L);
	openssl_register_engine(L);
	openssl_register_ssl(L);
	openssl_register_ocsp(L);

#if LUA_VERSION_NUM==501
    luaL_register(L,"openssl",eay_functions);
#elif LUA_VERSION_NUM==502
    lua_newtable(L);
    luaL_setfuncs(L, eay_functions, 0);
#endif
	setNamedIntegers(L, consts);

	/* third part */
	luaopen_bn(L);
	lua_setfield(L, -2, "bn");

    return 1;
}
示例#19
0
void open_oslibext(lua_State * L)
{

    find_env(L);

    lua_getglobal(L, "os");
    lua_pushcfunction(L, ex_sleep);
    lua_setfield(L, -2, "sleep");
    lua_pushliteral(L, OS_PLATTYPE);
    lua_setfield(L, -2, "type");
    lua_pushliteral(L, OS_PLATNAME);
    lua_setfield(L, -2, "name");
    lua_pushcfunction(L, ex_uname);
    lua_setfield(L, -2, "uname");
#if (! defined (_WIN32))  && (! defined (__SUNOS__))
    lua_pushcfunction(L, os_times);
    lua_setfield(L, -2, "times");
#endif
#if ! defined (__SUNOS__)
    lua_pushcfunction(L, os_gettimeofday);
    lua_setfield(L, -2, "gettimeofday");
#endif

    lua_pushcfunction(L, os_setenv);
    lua_setfield(L, -2, "setenv");
    lua_pushcfunction(L, os_exec);
    lua_setfield(L, -2, "exec");
    lua_pushcfunction(L, os_spawn);
    lua_setfield(L, -2, "spawn");
    lua_pushcfunction(L, os_execute);
    lua_setfield(L, -2, "execute");
    lua_pushcfunction(L, os_tmpdir);
    lua_setfield(L, -2, "tmpdir");

    lua_pop(L, 1);              /* pop the table */
}
示例#20
0
int gt_lua_open_encseq(lua_State *L)
{
#ifndef NDEBUG
  int stack_size;
#endif
  gt_assert(L);
#ifndef NDEBUG
  stack_size = lua_gettop(L);
#endif
  luaL_newmetatable(L, ENCSEQ_METATABLE);
  lua_pushvalue(L, -1); /* duplicate the metatable */
  lua_setfield(L, -2, "__index");
  lua_pushstring(L, "__gc");
  lua_pushcfunction(L, encseq_lua_delete);
  lua_settable(L, -3);
  luaL_register(L, NULL, encseq_lib_m);
  gt_lua_export_metatable(L, ENCSEQ_METATABLE);

  luaL_newmetatable(L, ENCSEQ_BUFFER_METATABLE);
  lua_pushcfunction(L, encseq_lua_index_buffer);
  lua_setfield(L, -2, "__index");
  lua_pushstring(L, "__gc");
  lua_pushcfunction(L, encseq_lua_delete_buffer);
  lua_settable(L, -3);
  lua_pop(L, 1);

  luaL_newmetatable(L, ENCSEQ_READER_METATABLE);
  lua_pushvalue(L, -1); /* duplicate the metatable */
  lua_setfield(L, -2, "__index");
  lua_pushstring(L, "__gc");
  lua_pushcfunction(L, encseq_reader_lua_delete);
  lua_settable(L, -3);
  luaL_register(L, NULL, encseq_reader_lib_m);
  gt_lua_export_metatable(L, ENCSEQ_READER_METATABLE);

  luaL_newmetatable(L, ENCSEQ_ENCODER_METATABLE);
  lua_pushvalue(L, -1); /* duplicate the metatable */
  lua_setfield(L, -2, "__index");
  lua_pushstring(L, "__gc");
  lua_pushcfunction(L, encseq_encoder_lua_delete);
  lua_settable(L, -3);
  luaL_register(L, NULL, encseq_encoder_lib_m);
  gt_lua_export_metatable(L, ENCSEQ_ENCODER_METATABLE);

  luaL_newmetatable(L, ENCSEQ_LOADER_METATABLE);
  lua_pushvalue(L, -1); /* duplicate the metatable */
  lua_setfield(L, -2, "__index");
  lua_pushstring(L, "__gc");
  lua_pushcfunction(L, encseq_loader_lua_delete);
  lua_settable(L, -3);
  luaL_register(L, NULL, encseq_loader_lib_m);
  gt_lua_export_metatable(L, ENCSEQ_LOADER_METATABLE);

  luaL_newmetatable(L, ENCSEQ_BUILDER_METATABLE);
  lua_pushvalue(L, -1); /* duplicate the metatable */
  lua_setfield(L, -2, "__index");
  lua_pushstring(L, "__gc");
  lua_pushcfunction(L, encseq_builder_lua_delete);
  lua_settable(L, -3);
  luaL_register(L, NULL, encseq_builder_lib_m);
  gt_lua_export_metatable(L, ENCSEQ_BUILDER_METATABLE);

  luaL_register(L, "gt", encseq_lib_f);
  lua_pop(L, 1);
  gt_assert(lua_gettop(L) == stack_size);
  return 1;
}
示例#21
0
static int
parse(lua_State *L)
{
	size_t len;
	const char *str = luaL_checklstring(L, 1, &len);
	const char *start;
	unsigned char state = S_BEG;
	int arg = 0;

	lua_settop(L, 1);
	lua_createtable(L, 4, 4);

	for (; len > 0; len--, str++) {
		unsigned char c = *str;

		state = state_table[state][c < sizeof(ascii_class) ?
			                   ascii_class[c] : C_ETC];
		switch (state) {
		case S_NI1:
		case S_LP1:
			start = str + 1;
			break;

		case S_CM1:
		case S_PA1:
			start = str;
			break;

		case S_US1:
		case S_NIS:
			lua_pushlstring(L, start, str - start);
			lua_setfield(L, -2, "nick");
			start = str + 1;
			break;

		case S_USS:
		case S_HO1:
			lua_pushlstring(L, start, str - start);
			lua_setfield(L, -2, "user");
			start = str + 1;
			break;

		case S_HSS:
			lua_pushlstring(L, start, str - start);
			lua_setfield(L, -2, "host");
			break;

		case S_CMS:
			lua_pushlstring(L, start, str - start);
			lua_setfield(L, -2, "command");
			break;

		case S_PAS:
		case S_CR1:
			lua_pushlstring(L, start, str - start);
			lua_rawseti(L, -2, ++arg);
			break;

		case S____:
			goto error;
		}
	}

	if (state == S_LFF)
		return 1;

error:
	lua_pushnil(L);
	lua_pushliteral(L, "parse error");
	return 2;
}
    void callback(int type, void *event)
    {
        if (type == GGEOLOCATION_LOCATION_UPDATE_EVENT && !isUpdatingLocation_)
            return;

        if (type == GGEOLOCATION_HEADING_UPDATE_EVENT && !isUpdatingHeading_)
            return;

        if (type == GGEOLOCATION_ERROR_EVENT && !isUpdatingLocation_)
            return;

        if (type == GGEOLOCATION_LOCATION_UPDATE_EVENT && !hasEventListener(LOCATION_UPDATE))
            return;

        if (type == GGEOLOCATION_HEADING_UPDATE_EVENT && !hasEventListener(HEADING_UPDATE))
            return;

        if (type == GGEOLOCATION_ERROR_EVENT && !hasEventListener(ERROR))
            return;

        luaL_rawgetptr(L, LUA_REGISTRYINDEX, &key);
        luaL_rawgetptr(L, -1, this);

        if (lua_isnil(L, -1))
        {
            lua_pop(L, 2);
            return;
        }

        lua_getfield(L, -1, "dispatchEvent");

        lua_pushvalue(L, -2); // create copy of geolocation

        if (type == GGEOLOCATION_LOCATION_UPDATE_EVENT)
        {
            lua_getfield(L, -1, "__updateLocationEvent");

            ggeolocation_LocationUpdateEvent *event2 = (ggeolocation_LocationUpdateEvent*)event;

            lua_pushnumber(L, event2->latitude);
            lua_setfield(L, -2, "latitude");
            lua_pushnumber(L, event2->longitude);
            lua_setfield(L, -2, "longitude");
            lua_pushnumber(L, event2->altitude);
            lua_setfield(L, -2, "altitude");
        }
        else if (type == GGEOLOCATION_HEADING_UPDATE_EVENT)
        {
            lua_getfield(L, -1, "__updateHeadingEvent");

            ggeolocation_HeadingUpdateEvent *event2 = (ggeolocation_HeadingUpdateEvent*)event;

            lua_pushnumber(L, event2->magneticHeading);
            lua_setfield(L, -2, "magneticHeading");
            lua_pushnumber(L, event2->trueHeading);
            lua_setfield(L, -2, "trueHeading");
        }
        else if (type == GGEOLOCATION_ERROR_EVENT)
        {
            lua_getfield(L, -1, "__errorEvent");
        }

        lua_call(L, 2, 0); // call geolocation:dispatchEvent(event)

        lua_pop(L, 2);
    }
示例#23
0
static void setboolfield (lua_State *L, const char *key, int value) {
  if (value < 0)  /* undefined? */
    return;  /* does not set field */
  lua_pushboolean(L, value);
  lua_setfield(L, -2, key);
}
示例#24
0
static void settabsb (lua_State *L, const char *i, int v) {
  lua_pushboolean(L, v);
  lua_setfield(L, -2, i);
}
示例#25
0
static int
magnet_get_script(lua_State * const L, const char * const fn)
{
	struct stat st;
	time_t mtime = 0;

	assert(lua_gettop(L) == 0);

	if (stat(fn, &st) == -1)
		return EXIT_FAILURE;

	mtime = st.st_mtime;

	lua_getfield(L, LUA_GLOBALSINDEX, "magnet"); 
	assert(lua_istable(L, -1));
	lua_getfield(L, -1, "cache");
	assert(lua_istable(L, -1));

	lua_getfield(L, -1, fn);

	/* magnet.cache['<script>'] is not a table for some reason, re-cache. */
	if (!lua_istable(L, -1))
	{
		lua_pop(L, 3); /* Pop the nil. */
		if (magnet_cache_script(L, fn, mtime))
			return EXIT_FAILURE;
	}
	else
	{
		lua_getfield(L, -1, "mtime");
		assert(lua_isnumber(L, -1));

		/* Script has not been modified, continue as usual. */
		if (mtime == lua_tointeger(L, -1))
		{
			lua_Integer hits;
			lua_pop(L, 1);
	
			/* Increment the hit counter. */	
			lua_getfield(L, -1, "hits");
			hits = lua_tointeger(L, -1);
			lua_pop(L, 1);
			lua_pushinteger(L, hits + 1);
			lua_setfield(L, -2, "hits");

			/* It is in the cache. */
			assert(lua_istable(L, -1));
			lua_getfield(L, -1, "script");
			assert(lua_isfunction(L, -1));
			lua_insert(L, -4);
			lua_pop(L, 3);
			assert(lua_isfunction(L, -1));
		}
		/* Recorded magnet.cache['<script>'].mtime does
		** not match the actual mtime, re-cache. */
		else
		{
			lua_pop(L, 4);
			if (magnet_cache_script(L, fn, mtime))
				return EXIT_FAILURE;
		}
	}
	/* This should be the function (top of Lua stack). */
	assert(lua_gettop(L) == 1);

	return EXIT_SUCCESS;
}
示例#26
0
static void settabss (lua_State *L, const char *i, const char *v) {
  lua_pushstring(L, v);
  lua_setfield(L, -2, i);
}
示例#27
0
static void newfenv (lua_State *L, lua_CFunction cls) {
  lua_createtable(L, 0, 1);
  lua_pushcfunction(L, cls);
  lua_setfield(L, -2, "__close");
}
示例#28
0
static void settabsi (lua_State *L, const char *i, int v) {
  lua_pushinteger(L, v);
  lua_setfield(L, -2, i);
}
示例#29
0
文件: core.c 项目: esmil/lem
static int
parse_http_process(lua_State *T, struct lem_inputbuf *b)
{
	struct parse_http_state *s = (struct parse_http_state *)&b->pstate;
	unsigned int w = s->w;
	unsigned int r = b->start;
	unsigned char state = s->state;

	while (r < b->end) {
		unsigned char ch = b->buf[r++];

		state = state_table[state][ch > 127 ? C_ETC : ascii_class[ch]];
		/*lem_debug("char = %c (%hhu), state = %hhu", ch, ch, state);*/
		switch (state) {
		case SMTD:
		case SURI:
		case SMAV:
		case SDOT:
		case SMIV:
		case CMAV:
		case CDOT:
		case CMIV:
		case CNUM:
		case CTXT:
		case SVAL:
			b->buf[w++] = ch;
			break;

		case XKEY:
			state = SKEY;
			lua_pushlstring(T, b->buf, w);
			lua_rawset(T, -3);
			w = 0;
			/* fallthrough */

		case SKEY:
			if (ch >= 'A' && ch <= 'Z')
				ch += ('a' - 'A');
			b->buf[w++] = ch;
			break;

		case SRE1:
			lua_pushlstring(T, b->buf, w);
			lua_setfield(T, -2, "version");
			w = 0;
			lua_newtable(T);
			break;

		case X___:
			lem_debug("HTTP parse error");
			lua_settop(T, 0);
			lua_pushnil(T);
			lua_pushliteral(T, "parse error");
			return 2;

		case XMUS:
			state = SMUS;
			lua_pushlstring(T, b->buf, w);
			lua_setfield(T, -2, "method");
			w = 0;
			break;

		case XUHS:
			state = SUHS;
			lua_pushlstring(T, b->buf, w);
			lua_setfield(T, -2, "uri");
			w = 0;
			break;

		case XVNS:
			state = CVNS;
			lua_pushlstring(T, b->buf, w);
			lua_setfield(T, -2, "version");
			w = 0;
			break;

		case XNTS:
			state = CNTS;
			{
				unsigned int n = 0;
				unsigned int k;

				for (k = 0; k < w; k++) {
					n *= 10;
					n += b->buf[k] - '0';
				}

				lua_pushinteger(T, n);
			}
			lua_setfield(T, -2, "status");
			w = 0;
			break;

		case XRE1:
			state = SRE1;
			lua_pushlstring(T, b->buf, w);
			lua_setfield(T, -2, "text");
			w = 0;
			lua_newtable(T);
			break;

		case XCOL:
			state = SCOL;
			lua_pushlstring(T, b->buf, w);
			w = 0;
			break;

		case XVAL:
			state = SVAL;
			b->buf[w++] = ' ';
			b->buf[w++] = ch;
			break;

		case XEND:
			/* in case there are no headers this is false */
			if (lua_type(T, -1) == LUA_TSTRING) {
				lua_pushlstring(T, b->buf, w);
				lua_rawset(T, -3);
			}
			lua_setfield(T, -2, "headers");

			if (r == b->end)
				b->start = b->end = 0;
			else
				b->start = r;
			return 1;
		}
	}

	if (w == LEM_INPUTBUF_SIZE - 1) {
		b->start = b->end = 0;
		lua_settop(T, 0);
		lua_pushnil(T);
		lua_pushliteral(T, "out of buffer space");
		return 2;
	}

	b->start = b->end = w + 1;
	s->w = w;
	s->state = state;
	return 0;
}
示例#30
0
static void _register_lua_slurm_output_functions (void)
{
	/*
	 *  Register slurm output functions in a global "slurm" table
	 */
	lua_newtable (L);
	luaL_register (L, NULL, slurm_functions);

	/*
	 *  Create more user-friendly lua versions of SLURM log functions.
	 */
	luaL_loadstring (L, "slurm.error (string.format(unpack({...})))");
	lua_setfield (L, -2, "log_error");
	luaL_loadstring (L, "slurm.log (0, string.format(unpack({...})))");
	lua_setfield (L, -2, "log_info");
	luaL_loadstring (L, "slurm.log (1, string.format(unpack({...})))");
	lua_setfield (L, -2, "log_verbose");
	luaL_loadstring (L, "slurm.log (2, string.format(unpack({...})))");
	lua_setfield (L, -2, "log_debug");
	luaL_loadstring (L, "slurm.log (3, string.format(unpack({...})))");
	lua_setfield (L, -2, "log_debug2");
	luaL_loadstring (L, "slurm.log (4, string.format(unpack({...})))");
	lua_setfield (L, -2, "log_debug3");
	luaL_loadstring (L, "slurm.log (5, string.format(unpack({...})))");
	lua_setfield (L, -2, "log_debug4");
	luaL_loadstring (L, "slurm.user_msg (string.format(unpack({...})))");
	lua_setfield (L, -2, "log_user");

	/*
	 * Error codes: slurm.SUCCESS, slurm.FAILURE, slurm.ERROR, etc.
	 */
	lua_pushnumber (L, SLURM_FAILURE);
	lua_setfield (L, -2, "FAILURE");
	lua_pushnumber (L, SLURM_ERROR);
	lua_setfield (L, -2, "ERROR");
	lua_pushnumber (L, SLURM_SUCCESS);
	lua_setfield (L, -2, "SUCCESS");
	lua_pushnumber (L, ESLURM_INVALID_LICENSES);
	lua_setfield (L, -2, "ESLURM_INVALID_LICENSES");

	/*
	 * Other definitions needed to interpret data
	 * slurm.MEM_PER_CPU, slurm.NO_VAL, etc.
	 */
	lua_pushnumber (L, ALLOC_SID_ADMIN_HOLD);
	lua_setfield (L, -2, "ALLOC_SID_ADMIN_HOLD");
	lua_pushnumber (L, ALLOC_SID_USER_HOLD);
	lua_setfield (L, -2, "ALLOC_SID_USER_HOLD");
	lua_pushnumber (L, MAIL_JOB_BEGIN);
	lua_setfield (L, -2, "MAIL_JOB_BEGIN");
	lua_pushnumber (L, MAIL_JOB_END);
	lua_setfield (L, -2, "MAIL_JOB_END");
	lua_pushnumber (L, MAIL_JOB_FAIL);
	lua_setfield (L, -2, "MAIL_JOB_FAIL");
	lua_pushnumber (L, MAIL_JOB_REQUEUE);
	lua_setfield (L, -2, "MAIL_JOB_REQUEUE");
	lua_pushnumber (L, MAIL_JOB_STAGE_OUT);
	lua_setfield (L, -2, "MAIL_JOB_STAGE_OUT");
	lua_pushnumber (L, MEM_PER_CPU);
	lua_setfield (L, -2, "MEM_PER_CPU");
	lua_pushnumber (L, NICE_OFFSET);
	lua_setfield (L, -2, "NICE_OFFSET");
	lua_pushnumber (L, NO_VAL);
	lua_setfield (L, -2, "NO_VAL");

	lua_setglobal (L, "slurm");

	last_lua_jobs_update = 0;
	_update_jobs_global();
	last_lua_resv_update = 0;
	_update_resvs_global();
}