lauxlib.c 6.9 KB

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