lobject.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /*
  2. ** $Id: lobject.c $
  3. ** Some generic functions over Lua objects
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lobject_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <locale.h>
  10. #include <math.h>
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "lua.h"
  16. #include "lctype.h"
  17. #include "ldebug.h"
  18. #include "ldo.h"
  19. #include "lmem.h"
  20. #include "lobject.h"
  21. #include "lstate.h"
  22. #include "lstring.h"
  23. #include "lvm.h"
  24. /*
  25. ** Computes ceil(log2(x))
  26. */
  27. int luaO_ceillog2 (unsigned int x) {
  28. static const lu_byte log_2[256] = { /* log_2[i - 1] = ceil(log2(i)) */
  29. 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  30. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  31. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  32. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  33. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  34. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  35. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  36. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
  37. };
  38. int l = 0;
  39. x--;
  40. while (x >= 256) { l += 8; x >>= 8; }
  41. return l + log_2[x];
  42. }
  43. /*
  44. ** Encodes 'p'% as a floating-point byte, represented as (eeeexxxx).
  45. ** The exponent is represented using excess-7. Mimicking IEEE 754, the
  46. ** representation normalizes the number when possible, assuming an extra
  47. ** 1 before the mantissa (xxxx) and adding one to the exponent (eeee)
  48. ** to signal that. So, the real value is (1xxxx) * 2^(eeee - 7 - 1) if
  49. ** eeee != 0, and (xxxx) * 2^-7 otherwise (subnormal numbers).
  50. */
  51. unsigned int luaO_codeparam (unsigned int p) {
  52. if (p >= (cast(lu_mem, 0x1F) << (0xF - 7 - 1)) * 100u) /* overflow? */
  53. return 0xFF; /* return maximum value */
  54. else {
  55. p = (cast(l_uint32, p) * 128 + 99) / 100; /* round up the division */
  56. if (p < 0x10) /* subnormal number? */
  57. return p; /* exponent bits are already zero; nothing else to do */
  58. else {
  59. int log = luaO_ceillog2(p + 1) - 5; /* preserve 5 bits */
  60. return ((p >> log) - 0x10) | ((log + 1) << 4);
  61. }
  62. }
  63. }
  64. /*
  65. ** Computes 'p' times 'x', where 'p' is a floating-point byte.
  66. */
  67. l_obj luaO_applyparam (unsigned int p, l_obj x) {
  68. unsigned int m = p & 0xF; /* mantissa */
  69. int e = (p >> 4); /* exponent */
  70. if (e > 0) { /* normalized? */
  71. e--;
  72. m += 0x10; /* maximum 'm' is 0x1F */
  73. }
  74. e -= 7; /* correct excess-7 */
  75. if (e < 0) {
  76. e = -e;
  77. if (x < MAX_LOBJ / 0x1F) /* multiplication cannot overflow? */
  78. return (x * m) >> e; /* multiplying first gives more precision */
  79. else if ((x >> e) < MAX_LOBJ / 0x1F) /* cannot overflow after shift? */
  80. return (x >> e) * m;
  81. else /* real overflow */
  82. return MAX_LOBJ;
  83. }
  84. else {
  85. if (x < (MAX_LOBJ / 0x1F) >> e) /* no overflow? */
  86. return (x * m) << e; /* order doesn't matter here */
  87. else /* real overflow */
  88. return MAX_LOBJ;
  89. }
  90. }
  91. static lua_Integer intarith (lua_State *L, int op, lua_Integer v1,
  92. lua_Integer v2) {
  93. switch (op) {
  94. case LUA_OPADD: return intop(+, v1, v2);
  95. case LUA_OPSUB:return intop(-, v1, v2);
  96. case LUA_OPMUL:return intop(*, v1, v2);
  97. case LUA_OPMOD: return luaV_mod(L, v1, v2);
  98. case LUA_OPIDIV: return luaV_idiv(L, v1, v2);
  99. case LUA_OPBAND: return intop(&, v1, v2);
  100. case LUA_OPBOR: return intop(|, v1, v2);
  101. case LUA_OPBXOR: return intop(^, v1, v2);
  102. case LUA_OPSHL: return luaV_shiftl(v1, v2);
  103. case LUA_OPSHR: return luaV_shiftr(v1, v2);
  104. case LUA_OPUNM: return intop(-, 0, v1);
  105. case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1);
  106. default: lua_assert(0); return 0;
  107. }
  108. }
  109. static lua_Number numarith (lua_State *L, int op, lua_Number v1,
  110. lua_Number v2) {
  111. switch (op) {
  112. case LUA_OPADD: return luai_numadd(L, v1, v2);
  113. case LUA_OPSUB: return luai_numsub(L, v1, v2);
  114. case LUA_OPMUL: return luai_nummul(L, v1, v2);
  115. case LUA_OPDIV: return luai_numdiv(L, v1, v2);
  116. case LUA_OPPOW: return luai_numpow(L, v1, v2);
  117. case LUA_OPIDIV: return luai_numidiv(L, v1, v2);
  118. case LUA_OPUNM: return luai_numunm(L, v1);
  119. case LUA_OPMOD: return luaV_modf(L, v1, v2);
  120. default: lua_assert(0); return 0;
  121. }
  122. }
  123. int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2,
  124. TValue *res) {
  125. switch (op) {
  126. case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
  127. case LUA_OPSHL: case LUA_OPSHR:
  128. case LUA_OPBNOT: { /* operate only on integers */
  129. lua_Integer i1; lua_Integer i2;
  130. if (tointegerns(p1, &i1) && tointegerns(p2, &i2)) {
  131. setivalue(res, intarith(L, op, i1, i2));
  132. return 1;
  133. }
  134. else return 0; /* fail */
  135. }
  136. case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */
  137. lua_Number n1; lua_Number n2;
  138. if (tonumberns(p1, n1) && tonumberns(p2, n2)) {
  139. setfltvalue(res, numarith(L, op, n1, n2));
  140. return 1;
  141. }
  142. else return 0; /* fail */
  143. }
  144. default: { /* other operations */
  145. lua_Number n1; lua_Number n2;
  146. if (ttisinteger(p1) && ttisinteger(p2)) {
  147. setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2)));
  148. return 1;
  149. }
  150. else if (tonumberns(p1, n1) && tonumberns(p2, n2)) {
  151. setfltvalue(res, numarith(L, op, n1, n2));
  152. return 1;
  153. }
  154. else return 0; /* fail */
  155. }
  156. }
  157. }
  158. void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
  159. StkId res) {
  160. if (!luaO_rawarith(L, op, p1, p2, s2v(res))) {
  161. /* could not perform raw operation; try metamethod */
  162. luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD));
  163. }
  164. }
  165. int luaO_hexavalue (int c) {
  166. if (lisdigit(c)) return c - '0';
  167. else return (ltolower(c) - 'a') + 10;
  168. }
  169. static int isneg (const char **s) {
  170. if (**s == '-') { (*s)++; return 1; }
  171. else if (**s == '+') (*s)++;
  172. return 0;
  173. }
  174. /*
  175. ** {==================================================================
  176. ** Lua's implementation for 'lua_strx2number'
  177. ** ===================================================================
  178. */
  179. #if !defined(lua_strx2number)
  180. /* maximum number of significant digits to read (to avoid overflows
  181. even with single floats) */
  182. #define MAXSIGDIG 30
  183. /*
  184. ** convert a hexadecimal numeric string to a number, following
  185. ** C99 specification for 'strtod'
  186. */
  187. static lua_Number lua_strx2number (const char *s, char **endptr) {
  188. int dot = lua_getlocaledecpoint();
  189. lua_Number r = l_mathop(0.0); /* result (accumulator) */
  190. int sigdig = 0; /* number of significant digits */
  191. int nosigdig = 0; /* number of non-significant digits */
  192. int e = 0; /* exponent correction */
  193. int neg; /* 1 if number is negative */
  194. int hasdot = 0; /* true after seen a dot */
  195. *endptr = cast_charp(s); /* nothing is valid yet */
  196. while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
  197. neg = isneg(&s); /* check sign */
  198. if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
  199. return l_mathop(0.0); /* invalid format (no '0x') */
  200. for (s += 2; ; s++) { /* skip '0x' and read numeral */
  201. if (*s == dot) {
  202. if (hasdot) break; /* second dot? stop loop */
  203. else hasdot = 1;
  204. }
  205. else if (lisxdigit(cast_uchar(*s))) {
  206. if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */
  207. nosigdig++;
  208. else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
  209. r = (r * l_mathop(16.0)) + luaO_hexavalue(*s);
  210. else e++; /* too many digits; ignore, but still count for exponent */
  211. if (hasdot) e--; /* decimal digit? correct exponent */
  212. }
  213. else break; /* neither a dot nor a digit */
  214. }
  215. if (nosigdig + sigdig == 0) /* no digits? */
  216. return l_mathop(0.0); /* invalid format */
  217. *endptr = cast_charp(s); /* valid up to here */
  218. e *= 4; /* each digit multiplies/divides value by 2^4 */
  219. if (*s == 'p' || *s == 'P') { /* exponent part? */
  220. int exp1 = 0; /* exponent value */
  221. int neg1; /* exponent sign */
  222. s++; /* skip 'p' */
  223. neg1 = isneg(&s); /* sign */
  224. if (!lisdigit(cast_uchar(*s)))
  225. return l_mathop(0.0); /* invalid; must have at least one digit */
  226. while (lisdigit(cast_uchar(*s))) /* read exponent */
  227. exp1 = exp1 * 10 + *(s++) - '0';
  228. if (neg1) exp1 = -exp1;
  229. e += exp1;
  230. *endptr = cast_charp(s); /* valid up to here */
  231. }
  232. if (neg) r = -r;
  233. return l_mathop(ldexp)(r, e);
  234. }
  235. #endif
  236. /* }====================================================== */
  237. /* maximum length of a numeral to be converted to a number */
  238. #if !defined (L_MAXLENNUM)
  239. #define L_MAXLENNUM 200
  240. #endif
  241. /*
  242. ** Convert string 's' to a Lua number (put in 'result'). Return NULL on
  243. ** fail or the address of the ending '\0' on success. ('mode' == 'x')
  244. ** means a hexadecimal numeral.
  245. */
  246. static const char *l_str2dloc (const char *s, lua_Number *result, int mode) {
  247. char *endptr;
  248. *result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */
  249. : lua_str2number(s, &endptr);
  250. if (endptr == s) return NULL; /* nothing recognized? */
  251. while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */
  252. return (*endptr == '\0') ? endptr : NULL; /* OK iff no trailing chars */
  253. }
  254. /*
  255. ** Convert string 's' to a Lua number (put in 'result') handling the
  256. ** current locale.
  257. ** This function accepts both the current locale or a dot as the radix
  258. ** mark. If the conversion fails, it may mean number has a dot but
  259. ** locale accepts something else. In that case, the code copies 's'
  260. ** to a buffer (because 's' is read-only), changes the dot to the
  261. ** current locale radix mark, and tries to convert again.
  262. ** The variable 'mode' checks for special characters in the string:
  263. ** - 'n' means 'inf' or 'nan' (which should be rejected)
  264. ** - 'x' means a hexadecimal numeral
  265. ** - '.' just optimizes the search for the common case (no special chars)
  266. */
  267. static const char *l_str2d (const char *s, lua_Number *result) {
  268. const char *endptr;
  269. const char *pmode = strpbrk(s, ".xXnN"); /* look for special chars */
  270. int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0;
  271. if (mode == 'n') /* reject 'inf' and 'nan' */
  272. return NULL;
  273. endptr = l_str2dloc(s, result, mode); /* try to convert */
  274. if (endptr == NULL) { /* failed? may be a different locale */
  275. char buff[L_MAXLENNUM + 1];
  276. const char *pdot = strchr(s, '.');
  277. if (pdot == NULL || strlen(s) > L_MAXLENNUM)
  278. return NULL; /* string too long or no dot; fail */
  279. strcpy(buff, s); /* copy string to buffer */
  280. buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */
  281. endptr = l_str2dloc(buff, result, mode); /* try again */
  282. if (endptr != NULL)
  283. endptr = s + (endptr - buff); /* make relative to 's' */
  284. }
  285. return endptr;
  286. }
  287. #define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10)
  288. #define MAXLASTD cast_int(LUA_MAXINTEGER % 10)
  289. static const char *l_str2int (const char *s, lua_Integer *result) {
  290. lua_Unsigned a = 0;
  291. int empty = 1;
  292. int neg;
  293. while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
  294. neg = isneg(&s);
  295. if (s[0] == '0' &&
  296. (s[1] == 'x' || s[1] == 'X')) { /* hex? */
  297. s += 2; /* skip '0x' */
  298. for (; lisxdigit(cast_uchar(*s)); s++) {
  299. a = a * 16 + luaO_hexavalue(*s);
  300. empty = 0;
  301. }
  302. }
  303. else { /* decimal */
  304. for (; lisdigit(cast_uchar(*s)); s++) {
  305. int d = *s - '0';
  306. if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */
  307. return NULL; /* do not accept it (as integer) */
  308. a = a * 10 + d;
  309. empty = 0;
  310. }
  311. }
  312. while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */
  313. if (empty || *s != '\0') return NULL; /* something wrong in the numeral */
  314. else {
  315. *result = l_castU2S((neg) ? 0u - a : a);
  316. return s;
  317. }
  318. }
  319. size_t luaO_str2num (const char *s, TValue *o) {
  320. lua_Integer i; lua_Number n;
  321. const char *e;
  322. if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */
  323. setivalue(o, i);
  324. }
  325. else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */
  326. setfltvalue(o, n);
  327. }
  328. else
  329. return 0; /* conversion failed */
  330. return (e - s) + 1; /* success; return string size */
  331. }
  332. int luaO_utf8esc (char *buff, unsigned long x) {
  333. int n = 1; /* number of bytes put in buffer (backwards) */
  334. lua_assert(x <= 0x7FFFFFFFu);
  335. if (x < 0x80) /* ascii? */
  336. buff[UTF8BUFFSZ - 1] = cast_char(x);
  337. else { /* need continuation bytes */
  338. unsigned int mfb = 0x3f; /* maximum that fits in first byte */
  339. do { /* add continuation bytes */
  340. buff[UTF8BUFFSZ - (n++)] = cast_char(0x80 | (x & 0x3f));
  341. x >>= 6; /* remove added bits */
  342. mfb >>= 1; /* now there is one less bit available in first byte */
  343. } while (x > mfb); /* still needs continuation byte? */
  344. buff[UTF8BUFFSZ - n] = cast_char((~mfb << 1) | x); /* add first byte */
  345. }
  346. return n;
  347. }
  348. /*
  349. ** Maximum length of the conversion of a number to a string. Must be
  350. ** enough to accommodate both LUA_INTEGER_FMT and LUA_NUMBER_FMT.
  351. ** (For a long long int, this is 19 digits plus a sign and a final '\0',
  352. ** adding to 21. For a long double, it can go to a sign, 33 digits,
  353. ** the dot, an exponent letter, an exponent sign, 5 exponent digits,
  354. ** and a final '\0', adding to 43.)
  355. */
  356. #define MAXNUMBER2STR 44
  357. /*
  358. ** Convert a number object to a string, adding it to a buffer
  359. */
  360. static int tostringbuff (TValue *obj, char *buff) {
  361. int len;
  362. lua_assert(ttisnumber(obj));
  363. if (ttisinteger(obj))
  364. len = lua_integer2str(buff, MAXNUMBER2STR, ivalue(obj));
  365. else {
  366. len = lua_number2str(buff, MAXNUMBER2STR, fltvalue(obj));
  367. if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
  368. buff[len++] = lua_getlocaledecpoint();
  369. buff[len++] = '0'; /* adds '.0' to result */
  370. }
  371. }
  372. return len;
  373. }
  374. /*
  375. ** Convert a number object to a Lua string, replacing the value at 'obj'
  376. */
  377. void luaO_tostring (lua_State *L, TValue *obj) {
  378. char buff[MAXNUMBER2STR];
  379. int len = tostringbuff(obj, buff);
  380. setsvalue(L, obj, luaS_newlstr(L, buff, len));
  381. }
  382. /*
  383. ** {==================================================================
  384. ** 'luaO_pushvfstring'
  385. ** ===================================================================
  386. */
  387. /*
  388. ** Size for buffer space used by 'luaO_pushvfstring'. It should be
  389. ** (LUA_IDSIZE + MAXNUMBER2STR) + a minimal space for basic messages,
  390. ** so that 'luaG_addinfo' can work directly on the buffer.
  391. */
  392. #define BUFVFS (LUA_IDSIZE + MAXNUMBER2STR + 95)
  393. /* buffer used by 'luaO_pushvfstring' */
  394. typedef struct BuffFS {
  395. lua_State *L;
  396. int pushed; /* true if there is a part of the result on the stack */
  397. int blen; /* length of partial string in 'space' */
  398. char space[BUFVFS]; /* holds last part of the result */
  399. } BuffFS;
  400. /*
  401. ** Push given string to the stack, as part of the result, and
  402. ** join it to previous partial result if there is one.
  403. ** It may call 'luaV_concat' while using one slot from EXTRA_STACK.
  404. ** This call cannot invoke metamethods, as both operands must be
  405. ** strings. It can, however, raise an error if the result is too
  406. ** long. In that case, 'luaV_concat' frees the extra slot before
  407. ** raising the error.
  408. */
  409. static void pushstr (BuffFS *buff, const char *str, size_t lstr) {
  410. lua_State *L = buff->L;
  411. setsvalue2s(L, L->top.p, luaS_newlstr(L, str, lstr));
  412. L->top.p++; /* may use one slot from EXTRA_STACK */
  413. if (!buff->pushed) /* no previous string on the stack? */
  414. buff->pushed = 1; /* now there is one */
  415. else /* join previous string with new one */
  416. luaV_concat(L, 2);
  417. }
  418. /*
  419. ** empty the buffer space into the stack
  420. */
  421. static void clearbuff (BuffFS *buff) {
  422. pushstr(buff, buff->space, buff->blen); /* push buffer contents */
  423. buff->blen = 0; /* space now is empty */
  424. }
  425. /*
  426. ** Get a space of size 'sz' in the buffer. If buffer has not enough
  427. ** space, empty it. 'sz' must fit in an empty buffer.
  428. */
  429. static char *getbuff (BuffFS *buff, int sz) {
  430. lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS);
  431. if (sz > BUFVFS - buff->blen) /* not enough space? */
  432. clearbuff(buff);
  433. return buff->space + buff->blen;
  434. }
  435. #define addsize(b,sz) ((b)->blen += (sz))
  436. /*
  437. ** Add 'str' to the buffer. If string is larger than the buffer space,
  438. ** push the string directly to the stack.
  439. */
  440. static void addstr2buff (BuffFS *buff, const char *str, size_t slen) {
  441. if (slen <= BUFVFS) { /* does string fit into buffer? */
  442. char *bf = getbuff(buff, cast_int(slen));
  443. memcpy(bf, str, slen); /* add string to buffer */
  444. addsize(buff, cast_int(slen));
  445. }
  446. else { /* string larger than buffer */
  447. clearbuff(buff); /* string comes after buffer's content */
  448. pushstr(buff, str, slen); /* push string */
  449. }
  450. }
  451. /*
  452. ** Add a numeral to the buffer.
  453. */
  454. static void addnum2buff (BuffFS *buff, TValue *num) {
  455. char *numbuff = getbuff(buff, MAXNUMBER2STR);
  456. int len = tostringbuff(num, numbuff); /* format number into 'numbuff' */
  457. addsize(buff, len);
  458. }
  459. /*
  460. ** this function handles only '%d', '%c', '%f', '%p', '%s', and '%%'
  461. conventional formats, plus Lua-specific '%I' and '%U'
  462. */
  463. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  464. BuffFS buff; /* holds last part of the result */
  465. const char *e; /* points to next '%' */
  466. buff.pushed = buff.blen = 0;
  467. buff.L = L;
  468. while ((e = strchr(fmt, '%')) != NULL) {
  469. addstr2buff(&buff, fmt, e - fmt); /* add 'fmt' up to '%' */
  470. switch (*(e + 1)) { /* conversion specifier */
  471. case 's': { /* zero-terminated string */
  472. const char *s = va_arg(argp, char *);
  473. if (s == NULL) s = "(null)";
  474. addstr2buff(&buff, s, strlen(s));
  475. break;
  476. }
  477. case 'c': { /* an 'int' as a character */
  478. char c = cast_uchar(va_arg(argp, int));
  479. addstr2buff(&buff, &c, sizeof(char));
  480. break;
  481. }
  482. case 'd': { /* an 'int' */
  483. TValue num;
  484. setivalue(&num, va_arg(argp, int));
  485. addnum2buff(&buff, &num);
  486. break;
  487. }
  488. case 'I': { /* a 'lua_Integer' */
  489. TValue num;
  490. setivalue(&num, cast(lua_Integer, va_arg(argp, l_uacInt)));
  491. addnum2buff(&buff, &num);
  492. break;
  493. }
  494. case 'f': { /* a 'lua_Number' */
  495. TValue num;
  496. setfltvalue(&num, cast_num(va_arg(argp, l_uacNumber)));
  497. addnum2buff(&buff, &num);
  498. break;
  499. }
  500. case 'p': { /* a pointer */
  501. const int sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */
  502. char *bf = getbuff(&buff, sz);
  503. void *p = va_arg(argp, void *);
  504. int len = lua_pointer2str(bf, sz, p);
  505. addsize(&buff, len);
  506. break;
  507. }
  508. case 'U': { /* a 'long' as a UTF-8 sequence */
  509. char bf[UTF8BUFFSZ];
  510. int len = luaO_utf8esc(bf, va_arg(argp, long));
  511. addstr2buff(&buff, bf + UTF8BUFFSZ - len, len);
  512. break;
  513. }
  514. case '%': {
  515. addstr2buff(&buff, "%", 1);
  516. break;
  517. }
  518. default: {
  519. luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'",
  520. *(e + 1));
  521. }
  522. }
  523. fmt = e + 2; /* skip '%' and the specifier */
  524. }
  525. addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */
  526. clearbuff(&buff); /* empty buffer into the stack */
  527. lua_assert(buff.pushed == 1);
  528. return getstr(tsvalue(s2v(L->top.p - 1)));
  529. }
  530. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  531. const char *msg;
  532. va_list argp;
  533. va_start(argp, fmt);
  534. msg = luaO_pushvfstring(L, fmt, argp);
  535. va_end(argp);
  536. return msg;
  537. }
  538. /* }================================================================== */
  539. #define RETS "..."
  540. #define PRE "[string \""
  541. #define POS "\"]"
  542. #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
  543. void luaO_chunkid (char *out, const char *source, size_t srclen) {
  544. size_t bufflen = LUA_IDSIZE; /* free space in buffer */
  545. if (*source == '=') { /* 'literal' source */
  546. if (srclen <= bufflen) /* small enough? */
  547. memcpy(out, source + 1, srclen * sizeof(char));
  548. else { /* truncate it */
  549. addstr(out, source + 1, bufflen - 1);
  550. *out = '\0';
  551. }
  552. }
  553. else if (*source == '@') { /* file name */
  554. if (srclen <= bufflen) /* small enough? */
  555. memcpy(out, source + 1, srclen * sizeof(char));
  556. else { /* add '...' before rest of name */
  557. addstr(out, RETS, LL(RETS));
  558. bufflen -= LL(RETS);
  559. memcpy(out, source + 1 + srclen - bufflen, bufflen * sizeof(char));
  560. }
  561. }
  562. else { /* string; format as [string "source"] */
  563. const char *nl = strchr(source, '\n'); /* find first new line (if any) */
  564. addstr(out, PRE, LL(PRE)); /* add prefix */
  565. bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
  566. if (srclen < bufflen && nl == NULL) { /* small one-line source? */
  567. addstr(out, source, srclen); /* keep it */
  568. }
  569. else {
  570. if (nl != NULL) srclen = nl - source; /* stop at first newline */
  571. if (srclen > bufflen) srclen = bufflen;
  572. addstr(out, source, srclen);
  573. addstr(out, RETS, LL(RETS));
  574. }
  575. memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
  576. }
  577. }