History

Lua 5.3.0-work2

Lua Team committed 12 years ago

Commit ad386d91 parent b83a40a60 files · +3280 −2672Browse files

Showing the changes to src/lobject.c (1 of 60 files). Show all

Unified · Split

src/lobject.c+88−38
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lobject.c,v 2.67 2013/06/25 18:58:32 roberto Exp $
2+** $Id: lobject.c,v 2.76 2014/03/21 13:52:33 roberto Exp $
33 ** Some generic functions over Lua objects
44 ** See Copyright Notice in lua.h
55 */
@@ -77,22 +77,30 @@ static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, · 71 unmodified lines
7777 case LUA_OPSUB:return intop(-, v1, v2);
7878 case LUA_OPMUL:return intop(*, v1, v2);
7979 case LUA_OPMOD: return luaV_mod(L, v1, v2);
80− case LUA_OPPOW: return luaV_pow(v1, v2);
81− case LUA_OPUNM: return -v1;
80+ case LUA_OPPOW: return luaV_pow(L, v1, v2);
81+ case LUA_OPIDIV: return luaV_div(L, v1, v2);
82+ case LUA_OPBAND: return intop(&, v1, v2);
83+ case LUA_OPBOR: return intop(|, v1, v2);
84+ case LUA_OPBXOR: return intop(^, v1, v2);
85+ case LUA_OPSHL: return luaV_shiftl(v1, v2);
86+ case LUA_OPSHR: return luaV_shiftl(v1, -v2);
87+ case LUA_OPUNM: return intop(-, 0, v1);
88+ case LUA_OPBNOT: return intop(^, cast_integer(-1), v1);
8289 default: lua_assert(0); return 0;
8390 }
8491 }
8592
8693
87−static lua_Number numarith (int op, lua_Number v1, lua_Number v2) {
94+static lua_Number numarith (lua_State *L, int op, lua_Number v1,
95+ lua_Number v2) {
8896 switch (op) {
89− case LUA_OPADD: return luai_numadd(NULL, v1, v2);
90− case LUA_OPSUB: return luai_numsub(NULL, v1, v2);
91− case LUA_OPMUL: return luai_nummul(NULL, v1, v2);
92− case LUA_OPDIV: return luai_numdiv(NULL, v1, v2);
93− case LUA_OPMOD: return luai_nummod(NULL, v1, v2);
94− case LUA_OPPOW: return luai_numpow(NULL, v1, v2);
95− case LUA_OPUNM: return luai_numunm(NULL, v1);
97+ case LUA_OPADD: return luai_numadd(L, v1, v2);
98+ case LUA_OPSUB: return luai_numsub(L, v1, v2);
99+ case LUA_OPMUL: return luai_nummul(L, v1, v2);
100+ case LUA_OPDIV: return luai_numdiv(L, v1, v2);
101+ case LUA_OPMOD: return luai_nummod(L, v1, v2);
102+ case LUA_OPPOW: return luai_numpow(L, v1, v2);
103+ case LUA_OPUNM: return luai_numunm(L, v1);
96104 default: lua_assert(0); return 0;
97105 }
98106 }
@@ -100,29 +108,40 @@ static lua_Number numarith (int op, lua_Number v1, lua_Number v2) { · 1 unmodified line
100108
101109 void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
102110 TValue *res) {
103− if (op == LUA_OPIDIV) { /* operates only on integers */
104− lua_Integer i1; lua_Integer i2;
105− if (tointeger(p1, &i1) && tointeger(p2, &i2)) {
106− setivalue(res, luaV_div(L, i1, i2));
107− return;
111+ switch (op) {
112+ case LUA_OPIDIV: case LUA_OPBAND: case LUA_OPBOR:
113+ case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR:
114+ case LUA_OPBNOT: { /* operates only on integers */
115+ lua_Integer i1; lua_Integer i2;
116+ if (tointeger(p1, &i1) && tointeger(p2, &i2)) {
117+ setivalue(res, intarith(L, op, i1, i2));
118+ return;
119+ }
120+ else break; /* go to the end */
108121 }
109− /* else go to the end */
110− }
111− else { /* other operations */
112− lua_Number n1; lua_Number n2;
113− if (ttisinteger(p1) && ttisinteger(p2) && op != LUA_OPDIV &&
114− (op != LUA_OPPOW || ivalue(p2) >= 0)) {
115− setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2)));
116− return;
122+ case LUA_OPDIV: { /* operates only on floats */
123+ lua_Number n1; lua_Number n2;
124+ if (tonumber(p1, &n1) && tonumber(p2, &n2)) {
125+ setnvalue(res, numarith(L, op, n1, n2));
126+ return;
127+ }
128+ else break; /* go to the end */
117129 }
118− else if (tonumber(p1, &n1) && tonumber(p2, &n2)) {
119− setnvalue(res, numarith(op, n1, n2));
120− return;
130+ default: { /* other operations */
131+ lua_Number n1; lua_Number n2;
132+ if (ttisinteger(p1) && ttisinteger(p2)) {
133+ setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2)));
134+ return;
135+ }
136+ else if (tonumber(p1, &n1) && tonumber(p2, &n2)) {
137+ setnvalue(res, numarith(L, op, n1, n2));
138+ return;
139+ }
140+ else break; /* go to the end */
121141 }
122− /* else go to the end */
123142 }
124− /* could not perform raw operation; try metmethod */
125− lua_assert(L != NULL); /* cannot fail when folding (compile time) */
143+ /* could not perform raw operation; try metamethod */
144+ lua_assert(L != NULL); /* should not fail when folding (compile time) */
126145 luaT_trybinTM(L, p1, p2, res, cast(TMS, op - LUA_OPADD + TM_ADD));
127146 }
128147
@@ -141,22 +160,26 @@ static int isneg (const char **s) { · 12 unmodified lines
141160
142161
143162 /*
163+** {======================================================
144164 ** lua_strx2number converts an hexadecimal numeric string to a number.
145165 ** In C99, 'strtod' does both conversions. C89, however, has no function
146166 ** to convert floating hexadecimal strings to numbers. For these
147167 ** systems, you can leave 'lua_strx2number' undefined and Lua will
148168 ** provide its own implementation.
169+** =======================================================
149170 */
150−#if defined(LUA_USE_STRTODHEX)
171+#if !defined(lua_strx2number) /* { */
172+
173+#if defined(LUA_USE_C99) /* { */
174+
151175 #define lua_strx2number(s,p) lua_str2number(s,p)
152−#endif
153176
177+#else /* }{ */
154178
155−#if !defined(lua_strx2number)
179+/* Lua's implementation for 'lua_strx2number' */
156180
157181 #include <math.h>
158182
159−
160183 /* maximum number of significant digits to read (to avoid overflows
161184 even with single floats) */
162185 #define MAXSIGDIG 30
@@ -219,7 +242,11 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { · 56 unmodified lines
219242 return l_mathop(ldexp)(r, e);
220243 }
221244
222−#endif
245+#endif /* } */
246+
247+#endif /* } */
248+
249+/* }====================================================== */
223250
224251
225252 int luaO_str2d (const char *s, size_t len, lua_Number *result) {
@@ -260,19 +287,36 @@ int luaO_str2int (const char *s, size_t len, lua_Integer *result) { · 34 unmodified lines
260287 while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */
261288 if (empty || s != ends) return 0; /* something wrong in the numeral */
262289 else {
263− if (neg) *result = -cast(lua_Integer, a);
264− else *result = cast(lua_Integer, a);
290+ *result = cast_integer((neg) ? 0u - a : a);
265291 return 1;
266292 }
267293 }
268294
269295
296+int luaO_utf8esc (char *buff, unsigned int x) {
297+ int n = 1; /* number of bytes put in buffer (backwards) */
298+ if (x < 0x80) /* ascii? */
299+ buff[UTF8BUFFSZ - 1] = x;
300+ else { /* need continuation bytes */
301+ unsigned int mfb = 0x3f; /* maximum that fits in first byte */
302+ do {
303+ buff[UTF8BUFFSZ - (n++)] = 0x80 | (x & 0x3f); /* add continuation byte */
304+ x >>= 6; /* remove added bits */
305+ mfb >>= 1; /* now there is one less bit available in first byte */
306+ } while (x > mfb); /* still needs continuation byte? */
307+ buff[UTF8BUFFSZ - n] = (~mfb << 1) | x; /* add first byte */
308+ }
309+ return n;
310+}
311+
312+
270313 static void pushstr (lua_State *L, const char *str, size_t l) {
271314 setsvalue2s(L, L->top++, luaS_newlstr(L, str, l));
272315 }
273316
274317
275−/* this function handles only `%d', `%c', %f, %p, and `%s' formats */
318+/* this function handles only '%d', '%c', '%f', '%p', and '%s'
319+ conventional formats, plus Lua-specific '%L' and '%U' */
276320 const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
277321 int n = 0;
278322 for (;;) {
@@ -311,6 +355,12 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { · 32 unmodified lines
311355 pushstr(L, buff, l);
312356 break;
313357 }
358+ case 'U': {
359+ char buff[UTF8BUFFSZ];
360+ int l = luaO_utf8esc(buff, va_arg(argp, int));
361+ pushstr(L, buff + UTF8BUFFSZ - l, l);
362+ break;
363+ }
314364 case '%': {
315365 pushstr(L, "%", 1);
316366 break;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t