lua_namespace.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <lua.h>
  2. //#include <lualib.h>
  3. #include <lauxlib.h>
  4. #include <namespace.h>
  5. #define check_nsm(L) \
  6. *(LSUP_NSMap **)luaL_checkudata(L, 1, "LSUP.NSMap")
  7. lua_State *L;
  8. /*
  9. * Factory methods.
  10. */
  11. static int l_nsm_new (lua_State *L)
  12. {
  13. LSUP_NSMap **nsm_p = lua_newuserdatauv (L, sizeof (*nsm_p), 1);
  14. luaL_getmetatable (L, "LSUP.NSMap");
  15. lua_setmetatable (L, -2);
  16. *nsm_p = LSUP_nsmap_new ();
  17. if (!*nsm_p) luaL_error (L, "Error while creating a term!");
  18. return 1;
  19. }
  20. /*
  21. * Class methods.
  22. */
  23. static int l_nsm_gc (lua_State *L)
  24. {
  25. LSUP_NSMap *nsm = check_nsm (L);
  26. if (nsm) LSUP_nsmap_free (nsm);
  27. return 0;
  28. }
  29. /*
  30. * Library setup.
  31. */
  32. static const struct luaL_Reg nsm_lib_fn [] = {
  33. {"nsm_new", l_nsm_new},
  34. {NULL}
  35. };
  36. static const struct luaL_Reg nsm_lib_meth [] = {
  37. {"__gc", l_nsm_gc},
  38. {NULL}
  39. };
  40. int luaopen_namespace (lua_State *L)
  41. {
  42. luaL_newmetatable (L, "LSUP.NSMap");
  43. lua_pushvalue (L, -1);
  44. lua_setfield (L, -2, "__index");
  45. luaL_setfuncs (L, nsm_lib_meth, 0);
  46. luaL_newlib (L, nsm_lib_fn);
  47. return 1;
  48. }