1
0
mirror of https://github.com/lua/lua.git synced 2025-01-28 06:03:00 +08:00

macros luai_num* take a state L (when available) as argument, to allow

them to generate errors (and other facilities)
This commit is contained in:
Roberto Ierusalimschy 2006-08-07 16:14:30 -03:00
parent ca7e5b5cb6
commit dfe2f1eeff
4 changed files with 38 additions and 38 deletions

18
lcode.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lcode.c,v 2.25 2006/03/21 19:28:49 roberto Exp roberto $ ** $Id: lcode.c,v 2.26 2006/06/22 16:12:59 roberto Exp roberto $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -637,21 +637,21 @@ static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
v1 = e1->u.nval; v1 = e1->u.nval;
v2 = e2->u.nval; v2 = e2->u.nval;
switch (op) { switch (op) {
case OP_ADD: r = luai_numadd(v1, v2); break; case OP_ADD: r = luai_numadd(NULL, v1, v2); break;
case OP_SUB: r = luai_numsub(v1, v2); break; case OP_SUB: r = luai_numsub(NULL, v1, v2); break;
case OP_MUL: r = luai_nummul(v1, v2); break; case OP_MUL: r = luai_nummul(NULL, v1, v2); break;
case OP_DIV: case OP_DIV:
if (v2 == 0) return 0; /* do not attempt to divide by 0 */ if (v2 == 0) return 0; /* do not attempt to divide by 0 */
r = luai_numdiv(v1, v2); break; r = luai_numdiv(NULL, v1, v2); break;
case OP_MOD: case OP_MOD:
if (v2 == 0) return 0; /* do not attempt to divide by 0 */ if (v2 == 0) return 0; /* do not attempt to divide by 0 */
r = luai_nummod(v1, v2); break; r = luai_nummod(NULL, v1, v2); break;
case OP_POW: r = luai_numpow(v1, v2); break; case OP_POW: r = luai_numpow(NULL, v1, v2); break;
case OP_UNM: r = luai_numunm(v1); break; case OP_UNM: r = luai_numunm(NULL, v1); break;
case OP_LEN: return 0; /* no constant folding for 'len' */ case OP_LEN: return 0; /* no constant folding for 'len' */
default: lua_assert(0); r = 0; break; default: lua_assert(0); r = 0; break;
} }
if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */ if (luai_numisnan(NULL, r)) return 0; /* do not attempt to produce NaN */
e1->u.nval = r; e1->u.nval = r;
return 1; return 1;
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.c,v 2.32 2006/01/18 11:49:02 roberto Exp roberto $ ** $Id: ltable.c,v 2.33 2006/07/11 15:53:29 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -494,7 +494,7 @@ TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
return cast(TValue *, p); return cast(TValue *, p);
else { else {
if (ttisnil(key)) luaG_runerror(L, "table index is nil"); if (ttisnil(key)) luaG_runerror(L, "table index is nil");
else if (ttisnumber(key) && luai_numisnan(nvalue(key))) else if (ttisnumber(key) && luai_numisnan(L, nvalue(key)))
luaG_runerror(L, "table index is NaN"); luaG_runerror(L, "table index is NaN");
return newkey(L, t, key); return newkey(L, t, key);
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: luaconf.h,v 1.82 2006/04/10 18:27:23 roberto Exp roberto $ ** $Id: luaconf.h,v 1.83 2006/08/04 13:34:37 roberto Exp roberto $
** Configuration file for Lua ** Configuration file for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -529,17 +529,17 @@
*/ */
#if defined(LUA_CORE) #if defined(LUA_CORE)
#include <math.h> #include <math.h>
#define luai_numadd(a,b) ((a)+(b)) #define luai_numadd(L,a,b) ((a)+(b))
#define luai_numsub(a,b) ((a)-(b)) #define luai_numsub(L,a,b) ((a)-(b))
#define luai_nummul(a,b) ((a)*(b)) #define luai_nummul(L,a,b) ((a)*(b))
#define luai_numdiv(a,b) ((a)/(b)) #define luai_numdiv(L,a,b) ((a)/(b))
#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b)) #define luai_nummod(L,a,b) ((a) - floor((a)/(b))*(b))
#define luai_numpow(a,b) (pow(a,b)) #define luai_numpow(L,a,b) (pow(a,b))
#define luai_numunm(a) (-(a)) #define luai_numunm(L,a) (-(a))
#define luai_numeq(a,b) ((a)==(b)) #define luai_numeq(a,b) ((a)==(b))
#define luai_numlt(a,b) ((a)<(b)) #define luai_numlt(L,a,b) ((a)<(b))
#define luai_numle(a,b) ((a)<=(b)) #define luai_numle(L,a,b) ((a)<=(b))
#define luai_numisnan(a) (!luai_numeq((a), (a))) #define luai_numisnan(L,a) (!luai_numeq((a), (a)))
#endif #endif

32
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 2.65 2006/07/14 14:40:12 roberto Exp $ ** $Id: lvm.c,v 2.65 2006/07/14 16:22:24 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -225,7 +225,7 @@ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
if (ttype(l) != ttype(r)) if (ttype(l) != ttype(r))
return luaG_ordererror(L, l, r); return luaG_ordererror(L, l, r);
else if (ttisnumber(l)) else if (ttisnumber(l))
return luai_numlt(nvalue(l), nvalue(r)); return luai_numlt(L, nvalue(l), nvalue(r));
else if (ttisstring(l)) else if (ttisstring(l))
return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
else if ((res = call_orderTM(L, l, r, TM_LT)) != -1) else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
@ -239,7 +239,7 @@ static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
if (ttype(l) != ttype(r)) if (ttype(l) != ttype(r))
return luaG_ordererror(L, l, r); return luaG_ordererror(L, l, r);
else if (ttisnumber(l)) else if (ttisnumber(l))
return luai_numle(nvalue(l), nvalue(r)); return luai_numle(L, nvalue(l), nvalue(r));
else if (ttisstring(l)) else if (ttisstring(l))
return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */ else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
@ -318,13 +318,13 @@ static void Arith (lua_State *L, StkId ra, const TValue *rb,
(c = luaV_tonumber(rc, &tempc)) != NULL) { (c = luaV_tonumber(rc, &tempc)) != NULL) {
lua_Number nb = nvalue(b), nc = nvalue(c); lua_Number nb = nvalue(b), nc = nvalue(c);
switch (op) { switch (op) {
case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break; case TM_ADD: setnvalue(ra, luai_numadd(L, nb, nc)); break;
case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break; case TM_SUB: setnvalue(ra, luai_numsub(L, nb, nc)); break;
case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break; case TM_MUL: setnvalue(ra, luai_nummul(L, nb, nc)); break;
case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break; case TM_DIV: setnvalue(ra, luai_numdiv(L, nb, nc)); break;
case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break; case TM_MOD: setnvalue(ra, luai_nummod(L, nb, nc)); break;
case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break; case TM_POW: setnvalue(ra, luai_numpow(L, nb, nc)); break;
case TM_UNM: setnvalue(ra, luai_numunm(nb)); break; case TM_UNM: setnvalue(ra, luai_numunm(L, nb)); break;
default: lua_assert(0); break; default: lua_assert(0); break;
} }
} }
@ -362,7 +362,7 @@ static void Arith (lua_State *L, StkId ra, const TValue *rb,
TValue *rc = RKC(i); \ TValue *rc = RKC(i); \
if (ttisnumber(rb) && ttisnumber(rc)) { \ if (ttisnumber(rb) && ttisnumber(rc)) { \
lua_Number nb = nvalue(rb), nc = nvalue(rc); \ lua_Number nb = nvalue(rb), nc = nvalue(rc); \
setnvalue(ra, op(nb, nc)); \ setnvalue(ra, op(L, nb, nc)); \
} \ } \
else \ else \
Protect(Arith(L, ra, rb, rc, tm)); \ Protect(Arith(L, ra, rb, rc, tm)); \
@ -498,7 +498,7 @@ void luaV_execute (lua_State *L, int nexeccalls) {
TValue *rb = RB(i); TValue *rb = RB(i);
if (ttisnumber(rb)) { if (ttisnumber(rb)) {
lua_Number nb = nvalue(rb); lua_Number nb = nvalue(rb);
setnvalue(ra, luai_numunm(nb)); setnvalue(ra, luai_numunm(L, nb));
} }
else { else {
Protect(Arith(L, ra, rb, rb, TM_UNM)); Protect(Arith(L, ra, rb, rb, TM_UNM));
@ -652,10 +652,10 @@ void luaV_execute (lua_State *L, int nexeccalls) {
} }
case OP_FORLOOP: { case OP_FORLOOP: {
lua_Number step = nvalue(ra+2); lua_Number step = nvalue(ra+2);
lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */ lua_Number idx = luai_numadd(L, nvalue(ra), step); /* increment index */
lua_Number limit = nvalue(ra+1); lua_Number limit = nvalue(ra+1);
if (luai_numlt(0, step) ? luai_numle(idx, limit) if (luai_numlt(L, 0, step) ? luai_numle(L, idx, limit)
: luai_numle(limit, idx)) { : luai_numle(L, limit, idx)) {
dojump(L, pc, GETARG_sBx(i)); /* jump back */ dojump(L, pc, GETARG_sBx(i)); /* jump back */
setnvalue(ra, idx); /* update internal index... */ setnvalue(ra, idx); /* update internal index... */
setnvalue(ra+3, idx); /* ...and external index */ setnvalue(ra+3, idx); /* ...and external index */
@ -673,7 +673,7 @@ void luaV_execute (lua_State *L, int nexeccalls) {
luaG_runerror(L, LUA_QL("for") " limit must be a number"); luaG_runerror(L, LUA_QL("for") " limit must be a number");
else if (!tonumber(pstep, ra+2)) else if (!tonumber(pstep, ra+2))
luaG_runerror(L, LUA_QL("for") " step must be a number"); luaG_runerror(L, LUA_QL("for") " step must be a number");
setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep))); setnvalue(ra, luai_numsub(L, nvalue(ra), nvalue(pstep)));
dojump(L, pc, GETARG_sBx(i)); dojump(L, pc, GETARG_sBx(i));
continue; continue;
} }