You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.0 KiB

  1. /*
  2. ** Compat-5.1
  3. ** Copyright Kepler Project 2004-2006 (http://www.keplerproject.org/compat)
  4. ** $Id: compat-5.1.c,v 1.13 2006/02/20 21:12:47 carregal Exp $
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "lua.h"
  9. #include "lauxlib.h"
  10. #include "compat-5.1.h"
  11. static void getfield(lua_State *L, int idx, const char *name) {
  12. const char *end = strchr(name, '.');
  13. lua_pushvalue(L, idx);
  14. while (end) {
  15. lua_pushlstring(L, name, end - name);
  16. lua_gettable(L, -2);
  17. lua_remove(L, -2);
  18. if (lua_isnil(L, -1)) return;
  19. name = end+1;
  20. end = strchr(name, '.');
  21. }
  22. lua_pushstring(L, name);
  23. lua_gettable(L, -2);
  24. lua_remove(L, -2);
  25. }
  26. static void setfield(lua_State *L, int idx, const char *name) {
  27. const char *end = strchr(name, '.');
  28. lua_pushvalue(L, idx);
  29. while (end) {
  30. lua_pushlstring(L, name, end - name);
  31. lua_gettable(L, -2);
  32. /* create table if not found */
  33. if (lua_isnil(L, -1)) {
  34. lua_pop(L, 1);
  35. lua_newtable(L);
  36. lua_pushlstring(L, name, end - name);
  37. lua_pushvalue(L, -2);
  38. lua_settable(L, -4);
  39. }
  40. lua_remove(L, -2);
  41. name = end+1;
  42. end = strchr(name, '.');
  43. }
  44. lua_pushstring(L, name);
  45. lua_pushvalue(L, -3);
  46. lua_settable(L, -3);
  47. lua_pop(L, 2);
  48. }
  49. LUALIB_API void luaL_module(lua_State *L, const char *libname,
  50. const luaL_reg *l, int nup) {
  51. if (libname) {
  52. getfield(L, LUA_GLOBALSINDEX, libname); /* check whether lib already exists */
  53. if (lua_isnil(L, -1)) {
  54. int env, ns;
  55. lua_pop(L, 1); /* get rid of nil */
  56. lua_pushliteral(L, "require");
  57. lua_gettable(L, LUA_GLOBALSINDEX); /* look for require */
  58. lua_getfenv(L, -1); /* getfenv(require) */
  59. lua_remove(L, -2); /* remove function require */
  60. env = lua_gettop(L);
  61. lua_newtable(L); /* create namespace for lib */
  62. ns = lua_gettop(L);
  63. getfield(L, env, "package.loaded"); /* get package.loaded table */
  64. if (lua_isnil(L, -1)) { /* create package.loaded table */
  65. lua_pop(L, 1); /* remove previous result */
  66. lua_newtable(L);
  67. lua_pushvalue(L, -1);
  68. setfield(L, env, "package.loaded");
  69. }
  70. else if (!lua_istable(L, -1))
  71. luaL_error(L, "name conflict for library `%s'", libname);
  72. lua_pushstring(L, libname);
  73. lua_pushvalue(L, ns);
  74. lua_settable(L, -3); /* package.loaded[libname] = ns */
  75. lua_pop(L, 1); /* get rid of package.loaded table */
  76. lua_pushvalue(L, ns); /* copy namespace */
  77. setfield(L, LUA_GLOBALSINDEX, libname);
  78. lua_remove (L, env); /* remove env */
  79. }
  80. lua_insert(L, -(nup+1)); /* move library table to below upvalues */
  81. }
  82. for (; l->name; l++) {
  83. int i;
  84. lua_pushstring(L, l->name);
  85. for (i=0; i<nup; i++) /* copy upvalues to the top */
  86. lua_pushvalue(L, -(nup+1));
  87. lua_pushcclosure(L, l->func, nup);
  88. lua_settable(L, -(nup+3));
  89. }
  90. lua_pop(L, nup); /* remove upvalues */
  91. }