]>
git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/lua-5.3.3/src/lobject.c
2 ** $Id: lobject.c,v 2.111 2016/05/20 14:07:48 roberto Exp $
3 ** Some generic functions over Lua objects
4 ** See Copyright Notice in lua.h
33 LUAI_DDEF
const TValue luaO_nilobject_
= {NILCONSTANT
};
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.
41 int luaO_int2fb (unsigned int x
) {
42 int e
= 0; /* exponent */
44 while (x
>= (8 << 4)) { /* coarse steps */
45 x
= (x
+ 0xf) >> 4; /* x = ceil(x / 16) */
48 while (x
>= (8 << 1)) { /* fine steps */
49 x
= (x
+ 1) >> 1; /* x = ceil(x / 2) */
52 return ((e
+1) << 3) | (cast_int(x
) - 8);
57 int luaO_fb2int (int x
) {
58 return (x
< 8) ? x
: ((x
& 7) + 8) << ((x
>> 3) - 1);
63 ** Computes ceil(log2(x))
65 int luaO_ceillog2 (unsigned int x
) {
66 static const lu_byte log_2
[256] = { /* log_2[i] = ceil(log2(i - 1)) */
67 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,
68 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,
69 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,
70 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,
71 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,
72 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,
73 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,
74 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 while (x
>= 256) { l
+= 8; x
>>= 8; }
83 static lua_Integer
intarith (lua_State
*L
, int op
, lua_Integer v1
,
86 case LUA_OPADD
: return intop(+, v1
, v2
);
87 case LUA_OPSUB
:return intop(-, v1
, v2
);
88 case LUA_OPMUL
:return intop(*, v1
, v2
);
89 case LUA_OPMOD
: return luaV_mod(L
, v1
, v2
);
90 case LUA_OPIDIV
: return luaV_div(L
, v1
, v2
);
91 case LUA_OPBAND
: return intop(&, v1
, v2
);
92 case LUA_OPBOR
: return intop(|, v1
, v2
);
93 case LUA_OPBXOR
: return intop(^, v1
, v2
);
94 case LUA_OPSHL
: return luaV_shiftl(v1
, v2
);
95 case LUA_OPSHR
: return luaV_shiftl(v1
, -v2
);
96 case LUA_OPUNM
: return intop(-, 0, v1
);
97 case LUA_OPBNOT
: return intop(^, ~l_castS2U(0), v1
);
98 default: lua_assert(0); return 0;
103 static lua_Number
numarith (lua_State
*L
, int op
, lua_Number v1
,
106 case LUA_OPADD
: return luai_numadd(L
, v1
, v2
);
107 case LUA_OPSUB
: return luai_numsub(L
, v1
, v2
);
108 case LUA_OPMUL
: return luai_nummul(L
, v1
, v2
);
109 case LUA_OPDIV
: return luai_numdiv(L
, v1
, v2
);
110 case LUA_OPPOW
: return luai_numpow(L
, v1
, v2
);
111 case LUA_OPIDIV
: return luai_numidiv(L
, v1
, v2
);
112 case LUA_OPUNM
: return luai_numunm(L
, v1
);
115 luai_nummod(L
, v1
, v2
, m
);
118 default: lua_assert(0); return 0;
123 void luaO_arith (lua_State
*L
, int op
, const TValue
*p1
, const TValue
*p2
,
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 (tointeger(p1
, &i1
) && tointeger(p2
, &i2
)) {
131 setivalue(res
, intarith(L
, op
, i1
, i2
));
134 else break; /* go to the end */
136 case LUA_OPDIV
: case LUA_OPPOW
: { /* operate only on floats */
137 lua_Number n1
; lua_Number n2
;
138 if (tonumber(p1
, &n1
) && tonumber(p2
, &n2
)) {
139 setfltvalue(res
, numarith(L
, op
, n1
, n2
));
142 else break; /* go to the end */
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
)));
150 else if (tonumber(p1
, &n1
) && tonumber(p2
, &n2
)) {
151 setfltvalue(res
, numarith(L
, op
, n1
, n2
));
154 else break; /* go to the end */
157 /* could not perform raw operation; try metamethod */
158 lua_assert(L
!= NULL
); /* should not fail when folding (compile time) */
159 luaT_trybinTM(L
, p1
, p2
, res
, cast(TMS
, (op
- LUA_OPADD
) + TM_ADD
));
163 int luaO_hexavalue (int c
) {
164 if (lisdigit(c
)) return c
- '0';
165 else return (ltolower(c
) - 'a') + 10;
169 static int isneg (const char **s
) {
170 if (**s
== '-') { (*s
)++; return 1; }
171 else if (**s
== '+') (*s
)++;
178 ** {==================================================================
179 ** Lua's implementation for 'lua_strx2number'
180 ** ===================================================================
183 #if !defined(lua_strx2number)
185 /* maximum number of significant digits to read (to avoid overflows
186 even with single floats) */
190 ** convert an hexadecimal numeric string to a number, following
191 ** C99 specification for 'strtod'
193 static lua_Number
lua_strx2number (const char *s
, char **endptr
) {
194 int dot
= lua_getlocaledecpoint();
195 lua_Number r
= 0.0; /* result (accumulator) */
196 int sigdig
= 0; /* number of significant digits */
197 int nosigdig
= 0; /* number of non-significant digits */
198 int e
= 0; /* exponent correction */
199 int neg
; /* 1 if number is negative */
200 int hasdot
= 0; /* true after seen a dot */
201 *endptr
= cast(char *, s
); /* nothing is valid yet */
202 while (lisspace(cast_uchar(*s
))) s
++; /* skip initial spaces */
203 neg
= isneg(&s
); /* check signal */
204 if (!(*s
== '0' && (*(s
+ 1) == 'x' || *(s
+ 1) == 'X'))) /* check '0x' */
205 return 0.0; /* invalid format (no '0x') */
206 for (s
+= 2; ; s
++) { /* skip '0x' and read numeral */
208 if (hasdot
) break; /* second dot? stop loop */
211 else if (lisxdigit(cast_uchar(*s
))) {
212 if (sigdig
== 0 && *s
== '0') /* non-significant digit (zero)? */
214 else if (++sigdig
<= MAXSIGDIG
) /* can read it without overflow? */
215 r
= (r
* cast_num(16.0)) + luaO_hexavalue(*s
);
216 else e
++; /* too many digits; ignore, but still count for exponent */
217 if (hasdot
) e
--; /* decimal digit? correct exponent */
219 else break; /* neither a dot nor a digit */
221 if (nosigdig
+ sigdig
== 0) /* no digits? */
222 return 0.0; /* invalid format */
223 *endptr
= cast(char *, s
); /* valid up to here */
224 e
*= 4; /* each digit multiplies/divides value by 2^4 */
225 if (*s
== 'p' || *s
== 'P') { /* exponent part? */
226 int exp1
= 0; /* exponent value */
227 int neg1
; /* exponent signal */
229 neg1
= isneg(&s
); /* signal */
230 if (!lisdigit(cast_uchar(*s
)))
231 return 0.0; /* invalid; must have at least one digit */
232 while (lisdigit(cast_uchar(*s
))) /* read exponent */
233 exp1
= exp1
* 10 + *(s
++) - '0';
234 if (neg1
) exp1
= -exp1
;
236 *endptr
= cast(char *, s
); /* valid up to here */
239 return l_mathop(ldexp
)(r
, e
);
243 /* }====================================================== */
246 /* maximum length of a numeral */
247 #if !defined (L_MAXLENNUM)
248 #define L_MAXLENNUM 200
251 static const char *l_str2dloc (const char *s
, lua_Number
*result
, int mode
) {
253 *result
= (mode
== 'x') ? lua_strx2number(s
, &endptr
) /* try to convert */
254 : lua_str2number(s
, &endptr
);
255 if (endptr
== s
) return NULL
; /* nothing recognized? */
256 while (lisspace(cast_uchar(*endptr
))) endptr
++; /* skip trailing spaces */
257 return (*endptr
== '\0') ? endptr
: NULL
; /* OK if no trailing characters */
262 ** Convert string 's' to a Lua number (put in 'result'). Return NULL
263 ** on fail or the address of the ending '\0' on success.
264 ** 'pmode' points to (and 'mode' contains) special things in the string:
265 ** - 'x'/'X' means an hexadecimal numeral
266 ** - 'n'/'N' means 'inf' or 'nan' (which should be rejected)
267 ** - '.' just optimizes the search for the common case (nothing special)
268 ** This function accepts both the current locale or a dot as the radix
269 ** mark. If the convertion fails, it may mean number has a dot but
270 ** locale accepts something else. In that case, the code copies 's'
271 ** to a buffer (because 's' is read-only), changes the dot to the
272 ** current locale radix mark, and tries to convert again.
274 static const char *l_str2d (const char *s
, lua_Number
*result
) {
276 const char *pmode
= strpbrk(s
, ".xXnN");
277 int mode
= pmode
? ltolower(cast_uchar(*pmode
)) : 0;
278 if (mode
== 'n') /* reject 'inf' and 'nan' */
280 endptr
= l_str2dloc(s
, result
, mode
); /* try to convert */
281 if (endptr
== NULL
) { /* failed? may be a different locale */
282 char buff
[L_MAXLENNUM
+ 1];
283 char *pdot
= strchr(s
, '.');
284 if (strlen(s
) > L_MAXLENNUM
|| pdot
== NULL
)
285 return NULL
; /* string too long or no dot; fail */
286 strcpy(buff
, s
); /* copy string to buffer */
287 buff
[pdot
- s
] = lua_getlocaledecpoint(); /* correct decimal point */
288 endptr
= l_str2dloc(buff
, result
, mode
); /* try again */
290 endptr
= s
+ (endptr
- buff
); /* make relative to 's' */
296 #define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10)
297 #define MAXLASTD cast_int(LUA_MAXINTEGER % 10)
299 static const char *l_str2int (const char *s
, lua_Integer
*result
) {
303 while (lisspace(cast_uchar(*s
))) s
++; /* skip initial spaces */
306 (s
[1] == 'x' || s
[1] == 'X')) { /* hex? */
307 s
+= 2; /* skip '0x' */
308 for (; lisxdigit(cast_uchar(*s
)); s
++) {
309 a
= a
* 16 + luaO_hexavalue(*s
);
314 for (; lisdigit(cast_uchar(*s
)); s
++) {
316 if (a
>= MAXBY10
&& (a
> MAXBY10
|| d
> MAXLASTD
+ neg
)) /* overflow? */
317 return NULL
; /* do not accept it (as integer) */
322 while (lisspace(cast_uchar(*s
))) s
++; /* skip trailing spaces */
323 if (empty
|| *s
!= '\0') return NULL
; /* something wrong in the numeral */
325 *result
= l_castU2S((neg
) ? 0u - a
: a
);
331 size_t luaO_str2num (const char *s
, TValue
*o
) {
332 lua_Integer i
; lua_Number n
;
334 if ((e
= l_str2int(s
, &i
)) != NULL
) { /* try as an integer */
337 else if ((e
= l_str2d(s
, &n
)) != NULL
) { /* else try as a float */
341 return 0; /* conversion failed */
342 return (e
- s
) + 1; /* success; return string size */
346 int luaO_utf8esc (char *buff
, unsigned long x
) {
347 int n
= 1; /* number of bytes put in buffer (backwards) */
348 lua_assert(x
<= 0x10FFFF);
349 if (x
< 0x80) /* ascii? */
350 buff
[UTF8BUFFSZ
- 1] = cast(char, x
);
351 else { /* need continuation bytes */
352 unsigned int mfb
= 0x3f; /* maximum that fits in first byte */
353 do { /* add continuation bytes */
354 buff
[UTF8BUFFSZ
- (n
++)] = cast(char, 0x80 | (x
& 0x3f));
355 x
>>= 6; /* remove added bits */
356 mfb
>>= 1; /* now there is one less bit available in first byte */
357 } while (x
> mfb
); /* still needs continuation byte? */
358 buff
[UTF8BUFFSZ
- n
] = cast(char, (~mfb
<< 1) | x
); /* add first byte */
364 /* maximum length of the conversion of a number to a string */
365 #define MAXNUMBER2STR 50
369 ** Convert a number object to a string
371 void luaO_tostring (lua_State
*L
, StkId obj
) {
372 char buff
[MAXNUMBER2STR
];
374 lua_assert(ttisnumber(obj
));
375 if (ttisinteger(obj
))
376 len
= lua_integer2str(buff
, sizeof(buff
), ivalue(obj
));
378 len
= lua_number2str(buff
, sizeof(buff
), fltvalue(obj
));
379 #if !defined(LUA_COMPAT_FLOATSTRING)
380 if (buff
[strspn(buff
, "-0123456789")] == '\0') { /* looks like an int? */
381 buff
[len
++] = lua_getlocaledecpoint();
382 buff
[len
++] = '0'; /* adds '.0' to result */
386 setsvalue2s(L
, obj
, luaS_newlstr(L
, buff
, len
));
390 static void pushstr (lua_State
*L
, const char *str
, size_t l
) {
391 setsvalue2s(L
, L
->top
, luaS_newlstr(L
, str
, l
));
397 ** this function handles only '%d', '%c', '%f', '%p', and '%s'
398 conventional formats, plus Lua-specific '%I' and '%U'
400 const char *luaO_pushvfstring (lua_State
*L
, const char *fmt
, va_list argp
) {
403 const char *e
= strchr(fmt
, '%');
404 if (e
== NULL
) break;
405 pushstr(L
, fmt
, e
- fmt
);
407 case 's': { /* zero-terminated string */
408 const char *s
= va_arg(argp
, char *);
409 if (s
== NULL
) s
= "(null)";
410 pushstr(L
, s
, strlen(s
));
413 case 'c': { /* an 'int' as a character */
414 char buff
= cast(char, va_arg(argp
, int));
415 if (lisprint(cast_uchar(buff
)))
416 pushstr(L
, &buff
, 1);
417 else /* non-printable character; print its code */
418 luaO_pushfstring(L
, "<\\%d>", cast_uchar(buff
));
421 case 'd': { /* an 'int' */
422 setivalue(L
->top
, va_arg(argp
, int));
425 case 'I': { /* a 'lua_Integer' */
426 setivalue(L
->top
, cast(lua_Integer
, va_arg(argp
, l_uacInt
)));
429 case 'f': { /* a 'lua_Number' */
430 setfltvalue(L
->top
, cast_num(va_arg(argp
, l_uacNumber
)));
431 top2str
: /* convert the top element to a string */
433 luaO_tostring(L
, L
->top
- 1);
436 case 'p': { /* a pointer */
437 char buff
[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */
438 int l
= l_sprintf(buff
, sizeof(buff
), "%p", va_arg(argp
, void *));
442 case 'U': { /* an 'int' as a UTF-8 sequence */
443 char buff
[UTF8BUFFSZ
];
444 int l
= luaO_utf8esc(buff
, cast(long, va_arg(argp
, long)));
445 pushstr(L
, buff
+ UTF8BUFFSZ
- l
, l
);
453 luaG_runerror(L
, "invalid option '%%%c' to 'lua_pushfstring'",
460 luaD_checkstack(L
, 1);
461 pushstr(L
, fmt
, strlen(fmt
));
462 if (n
> 0) luaV_concat(L
, n
+ 1);
463 return svalue(L
->top
- 1);
467 const char *luaO_pushfstring (lua_State
*L
, const char *fmt
, ...) {
471 msg
= luaO_pushvfstring(L
, fmt
, argp
);
477 /* number of chars of a literal string without the ending \0 */
478 #define LL(x) (sizeof(x)/sizeof(char) - 1)
481 #define PRE "[string \""
484 #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
486 void luaO_chunkid (char *out
, const char *source
, size_t bufflen
) {
487 size_t l
= strlen(source
);
488 if (*source
== '=') { /* 'literal' source */
489 if (l
<= bufflen
) /* small enough? */
490 memcpy(out
, source
+ 1, l
* sizeof(char));
491 else { /* truncate it */
492 addstr(out
, source
+ 1, bufflen
- 1);
496 else if (*source
== '@') { /* file name */
497 if (l
<= bufflen
) /* small enough? */
498 memcpy(out
, source
+ 1, l
* sizeof(char));
499 else { /* add '...' before rest of name */
500 addstr(out
, RETS
, LL(RETS
));
502 memcpy(out
, source
+ 1 + l
- bufflen
, bufflen
* sizeof(char));
505 else { /* string; format as [string "source"] */
506 const char *nl
= strchr(source
, '\n'); /* find first new line (if any) */
507 addstr(out
, PRE
, LL(PRE
)); /* add prefix */
508 bufflen
-= LL(PRE RETS POS
) + 1; /* save space for prefix+suffix+'\0' */
509 if (l
< bufflen
&& nl
== NULL
) { /* small one-line source? */
510 addstr(out
, source
, l
); /* keep it */
513 if (nl
!= NULL
) l
= nl
- source
; /* stop at first newline */
514 if (l
> bufflen
) l
= bufflen
;
515 addstr(out
, source
, l
);
516 addstr(out
, RETS
, LL(RETS
));
518 memcpy(out
, POS
, (LL(POS
) + 1) * sizeof(char));