/tags/JSTUDIO3D_2_5_x/source/Engine/External/Lua/lauxlib.c

# · C · 653 lines · 486 code · 131 blank · 36 comment · 111 complexity · 2691326d6c5e94386e2fd6c784455fde MD5 · raw file

  1. /*
  2. ** $Id: lauxlib.c,v 1.1 2007-02-13 21:36:58 paradoxnj Exp $
  3. ** Auxiliary functions for building Lua libraries
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <errno.h>
  8. #include <stdarg.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. /* This file uses only the official API of Lua.
  13. ** Any function declared here could be written as an application function.
  14. */
  15. #define lauxlib_c
  16. #define LUA_LIB
  17. #include "lua.h"
  18. #include "lauxlib.h"
  19. #define FREELIST_REF 0 /* free list of references */
  20. /* convert a stack index to positive */
  21. #define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
  22. lua_gettop(L) + (i) + 1)
  23. /*
  24. ** {======================================================
  25. ** Error-report functions
  26. ** =======================================================
  27. */
  28. LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
  29. lua_Debug ar;
  30. if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
  31. return luaL_error(L, "bad argument #%d (%s)", narg, extramsg);
  32. lua_getinfo(L, "n", &ar);
  33. if (strcmp(ar.namewhat, "method") == 0) {
  34. narg--; /* do not count `self' */
  35. if (narg == 0) /* error is in the self argument itself? */
  36. return luaL_error(L, "calling " LUA_QS " on bad self (%s)",
  37. ar.name, extramsg);
  38. }
  39. if (ar.name == NULL)
  40. ar.name = "?";
  41. return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",
  42. narg, ar.name, extramsg);
  43. }
  44. LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
  45. const char *msg = lua_pushfstring(L, "%s expected, got %s",
  46. tname, luaL_typename(L, narg));
  47. return luaL_argerror(L, narg, msg);
  48. }
  49. static void tag_error (lua_State *L, int narg, int tag) {
  50. luaL_typerror(L, narg, lua_typename(L, tag));
  51. }
  52. LUALIB_API void luaL_where (lua_State *L, int level) {
  53. lua_Debug ar;
  54. if (lua_getstack(L, level, &ar)) { /* check function at level */
  55. lua_getinfo(L, "Sl", &ar); /* get info about it */
  56. if (ar.currentline > 0) { /* is there info? */
  57. lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
  58. return;
  59. }
  60. }
  61. lua_pushliteral(L, ""); /* else, no information available... */
  62. }
  63. LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
  64. va_list argp;
  65. va_start(argp, fmt);
  66. luaL_where(L, 1);
  67. lua_pushvfstring(L, fmt, argp);
  68. va_end(argp);
  69. lua_concat(L, 2);
  70. return lua_error(L);
  71. }
  72. /* }====================================================== */
  73. LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,
  74. const char *const lst[]) {
  75. const char *name = (def) ? luaL_optstring(L, narg, def) :
  76. luaL_checkstring(L, narg);
  77. int i;
  78. for (i=0; lst[i]; i++)
  79. if (strcmp(lst[i], name) == 0)
  80. return i;
  81. return luaL_argerror(L, narg,
  82. lua_pushfstring(L, "invalid option " LUA_QS, name));
  83. }
  84. LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
  85. lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */
  86. if (!lua_isnil(L, -1)) /* name already in use? */
  87. return 0; /* leave previous value on top, but return 0 */
  88. lua_pop(L, 1);
  89. lua_newtable(L); /* create metatable */
  90. lua_pushvalue(L, -1);
  91. lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */
  92. return 1;
  93. }
  94. LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
  95. void *p = lua_touserdata(L, ud);
  96. if (p != NULL) { /* value is a userdata? */
  97. if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
  98. lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
  99. if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */
  100. lua_pop(L, 2); /* remove both metatables */
  101. return p;
  102. }
  103. }
  104. }
  105. luaL_typerror(L, ud, tname); /* else error */
  106. return NULL; /* to avoid warnings */
  107. }
  108. LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
  109. if (!lua_checkstack(L, space))
  110. luaL_error(L, "stack overflow (%s)", mes);
  111. }
  112. LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {
  113. if (lua_type(L, narg) != t)
  114. tag_error(L, narg, t);
  115. }
  116. LUALIB_API void luaL_checkany (lua_State *L, int narg) {
  117. if (lua_type(L, narg) == LUA_TNONE)
  118. luaL_argerror(L, narg, "value expected");
  119. }
  120. LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
  121. const char *s = lua_tolstring(L, narg, len);
  122. if (!s) tag_error(L, narg, LUA_TSTRING);
  123. return s;
  124. }
  125. LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
  126. const char *def, size_t *len) {
  127. if (lua_isnoneornil(L, narg)) {
  128. if (len)
  129. *len = (def ? strlen(def) : 0);
  130. return def;
  131. }
  132. else return luaL_checklstring(L, narg, len);
  133. }
  134. LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
  135. lua_Number d = lua_tonumber(L, narg);
  136. if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
  137. tag_error(L, narg, LUA_TNUMBER);
  138. return d;
  139. }
  140. LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
  141. return luaL_opt(L, luaL_checknumber, narg, def);
  142. }
  143. LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
  144. lua_Integer d = lua_tointeger(L, narg);
  145. if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
  146. tag_error(L, narg, LUA_TNUMBER);
  147. return d;
  148. }
  149. LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
  150. lua_Integer def) {
  151. return luaL_opt(L, luaL_checkinteger, narg, def);
  152. }
  153. LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
  154. if (!lua_getmetatable(L, obj)) /* no metatable? */
  155. return 0;
  156. lua_pushstring(L, event);
  157. lua_rawget(L, -2);
  158. if (lua_isnil(L, -1)) {
  159. lua_pop(L, 2); /* remove metatable and metafield */
  160. return 0;
  161. }
  162. else {
  163. lua_remove(L, -2); /* remove only metatable */
  164. return 1;
  165. }
  166. }
  167. LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
  168. obj = abs_index(L, obj);
  169. if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
  170. return 0;
  171. lua_pushvalue(L, obj);
  172. lua_call(L, 1, 1);
  173. return 1;
  174. }
  175. LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
  176. const luaL_Reg *l) {
  177. luaI_openlib(L, libname, l, 0);
  178. }
  179. static int libsize (const luaL_Reg *l) {
  180. int size = 0;
  181. for (; l->name; l++) size++;
  182. return size;
  183. }
  184. LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
  185. const luaL_Reg *l, int nup) {
  186. if (libname) {
  187. int size = libsize(l);
  188. /* check whether lib already exists */
  189. luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", size);
  190. lua_getfield(L, -1, libname); /* get _LOADED[libname] */
  191. if (!lua_istable(L, -1)) { /* not found? */
  192. lua_pop(L, 1); /* remove previous result */
  193. /* try global variable (and create one if it does not exist) */
  194. if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)
  195. luaL_error(L, "name conflict for module " LUA_QS, libname);
  196. lua_pushvalue(L, -1);
  197. lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */
  198. }
  199. lua_remove(L, -2); /* remove _LOADED table */
  200. lua_insert(L, -(nup+1)); /* move library table to below upvalues */
  201. }
  202. for (; l->name; l++) {
  203. int i;
  204. for (i=0; i<nup; i++) /* copy upvalues to the top */
  205. lua_pushvalue(L, -nup);
  206. lua_pushcclosure(L, l->func, nup);
  207. lua_setfield(L, -(nup+2), l->name);
  208. }
  209. lua_pop(L, nup); /* remove upvalues */
  210. }
  211. /*
  212. ** {======================================================
  213. ** getn-setn: size for arrays
  214. ** =======================================================
  215. */
  216. #if defined(LUA_COMPAT_GETN)
  217. static int checkint (lua_State *L, int topop) {
  218. int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1;
  219. lua_pop(L, topop);
  220. return n;
  221. }
  222. static void getsizes (lua_State *L) {
  223. lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES");
  224. if (lua_isnil(L, -1)) { /* no `size' table? */
  225. lua_pop(L, 1); /* remove nil */
  226. lua_newtable(L); /* create it */
  227. lua_pushvalue(L, -1); /* `size' will be its own metatable */
  228. lua_setmetatable(L, -2);
  229. lua_pushliteral(L, "kv");
  230. lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */
  231. lua_pushvalue(L, -1);
  232. lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); /* store in register */
  233. }
  234. }
  235. LUALIB_API void luaL_setn (lua_State *L, int t, int n) {
  236. t = abs_index(L, t);
  237. lua_pushliteral(L, "n");
  238. lua_rawget(L, t);
  239. if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */
  240. lua_pushliteral(L, "n"); /* use it */
  241. lua_pushinteger(L, n);
  242. lua_rawset(L, t);
  243. }
  244. else { /* use `sizes' */
  245. getsizes(L);
  246. lua_pushvalue(L, t);
  247. lua_pushinteger(L, n);
  248. lua_rawset(L, -3); /* sizes[t] = n */
  249. lua_pop(L, 1); /* remove `sizes' */
  250. }
  251. }
  252. LUALIB_API int luaL_getn (lua_State *L, int t) {
  253. int n;
  254. t = abs_index(L, t);
  255. lua_pushliteral(L, "n"); /* try t.n */
  256. lua_rawget(L, t);
  257. if ((n = checkint(L, 1)) >= 0) return n;
  258. getsizes(L); /* else try sizes[t] */
  259. lua_pushvalue(L, t);
  260. lua_rawget(L, -2);
  261. if ((n = checkint(L, 2)) >= 0) return n;
  262. return (int)lua_objlen(L, t);
  263. }
  264. #endif
  265. /* }====================================================== */
  266. LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
  267. const char *r) {
  268. const char *wild;
  269. size_t l = strlen(p);
  270. luaL_Buffer b;
  271. luaL_buffinit(L, &b);
  272. while ((wild = strstr(s, p)) != NULL) {
  273. luaL_addlstring(&b, s, wild - s); /* push prefix */
  274. luaL_addstring(&b, r); /* push replacement in place of pattern */
  275. s = wild + l; /* continue after `p' */
  276. }
  277. luaL_addstring(&b, s); /* push last suffix */
  278. luaL_pushresult(&b);
  279. return lua_tostring(L, -1);
  280. }
  281. LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
  282. const char *fname, int szhint) {
  283. const char *e;
  284. lua_pushvalue(L, idx);
  285. do {
  286. e = strchr(fname, '.');
  287. if (e == NULL) e = fname + strlen(fname);
  288. lua_pushlstring(L, fname, e - fname);
  289. lua_rawget(L, -2);
  290. if (lua_isnil(L, -1)) { /* no such field? */
  291. lua_pop(L, 1); /* remove this nil */
  292. lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
  293. lua_pushlstring(L, fname, e - fname);
  294. lua_pushvalue(L, -2);
  295. lua_settable(L, -4); /* set new table into field */
  296. }
  297. else if (!lua_istable(L, -1)) { /* field has a non-table value? */
  298. lua_pop(L, 2); /* remove table and value */
  299. return fname; /* return problematic part of the name */
  300. }
  301. lua_remove(L, -2); /* remove previous table */
  302. fname = e + 1;
  303. } while (*e == '.');
  304. return NULL;
  305. }
  306. /*
  307. ** {======================================================
  308. ** Generic Buffer manipulation
  309. ** =======================================================
  310. */
  311. #define bufflen(B) ((B)->p - (B)->buffer)
  312. #define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
  313. #define LIMIT (LUA_MINSTACK/2)
  314. static int emptybuffer (luaL_Buffer *B) {
  315. size_t l = bufflen(B);
  316. if (l == 0) return 0; /* put nothing on stack */
  317. else {
  318. lua_pushlstring(B->L, B->buffer, l);
  319. B->p = B->buffer;
  320. B->lvl++;
  321. return 1;
  322. }
  323. }
  324. static void adjuststack (luaL_Buffer *B) {
  325. if (B->lvl > 1) {
  326. lua_State *L = B->L;
  327. int toget = 1; /* number of levels to concat */
  328. size_t toplen = lua_strlen(L, -1);
  329. do {
  330. size_t l = lua_strlen(L, -(toget+1));
  331. if (B->lvl - toget + 1 >= LIMIT || toplen > l) {
  332. toplen += l;
  333. toget++;
  334. }
  335. else break;
  336. } while (toget < B->lvl);
  337. lua_concat(L, toget);
  338. B->lvl = B->lvl - toget + 1;
  339. }
  340. }
  341. LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
  342. if (emptybuffer(B))
  343. adjuststack(B);
  344. return B->buffer;
  345. }
  346. LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
  347. while (l--)
  348. luaL_addchar(B, *s++);
  349. }
  350. LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
  351. luaL_addlstring(B, s, strlen(s));
  352. }
  353. LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
  354. emptybuffer(B);
  355. lua_concat(B->L, B->lvl);
  356. B->lvl = 1;
  357. }
  358. LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
  359. lua_State *L = B->L;
  360. size_t vl;
  361. const char *s = lua_tolstring(L, -1, &vl);
  362. if (vl <= bufffree(B)) { /* fit into buffer? */
  363. memcpy(B->p, s, vl); /* put it there */
  364. B->p += vl;
  365. lua_pop(L, 1); /* remove from stack */
  366. }
  367. else {
  368. if (emptybuffer(B))
  369. lua_insert(L, -2); /* put buffer before new value */
  370. B->lvl++; /* add new value into B stack */
  371. adjuststack(B);
  372. }
  373. }
  374. LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
  375. B->L = L;
  376. B->p = B->buffer;
  377. B->lvl = 0;
  378. }
  379. /* }====================================================== */
  380. LUALIB_API int luaL_ref (lua_State *L, int t) {
  381. int ref;
  382. t = abs_index(L, t);
  383. if (lua_isnil(L, -1)) {
  384. lua_pop(L, 1); /* remove from stack */
  385. return LUA_REFNIL; /* `nil' has a unique fixed reference */
  386. }
  387. lua_rawgeti(L, t, FREELIST_REF); /* get first free element */
  388. ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */
  389. lua_pop(L, 1); /* remove it from stack */
  390. if (ref != 0) { /* any free element? */
  391. lua_rawgeti(L, t, ref); /* remove it from list */
  392. lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */
  393. }
  394. else { /* no free elements */
  395. ref = (int)lua_objlen(L, t);
  396. ref++; /* create new reference */
  397. }
  398. lua_rawseti(L, t, ref);
  399. return ref;
  400. }
  401. LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
  402. if (ref >= 0) {
  403. t = abs_index(L, t);
  404. lua_rawgeti(L, t, FREELIST_REF);
  405. lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */
  406. lua_pushinteger(L, ref);
  407. lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */
  408. }
  409. }
  410. /*
  411. ** {======================================================
  412. ** Load functions
  413. ** =======================================================
  414. */
  415. typedef struct LoadF {
  416. int extraline;
  417. FILE *f;
  418. char buff[LUAL_BUFFERSIZE];
  419. } LoadF;
  420. static const char *getF (lua_State *L, void *ud, size_t *size) {
  421. LoadF *lf = (LoadF *)ud;
  422. (void)L;
  423. if (lf->extraline) {
  424. lf->extraline = 0;
  425. *size = 1;
  426. return "\n";
  427. }
  428. if (feof(lf->f)) return NULL;
  429. *size = fread(lf->buff, 1, LUAL_BUFFERSIZE, lf->f);
  430. return (*size > 0) ? lf->buff : NULL;
  431. }
  432. static int errfile (lua_State *L, const char *what, int fnameindex) {
  433. const char *serr = strerror(errno);
  434. const char *filename = lua_tostring(L, fnameindex) + 1;
  435. lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
  436. lua_remove(L, fnameindex);
  437. return LUA_ERRFILE;
  438. }
  439. LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
  440. LoadF lf;
  441. int status, readstatus;
  442. int c;
  443. int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
  444. lf.extraline = 0;
  445. if (filename == NULL) {
  446. lua_pushliteral(L, "=stdin");
  447. lf.f = stdin;
  448. }
  449. else {
  450. lua_pushfstring(L, "@%s", filename);
  451. lf.f = fopen(filename, "r");
  452. if (lf.f == NULL) return errfile(L, "open", fnameindex);
  453. }
  454. c = getc(lf.f);
  455. if (c == '#') { /* Unix exec. file? */
  456. lf.extraline = 1;
  457. while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */
  458. if (c == '\n') c = getc(lf.f);
  459. }
  460. if (c == LUA_SIGNATURE[0] && lf.f != stdin) { /* binary file? */
  461. fclose(lf.f);
  462. lf.f = fopen(filename, "rb"); /* reopen in binary mode */
  463. if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
  464. /* skip eventual `#!...' */
  465. while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
  466. lf.extraline = 0;
  467. }
  468. ungetc(c, lf.f);
  469. status = lua_load(L, getF, &lf, lua_tostring(L, -1));
  470. readstatus = ferror(lf.f);
  471. if (lf.f != stdin) fclose(lf.f); /* close file (even in case of errors) */
  472. if (readstatus) {
  473. lua_settop(L, fnameindex); /* ignore results from `lua_load' */
  474. return errfile(L, "read", fnameindex);
  475. }
  476. lua_remove(L, fnameindex);
  477. return status;
  478. }
  479. typedef struct LoadS {
  480. const char *s;
  481. size_t size;
  482. } LoadS;
  483. static const char *getS (lua_State *L, void *ud, size_t *size) {
  484. LoadS *ls = (LoadS *)ud;
  485. (void)L;
  486. if (ls->size == 0) return NULL;
  487. *size = ls->size;
  488. ls->size = 0;
  489. return ls->s;
  490. }
  491. LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
  492. const char *name) {
  493. LoadS ls;
  494. ls.s = buff;
  495. ls.size = size;
  496. return lua_load(L, getS, &ls, name);
  497. }
  498. LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) {
  499. return luaL_loadbuffer(L, s, strlen(s), s);
  500. }
  501. /* }====================================================== */
  502. static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
  503. (void)ud;
  504. (void)osize;
  505. if (nsize == 0) {
  506. free(ptr);
  507. return NULL;
  508. }
  509. else
  510. return realloc(ptr, nsize);
  511. }
  512. static int panic (lua_State *L) {
  513. (void)L; /* to avoid warnings */
  514. fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
  515. lua_tostring(L, -1));
  516. return 0;
  517. }
  518. LUALIB_API lua_State *luaL_newstate (void) {
  519. lua_State *L = lua_newstate(l_alloc, NULL);
  520. if (L) lua_atpanic(L, &panic);
  521. return L;
  522. }