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