1
0

lua-compat.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <lua.h>
  2. #include <lualib.h>
  3. #include <lauxlib.h>
  4. #if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501
  5. void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
  6. {
  7. luaL_checkstack(L, nup+1, "too many upvalues");
  8. for (; l->name != NULL; l++) { /* fill the table with given functions */
  9. int i;
  10. lua_pushstring(L, l->name);
  11. for (i = 0; i < nup; i++) /* copy upvalues to the top */
  12. lua_pushvalue(L, -(nup+1));
  13. lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
  14. lua_settable(L, -(nup + 3));
  15. }
  16. lua_pop(L, nup); /* remove upvalues */
  17. }
  18. void luaL_requiref(lua_State *L, char const* modname,
  19. lua_CFunction openf, int glb)
  20. {
  21. luaL_checkstack(L, 3, "not enough stack slots");
  22. lua_pushcfunction(L, openf);
  23. lua_pushstring(L, modname);
  24. lua_call(L, 1, 1);
  25. lua_getglobal(L, "package");
  26. lua_getfield(L, -1, "loaded");
  27. lua_replace(L, -2);
  28. lua_pushvalue(L, -2);
  29. lua_setfield(L, -2, modname);
  30. lua_pop(L, 1);
  31. if (glb) {
  32. lua_pushvalue(L, -1);
  33. lua_setglobal(L, modname);
  34. }
  35. }
  36. #endif