public static void LuaLOpenLibs(LuaState L) { for (int i = 0; i < lualibs.Length - 1; i++) { LuaLReg lib = lualibs[i]; LuaPushCFunction(L, lib.func); LuaPushString(L, lib.name); LuaCall(L, 1, 0); } }
public static void LuaIOpenLib (LuaState L, CharPtr libname, LuaLReg[] l, int nup) { if (libname!=null) { int size = LibSize(l); /* check whether lib already exists */ LuaLFindTable(L, LUA_REGISTRYINDEX, "_LOADED", 1); LuaGetField(L, -1, libname); /* get _LOADED[libname] */ if (!LuaIsTable(L, -1)) { /* not found? */ LuaPop(L, 1); /* remove previous result */ /* try global variable (and create one if it does not exist) */ if (LuaLFindTable(L, LUA_GLOBALSINDEX, libname, size) != null) LuaLError(L, "name conflict for module " + LUA_QS, libname); LuaPushValue(L, -1); LuaSetField(L, -3, libname); /* _LOADED[libname] = new table */ } LuaRemove(L, -2); /* remove _LOADED table */ LuaInsert(L, -(nup+1)); /* move library table to below upvalues */ } int reg_num = 0; for (; l[reg_num].name!=null; reg_num++) { int i; for (i=0; i<nup; i++) /* copy upvalues to the top */ LuaPushValue(L, -nup); LuaPushCClosure(L, l[reg_num].func, nup); LuaSetField(L, -(nup+2), l[reg_num].name); } LuaPop(L, nup); /* remove upvalues */ }
// we could just take the .Length member here, but let's try // to keep it as close to the C implementation as possible. private static int LibSize (LuaLReg[] l) { int size = 0; for (; l[size].name!=null; size++); return size; }
public static void LuaLRegister(LuaState L, CharPtr libname, LuaLReg[] l) { LuaIOpenLib(L, libname, l, 0); }