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