Line data Source code
1 : /*
2 : ** $Id: lobject.c,v 2.113.1.1 2017/04/19 17:29:57 roberto Exp $
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 : LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};
34 :
35 :
36 : /*
37 : ** converts an integer to a "floating point byte", represented as
38 : ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
39 : ** eeeee != 0 and (xxx) otherwise.
40 : */
41 2762 : int luaO_int2fb (unsigned int x) {
42 2762 : int e = 0; /* exponent */
43 2762 : if (x < 8) return x;
44 133 : while (x >= (8 << 4)) { /* coarse steps */
45 0 : x = (x + 0xf) >> 4; /* x = ceil(x / 16) */
46 0 : e += 4;
47 : }
48 166 : while (x >= (8 << 1)) { /* fine steps */
49 33 : x = (x + 1) >> 1; /* x = ceil(x / 2) */
50 33 : e++;
51 : }
52 133 : return ((e+1) << 3) | (cast_int(x) - 8);
53 : }
54 :
55 :
56 : /* converts back */
57 124330 : int luaO_fb2int (int x) {
58 124330 : return (x < 8) ? x : ((x & 7) + 8) << ((x >> 3) - 1);
59 : }
60 :
61 :
62 : /*
63 : ** Computes ceil(log2(x))
64 : */
65 106550 : int luaO_ceillog2 (unsigned int x) {
66 : #if defined(ARDUPILOT_BUILD) && (defined(__GNUC__) || defined(__clang__))
67 106550 : const int bitwidth = CHAR_BIT*sizeof(x);
68 106550 : x--; // 0 is outside function domain
69 106550 : const int clz = x ? __builtin_clz(x) : bitwidth; // clz(0) is undefined
70 106550 : return bitwidth-clz;
71 : #else
72 : static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */
73 : 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,
74 : 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,
75 : 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,
76 : 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,
77 : 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,
78 : 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,
79 : 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,
80 : 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
81 : };
82 : int l = 0;
83 : x--;
84 : while (x >= 256) { l += 8; x >>= 8; }
85 : return l + log_2[x];
86 : #endif
87 : }
88 :
89 :
90 725 : static lua_Integer intarith (lua_State *L, int op, lua_Integer v1,
91 : lua_Integer v2) {
92 725 : switch (op) {
93 34 : case LUA_OPADD: return intop(+, v1, v2);
94 10 : case LUA_OPSUB:return intop(-, v1, v2);
95 30 : case LUA_OPMUL:return intop(*, v1, v2);
96 4 : case LUA_OPMOD: return luaV_mod(L, v1, v2);
97 0 : case LUA_OPIDIV: return luaV_div(L, v1, v2);
98 0 : case LUA_OPBAND: return intop(&, v1, v2);
99 0 : case LUA_OPBOR: return intop(|, v1, v2);
100 0 : case LUA_OPBXOR: return intop(^, v1, v2);
101 54 : case LUA_OPSHL: return luaV_shiftl(v1, v2);
102 0 : case LUA_OPSHR: return luaV_shiftl(v1, -v2);
103 591 : case LUA_OPUNM: return intop(-, 0, v1);
104 2 : case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1);
105 0 : default: lua_assert(0); return 0;
106 : }
107 : }
108 :
109 :
110 231 : static lua_Number numarith (lua_State *L, int op, lua_Number v1,
111 : lua_Number v2) {
112 231 : switch (op) {
113 8 : case LUA_OPADD: return luai_numadd(L, v1, v2);
114 10 : case LUA_OPSUB: return luai_numsub(L, v1, v2);
115 2 : case LUA_OPMUL: return luai_nummul(L, v1, v2);
116 19 : case LUA_OPDIV: return luai_numdiv(L, v1, v2);
117 36 : case LUA_OPPOW: return luai_numpow(L, v1, v2);
118 4 : case LUA_OPIDIV: return luai_numidiv(L, v1, v2);
119 148 : case LUA_OPUNM: return luai_numunm(L, v1);
120 4 : case LUA_OPMOD: {
121 : lua_Number m;
122 4 : luai_nummod(L, v1, v2, m);
123 4 : return m;
124 : }
125 0 : default: lua_assert(0); return 0;
126 : }
127 : }
128 :
129 :
130 956 : void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
131 : TValue *res) {
132 956 : switch (op) {
133 56 : case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
134 : case LUA_OPSHL: case LUA_OPSHR:
135 : case LUA_OPBNOT: { /* operate only on integers */
136 : lua_Integer i1; lua_Integer i2;
137 56 : if (tointeger(p1, &i1) && tointeger(p2, &i2)) {
138 56 : setivalue(res, intarith(L, op, i1, i2));
139 56 : return;
140 : }
141 : else break; /* go to the end */
142 : }
143 55 : case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */
144 : lua_Number n1; lua_Number n2;
145 55 : if (tonumber(p1, &n1) && tonumber(p2, &n2)) {
146 55 : setfltvalue(res, numarith(L, op, n1, n2));
147 55 : return;
148 : }
149 : else break; /* go to the end */
150 : }
151 845 : default: { /* other operations */
152 : lua_Number n1; lua_Number n2;
153 845 : if (ttisinteger(p1) && ttisinteger(p2)) {
154 669 : setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2)));
155 845 : return;
156 : }
157 176 : else if (tonumber(p1, &n1) && tonumber(p2, &n2)) {
158 176 : setfltvalue(res, numarith(L, op, n1, n2));
159 176 : return;
160 : }
161 : else break; /* go to the end */
162 : }
163 : }
164 : /* could not perform raw operation; try metamethod */
165 : lua_assert(L != NULL); /* should not fail when folding (compile time) */
166 0 : luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD));
167 : }
168 :
169 :
170 5443 : int luaO_hexavalue (int c) {
171 5443 : if (lisdigit(c)) return c - '0';
172 2202 : else return (ltolower(c) - 'a') + 10;
173 : }
174 :
175 :
176 11853 : static int isneg (const char **s) {
177 11853 : if (**s == '-') { (*s)++; return 1; }
178 11789 : else if (**s == '+') (*s)++;
179 11789 : return 0;
180 : }
181 :
182 :
183 :
184 : /*
185 : ** {==================================================================
186 : ** Lua's implementation for 'lua_strx2number'
187 : ** ===================================================================
188 : */
189 :
190 : #if !defined(lua_strx2number)
191 :
192 : /* maximum number of significant digits to read (to avoid overflows
193 : even with single floats) */
194 : #define MAXSIGDIG 30
195 :
196 : /*
197 : ** convert an hexadecimal numeric string to a number, following
198 : ** C99 specification for 'strtod'
199 : */
200 : static lua_Number lua_strx2number (const char *s, char **endptr) {
201 : int dot = lua_getlocaledecpoint();
202 : lua_Number r = 0.0; /* result (accumulator) */
203 : int sigdig = 0; /* number of significant digits */
204 : int nosigdig = 0; /* number of non-significant digits */
205 : int e = 0; /* exponent correction */
206 : int neg; /* 1 if number is negative */
207 : int hasdot = 0; /* true after seen a dot */
208 : *endptr = cast(char *, s); /* nothing is valid yet */
209 : while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
210 : neg = isneg(&s); /* check signal */
211 : if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
212 : return 0.0; /* invalid format (no '0x') */
213 : for (s += 2; ; s++) { /* skip '0x' and read numeral */
214 : if (*s == dot) {
215 : if (hasdot) break; /* second dot? stop loop */
216 : else hasdot = 1;
217 : }
218 : else if (lisxdigit(cast_uchar(*s))) {
219 : if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */
220 : nosigdig++;
221 : else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
222 : r = (r * cast_num(16.0)) + luaO_hexavalue(*s);
223 : else e++; /* too many digits; ignore, but still count for exponent */
224 : if (hasdot) e--; /* decimal digit? correct exponent */
225 : }
226 : else break; /* neither a dot nor a digit */
227 : }
228 : if (nosigdig + sigdig == 0) /* no digits? */
229 : return 0.0; /* invalid format */
230 : *endptr = cast(char *, s); /* valid up to here */
231 : e *= 4; /* each digit multiplies/divides value by 2^4 */
232 : if (*s == 'p' || *s == 'P') { /* exponent part? */
233 : int exp1 = 0; /* exponent value */
234 : int neg1; /* exponent signal */
235 : s++; /* skip 'p' */
236 : neg1 = isneg(&s); /* signal */
237 : if (!lisdigit(cast_uchar(*s)))
238 : return 0.0; /* invalid; must have at least one digit */
239 : while (lisdigit(cast_uchar(*s))) /* read exponent */
240 : exp1 = exp1 * 10 + *(s++) - '0';
241 : if (neg1) exp1 = -exp1;
242 : e += exp1;
243 : *endptr = cast(char *, s); /* valid up to here */
244 : }
245 : if (neg) r = -r;
246 : return l_mathop(ldexp)(r, e);
247 : }
248 :
249 : #endif
250 : /* }====================================================== */
251 :
252 :
253 : /* maximum length of a numeral */
254 : #if !defined (L_MAXLENNUM)
255 : #define L_MAXLENNUM 200
256 : #endif
257 :
258 2065 : static const char *l_str2dloc (const char *s, lua_Number *result, int mode) {
259 : char *endptr;
260 111 : *result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */
261 2065 : : lua_str2number(s, &endptr);
262 2065 : if (endptr == s) return NULL; /* nothing recognized? */
263 1994 : while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */
264 1974 : return (*endptr == '\0') ? endptr : NULL; /* OK if no trailing characters */
265 : }
266 :
267 :
268 : /*
269 : ** Convert string 's' to a Lua number (put in 'result'). Return NULL
270 : ** on fail or the address of the ending '\0' on success.
271 : ** 'pmode' points to (and 'mode' contains) special things in the string:
272 : ** - 'x'/'X' means an hexadecimal numeral
273 : ** - 'n'/'N' means 'inf' or 'nan' (which should be rejected)
274 : ** - '.' just optimizes the search for the common case (nothing special)
275 : ** This function accepts both the current locale or a dot as the radix
276 : ** mark. If the conversion fails, it may mean number has a dot but
277 : ** locale accepts something else. In that case, the code copies 's'
278 : ** to a buffer (because 's' is read-only), changes the dot to the
279 : ** current locale radix mark, and tries to convert again.
280 : */
281 2060 : static const char *l_str2d (const char *s, lua_Number *result) {
282 : const char *endptr;
283 2060 : const char *pmode = strpbrk(s, ".xXnN");
284 2060 : int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0;
285 2060 : if (mode == 'n') /* reject 'inf' and 'nan' */
286 5 : return NULL;
287 2055 : endptr = l_str2dloc(s, result, mode); /* try to convert */
288 2055 : if (endptr == NULL) { /* failed? may be a different locale */
289 : char buff[L_MAXLENNUM + 1];
290 100 : const char *pdot = strchr(s, '.');
291 100 : if (strlen(s) > L_MAXLENNUM || pdot == NULL)
292 90 : return NULL; /* string too long or no dot; fail */
293 10 : strcpy(buff, s); /* copy string to buffer */
294 10 : buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */
295 10 : endptr = l_str2dloc(buff, result, mode); /* try again */
296 10 : if (endptr != NULL)
297 0 : endptr = s + (endptr - buff); /* make relative to 's' */
298 : }
299 1965 : return endptr;
300 : }
301 :
302 :
303 : #define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10)
304 : #define MAXLASTD cast_int(LUA_MAXINTEGER % 10)
305 :
306 11853 : static const char *l_str2int (const char *s, lua_Integer *result) {
307 11853 : lua_Unsigned a = 0;
308 11853 : int empty = 1;
309 : int neg;
310 11893 : while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
311 11853 : neg = isneg(&s);
312 11853 : if (s[0] == '0' &&
313 3717 : (s[1] == 'x' || s[1] == 'X')) { /* hex? */
314 171 : s += 2; /* skip '0x' */
315 5574 : for (; lisxdigit(cast_uchar(*s)); s++) {
316 5403 : a = a * 16 + luaO_hexavalue(*s);
317 5403 : empty = 0;
318 : }
319 : }
320 : else { /* decimal */
321 27838 : for (; lisdigit(cast_uchar(*s)); s++) {
322 16182 : int d = *s - '0';
323 16182 : if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */
324 26 : return NULL; /* do not accept it (as integer) */
325 16156 : a = a * 10 + d;
326 16156 : empty = 0;
327 : }
328 : }
329 11854 : while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */
330 11827 : if (empty || *s != '\0') return NULL; /* something wrong in the numeral */
331 : else {
332 9793 : *result = l_castU2S((neg) ? 0u - a : a);
333 9793 : return s;
334 : }
335 : }
336 :
337 :
338 11853 : size_t luaO_str2num (const char *s, TValue *o) {
339 : lua_Integer i; lua_Number n;
340 : const char *e;
341 11853 : if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */
342 9793 : setivalue(o, i);
343 : }
344 2060 : else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */
345 1955 : setfltvalue(o, n);
346 : }
347 : else
348 105 : return 0; /* conversion failed */
349 11748 : return (e - s) + 1; /* success; return string size */
350 : }
351 :
352 :
353 0 : int luaO_utf8esc (char *buff, unsigned long x) {
354 0 : int n = 1; /* number of bytes put in buffer (backwards) */
355 : lua_assert(x <= 0x10FFFF);
356 0 : if (x < 0x80) /* ascii? */
357 0 : buff[UTF8BUFFSZ - 1] = cast(char, x);
358 : else { /* need continuation bytes */
359 0 : unsigned int mfb = 0x3f; /* maximum that fits in first byte */
360 : do { /* add continuation bytes */
361 0 : buff[UTF8BUFFSZ - (n++)] = cast(char, 0x80 | (x & 0x3f));
362 0 : x >>= 6; /* remove added bits */
363 0 : mfb >>= 1; /* now there is one less bit available in first byte */
364 0 : } while (x > mfb); /* still needs continuation byte? */
365 0 : buff[UTF8BUFFSZ - n] = cast(char, (~mfb << 1) | x); /* add first byte */
366 : }
367 0 : return n;
368 : }
369 :
370 :
371 : /* maximum length of the conversion of a number to a string */
372 : #define MAXNUMBER2STR 50
373 :
374 :
375 : /*
376 : ** Convert a number object to a string
377 : */
378 361 : void luaO_tostring (lua_State *L, StkId obj) {
379 : char buff[MAXNUMBER2STR];
380 : size_t len;
381 : lua_assert(ttisnumber(obj));
382 361 : if (ttisinteger(obj))
383 228 : len = lua_integer2str(buff, sizeof(buff), ivalue(obj));
384 : else {
385 133 : len = lua_number2str(buff, sizeof(buff), fltvalue(obj));
386 : #if !defined(LUA_COMPAT_FLOATSTRING)
387 133 : if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
388 108 : buff[len++] = lua_getlocaledecpoint();
389 108 : buff[len++] = '0'; /* adds '.0' to result */
390 : }
391 : #endif
392 : }
393 361 : setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
394 361 : }
395 :
396 :
397 1886 : static void pushstr (lua_State *L, const char *str, size_t l) {
398 1886 : setsvalue2s(L, L->top, luaS_newlstr(L, str, l));
399 1886 : luaD_inctop(L);
400 1886 : }
401 :
402 :
403 : /*
404 : ** this function handles only '%d', '%c', '%f', '%p', and '%s'
405 : conventional formats, plus Lua-specific '%I' and '%U'
406 : */
407 664 : const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
408 664 : int n = 0;
409 775 : for (;;) {
410 1439 : const char *e = strchr(fmt, '%');
411 1439 : if (e == NULL) break;
412 775 : pushstr(L, fmt, e - fmt);
413 775 : switch (*(e+1)) {
414 441 : case 's': { /* zero-terminated string */
415 441 : const char *s = va_arg(argp, char *);
416 441 : if (s == NULL) s = "(null)";
417 441 : pushstr(L, s, strlen(s));
418 441 : break;
419 : }
420 2 : case 'c': { /* an 'int' as a character */
421 2 : char buff = cast(char, va_arg(argp, int));
422 2 : if (lisprint(cast_uchar(buff)))
423 2 : pushstr(L, &buff, 1);
424 : else /* non-printable character; print its code */
425 0 : luaO_pushfstring(L, "<\\%d>", cast_uchar(buff));
426 2 : break;
427 : }
428 34 : case 'd': { /* an 'int' */
429 34 : setivalue(L->top, va_arg(argp, int));
430 34 : goto top2str;
431 : }
432 186 : case 'I': { /* a 'lua_Integer' */
433 186 : setivalue(L->top, cast(lua_Integer, va_arg(argp, l_uacInt)));
434 186 : goto top2str;
435 : }
436 108 : case 'f': { /* a 'lua_Number' */
437 108 : setfltvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
438 328 : top2str: /* convert the top element to a string */
439 328 : luaD_inctop(L);
440 328 : luaO_tostring(L, L->top - 1);
441 328 : break;
442 : }
443 2 : case 'p': { /* a pointer */
444 : char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */
445 2 : void *p = va_arg(argp, void *);
446 2 : int l = lua_pointer2str(buff, sizeof(buff), p);
447 2 : pushstr(L, buff, l);
448 2 : break;
449 : }
450 : #if !defined(ARDUPILOT_BUILD)
451 : case 'U': { /* an 'int' as a UTF-8 sequence */
452 : char buff[UTF8BUFFSZ];
453 : int l = luaO_utf8esc(buff, cast(long, va_arg(argp, long)));
454 : pushstr(L, buff + UTF8BUFFSZ - l, l);
455 : break;
456 : }
457 : #endif
458 2 : case '%': {
459 2 : pushstr(L, "%", 1);
460 2 : break;
461 : }
462 0 : default: {
463 0 : luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'",
464 0 : *(e + 1));
465 : }
466 : }
467 775 : n += 2;
468 775 : fmt = e+2;
469 : }
470 664 : luaD_checkstack(L, 1);
471 664 : pushstr(L, fmt, strlen(fmt));
472 664 : if (n > 0) luaV_concat(L, n + 1);
473 664 : return svalue(L->top - 1);
474 : }
475 :
476 :
477 16 : const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
478 : const char *msg;
479 : va_list argp;
480 16 : va_start(argp, fmt);
481 16 : msg = luaO_pushvfstring(L, fmt, argp);
482 16 : va_end(argp);
483 16 : return msg;
484 : }
485 :
486 :
487 : /* number of chars of a literal string without the ending \0 */
488 : #define LL(x) (sizeof(x)/sizeof(char) - 1)
489 :
490 : #define RETS "..."
491 : #define PRE "[string \""
492 : #define POS "\"]"
493 :
494 : #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
495 :
496 56 : void luaO_chunkid (char *out, const char *source, size_t bufflen) {
497 56 : size_t l = strlen(source);
498 56 : if (*source == '=') { /* 'literal' source */
499 41 : if (l <= bufflen) /* small enough? */
500 41 : memcpy(out, source + 1, l * sizeof(char));
501 : else { /* truncate it */
502 0 : addstr(out, source + 1, bufflen - 1);
503 0 : *out = '\0';
504 : }
505 : }
506 15 : else if (*source == '@') { /* file name */
507 15 : if (l <= bufflen) /* small enough? */
508 15 : memcpy(out, source + 1, l * sizeof(char));
509 : else { /* add '...' before rest of name */
510 0 : addstr(out, RETS, LL(RETS));
511 0 : bufflen -= LL(RETS);
512 0 : memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
513 : }
514 : }
515 : else { /* string; format as [string "source"] */
516 0 : const char *nl = strchr(source, '\n'); /* find first new line (if any) */
517 0 : addstr(out, PRE, LL(PRE)); /* add prefix */
518 0 : bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
519 0 : if (l < bufflen && nl == NULL) { /* small one-line source? */
520 0 : addstr(out, source, l); /* keep it */
521 : }
522 : else {
523 0 : if (nl != NULL) l = nl - source; /* stop at first newline */
524 0 : if (l > bufflen) l = bufflen;
525 0 : addstr(out, source, l);
526 0 : addstr(out, RETS, LL(RETS));
527 : }
528 0 : memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
529 : }
530 56 : }
531 :
|