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