lauxlib.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. ** $Id: lauxlib.c,v 1.70 2002/05/15 18:57:44 roberto Exp roberto $
  3. ** Auxiliary functions for building Lua libraries
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdarg.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. /* This file uses only the official API of Lua.
  10. ** Any function declared here could be written as an application function.
  11. ** With care, these functions can be used by other libraries.
  12. */
  13. #include "lua.h"
  14. #include "lauxlib.h"
  15. #include "luadebug.h"
  16. #include "lualib.h"
  17. LUALIB_API int luaL_findstring (const char *name, const char *const list[]) {
  18. int i;
  19. for (i=0; list[i]; i++)
  20. if (strcmp(list[i], name) == 0)
  21. return i;
  22. return -1; /* name not found */
  23. }
  24. LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
  25. lua_Debug ar;
  26. lua_getstack(L, 0, &ar);
  27. lua_getinfo(L, "n", &ar);
  28. if (strcmp(ar.namewhat, "method") == 0) narg--; /* do not count `self' */
  29. if (ar.name == NULL)
  30. ar.name = "?";
  31. return luaL_verror(L, "bad argument #%d to `%s' (%s)",
  32. narg, ar.name, extramsg);
  33. }
  34. LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
  35. const char *msg = lua_pushfstring(L, "%s expected, got %s",
  36. tname, lua_typename(L, lua_type(L,narg)));
  37. return luaL_argerror(L, narg, msg);
  38. }
  39. static void tag_error (lua_State *L, int narg, int tag) {
  40. luaL_typerror(L, narg, lua_typename(L, tag));
  41. }
  42. LUALIB_API void luaL_check_stack (lua_State *L, int space, const char *mes) {
  43. if (!lua_checkstack(L, space))
  44. luaL_verror(L, "stack overflow (%s)", mes);
  45. }
  46. LUALIB_API void luaL_check_type (lua_State *L, int narg, int t) {
  47. if (lua_type(L, narg) != t)
  48. tag_error(L, narg, t);
  49. }
  50. LUALIB_API void luaL_check_any (lua_State *L, int narg) {
  51. if (lua_type(L, narg) == LUA_TNONE)
  52. luaL_argerror(L, narg, "value expected");
  53. }
  54. LUALIB_API const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
  55. const char *s = lua_tostring(L, narg);
  56. if (!s) tag_error(L, narg, LUA_TSTRING);
  57. if (len) *len = lua_strlen(L, narg);
  58. return s;
  59. }
  60. LUALIB_API const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, size_t *len) {
  61. if (lua_isnoneornil(L, narg)) {
  62. if (len)
  63. *len = (def ? strlen(def) : 0);
  64. return def;
  65. }
  66. else return luaL_check_lstr(L, narg, len);
  67. }
  68. LUALIB_API lua_Number luaL_check_number (lua_State *L, int narg) {
  69. lua_Number d = lua_tonumber(L, narg);
  70. if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
  71. tag_error(L, narg, LUA_TNUMBER);
  72. return d;
  73. }
  74. LUALIB_API lua_Number luaL_opt_number (lua_State *L, int narg, lua_Number def) {
  75. if (lua_isnoneornil(L, narg)) return def;
  76. else return luaL_check_number(L, narg);
  77. }
  78. LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
  79. if (!lua_getmetatable(L, obj)) /* no metatable? */
  80. return 0;
  81. lua_pushstring(L, event);
  82. lua_rawget(L, -2);
  83. if (lua_isnil(L, -1)) {
  84. lua_pop(L, 2); /* remove metatable and metafield */
  85. return 0;
  86. }
  87. lua_pushvalue(L, obj);
  88. lua_rawcall(L, 1, 1);
  89. return 1;
  90. }
  91. LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int nup) {
  92. for (; l->name; l++) {
  93. int i;
  94. lua_pushstring(L, l->name);
  95. for (i=0; i<nup; i++) /* copy upvalues to the top */
  96. lua_pushvalue(L, -(nup+1));
  97. lua_pushcclosure(L, l->func, nup);
  98. lua_settable(L, -(nup+3));
  99. }
  100. lua_pop(L, nup);
  101. }
  102. LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname,
  103. const luaL_reg *l, int nup) {
  104. lua_pushstring(L, libname);
  105. lua_insert(L, -(nup+1));
  106. lua_newtable(L);
  107. lua_insert(L, -(nup+1));
  108. luaL_openlib(L, l, nup);
  109. lua_settable(L, LUA_GLOBALSINDEX);
  110. }
  111. LUALIB_API int luaL_verror (lua_State *L, const char *fmt, ...) {
  112. lua_Debug ar;
  113. const char *msg;
  114. va_list argp;
  115. va_start(argp, fmt);
  116. msg = lua_pushvfstring(L, fmt, argp);
  117. va_end(argp);
  118. if (lua_getstack(L, 1, &ar)) { /* check calling function */
  119. lua_getinfo(L, "Snl", &ar);
  120. if (ar.currentline > 0)
  121. lua_pushfstring(L, "%s:%d: %s", ar.short_src, ar.currentline, msg);
  122. }
  123. return lua_errorobj(L);
  124. }
  125. /*
  126. ** {======================================================
  127. ** Generic Buffer manipulation
  128. ** =======================================================
  129. */
  130. #define buffempty(B) ((B)->p == (B)->buffer)
  131. #define bufflen(B) ((B)->p - (B)->buffer)
  132. #define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
  133. #define LIMIT (LUA_MINSTACK/2)
  134. static int emptybuffer (luaL_Buffer *B) {
  135. size_t l = bufflen(B);
  136. if (l == 0) return 0; /* put nothing on stack */
  137. else {
  138. lua_pushlstring(B->L, B->buffer, l);
  139. B->p = B->buffer;
  140. B->level++;
  141. return 1;
  142. }
  143. }
  144. static void adjuststack (luaL_Buffer *B) {
  145. if (B->level > 1) {
  146. lua_State *L = B->L;
  147. int toget = 1; /* number of levels to concat */
  148. size_t toplen = lua_strlen(L, -1);
  149. do {
  150. size_t l = lua_strlen(L, -(toget+1));
  151. if (B->level - toget + 1 >= LIMIT || toplen > l) {
  152. toplen += l;
  153. toget++;
  154. }
  155. else break;
  156. } while (toget < B->level);
  157. lua_concat(L, toget);
  158. B->level = B->level - toget + 1;
  159. }
  160. }
  161. LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
  162. if (emptybuffer(B))
  163. adjuststack(B);
  164. return B->buffer;
  165. }
  166. LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
  167. while (l--)
  168. luaL_putchar(B, *s++);
  169. }
  170. LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
  171. luaL_addlstring(B, s, strlen(s));
  172. }
  173. LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
  174. emptybuffer(B);
  175. lua_concat(B->L, B->level);
  176. B->level = 1;
  177. }
  178. LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
  179. lua_State *L = B->L;
  180. size_t vl = lua_strlen(L, -1);
  181. if (vl <= bufffree(B)) { /* fit into buffer? */
  182. memcpy(B->p, lua_tostring(L, -1), vl); /* put it there */
  183. B->p += vl;
  184. lua_pop(L, 1); /* remove from stack */
  185. }
  186. else {
  187. if (emptybuffer(B))
  188. lua_insert(L, -2); /* put buffer before new value */
  189. B->level++; /* add new value into B stack */
  190. adjuststack(B);
  191. }
  192. }
  193. LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
  194. B->L = L;
  195. B->p = B->buffer;
  196. B->level = 0;
  197. }
  198. /* }====================================================== */
  199. LUALIB_API int luaL_ref (lua_State *L, int t) {
  200. int ref;
  201. lua_rawgeti(L, t, 0); /* get first free element */
  202. ref = (int)lua_tonumber(L, -1);
  203. lua_pop(L, 1); /* remove it from stack */
  204. if (ref != 0) { /* any free element? */
  205. lua_rawgeti(L, t, ref); /* remove it from list */
  206. lua_rawseti(L, t, 0);
  207. }
  208. else { /* no free elements */
  209. ref = lua_getn(L, t) + 1; /* use next `n' */
  210. lua_pushliteral(L, "n");
  211. lua_pushnumber(L, ref);
  212. lua_rawset(L, t); /* n = n+1 */
  213. }
  214. lua_rawseti(L, t, ref);
  215. return ref;
  216. }
  217. LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
  218. if (ref >= 0) {
  219. lua_rawgeti(L, t, 0);
  220. lua_pushnumber(L, ref);
  221. lua_rawseti(L, t, 0);
  222. lua_rawseti(L, t, ref);
  223. }
  224. }