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; }
void CLuaMain::InitVM ( void ) { assert( !m_luaVM ); // Create a new VM m_luaVM = lua_open (); m_pLuaManager->OnLuaMainOpenVM( this, m_luaVM ); // Set the instruction count hook lua_sethook ( m_luaVM, InstructionCountHook, LUA_MASKCOUNT, HOOK_INSTRUCTION_COUNT ); // Load LUA libraries luaopen_base ( m_luaVM ); luaopen_math ( m_luaVM ); luaopen_string ( m_luaVM ); luaopen_table ( m_luaVM ); luaopen_debug ( m_luaVM ); luaopen_utf8 ( m_luaVM ); // Initialize security restrictions. Very important to prevent lua trojans and viruses! InitSecurity (); // Registering C functions CLuaCFunctions::RegisterFunctionsWithVM ( m_luaVM ); // Create class metatables InitClasses ( m_luaVM ); // Oli: Don't forget to add new ones to CLuaManager::LoadCFunctions. Thanks! // create global vars lua_pushelement ( m_luaVM, g_pGame->GetMapManager()->GetRootElement() ); lua_setglobal ( m_luaVM, "root" ); lua_pushresource ( m_luaVM, m_pResource ); lua_setglobal ( m_luaVM, "resource" ); lua_pushelement ( m_luaVM, m_pResource->GetResourceRootElement () ); lua_setglobal ( m_luaVM, "resourceRoot" ); // Load pre-loaded lua code LoadScript ( szPreloadedScript ); }