xref: /illumos-gate/usr/src/uts/common/fs/zfs/lua/lobject.c (revision dfc115332c94a2f62058ac7f2bce7631fbd20b3d)
1 /*
2 ** $Id: lobject.c,v 2.58.1.1 2013/04/12 18:48:47 roberto Exp $
3 ** Some generic functions over Lua objects
4 ** See Copyright Notice in lua.h
5 */
6 
7 #include <sys/zfs_context.h>
8 
9 #define lobject_c
10 #define LUA_CORE
11 
12 #include "lua.h"
13 
14 #include "lctype.h"
15 #include "ldebug.h"
16 #include "ldo.h"
17 #include "lmem.h"
18 #include "lobject.h"
19 #include "lstate.h"
20 #include "lstring.h"
21 #include "lvm.h"
22 
23 
24 
25 LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};
26 
27 
28 /*
29 ** converts an integer to a "floating point byte", represented as
30 ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
31 ** eeeee != 0 and (xxx) otherwise.
32 */
luaO_int2fb(unsigned int x)33 int luaO_int2fb (unsigned int x) {
34   int e = 0;  /* exponent */
35   if (x < 8) return x;
36   while (x >= 0x10) {
37     x = (x+1) >> 1;
38     e++;
39   }
40   return ((e+1) << 3) | (cast_int(x) - 8);
41 }
42 
43 
44 /* converts back */
luaO_fb2int(int x)45 int luaO_fb2int (int x) {
46   int e = (x >> 3) & 0x1f;
47   if (e == 0) return x;
48   else return ((x & 7) + 8) << (e - 1);
49 }
50 
51 
luaO_ceillog2(unsigned int x)52 int luaO_ceillog2 (unsigned int x) {
53   static const lu_byte log_2[256] = {
54     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,
55     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,
56     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,
57     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,
58     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,
59     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,
60     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,
61     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
62   };
63   int l = 0;
64   x--;
65   while (x >= 256) { l += 8; x >>= 8; }
66   return l + log_2[x];
67 }
68 
69 
luaO_arith(int op,lua_Number v1,lua_Number v2)70 lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {
71   switch (op) {
72     case LUA_OPADD: return luai_numadd(NULL, v1, v2);
73     case LUA_OPSUB: return luai_numsub(NULL, v1, v2);
74     case LUA_OPMUL: return luai_nummul(NULL, v1, v2);
75     case LUA_OPDIV: return luai_numdiv(NULL, v1, v2);
76     case LUA_OPMOD: return luai_nummod(NULL, v1, v2);
77     case LUA_OPPOW: return luai_numpow(NULL, v1, v2);
78     case LUA_OPUNM: return luai_numunm(NULL, v1);
79     default: lua_assert(0); return 0;
80   }
81 }
82 
83 
luaO_hexavalue(int c)84 int luaO_hexavalue (int c) {
85   if (lisdigit(c)) return c - '0';
86   else return ltolower(c) - 'a' + 10;
87 }
88 
89 
90 #if !defined(lua_strx2number)
91 
92 
93 
isneg(const char ** s)94 static int isneg (const char **s) {
95   if (**s == '-') { (*s)++; return 1; }
96   else if (**s == '+') (*s)++;
97   return 0;
98 }
99 
100 
readhexa(const char ** s,lua_Number r,int * count)101 static lua_Number readhexa (const char **s, lua_Number r, int *count) {
102   for (; lisxdigit(cast_uchar(**s)); (*s)++) {  /* read integer part */
103     r = (r * cast_num(16.0)) + cast_num(luaO_hexavalue(cast_uchar(**s)));
104     (*count)++;
105   }
106   return r;
107 }
108 
109 
110 /*
111 ** convert an hexadecimal numeric string to a number, following
112 ** C99 specification for 'strtod'
113 */
lua_strx2number(const char * s,char ** endptr)114 static lua_Number lua_strx2number (const char *s, char **endptr) {
115   lua_Number r = 0.0;
116   int e = 0, i = 0;
117   int neg = 0;  /* 1 if number is negative */
118   *endptr = cast(char *, s);  /* nothing is valid yet */
119   while (lisspace(cast_uchar(*s))) s++;  /* skip initial spaces */
120   neg = isneg(&s);  /* check signal */
121   if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X')))  /* check '0x' */
122     return 0.0;  /* invalid format (no '0x') */
123   s += 2;  /* skip '0x' */
124   r = readhexa(&s, r, &i);  /* read integer part */
125   if (*s == '.') {
126     s++;  /* skip dot */
127     r = readhexa(&s, r, &e);  /* read fractional part */
128   }
129   if (i == 0 && e == 0)
130     return 0.0;  /* invalid format (no digit) */
131   e *= -4;  /* each fractional digit divides value by 2^-4 */
132   *endptr = cast(char *, s);  /* valid up to here */
133   if (*s == 'p' || *s == 'P') {  /* exponent part? */
134     int exp1 = 0;
135     int neg1;
136     s++;  /* skip 'p' */
137     neg1 = isneg(&s);  /* signal */
138     if (!lisdigit(cast_uchar(*s)))
139       goto ret;  /* must have at least one digit */
140     while (lisdigit(cast_uchar(*s)))  /* read exponent */
141       exp1 = exp1 * 10 + *(s++) - '0';
142     if (neg1) exp1 = -exp1;
143     e += exp1;
144   }
145   *endptr = cast(char *, s);  /* valid up to here */
146  ret:
147   if (neg) r = -r;
148   return (r * (1 << e));
149 }
150 
151 #endif
152 
153 
luaO_str2d(const char * s,size_t len,lua_Number * result)154 int luaO_str2d (const char *s, size_t len, lua_Number *result) {
155   char *endptr;
156   if (strpbrk(s, "nN"))  /* reject 'inf' and 'nan' */
157     return 0;
158   else if (strpbrk(s, "xX"))  /* hexa? */
159     *result = lua_strx2number(s, &endptr);
160   else
161     *result = lua_str2number(s, &endptr);
162   if (endptr == s) return 0;  /* nothing recognized */
163   while (lisspace(cast_uchar(*endptr))) endptr++;
164   return (endptr == s + len);  /* OK if no trailing characters */
165 }
166 
167 
168 
pushstr(lua_State * L,const char * str,size_t l)169 static void pushstr (lua_State *L, const char *str, size_t l) {
170   setsvalue2s(L, L->top++, luaS_newlstr(L, str, l));
171 }
172 
173 
174 /* this function handles only `%d', `%c', %f, %p, and `%s' formats */
luaO_pushvfstring(lua_State * L,const char * fmt,va_list argp)175 const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
176   int n = 0;
177   for (;;) {
178     const char *e = strchr(fmt, '%');
179     if (e == NULL) break;
180     luaD_checkstack(L, 2);  /* fmt + item */
181     pushstr(L, fmt, e - fmt);
182     switch (*(e+1)) {
183       case 's': {
184         const char *s = va_arg(argp, char *);
185         if (s == NULL) s = "(null)";
186         pushstr(L, s, strlen(s));
187         break;
188       }
189       case 'c': {
190         char buff;
191         buff = cast(char, va_arg(argp, int));
192         pushstr(L, &buff, 1);
193         break;
194       }
195       case 'd': {
196         setnvalue(L->top++, cast_num(va_arg(argp, int)));
197         break;
198       }
199       case 'f': {
200         setnvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));
201         break;
202       }
203       case 'p': {
204         char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
205         int l = lcompat_sprintf(buff, "%p", va_arg(argp, void *));
206         pushstr(L, buff, l);
207         break;
208       }
209       case '%': {
210         pushstr(L, "%", 1);
211         break;
212       }
213       default: {
214         luaG_runerror(L,
215             "invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"),
216             *(e + 1));
217       }
218     }
219     n += 2;
220     fmt = e+2;
221   }
222   luaD_checkstack(L, 1);
223   pushstr(L, fmt, strlen(fmt));
224   if (n > 0) luaV_concat(L, n + 1);
225   return svalue(L->top - 1);
226 }
227 
228 
luaO_pushfstring(lua_State * L,const char * fmt,...)229 const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
230   const char *msg;
231   va_list argp;
232   va_start(argp, fmt);
233   msg = luaO_pushvfstring(L, fmt, argp);
234   va_end(argp);
235   return msg;
236 }
237 
238 
239 /* number of chars of a literal string without the ending \0 */
240 #define LL(x)	(sizeof(x)/sizeof(char) - 1)
241 
242 #define RETS	"..."
243 #define PRE	"[string \""
244 #define POS	"\"]"
245 
246 #define addstr(a,b,l)	( memcpy(a,b,(l) * sizeof(char)), a += (l) )
247 
luaO_chunkid(char * out,const char * source,size_t bufflen)248 void luaO_chunkid (char *out, const char *source, size_t bufflen) {
249   size_t l = strlen(source);
250   if (*source == '=') {  /* 'literal' source */
251     if (l <= bufflen)  /* small enough? */
252       memcpy(out, source + 1, l * sizeof(char));
253     else {  /* truncate it */
254       addstr(out, source + 1, bufflen - 1);
255       *out = '\0';
256     }
257   }
258   else if (*source == '@') {  /* file name */
259     if (l <= bufflen)  /* small enough? */
260       memcpy(out, source + 1, l * sizeof(char));
261     else {  /* add '...' before rest of name */
262       addstr(out, RETS, LL(RETS));
263       bufflen -= LL(RETS);
264       memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
265     }
266   }
267   else {  /* string; format as [string "source"] */
268     const char *nl = strchr(source, '\n');  /* find first new line (if any) */
269     addstr(out, PRE, LL(PRE));  /* add prefix */
270     bufflen -= LL(PRE RETS POS) + 1;  /* save space for prefix+suffix+'\0' */
271     if (l < bufflen && nl == NULL) {  /* small one-line source? */
272       addstr(out, source, l);  /* keep it */
273     }
274     else {
275       if (nl != NULL) l = nl - source;  /* stop at first newline */
276       if (l > bufflen) l = bufflen;
277       addstr(out, source, l);
278       addstr(out, RETS, LL(RETS));
279     }
280     memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
281   }
282 }
283 
284