/source/texk/web2c/luatexdir/thirdlib/luasocket/src/auxiliar.c

https://bitbucket.org/clerkma/eat · C · 158 lines · 95 code · 13 blank · 50 comment · 12 complexity · 2b40a874061c664c714c1684af50e43b MD5 · raw file

  1. /*=========================================================================*\
  2. * Auxiliar routines for class hierarchy manipulation
  3. * LuaSocket toolkit
  4. \*=========================================================================*/
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include "auxiliar.h"
  8. /*=========================================================================*\
  9. * Exported functions
  10. \*=========================================================================*/
  11. /*-------------------------------------------------------------------------*\
  12. * Initializes the module
  13. \*-------------------------------------------------------------------------*/
  14. int auxiliar_open(lua_State *L) {
  15. (void) L;
  16. return 0;
  17. }
  18. /*-------------------------------------------------------------------------*\
  19. * Creates a new class with given methods
  20. * Methods whose names start with __ are passed directly to the metatable.
  21. \*-------------------------------------------------------------------------*/
  22. void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func) {
  23. luaL_newmetatable(L, classname); /* mt */
  24. /* create __index table to place methods */
  25. lua_pushstring(L, "__index"); /* mt,"__index" */
  26. lua_newtable(L); /* mt,"__index",it */
  27. /* put class name into class metatable */
  28. lua_pushstring(L, "class"); /* mt,"__index",it,"class" */
  29. lua_pushstring(L, classname); /* mt,"__index",it,"class",classname */
  30. lua_rawset(L, -3); /* mt,"__index",it */
  31. /* pass all methods that start with _ to the metatable, and all others
  32. * to the index table */
  33. for (; func->name; func++) { /* mt,"__index",it */
  34. lua_pushstring(L, func->name);
  35. lua_pushcfunction(L, func->func);
  36. lua_rawset(L, func->name[0] == '_' ? -5: -3);
  37. }
  38. lua_rawset(L, -3); /* mt */
  39. lua_pop(L, 1);
  40. }
  41. /*-------------------------------------------------------------------------*\
  42. * Prints the value of a class in a nice way
  43. \*-------------------------------------------------------------------------*/
  44. int auxiliar_tostring(lua_State *L) {
  45. char buf[32];
  46. if (!lua_getmetatable(L, 1)) goto error;
  47. lua_pushstring(L, "__index");
  48. lua_gettable(L, -2);
  49. if (!lua_istable(L, -1)) goto error;
  50. lua_pushstring(L, "class");
  51. lua_gettable(L, -2);
  52. if (!lua_isstring(L, -1)) goto error;
  53. sprintf(buf, "%p", lua_touserdata(L, 1));
  54. lua_pushfstring(L, "%s: %s", lua_tostring(L, -1), buf);
  55. return 1;
  56. error:
  57. lua_pushstring(L, "invalid object passed to 'auxiliar.c:__tostring'");
  58. lua_error(L);
  59. return 1;
  60. }
  61. /*-------------------------------------------------------------------------*\
  62. * Insert class into group
  63. \*-------------------------------------------------------------------------*/
  64. void auxiliar_add2group(lua_State *L, const char *classname, const char *groupname) {
  65. luaL_getmetatable(L, classname);
  66. lua_pushstring(L, groupname);
  67. lua_pushboolean(L, 1);
  68. lua_rawset(L, -3);
  69. lua_pop(L, 1);
  70. }
  71. /*-------------------------------------------------------------------------*\
  72. * Make sure argument is a boolean
  73. \*-------------------------------------------------------------------------*/
  74. int auxiliar_checkboolean(lua_State *L, int objidx) {
  75. if (!lua_isboolean(L, objidx))
  76. auxiliar_typeerror(L, objidx, lua_typename(L, LUA_TBOOLEAN));
  77. return lua_toboolean(L, objidx);
  78. }
  79. /*-------------------------------------------------------------------------*\
  80. * Return userdata pointer if object belongs to a given class, abort with
  81. * error otherwise
  82. \*-------------------------------------------------------------------------*/
  83. void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx) {
  84. void *data = auxiliar_getclassudata(L, classname, objidx);
  85. if (!data) {
  86. char msg[45];
  87. sprintf(msg, "%.35s expected", classname);
  88. luaL_argerror(L, objidx, msg);
  89. }
  90. return data;
  91. }
  92. /*-------------------------------------------------------------------------*\
  93. * Return userdata pointer if object belongs to a given group, abort with
  94. * error otherwise
  95. \*-------------------------------------------------------------------------*/
  96. void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx) {
  97. void *data = auxiliar_getgroupudata(L, groupname, objidx);
  98. if (!data) {
  99. char msg[45];
  100. sprintf(msg, "%.35s expected", groupname);
  101. luaL_argerror(L, objidx, msg);
  102. }
  103. return data;
  104. }
  105. /*-------------------------------------------------------------------------*\
  106. * Set object class
  107. \*-------------------------------------------------------------------------*/
  108. void auxiliar_setclass(lua_State *L, const char *classname, int objidx) {
  109. luaL_getmetatable(L, classname);
  110. if (objidx < 0) objidx--;
  111. lua_setmetatable(L, objidx);
  112. }
  113. /*-------------------------------------------------------------------------*\
  114. * Get a userdata pointer if object belongs to a given group. Return NULL
  115. * otherwise
  116. \*-------------------------------------------------------------------------*/
  117. void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx) {
  118. if (!lua_getmetatable(L, objidx))
  119. return NULL;
  120. lua_pushstring(L, groupname);
  121. lua_rawget(L, -2);
  122. if (lua_isnil(L, -1)) {
  123. lua_pop(L, 2);
  124. return NULL;
  125. } else {
  126. lua_pop(L, 2);
  127. return lua_touserdata(L, objidx);
  128. }
  129. }
  130. /*-------------------------------------------------------------------------*\
  131. * Get a userdata pointer if object belongs to a given class. Return NULL
  132. * otherwise
  133. \*-------------------------------------------------------------------------*/
  134. void *auxiliar_getclassudata(lua_State *L, const char *classname, int objidx) {
  135. return luaL_checkudata(L, objidx, classname);
  136. }
  137. /*-------------------------------------------------------------------------*\
  138. * Throws error when argument does not have correct type.
  139. * Used to be part of lauxlib in Lua 5.1, was dropped from 5.2.
  140. \*-------------------------------------------------------------------------*/
  141. int auxiliar_typeerror (lua_State *L, int narg, const char *tname) {
  142. const char *msg = lua_pushfstring(L, "%s expected, got %s", tname,
  143. luaL_typename(L, narg));
  144. return luaL_argerror(L, narg, msg);
  145. }