1*0a6a1f1dSLionel Sambuc /* $NetBSD: lobject.c,v 1.5 2015/10/08 13:21:00 mbalmer Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*
4*0a6a1f1dSLionel Sambuc ** Id: lobject.c,v 2.104 2015/04/11 18:30:08 roberto Exp
511be35a1SLionel Sambuc ** Some generic functions over Lua objects
611be35a1SLionel Sambuc ** See Copyright Notice in lua.h
711be35a1SLionel Sambuc */
811be35a1SLionel Sambuc
9*0a6a1f1dSLionel Sambuc #define lobject_c
10*0a6a1f1dSLionel Sambuc #define LUA_CORE
11*0a6a1f1dSLionel Sambuc
12*0a6a1f1dSLionel Sambuc #include "lprefix.h"
13*0a6a1f1dSLionel Sambuc
14*0a6a1f1dSLionel Sambuc
15*0a6a1f1dSLionel Sambuc #ifndef _KERNEL
16*0a6a1f1dSLionel Sambuc #include <locale.h>
17*0a6a1f1dSLionel Sambuc #include <math.h>
1811be35a1SLionel Sambuc #include <stdarg.h>
1911be35a1SLionel Sambuc #include <stdio.h>
2011be35a1SLionel Sambuc #include <stdlib.h>
2111be35a1SLionel Sambuc #include <string.h>
22*0a6a1f1dSLionel Sambuc #endif
2311be35a1SLionel Sambuc
2411be35a1SLionel Sambuc #include "lua.h"
2511be35a1SLionel Sambuc
26*0a6a1f1dSLionel Sambuc #include "lctype.h"
27*0a6a1f1dSLionel Sambuc #include "ldebug.h"
2811be35a1SLionel Sambuc #include "ldo.h"
2911be35a1SLionel Sambuc #include "lmem.h"
3011be35a1SLionel Sambuc #include "lobject.h"
3111be35a1SLionel Sambuc #include "lstate.h"
3211be35a1SLionel Sambuc #include "lstring.h"
3311be35a1SLionel Sambuc #include "lvm.h"
3411be35a1SLionel Sambuc
3511be35a1SLionel Sambuc
3611be35a1SLionel Sambuc
37*0a6a1f1dSLionel Sambuc LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};
3811be35a1SLionel Sambuc
3911be35a1SLionel Sambuc
4011be35a1SLionel Sambuc /*
4111be35a1SLionel Sambuc ** converts an integer to a "floating point byte", represented as
4211be35a1SLionel Sambuc ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
4311be35a1SLionel Sambuc ** eeeee != 0 and (xxx) otherwise.
4411be35a1SLionel Sambuc */
luaO_int2fb(unsigned int x)4511be35a1SLionel Sambuc int luaO_int2fb (unsigned int x) {
46*0a6a1f1dSLionel Sambuc int e = 0; /* exponent */
47*0a6a1f1dSLionel Sambuc if (x < 8) return x;
48*0a6a1f1dSLionel Sambuc while (x >= (8 << 4)) { /* coarse steps */
49*0a6a1f1dSLionel Sambuc x = (x + 0xf) >> 4; /* x = ceil(x / 16) */
50*0a6a1f1dSLionel Sambuc e += 4;
51*0a6a1f1dSLionel Sambuc }
52*0a6a1f1dSLionel Sambuc while (x >= (8 << 1)) { /* fine steps */
53*0a6a1f1dSLionel Sambuc x = (x + 1) >> 1; /* x = ceil(x / 2) */
5411be35a1SLionel Sambuc e++;
5511be35a1SLionel Sambuc }
56*0a6a1f1dSLionel Sambuc return ((e+1) << 3) | (cast_int(x) - 8);
5711be35a1SLionel Sambuc }
5811be35a1SLionel Sambuc
5911be35a1SLionel Sambuc
6011be35a1SLionel Sambuc /* converts back */
luaO_fb2int(int x)6111be35a1SLionel Sambuc int luaO_fb2int (int x) {
62*0a6a1f1dSLionel Sambuc int e = (x >> 3) & 0x1f;
6311be35a1SLionel Sambuc if (e == 0) return x;
6411be35a1SLionel Sambuc else return ((x & 7) + 8) << (e - 1);
6511be35a1SLionel Sambuc }
6611be35a1SLionel Sambuc
6711be35a1SLionel Sambuc
68*0a6a1f1dSLionel Sambuc /*
69*0a6a1f1dSLionel Sambuc ** Computes ceil(log2(x))
70*0a6a1f1dSLionel Sambuc */
luaO_ceillog2(unsigned int x)71*0a6a1f1dSLionel Sambuc int luaO_ceillog2 (unsigned int x) {
72*0a6a1f1dSLionel Sambuc static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */
7311be35a1SLionel Sambuc 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,
7411be35a1SLionel Sambuc 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,
7511be35a1SLionel Sambuc 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,
7611be35a1SLionel Sambuc 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,
7711be35a1SLionel Sambuc 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,
7811be35a1SLionel Sambuc 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,
7911be35a1SLionel Sambuc 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,
8011be35a1SLionel Sambuc 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
8111be35a1SLionel Sambuc };
82*0a6a1f1dSLionel Sambuc int l = 0;
83*0a6a1f1dSLionel Sambuc x--;
8411be35a1SLionel Sambuc while (x >= 256) { l += 8; x >>= 8; }
8511be35a1SLionel Sambuc return l + log_2[x];
8611be35a1SLionel Sambuc }
8711be35a1SLionel Sambuc
8811be35a1SLionel Sambuc
intarith(lua_State * L,int op,lua_Integer v1,lua_Integer v2)89*0a6a1f1dSLionel Sambuc static lua_Integer intarith (lua_State *L, int op, lua_Integer v1,
90*0a6a1f1dSLionel Sambuc lua_Integer v2) {
91*0a6a1f1dSLionel Sambuc switch (op) {
92*0a6a1f1dSLionel Sambuc case LUA_OPADD: return intop(+, v1, v2);
93*0a6a1f1dSLionel Sambuc case LUA_OPSUB:return intop(-, v1, v2);
94*0a6a1f1dSLionel Sambuc case LUA_OPMUL:return intop(*, v1, v2);
95*0a6a1f1dSLionel Sambuc case LUA_OPMOD: return luaV_mod(L, v1, v2);
96*0a6a1f1dSLionel Sambuc case LUA_OPIDIV: return luaV_div(L, v1, v2);
97*0a6a1f1dSLionel Sambuc case LUA_OPBAND: return intop(&, v1, v2);
98*0a6a1f1dSLionel Sambuc case LUA_OPBOR: return intop(|, v1, v2);
99*0a6a1f1dSLionel Sambuc case LUA_OPBXOR: return intop(^, v1, v2);
100*0a6a1f1dSLionel Sambuc case LUA_OPSHL: return luaV_shiftl(v1, v2);
101*0a6a1f1dSLionel Sambuc case LUA_OPSHR: return luaV_shiftl(v1, -v2);
102*0a6a1f1dSLionel Sambuc case LUA_OPUNM: return intop(-, 0, v1);
103*0a6a1f1dSLionel Sambuc case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1);
104*0a6a1f1dSLionel Sambuc default: lua_assert(0); return 0;
10511be35a1SLionel Sambuc }
10611be35a1SLionel Sambuc }
10711be35a1SLionel Sambuc
10811be35a1SLionel Sambuc
109*0a6a1f1dSLionel Sambuc #ifndef _KERNEL
numarith(lua_State * L,int op,lua_Number v1,lua_Number v2)110*0a6a1f1dSLionel Sambuc static lua_Number numarith (lua_State *L, int op, lua_Number v1,
111*0a6a1f1dSLionel Sambuc lua_Number v2) {
112*0a6a1f1dSLionel Sambuc switch (op) {
113*0a6a1f1dSLionel Sambuc case LUA_OPADD: return luai_numadd(L, v1, v2);
114*0a6a1f1dSLionel Sambuc case LUA_OPSUB: return luai_numsub(L, v1, v2);
115*0a6a1f1dSLionel Sambuc case LUA_OPMUL: return luai_nummul(L, v1, v2);
116*0a6a1f1dSLionel Sambuc case LUA_OPDIV: return luai_numdiv(L, v1, v2);
117*0a6a1f1dSLionel Sambuc case LUA_OPPOW: return luai_numpow(L, v1, v2);
118*0a6a1f1dSLionel Sambuc case LUA_OPIDIV: return luai_numidiv(L, v1, v2);
119*0a6a1f1dSLionel Sambuc case LUA_OPUNM: return luai_numunm(L, v1);
120*0a6a1f1dSLionel Sambuc case LUA_OPMOD: {
121*0a6a1f1dSLionel Sambuc lua_Number m;
122*0a6a1f1dSLionel Sambuc luai_nummod(L, v1, v2, m);
123*0a6a1f1dSLionel Sambuc return m;
124*0a6a1f1dSLionel Sambuc }
125*0a6a1f1dSLionel Sambuc default: lua_assert(0); return 0;
126*0a6a1f1dSLionel Sambuc }
127*0a6a1f1dSLionel Sambuc }
128*0a6a1f1dSLionel Sambuc #endif
129*0a6a1f1dSLionel Sambuc
130*0a6a1f1dSLionel Sambuc
luaO_arith(lua_State * L,int op,const TValue * p1,const TValue * p2,TValue * res)131*0a6a1f1dSLionel Sambuc void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
132*0a6a1f1dSLionel Sambuc TValue *res) {
133*0a6a1f1dSLionel Sambuc switch (op) {
134*0a6a1f1dSLionel Sambuc case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
135*0a6a1f1dSLionel Sambuc case LUA_OPSHL: case LUA_OPSHR:
136*0a6a1f1dSLionel Sambuc case LUA_OPBNOT: { /* operate only on integers */
137*0a6a1f1dSLionel Sambuc lua_Integer i1; lua_Integer i2;
138*0a6a1f1dSLionel Sambuc if (tointeger(p1, &i1) && tointeger(p2, &i2)) {
139*0a6a1f1dSLionel Sambuc setivalue(res, intarith(L, op, i1, i2));
140*0a6a1f1dSLionel Sambuc return;
141*0a6a1f1dSLionel Sambuc }
142*0a6a1f1dSLionel Sambuc else break; /* go to the end */
143*0a6a1f1dSLionel Sambuc }
144*0a6a1f1dSLionel Sambuc #ifndef _KERNEL
145*0a6a1f1dSLionel Sambuc case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */
146*0a6a1f1dSLionel Sambuc lua_Number n1; lua_Number n2;
147*0a6a1f1dSLionel Sambuc if (tonumber(p1, &n1) && tonumber(p2, &n2)) {
148*0a6a1f1dSLionel Sambuc setfltvalue(res, numarith(L, op, n1, n2));
149*0a6a1f1dSLionel Sambuc return;
150*0a6a1f1dSLionel Sambuc }
151*0a6a1f1dSLionel Sambuc else break; /* go to the end */
152*0a6a1f1dSLionel Sambuc }
153*0a6a1f1dSLionel Sambuc #endif
154*0a6a1f1dSLionel Sambuc default: { /* other operations */
155*0a6a1f1dSLionel Sambuc #ifndef _KERNEL
156*0a6a1f1dSLionel Sambuc lua_Number n1; lua_Number n2;
157*0a6a1f1dSLionel Sambuc if (ttisinteger(p1) && ttisinteger(p2)) {
158*0a6a1f1dSLionel Sambuc setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2)));
159*0a6a1f1dSLionel Sambuc return;
160*0a6a1f1dSLionel Sambuc }
161*0a6a1f1dSLionel Sambuc else if (tonumber(p1, &n1) && tonumber(p2, &n2)) {
162*0a6a1f1dSLionel Sambuc setfltvalue(res, numarith(L, op, n1, n2));
163*0a6a1f1dSLionel Sambuc return;
164*0a6a1f1dSLionel Sambuc }
165*0a6a1f1dSLionel Sambuc #else /* _KERNEL */
166*0a6a1f1dSLionel Sambuc lua_Integer i1; lua_Integer i2;
167*0a6a1f1dSLionel Sambuc if (tointeger(p1, &i1) && tointeger(p2, &i2)) {
168*0a6a1f1dSLionel Sambuc setivalue(res, intarith(L, op, i1, i2));
169*0a6a1f1dSLionel Sambuc return;
170*0a6a1f1dSLionel Sambuc }
171*0a6a1f1dSLionel Sambuc #endif
172*0a6a1f1dSLionel Sambuc else break; /* go to the end */
173*0a6a1f1dSLionel Sambuc }
174*0a6a1f1dSLionel Sambuc }
175*0a6a1f1dSLionel Sambuc /* could not perform raw operation; try metamethod */
176*0a6a1f1dSLionel Sambuc lua_assert(L != NULL); /* should not fail when folding (compile time) */
177*0a6a1f1dSLionel Sambuc luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD));
178*0a6a1f1dSLionel Sambuc }
179*0a6a1f1dSLionel Sambuc
180*0a6a1f1dSLionel Sambuc
luaO_hexavalue(int c)181*0a6a1f1dSLionel Sambuc int luaO_hexavalue (int c) {
182*0a6a1f1dSLionel Sambuc if (lisdigit(c)) return c - '0';
183*0a6a1f1dSLionel Sambuc else return (ltolower(c) - 'a') + 10;
184*0a6a1f1dSLionel Sambuc }
185*0a6a1f1dSLionel Sambuc
186*0a6a1f1dSLionel Sambuc
isneg(const char ** s)187*0a6a1f1dSLionel Sambuc static int isneg (const char **s) {
188*0a6a1f1dSLionel Sambuc if (**s == '-') { (*s)++; return 1; }
189*0a6a1f1dSLionel Sambuc else if (**s == '+') (*s)++;
190*0a6a1f1dSLionel Sambuc return 0;
191*0a6a1f1dSLionel Sambuc }
192*0a6a1f1dSLionel Sambuc
193*0a6a1f1dSLionel Sambuc
194*0a6a1f1dSLionel Sambuc
195*0a6a1f1dSLionel Sambuc #ifndef _KERNEL
196*0a6a1f1dSLionel Sambuc /*
197*0a6a1f1dSLionel Sambuc ** {==================================================================
198*0a6a1f1dSLionel Sambuc ** Lua's implementation for 'lua_strx2number'
199*0a6a1f1dSLionel Sambuc ** ===================================================================
200*0a6a1f1dSLionel Sambuc */
201*0a6a1f1dSLionel Sambuc
202*0a6a1f1dSLionel Sambuc #if !defined(lua_strx2number)
203*0a6a1f1dSLionel Sambuc
204*0a6a1f1dSLionel Sambuc /* maximum number of significant digits to read (to avoid overflows
205*0a6a1f1dSLionel Sambuc even with single floats) */
206*0a6a1f1dSLionel Sambuc #define MAXSIGDIG 30
207*0a6a1f1dSLionel Sambuc
208*0a6a1f1dSLionel Sambuc /*
209*0a6a1f1dSLionel Sambuc ** convert an hexadecimal numeric string to a number, following
210*0a6a1f1dSLionel Sambuc ** C99 specification for 'strtod'
211*0a6a1f1dSLionel Sambuc */
lua_strx2number(const char * s,char ** endptr)212*0a6a1f1dSLionel Sambuc static lua_Number lua_strx2number (const char *s, char **endptr) {
213*0a6a1f1dSLionel Sambuc int dot = lua_getlocaledecpoint();
214*0a6a1f1dSLionel Sambuc lua_Number r = 0.0; /* result (accumulator) */
215*0a6a1f1dSLionel Sambuc int sigdig = 0; /* number of significant digits */
216*0a6a1f1dSLionel Sambuc int nosigdig = 0; /* number of non-significant digits */
217*0a6a1f1dSLionel Sambuc int e = 0; /* exponent correction */
218*0a6a1f1dSLionel Sambuc int neg; /* 1 if number is negative */
219*0a6a1f1dSLionel Sambuc int hasdot = 0; /* true after seen a dot */
220*0a6a1f1dSLionel Sambuc *endptr = cast(char *, s); /* nothing is valid yet */
221*0a6a1f1dSLionel Sambuc while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
222*0a6a1f1dSLionel Sambuc neg = isneg(&s); /* check signal */
223*0a6a1f1dSLionel Sambuc if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
224*0a6a1f1dSLionel Sambuc return 0.0; /* invalid format (no '0x') */
225*0a6a1f1dSLionel Sambuc for (s += 2; ; s++) { /* skip '0x' and read numeral */
226*0a6a1f1dSLionel Sambuc if (*s == dot) {
227*0a6a1f1dSLionel Sambuc if (hasdot) break; /* second dot? stop loop */
228*0a6a1f1dSLionel Sambuc else hasdot = 1;
229*0a6a1f1dSLionel Sambuc }
230*0a6a1f1dSLionel Sambuc else if (lisxdigit(cast_uchar(*s))) {
231*0a6a1f1dSLionel Sambuc if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */
232*0a6a1f1dSLionel Sambuc nosigdig++;
233*0a6a1f1dSLionel Sambuc else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
234*0a6a1f1dSLionel Sambuc r = (r * cast_num(16.0)) + luaO_hexavalue(*s);
235*0a6a1f1dSLionel Sambuc else e++; /* too many digits; ignore, but still count for exponent */
236*0a6a1f1dSLionel Sambuc if (hasdot) e--; /* decimal digit? correct exponent */
237*0a6a1f1dSLionel Sambuc }
238*0a6a1f1dSLionel Sambuc else break; /* neither a dot nor a digit */
239*0a6a1f1dSLionel Sambuc }
240*0a6a1f1dSLionel Sambuc if (nosigdig + sigdig == 0) /* no digits? */
241*0a6a1f1dSLionel Sambuc return 0.0; /* invalid format */
242*0a6a1f1dSLionel Sambuc *endptr = cast(char *, s); /* valid up to here */
243*0a6a1f1dSLionel Sambuc e *= 4; /* each digit multiplies/divides value by 2^4 */
244*0a6a1f1dSLionel Sambuc if (*s == 'p' || *s == 'P') { /* exponent part? */
245*0a6a1f1dSLionel Sambuc int exp1 = 0; /* exponent value */
246*0a6a1f1dSLionel Sambuc int neg1; /* exponent signal */
247*0a6a1f1dSLionel Sambuc s++; /* skip 'p' */
248*0a6a1f1dSLionel Sambuc neg1 = isneg(&s); /* signal */
249*0a6a1f1dSLionel Sambuc if (!lisdigit(cast_uchar(*s)))
250*0a6a1f1dSLionel Sambuc return 0.0; /* invalid; must have at least one digit */
251*0a6a1f1dSLionel Sambuc while (lisdigit(cast_uchar(*s))) /* read exponent */
252*0a6a1f1dSLionel Sambuc exp1 = exp1 * 10 + *(s++) - '0';
253*0a6a1f1dSLionel Sambuc if (neg1) exp1 = -exp1;
254*0a6a1f1dSLionel Sambuc e += exp1;
255*0a6a1f1dSLionel Sambuc *endptr = cast(char *, s); /* valid up to here */
256*0a6a1f1dSLionel Sambuc }
257*0a6a1f1dSLionel Sambuc if (neg) r = -r;
258*0a6a1f1dSLionel Sambuc return l_mathop(ldexp)(r, e);
259*0a6a1f1dSLionel Sambuc }
260*0a6a1f1dSLionel Sambuc
261*0a6a1f1dSLionel Sambuc #endif
262*0a6a1f1dSLionel Sambuc /* }====================================================== */
263*0a6a1f1dSLionel Sambuc
264*0a6a1f1dSLionel Sambuc
l_str2d(const char * s,lua_Number * result)265*0a6a1f1dSLionel Sambuc static const char *l_str2d (const char *s, lua_Number *result) {
26611be35a1SLionel Sambuc char *endptr;
267*0a6a1f1dSLionel Sambuc if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */
268*0a6a1f1dSLionel Sambuc return NULL;
269*0a6a1f1dSLionel Sambuc else if (strpbrk(s, "xX")) /* hex? */
270*0a6a1f1dSLionel Sambuc *result = lua_strx2number(s, &endptr);
271*0a6a1f1dSLionel Sambuc else
27211be35a1SLionel Sambuc *result = lua_str2number(s, &endptr);
273*0a6a1f1dSLionel Sambuc if (endptr == s) return NULL; /* nothing recognized */
274*0a6a1f1dSLionel Sambuc while (lisspace(cast_uchar(*endptr))) endptr++;
275*0a6a1f1dSLionel Sambuc return (*endptr == '\0' ? endptr : NULL); /* OK if no trailing characters */
276*0a6a1f1dSLionel Sambuc }
277*0a6a1f1dSLionel Sambuc #endif /* _KERNEL */
278*0a6a1f1dSLionel Sambuc
279*0a6a1f1dSLionel Sambuc
l_str2int(const char * s,lua_Integer * result)280*0a6a1f1dSLionel Sambuc static const char *l_str2int (const char *s, lua_Integer *result) {
281*0a6a1f1dSLionel Sambuc lua_Unsigned a = 0;
282*0a6a1f1dSLionel Sambuc int empty = 1;
283*0a6a1f1dSLionel Sambuc int neg;
284*0a6a1f1dSLionel Sambuc while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
285*0a6a1f1dSLionel Sambuc neg = isneg(&s);
286*0a6a1f1dSLionel Sambuc if (s[0] == '0' &&
287*0a6a1f1dSLionel Sambuc (s[1] == 'x' || s[1] == 'X')) { /* hex? */
288*0a6a1f1dSLionel Sambuc s += 2; /* skip '0x' */
289*0a6a1f1dSLionel Sambuc for (; lisxdigit(cast_uchar(*s)); s++) {
290*0a6a1f1dSLionel Sambuc a = a * 16 + luaO_hexavalue(*s);
291*0a6a1f1dSLionel Sambuc empty = 0;
292*0a6a1f1dSLionel Sambuc }
293*0a6a1f1dSLionel Sambuc }
294*0a6a1f1dSLionel Sambuc else { /* decimal */
295*0a6a1f1dSLionel Sambuc for (; lisdigit(cast_uchar(*s)); s++) {
296*0a6a1f1dSLionel Sambuc a = a * 10 + *s - '0';
297*0a6a1f1dSLionel Sambuc empty = 0;
298*0a6a1f1dSLionel Sambuc }
299*0a6a1f1dSLionel Sambuc }
300*0a6a1f1dSLionel Sambuc while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */
301*0a6a1f1dSLionel Sambuc if (empty || *s != '\0') return NULL; /* something wrong in the numeral */
302*0a6a1f1dSLionel Sambuc else {
303*0a6a1f1dSLionel Sambuc *result = l_castU2S((neg) ? 0u - a : a);
304*0a6a1f1dSLionel Sambuc return s;
305*0a6a1f1dSLionel Sambuc }
30611be35a1SLionel Sambuc }
30711be35a1SLionel Sambuc
30811be35a1SLionel Sambuc
luaO_str2num(const char * s,TValue * o)309*0a6a1f1dSLionel Sambuc size_t luaO_str2num (const char *s, TValue *o) {
310*0a6a1f1dSLionel Sambuc #ifndef _KERNEL
311*0a6a1f1dSLionel Sambuc lua_Integer i; lua_Number n;
312*0a6a1f1dSLionel Sambuc #else /* _KERNEL */
313*0a6a1f1dSLionel Sambuc lua_Integer i;
314*0a6a1f1dSLionel Sambuc #endif
315*0a6a1f1dSLionel Sambuc const char *e;
316*0a6a1f1dSLionel Sambuc if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */
317*0a6a1f1dSLionel Sambuc setivalue(o, i);
318*0a6a1f1dSLionel Sambuc }
319*0a6a1f1dSLionel Sambuc #ifndef _KERNEL
320*0a6a1f1dSLionel Sambuc else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */
321*0a6a1f1dSLionel Sambuc setfltvalue(o, n);
322*0a6a1f1dSLionel Sambuc }
323*0a6a1f1dSLionel Sambuc #endif
324*0a6a1f1dSLionel Sambuc else
325*0a6a1f1dSLionel Sambuc return 0; /* conversion failed */
326*0a6a1f1dSLionel Sambuc return (e - s) + 1; /* success; return string size */
32711be35a1SLionel Sambuc }
32811be35a1SLionel Sambuc
32911be35a1SLionel Sambuc
luaO_utf8esc(char * buff,unsigned long x)330*0a6a1f1dSLionel Sambuc int luaO_utf8esc (char *buff, unsigned long x) {
331*0a6a1f1dSLionel Sambuc int n = 1; /* number of bytes put in buffer (backwards) */
332*0a6a1f1dSLionel Sambuc lua_assert(x <= 0x10FFFF);
333*0a6a1f1dSLionel Sambuc if (x < 0x80) /* ascii? */
334*0a6a1f1dSLionel Sambuc buff[UTF8BUFFSZ - 1] = cast(char, x);
335*0a6a1f1dSLionel Sambuc else { /* need continuation bytes */
336*0a6a1f1dSLionel Sambuc unsigned int mfb = 0x3f; /* maximum that fits in first byte */
337*0a6a1f1dSLionel Sambuc do { /* add continuation bytes */
338*0a6a1f1dSLionel Sambuc buff[UTF8BUFFSZ - (n++)] = cast(char, 0x80 | (x & 0x3f));
339*0a6a1f1dSLionel Sambuc x >>= 6; /* remove added bits */
340*0a6a1f1dSLionel Sambuc mfb >>= 1; /* now there is one less bit available in first byte */
341*0a6a1f1dSLionel Sambuc } while (x > mfb); /* still needs continuation byte? */
342*0a6a1f1dSLionel Sambuc buff[UTF8BUFFSZ - n] = cast(char, (~mfb << 1) | x); /* add first byte */
343*0a6a1f1dSLionel Sambuc }
344*0a6a1f1dSLionel Sambuc return n;
345*0a6a1f1dSLionel Sambuc }
346*0a6a1f1dSLionel Sambuc
347*0a6a1f1dSLionel Sambuc
348*0a6a1f1dSLionel Sambuc /* maximum length of the conversion of a number to a string */
349*0a6a1f1dSLionel Sambuc #define MAXNUMBER2STR 50
350*0a6a1f1dSLionel Sambuc
351*0a6a1f1dSLionel Sambuc
352*0a6a1f1dSLionel Sambuc /*
353*0a6a1f1dSLionel Sambuc ** Convert a number object to a string
354*0a6a1f1dSLionel Sambuc */
luaO_tostring(lua_State * L,StkId obj)355*0a6a1f1dSLionel Sambuc void luaO_tostring (lua_State *L, StkId obj) {
356*0a6a1f1dSLionel Sambuc char buff[MAXNUMBER2STR];
357*0a6a1f1dSLionel Sambuc size_t len;
358*0a6a1f1dSLionel Sambuc lua_assert(ttisnumber(obj));
359*0a6a1f1dSLionel Sambuc #ifndef _KERNEL
360*0a6a1f1dSLionel Sambuc if (ttisinteger(obj))
361*0a6a1f1dSLionel Sambuc len = lua_integer2str(buff, ivalue(obj));
362*0a6a1f1dSLionel Sambuc else {
363*0a6a1f1dSLionel Sambuc len = lua_number2str(buff, fltvalue(obj));
364*0a6a1f1dSLionel Sambuc #if !defined(LUA_COMPAT_FLOATSTRING)
365*0a6a1f1dSLionel Sambuc if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
366*0a6a1f1dSLionel Sambuc buff[len++] = lua_getlocaledecpoint();
367*0a6a1f1dSLionel Sambuc buff[len++] = '0'; /* adds '.0' to result */
368*0a6a1f1dSLionel Sambuc }
369*0a6a1f1dSLionel Sambuc #endif
370*0a6a1f1dSLionel Sambuc }
371*0a6a1f1dSLionel Sambuc #else /* _KERNEL */
372*0a6a1f1dSLionel Sambuc lua_assert(ttisinteger(obj));
373*0a6a1f1dSLionel Sambuc len = lua_integer2str(buff, ivalue(obj));
374*0a6a1f1dSLionel Sambuc #endif
375*0a6a1f1dSLionel Sambuc setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
376*0a6a1f1dSLionel Sambuc }
377*0a6a1f1dSLionel Sambuc
378*0a6a1f1dSLionel Sambuc
pushstr(lua_State * L,const char * str,size_t l)379*0a6a1f1dSLionel Sambuc static void pushstr (lua_State *L, const char *str, size_t l) {
380*0a6a1f1dSLionel Sambuc setsvalue2s(L, L->top++, luaS_newlstr(L, str, l));
381*0a6a1f1dSLionel Sambuc }
382*0a6a1f1dSLionel Sambuc
383*0a6a1f1dSLionel Sambuc
384*0a6a1f1dSLionel Sambuc /* this function handles only '%d', '%c', '%f', '%p', and '%s'
385*0a6a1f1dSLionel Sambuc conventional formats, plus Lua-specific '%I' and '%U' */
luaO_pushvfstring(lua_State * L,const char * fmt,va_list argp)38611be35a1SLionel Sambuc const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
387*0a6a1f1dSLionel Sambuc int n = 0;
38811be35a1SLionel Sambuc for (;;) {
38911be35a1SLionel Sambuc const char *e = strchr(fmt, '%');
39011be35a1SLionel Sambuc if (e == NULL) break;
391*0a6a1f1dSLionel Sambuc luaD_checkstack(L, 2); /* fmt + item */
392*0a6a1f1dSLionel Sambuc pushstr(L, fmt, e - fmt);
39311be35a1SLionel Sambuc switch (*(e+1)) {
39411be35a1SLionel Sambuc case 's': {
39511be35a1SLionel Sambuc const char *s = va_arg(argp, char *);
39611be35a1SLionel Sambuc if (s == NULL) s = "(null)";
397*0a6a1f1dSLionel Sambuc pushstr(L, s, strlen(s));
39811be35a1SLionel Sambuc break;
39911be35a1SLionel Sambuc }
40011be35a1SLionel Sambuc case 'c': {
401*0a6a1f1dSLionel Sambuc char buff = cast(char, va_arg(argp, int));
402*0a6a1f1dSLionel Sambuc if (lisprint(cast_uchar(buff)))
403*0a6a1f1dSLionel Sambuc pushstr(L, &buff, 1);
404*0a6a1f1dSLionel Sambuc else /* non-printable character; print its code */
405*0a6a1f1dSLionel Sambuc luaO_pushfstring(L, "<\\%d>", cast_uchar(buff));
40611be35a1SLionel Sambuc break;
40711be35a1SLionel Sambuc }
40811be35a1SLionel Sambuc case 'd': {
409*0a6a1f1dSLionel Sambuc setivalue(L->top++, va_arg(argp, int));
410*0a6a1f1dSLionel Sambuc luaO_tostring(L, L->top - 1);
41111be35a1SLionel Sambuc break;
41211be35a1SLionel Sambuc }
413*0a6a1f1dSLionel Sambuc case 'I': {
414*0a6a1f1dSLionel Sambuc setivalue(L->top++, cast(lua_Integer, va_arg(argp, l_uacInt)));
415*0a6a1f1dSLionel Sambuc luaO_tostring(L, L->top - 1);
416*0a6a1f1dSLionel Sambuc break;
417*0a6a1f1dSLionel Sambuc }
418*0a6a1f1dSLionel Sambuc #ifndef _KERNEL
41911be35a1SLionel Sambuc case 'f': {
420*0a6a1f1dSLionel Sambuc setfltvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));
421*0a6a1f1dSLionel Sambuc luaO_tostring(L, L->top - 1);
42211be35a1SLionel Sambuc break;
42311be35a1SLionel Sambuc }
424*0a6a1f1dSLionel Sambuc #endif
42511be35a1SLionel Sambuc case 'p': {
426*0a6a1f1dSLionel Sambuc char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */
427*0a6a1f1dSLionel Sambuc int l = sprintf(buff, "%p", va_arg(argp, void *));
428*0a6a1f1dSLionel Sambuc pushstr(L, buff, l);
429*0a6a1f1dSLionel Sambuc break;
430*0a6a1f1dSLionel Sambuc }
431*0a6a1f1dSLionel Sambuc case 'U': {
432*0a6a1f1dSLionel Sambuc char buff[UTF8BUFFSZ];
433*0a6a1f1dSLionel Sambuc int l = luaO_utf8esc(buff, cast(long, va_arg(argp, long)));
434*0a6a1f1dSLionel Sambuc pushstr(L, buff + UTF8BUFFSZ - l, l);
43511be35a1SLionel Sambuc break;
43611be35a1SLionel Sambuc }
43711be35a1SLionel Sambuc case '%': {
438*0a6a1f1dSLionel Sambuc pushstr(L, "%", 1);
43911be35a1SLionel Sambuc break;
44011be35a1SLionel Sambuc }
44111be35a1SLionel Sambuc default: {
442*0a6a1f1dSLionel Sambuc luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'",
443*0a6a1f1dSLionel Sambuc *(e + 1));
44411be35a1SLionel Sambuc }
44511be35a1SLionel Sambuc }
44611be35a1SLionel Sambuc n += 2;
44711be35a1SLionel Sambuc fmt = e+2;
44811be35a1SLionel Sambuc }
449*0a6a1f1dSLionel Sambuc luaD_checkstack(L, 1);
450*0a6a1f1dSLionel Sambuc pushstr(L, fmt, strlen(fmt));
451*0a6a1f1dSLionel Sambuc if (n > 0) luaV_concat(L, n + 1);
45211be35a1SLionel Sambuc return svalue(L->top - 1);
45311be35a1SLionel Sambuc }
45411be35a1SLionel Sambuc
45511be35a1SLionel Sambuc
luaO_pushfstring(lua_State * L,const char * fmt,...)45611be35a1SLionel Sambuc const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
45711be35a1SLionel Sambuc const char *msg;
45811be35a1SLionel Sambuc va_list argp;
45911be35a1SLionel Sambuc va_start(argp, fmt);
46011be35a1SLionel Sambuc msg = luaO_pushvfstring(L, fmt, argp);
46111be35a1SLionel Sambuc va_end(argp);
46211be35a1SLionel Sambuc return msg;
46311be35a1SLionel Sambuc }
46411be35a1SLionel Sambuc
46511be35a1SLionel Sambuc
466*0a6a1f1dSLionel Sambuc /* number of chars of a literal string without the ending \0 */
467*0a6a1f1dSLionel Sambuc #define LL(x) (sizeof(x)/sizeof(char) - 1)
468*0a6a1f1dSLionel Sambuc
469*0a6a1f1dSLionel Sambuc #define RETS "..."
470*0a6a1f1dSLionel Sambuc #define PRE "[string \""
471*0a6a1f1dSLionel Sambuc #define POS "\"]"
472*0a6a1f1dSLionel Sambuc
473*0a6a1f1dSLionel Sambuc #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
474*0a6a1f1dSLionel Sambuc
luaO_chunkid(char * out,const char * source,size_t bufflen)47511be35a1SLionel Sambuc void luaO_chunkid (char *out, const char *source, size_t bufflen) {
476*0a6a1f1dSLionel Sambuc size_t l = strlen(source);
477*0a6a1f1dSLionel Sambuc if (*source == '=') { /* 'literal' source */
478*0a6a1f1dSLionel Sambuc if (l <= bufflen) /* small enough? */
479*0a6a1f1dSLionel Sambuc memcpy(out, source + 1, l * sizeof(char));
480*0a6a1f1dSLionel Sambuc else { /* truncate it */
481*0a6a1f1dSLionel Sambuc addstr(out, source + 1, bufflen - 1);
482*0a6a1f1dSLionel Sambuc *out = '\0';
48311be35a1SLionel Sambuc }
48411be35a1SLionel Sambuc }
485*0a6a1f1dSLionel Sambuc else if (*source == '@') { /* file name */
486*0a6a1f1dSLionel Sambuc if (l <= bufflen) /* small enough? */
487*0a6a1f1dSLionel Sambuc memcpy(out, source + 1, l * sizeof(char));
488*0a6a1f1dSLionel Sambuc else { /* add '...' before rest of name */
489*0a6a1f1dSLionel Sambuc addstr(out, RETS, LL(RETS));
490*0a6a1f1dSLionel Sambuc bufflen -= LL(RETS);
491*0a6a1f1dSLionel Sambuc memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
49211be35a1SLionel Sambuc }
493*0a6a1f1dSLionel Sambuc }
494*0a6a1f1dSLionel Sambuc else { /* string; format as [string "source"] */
495*0a6a1f1dSLionel Sambuc const char *nl = strchr(source, '\n'); /* find first new line (if any) */
496*0a6a1f1dSLionel Sambuc addstr(out, PRE, LL(PRE)); /* add prefix */
497*0a6a1f1dSLionel Sambuc bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
498*0a6a1f1dSLionel Sambuc if (l < bufflen && nl == NULL) { /* small one-line source? */
499*0a6a1f1dSLionel Sambuc addstr(out, source, l); /* keep it */
500*0a6a1f1dSLionel Sambuc }
501*0a6a1f1dSLionel Sambuc else {
502*0a6a1f1dSLionel Sambuc if (nl != NULL) l = nl - source; /* stop at first newline */
503*0a6a1f1dSLionel Sambuc if (l > bufflen) l = bufflen;
504*0a6a1f1dSLionel Sambuc addstr(out, source, l);
505*0a6a1f1dSLionel Sambuc addstr(out, RETS, LL(RETS));
506*0a6a1f1dSLionel Sambuc }
507*0a6a1f1dSLionel Sambuc memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
508*0a6a1f1dSLionel Sambuc }
509*0a6a1f1dSLionel Sambuc }
510*0a6a1f1dSLionel Sambuc
511