1 /* $NetBSD: lobject.c,v 1.5 2015/10/08 13:21:00 mbalmer Exp $ */
4 ** Id: lobject.c,v 2.104 2015/04/11 18:30:08 roberto Exp
5 ** Some generic functions over Lua objects
6 ** See Copyright Notice in lua.h
37 LUAI_DDEF
const TValue luaO_nilobject_
= {NILCONSTANT
};
41 ** converts an integer to a "floating point byte", represented as
42 ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
43 ** eeeee != 0 and (xxx) otherwise.
45 int luaO_int2fb (unsigned int x
) {
46 int e
= 0; /* exponent */
48 while (x
>= (8 << 4)) { /* coarse steps */
49 x
= (x
+ 0xf) >> 4; /* x = ceil(x / 16) */
52 while (x
>= (8 << 1)) { /* fine steps */
53 x
= (x
+ 1) >> 1; /* x = ceil(x / 2) */
56 return ((e
+1) << 3) | (cast_int(x
) - 8);
61 int luaO_fb2int (int x
) {
62 int e
= (x
>> 3) & 0x1f;
64 else return ((x
& 7) + 8) << (e
- 1);
69 ** Computes ceil(log2(x))
71 int luaO_ceillog2 (unsigned int x
) {
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
84 while (x
>= 256) { l
+= 8; x
>>= 8; }
89 static lua_Integer
intarith (lua_State
*L
, int op
, lua_Integer v1
,
92 case LUA_OPADD
: return intop(+, v1
, v2
);
93 case LUA_OPSUB
:return intop(-, v1
, v2
);
94 case LUA_OPMUL
:return intop(*, v1
, v2
);
95 case LUA_OPMOD
: return luaV_mod(L
, v1
, v2
);
96 case LUA_OPIDIV
: return luaV_div(L
, v1
, v2
);
97 case LUA_OPBAND
: return intop(&, v1
, v2
);
98 case LUA_OPBOR
: return intop(|, v1
, v2
);
99 case LUA_OPBXOR
: return intop(^, v1
, v2
);
100 case LUA_OPSHL
: return luaV_shiftl(v1
, v2
);
101 case LUA_OPSHR
: return luaV_shiftl(v1
, -v2
);
102 case LUA_OPUNM
: return intop(-, 0, v1
);
103 case LUA_OPBNOT
: return intop(^, ~l_castS2U(0), v1
);
104 default: lua_assert(0); return 0;
110 static lua_Number
numarith (lua_State
*L
, int op
, lua_Number v1
,
113 case LUA_OPADD
: return luai_numadd(L
, v1
, v2
);
114 case LUA_OPSUB
: return luai_numsub(L
, v1
, v2
);
115 case LUA_OPMUL
: return luai_nummul(L
, v1
, v2
);
116 case LUA_OPDIV
: return luai_numdiv(L
, v1
, v2
);
117 case LUA_OPPOW
: return luai_numpow(L
, v1
, v2
);
118 case LUA_OPIDIV
: return luai_numidiv(L
, v1
, v2
);
119 case LUA_OPUNM
: return luai_numunm(L
, v1
);
122 luai_nummod(L
, v1
, v2
, m
);
125 default: lua_assert(0); return 0;
131 void luaO_arith (lua_State
*L
, int op
, const TValue
*p1
, const TValue
*p2
,
134 case LUA_OPBAND
: case LUA_OPBOR
: case LUA_OPBXOR
:
135 case LUA_OPSHL
: case LUA_OPSHR
:
136 case LUA_OPBNOT
: { /* operate only on integers */
137 lua_Integer i1
; lua_Integer i2
;
138 if (tointeger(p1
, &i1
) && tointeger(p2
, &i2
)) {
139 setivalue(res
, intarith(L
, op
, i1
, i2
));
142 else break; /* go to the end */
145 case LUA_OPDIV
: case LUA_OPPOW
: { /* operate only on floats */
146 lua_Number n1
; lua_Number n2
;
147 if (tonumber(p1
, &n1
) && tonumber(p2
, &n2
)) {
148 setfltvalue(res
, numarith(L
, op
, n1
, n2
));
151 else break; /* go to the end */
154 default: { /* other operations */
156 lua_Number n1
; lua_Number n2
;
157 if (ttisinteger(p1
) && ttisinteger(p2
)) {
158 setivalue(res
, intarith(L
, op
, ivalue(p1
), ivalue(p2
)));
161 else if (tonumber(p1
, &n1
) && tonumber(p2
, &n2
)) {
162 setfltvalue(res
, numarith(L
, op
, n1
, n2
));
166 lua_Integer i1
; lua_Integer i2
;
167 if (tointeger(p1
, &i1
) && tointeger(p2
, &i2
)) {
168 setivalue(res
, intarith(L
, op
, i1
, i2
));
172 else break; /* go to the end */
175 /* could not perform raw operation; try metamethod */
176 lua_assert(L
!= NULL
); /* should not fail when folding (compile time) */
177 luaT_trybinTM(L
, p1
, p2
, res
, cast(TMS
, (op
- LUA_OPADD
) + TM_ADD
));
181 int luaO_hexavalue (int c
) {
182 if (lisdigit(c
)) return c
- '0';
183 else return (ltolower(c
) - 'a') + 10;
187 static int isneg (const char **s
) {
188 if (**s
== '-') { (*s
)++; return 1; }
189 else if (**s
== '+') (*s
)++;
197 ** {==================================================================
198 ** Lua's implementation for 'lua_strx2number'
199 ** ===================================================================
202 #if !defined(lua_strx2number)
204 /* maximum number of significant digits to read (to avoid overflows
205 even with single floats) */
209 ** convert an hexadecimal numeric string to a number, following
210 ** C99 specification for 'strtod'
212 static lua_Number
lua_strx2number (const char *s
, char **endptr
) {
213 int dot
= lua_getlocaledecpoint();
214 lua_Number r
= 0.0; /* result (accumulator) */
215 int sigdig
= 0; /* number of significant digits */
216 int nosigdig
= 0; /* number of non-significant digits */
217 int e
= 0; /* exponent correction */
218 int neg
; /* 1 if number is negative */
219 int hasdot
= 0; /* true after seen a dot */
220 *endptr
= cast(char *, s
); /* nothing is valid yet */
221 while (lisspace(cast_uchar(*s
))) s
++; /* skip initial spaces */
222 neg
= isneg(&s
); /* check signal */
223 if (!(*s
== '0' && (*(s
+ 1) == 'x' || *(s
+ 1) == 'X'))) /* check '0x' */
224 return 0.0; /* invalid format (no '0x') */
225 for (s
+= 2; ; s
++) { /* skip '0x' and read numeral */
227 if (hasdot
) break; /* second dot? stop loop */
230 else if (lisxdigit(cast_uchar(*s
))) {
231 if (sigdig
== 0 && *s
== '0') /* non-significant digit (zero)? */
233 else if (++sigdig
<= MAXSIGDIG
) /* can read it without overflow? */
234 r
= (r
* cast_num(16.0)) + luaO_hexavalue(*s
);
235 else e
++; /* too many digits; ignore, but still count for exponent */
236 if (hasdot
) e
--; /* decimal digit? correct exponent */
238 else break; /* neither a dot nor a digit */
240 if (nosigdig
+ sigdig
== 0) /* no digits? */
241 return 0.0; /* invalid format */
242 *endptr
= cast(char *, s
); /* valid up to here */
243 e
*= 4; /* each digit multiplies/divides value by 2^4 */
244 if (*s
== 'p' || *s
== 'P') { /* exponent part? */
245 int exp1
= 0; /* exponent value */
246 int neg1
; /* exponent signal */
248 neg1
= isneg(&s
); /* signal */
249 if (!lisdigit(cast_uchar(*s
)))
250 return 0.0; /* invalid; must have at least one digit */
251 while (lisdigit(cast_uchar(*s
))) /* read exponent */
252 exp1
= exp1
* 10 + *(s
++) - '0';
253 if (neg1
) exp1
= -exp1
;
255 *endptr
= cast(char *, s
); /* valid up to here */
258 return l_mathop(ldexp
)(r
, e
);
262 /* }====================================================== */
265 static const char *l_str2d (const char *s
, lua_Number
*result
) {
267 if (strpbrk(s
, "nN")) /* reject 'inf' and 'nan' */
269 else if (strpbrk(s
, "xX")) /* hex? */
270 *result
= lua_strx2number(s
, &endptr
);
272 *result
= lua_str2number(s
, &endptr
);
273 if (endptr
== s
) return NULL
; /* nothing recognized */
274 while (lisspace(cast_uchar(*endptr
))) endptr
++;
275 return (*endptr
== '\0' ? endptr
: NULL
); /* OK if no trailing characters */
280 static const char *l_str2int (const char *s
, lua_Integer
*result
) {
284 while (lisspace(cast_uchar(*s
))) s
++; /* skip initial spaces */
287 (s
[1] == 'x' || s
[1] == 'X')) { /* hex? */
288 s
+= 2; /* skip '0x' */
289 for (; lisxdigit(cast_uchar(*s
)); s
++) {
290 a
= a
* 16 + luaO_hexavalue(*s
);
295 for (; lisdigit(cast_uchar(*s
)); s
++) {
296 a
= a
* 10 + *s
- '0';
300 while (lisspace(cast_uchar(*s
))) s
++; /* skip trailing spaces */
301 if (empty
|| *s
!= '\0') return NULL
; /* something wrong in the numeral */
303 *result
= l_castU2S((neg
) ? 0u - a
: a
);
309 size_t luaO_str2num (const char *s
, TValue
*o
) {
311 lua_Integer i
; lua_Number n
;
316 if ((e
= l_str2int(s
, &i
)) != NULL
) { /* try as an integer */
320 else if ((e
= l_str2d(s
, &n
)) != NULL
) { /* else try as a float */
325 return 0; /* conversion failed */
326 return (e
- s
) + 1; /* success; return string size */
330 int luaO_utf8esc (char *buff
, unsigned long x
) {
331 int n
= 1; /* number of bytes put in buffer (backwards) */
332 lua_assert(x
<= 0x10FFFF);
333 if (x
< 0x80) /* ascii? */
334 buff
[UTF8BUFFSZ
- 1] = cast(char, x
);
335 else { /* need continuation bytes */
336 unsigned int mfb
= 0x3f; /* maximum that fits in first byte */
337 do { /* add continuation bytes */
338 buff
[UTF8BUFFSZ
- (n
++)] = cast(char, 0x80 | (x
& 0x3f));
339 x
>>= 6; /* remove added bits */
340 mfb
>>= 1; /* now there is one less bit available in first byte */
341 } while (x
> mfb
); /* still needs continuation byte? */
342 buff
[UTF8BUFFSZ
- n
] = cast(char, (~mfb
<< 1) | x
); /* add first byte */
348 /* maximum length of the conversion of a number to a string */
349 #define MAXNUMBER2STR 50
353 ** Convert a number object to a string
355 void luaO_tostring (lua_State
*L
, StkId obj
) {
356 char buff
[MAXNUMBER2STR
];
358 lua_assert(ttisnumber(obj
));
360 if (ttisinteger(obj
))
361 len
= lua_integer2str(buff
, ivalue(obj
));
363 len
= lua_number2str(buff
, fltvalue(obj
));
364 #if !defined(LUA_COMPAT_FLOATSTRING)
365 if (buff
[strspn(buff
, "-0123456789")] == '\0') { /* looks like an int? */
366 buff
[len
++] = lua_getlocaledecpoint();
367 buff
[len
++] = '0'; /* adds '.0' to result */
372 lua_assert(ttisinteger(obj
));
373 len
= lua_integer2str(buff
, ivalue(obj
));
375 setsvalue2s(L
, obj
, luaS_newlstr(L
, buff
, len
));
379 static void pushstr (lua_State
*L
, const char *str
, size_t l
) {
380 setsvalue2s(L
, L
->top
++, luaS_newlstr(L
, str
, l
));
384 /* this function handles only '%d', '%c', '%f', '%p', and '%s'
385 conventional formats, plus Lua-specific '%I' and '%U' */
386 const char *luaO_pushvfstring (lua_State
*L
, const char *fmt
, va_list argp
) {
389 const char *e
= strchr(fmt
, '%');
390 if (e
== NULL
) break;
391 luaD_checkstack(L
, 2); /* fmt + item */
392 pushstr(L
, fmt
, e
- fmt
);
395 const char *s
= va_arg(argp
, char *);
396 if (s
== NULL
) s
= "(null)";
397 pushstr(L
, s
, strlen(s
));
401 char buff
= cast(char, va_arg(argp
, int));
402 if (lisprint(cast_uchar(buff
)))
403 pushstr(L
, &buff
, 1);
404 else /* non-printable character; print its code */
405 luaO_pushfstring(L
, "<\\%d>", cast_uchar(buff
));
409 setivalue(L
->top
++, va_arg(argp
, int));
410 luaO_tostring(L
, L
->top
- 1);
414 setivalue(L
->top
++, cast(lua_Integer
, va_arg(argp
, l_uacInt
)));
415 luaO_tostring(L
, L
->top
- 1);
420 setfltvalue(L
->top
++, cast_num(va_arg(argp
, l_uacNumber
)));
421 luaO_tostring(L
, L
->top
- 1);
426 char buff
[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */
427 int l
= sprintf(buff
, "%p", va_arg(argp
, void *));
432 char buff
[UTF8BUFFSZ
];
433 int l
= luaO_utf8esc(buff
, cast(long, va_arg(argp
, long)));
434 pushstr(L
, buff
+ UTF8BUFFSZ
- l
, l
);
442 luaG_runerror(L
, "invalid option '%%%c' to 'lua_pushfstring'",
449 luaD_checkstack(L
, 1);
450 pushstr(L
, fmt
, strlen(fmt
));
451 if (n
> 0) luaV_concat(L
, n
+ 1);
452 return svalue(L
->top
- 1);
456 const char *luaO_pushfstring (lua_State
*L
, const char *fmt
, ...) {
460 msg
= luaO_pushvfstring(L
, fmt
, argp
);
466 /* number of chars of a literal string without the ending \0 */
467 #define LL(x) (sizeof(x)/sizeof(char) - 1)
470 #define PRE "[string \""
473 #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
475 void luaO_chunkid (char *out
, const char *source
, size_t bufflen
) {
476 size_t l
= strlen(source
);
477 if (*source
== '=') { /* 'literal' source */
478 if (l
<= bufflen
) /* small enough? */
479 memcpy(out
, source
+ 1, l
* sizeof(char));
480 else { /* truncate it */
481 addstr(out
, source
+ 1, bufflen
- 1);
485 else if (*source
== '@') { /* file name */
486 if (l
<= bufflen
) /* small enough? */
487 memcpy(out
, source
+ 1, l
* sizeof(char));
488 else { /* add '...' before rest of name */
489 addstr(out
, RETS
, LL(RETS
));
491 memcpy(out
, source
+ 1 + l
- bufflen
, bufflen
* sizeof(char));
494 else { /* string; format as [string "source"] */
495 const char *nl
= strchr(source
, '\n'); /* find first new line (if any) */
496 addstr(out
, PRE
, LL(PRE
)); /* add prefix */
497 bufflen
-= LL(PRE RETS POS
) + 1; /* save space for prefix+suffix+'\0' */
498 if (l
< bufflen
&& nl
== NULL
) { /* small one-line source? */
499 addstr(out
, source
, l
); /* keep it */
502 if (nl
!= NULL
) l
= nl
- source
; /* stop at first newline */
503 if (l
> bufflen
) l
= bufflen
;
504 addstr(out
, source
, l
);
505 addstr(out
, RETS
, LL(RETS
));
507 memcpy(out
, POS
, (LL(POS
) + 1) * sizeof(char));