Beispiel #1
0
 int open_coroutine()
 {
     if (L)
     {
         luaopen_coroutine(L);
         return 0;
     }
     return -1;
 }
Beispiel #2
0
interp_t *interp_new(int flags, int ipcfd) {
	interp_t *interp = NULL;
	lua_State *L = NULL;
	static const luaL_Reg ipcfuncs[] = {
		{"ready", bipc_ready},
		{"connect", bipc_connect},
		{NULL, NULL},
	};

	L = luaL_newstate();
	if (L == NULL) {
		return NULL;
	}

	luaopen_base(L);
	luaopen_coroutine(L);
	luaopen_table(L);
	luaopen_string(L);
#if (LUA_VERSION_NUM >= 503)
	luaopen_utf8(L);
#endif
	luaopen_bit32(L);
	luaopen_math(L);
	luaopen_debug(L);
	luaopen_package(L);
	/* not opened: io, os */

	/* create a userdata object that represents the interpreter */
	interp = (interp_t*)lua_newuserdata(L, sizeof(interp_t));
	if (interp == NULL) {
		lua_close(L);
		return NULL;
	}

	memset(interp, 0, sizeof(interp_t));
	interp->ipcfd = ipcfd;
	interp->L = L;

	/* Build a metatable with the methods for our userdata object.
	 * Assign the userdata object to BIRK_IPCNAME */
	luaL_newmetatable(L, BIRK_IPCNAME);
	lua_pushvalue(L, -1);
	lua_setfield(L, -2, "__index");
	luaL_setfuncs(L, ipcfuncs, 0);
	lua_setmetatable(L, -2);
	lua_setglobal(L, BIRK_IPCNAME);

	if ((flags & INTERPF_LOADBIRK) && interp_load(interp, "birk") != BIRK_OK) {
		/* error message is set, caller should check it on return */
		return interp;
	}

	return interp;
}
Beispiel #3
0
qboolean G_LuaStartVM(lvm_t * vm)
{
	int             res = 0;
	char            homepath[MAX_QPATH], gamepath[MAX_QPATH];

	vm->L = luaL_newstate();
	if(!vm->L)
	{
		LUA_LOG("Lua: Lua failed to initialise.\n");
		return qfalse;
	}

	luaL_openlibs(vm->L);

	trap_Cvar_VariableStringBuffer("fs_homepath", homepath, sizeof(homepath));
	trap_Cvar_VariableStringBuffer("fs_game", gamepath, sizeof(gamepath));

	lua_getglobal(vm->L, LUA_LOADLIBNAME);
	if(lua_istable(vm->L, -1))
	{
		lua_pushstring(vm->L, va("%s%s%s%s?.lua;%s%s%s%slualib%slua%s?.lua",
			homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP,
			homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, LUA_DIRSEP, LUA_DIRSEP));
		lua_setfield(vm->L, -2, "path");
		lua_pushstring(vm->L, va("%s%s%s%s?.%s;%s%s%s%slualib%sclibs%s?.%s",
			homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, EXTENSION,
			homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, LUA_DIRSEP, LUA_DIRSEP, EXTENSION));
		lua_setfield(vm->L, -2, "cpath");
	}
	lua_pop(vm->L, 1);

	Lua_RegisterGlobal(vm->L, "LUA_PATH", va("%s%s%s%s?.lua;%s%s%s%slualib%slua%s?.lua",
		homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP,
		homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, LUA_DIRSEP, LUA_DIRSEP));
	Lua_RegisterGlobal(vm->L, "LUA_CPATH", va("%s%s%s%s?.%s;%s%s%s%slualib%sclibs%s?.%s",
		homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, EXTENSION,
		homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, LUA_DIRSEP, LUA_DIRSEP, EXTENSION));
	Lua_RegisterGlobal(vm->L, "LUA_DIRSEP", LUA_DIRSEP);

	lua_newtable(vm->L);
	Lua_RegConstInteger(vm->L, CS_PLAYERS);
	Lua_RegConstInteger(vm->L, EXEC_NOW);
	Lua_RegConstInteger(vm->L, EXEC_INSERT);
	Lua_RegConstInteger(vm->L, EXEC_APPEND);
	Lua_RegConstInteger(vm->L, FS_READ);
	Lua_RegConstInteger(vm->L, FS_WRITE);
	Lua_RegConstInteger(vm->L, FS_APPEND);
	Lua_RegConstInteger(vm->L, FS_APPEND_SYNC);
	Lua_RegConstInteger(vm->L, SAY_ALL);
	Lua_RegConstInteger(vm->L, SAY_TEAM);
	Lua_RegConstString(vm->L, HOSTARCH);

	luaopen_base(vm->L);
	luaopen_string(vm->L);
	luaopen_coroutine(vm->L);
	Luaopen_Game(vm->L);
	Luaopen_Qmath(vm->L);
	Luaopen_Mover(vm->L);
	Luaopen_Vector(vm->L);
	Luaopen_Entity(vm->L);
	Luaopen_Cinematic(vm->L);
	Luaopen_Sound(vm->L);
	Luaopen_Trace(vm->L);

	res = luaL_loadbuffer(vm->L, vm->code, vm->code_size, vm->filename);
	if(res == LUA_ERRSYNTAX)
	{
		LUA_LOG("Lua: syntax error during pre-compilation: %s\n", (char *)lua_tostring(vm->L, -1));
		G_Printf(S_COLOR_YELLOW "Lua: syntax error: %s\n", (char *)lua_tostring(vm->L, -1));
		lua_pop(vm->L, 1);
		vm->error++;
		return qfalse;
	}
	else if(res == LUA_ERRMEM)
	{
		LUA_LOG("Lua: memory allocation error #1 ( %s )\n", vm->filename);
		vm->error++;
		return qfalse;
	}

	if(!G_LuaCall(vm, "G_LuaStartVM", 0, 0))
		return qfalse;

	LUA_LOG("Lua: Loading %s\n", vm->filename);
	return qtrue;
}
Beispiel #4
0
bool LuaWrapper::openCoroutine()	{ luaopen_coroutine(m_luaState);	return true; } //协同库