summaryrefslogtreecommitdiff
path: root/src/lvm.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lvm.c')
-rw-r--r--src/lvm.c75
1 files changed, 39 insertions, 36 deletions
diff --git a/src/lvm.c b/src/lvm.c
index 1a295d1b..1fd2ae75 100644
--- a/src/lvm.c
+++ b/src/lvm.c
@@ -1,5 +1,5 @@
/*
-** $Id: lvm.c,v 2.18 2004/12/03 20:35:33 roberto Exp $
+** $Id: lvm.c,v 2.26 2005/02/23 17:30:22 roberto Exp $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@@ -47,7 +47,7 @@ int luaV_tostring (lua_State *L, StkId obj) {
if (!ttisnumber(obj))
return 0;
else {
- char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
+ char s[MAX_NUMBER2STR];
lua_number2str(s, nvalue(obj));
setsvalue2s(L, obj, luaS_new(L, s));
return 1;
@@ -113,7 +113,7 @@ StkId luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val,
const TValue *tm;
if (ttistable(t)) { /* `t' is a table? */
Table *h = hvalue(t);
- const TValue *res = luaH_get(h, key); /* do a primitive set */
+ const TValue *res = luaH_get(h, key); /* do a primitive get */
if (!ttisnil(res) || /* result is no nil? */
(tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
setobj2s(L, val, res);
@@ -241,7 +241,7 @@ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
if (ttype(l) != ttype(r))
return luaG_ordererror(L, l, r);
else if (ttisnumber(l))
- return nvalue(l) < nvalue(r);
+ return num_lt(nvalue(l), nvalue(r));
else if (ttisstring(l))
return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
@@ -255,7 +255,7 @@ static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
if (ttype(l) != ttype(r))
return luaG_ordererror(L, l, r);
else if (ttisnumber(l))
- return nvalue(l) <= nvalue(r);
+ return num_le(nvalue(l), nvalue(r));
else if (ttisstring(l))
return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
@@ -271,7 +271,7 @@ int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
lua_assert(ttype(t1) == ttype(t2));
switch (ttype(t1)) {
case LUA_TNIL: return 1;
- case LUA_TNUMBER: return nvalue(t1) == nvalue(t2);
+ case LUA_TNUMBER: return num_eq(nvalue(t1), nvalue(t2));
case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
case LUA_TUSERDATA: {
@@ -335,18 +335,11 @@ static StkId Arith (lua_State *L, StkId ra, const TValue *rb,
if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
(c = luaV_tonumber(rc, &tempc)) != NULL) {
switch (op) {
- case TM_ADD: setnvalue(ra, nvalue(b) + nvalue(c)); break;
- case TM_SUB: setnvalue(ra, nvalue(b) - nvalue(c)); break;
- case TM_MUL: setnvalue(ra, nvalue(b) * nvalue(c)); break;
- case TM_DIV: setnvalue(ra, nvalue(b) / nvalue(c)); break;
- case TM_POW: {
- const TValue *f = luaH_getstr(hvalue(gt(L)), G(L)->tmname[TM_POW]);
- if (!ttisfunction(f))
- luaG_runerror(L, "`__pow' (`^' operator) is not a function");
- prepTMcall(L, f, b, c);
- callTMres(L, ra);
- break;
- }
+ case TM_ADD: setnvalue(ra, num_add(nvalue(b), nvalue(c))); break;
+ case TM_SUB: setnvalue(ra, num_sub(nvalue(b), nvalue(c))); break;
+ case TM_MUL: setnvalue(ra, num_mul(nvalue(b), nvalue(c))); break;
+ case TM_DIV: setnvalue(ra, num_div(nvalue(b), nvalue(c))); break;
+ case TM_POW: setnvalue(ra, num_pow(nvalue(b), nvalue(c))); break;
default: lua_assert(0); break;
}
}
@@ -435,9 +428,11 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
continue;
}
case OP_GETGLOBAL: {
+ TValue g;
TValue *rb = KBx(i);
- lua_assert(ttisstring(rb) && ttistable(&cl->g));
- base = luaV_gettable(L, &cl->g, rb, ra, pc); /***/
+ sethvalue(L, &g, cl->env);
+ lua_assert(ttisstring(rb));
+ base = luaV_gettable(L, &g, rb, ra, pc); /***/
continue;
}
case OP_GETTABLE: {
@@ -445,8 +440,10 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
continue;
}
case OP_SETGLOBAL: {
- lua_assert(ttisstring(KBx(i)) && ttistable(&cl->g));
- base = luaV_settable(L, &cl->g, KBx(i), ra, pc); /***/
+ TValue g;
+ sethvalue(L, &g, cl->env);
+ lua_assert(ttisstring(KBx(i)));
+ base = luaV_settable(L, &g, KBx(i), ra, pc); /***/
continue;
}
case OP_SETUPVAL: {
@@ -461,8 +458,8 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
}
case OP_NEWTABLE: {
int b = GETARG_B(i);
- b = luaO_fb2int(b);
- sethvalue(L, ra, luaH_new(L, b, GETARG_C(i)));
+ int c = GETARG_C(i);
+ sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
L->ci->savedpc = pc;
luaC_checkGC(L); /***/
base = L->base;
@@ -478,7 +475,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
TValue *rb = RKB(i);
TValue *rc = RKC(i);
if (ttisnumber(rb) && ttisnumber(rc)) {
- setnvalue(ra, nvalue(rb) + nvalue(rc));
+ setnvalue(ra, num_add(nvalue(rb), nvalue(rc)));
}
else
base = Arith(L, ra, rb, rc, TM_ADD, pc); /***/
@@ -488,7 +485,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
TValue *rb = RKB(i);
TValue *rc = RKC(i);
if (ttisnumber(rb) && ttisnumber(rc)) {
- setnvalue(ra, nvalue(rb) - nvalue(rc));
+ setnvalue(ra, num_sub(nvalue(rb), nvalue(rc)));
}
else
base = Arith(L, ra, rb, rc, TM_SUB, pc); /***/
@@ -498,7 +495,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
TValue *rb = RKB(i);
TValue *rc = RKC(i);
if (ttisnumber(rb) && ttisnumber(rc)) {
- setnvalue(ra, nvalue(rb) * nvalue(rc));
+ setnvalue(ra, num_mul(nvalue(rb), nvalue(rc)));
}
else
base = Arith(L, ra, rb, rc, TM_MUL, pc); /***/
@@ -508,21 +505,27 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
TValue *rb = RKB(i);
TValue *rc = RKC(i);
if (ttisnumber(rb) && ttisnumber(rc)) {
- setnvalue(ra, nvalue(rb) / nvalue(rc));
+ setnvalue(ra, num_div(nvalue(rb), nvalue(rc)));
}
else
base = Arith(L, ra, rb, rc, TM_DIV, pc); /***/
continue;
}
case OP_POW: {
- base = Arith(L, ra, RKB(i), RKC(i), TM_POW, pc); /***/
+ TValue *rb = RKB(i);
+ TValue *rc = RKC(i);
+ if (ttisnumber(rb) && ttisnumber(rc)) {
+ setnvalue(ra, num_pow(nvalue(rb), nvalue(rc)));
+ }
+ else
+ base = Arith(L, ra, rb, rc, TM_POW, pc); /***/
continue;
}
case OP_UNM: {
const TValue *rb = RB(i);
TValue temp;
if (tonumber(rb, &temp)) {
- setnvalue(ra, -nvalue(rb));
+ setnvalue(ra, num_unm(nvalue(rb)));
}
else {
setnilvalue(&temp);
@@ -617,9 +620,9 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
int aux;
StkId func = ci->func;
StkId pfunc = (ci+1)->func; /* previous function index */
+ if (L->openupval) luaF_close(L, base);
base = ci->base = ci->func + ((ci+1)->base - pfunc);
L->base = base;
- if (L->openupval) luaF_close(L, base);
for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */
setobjs2s(L, func+aux, pfunc+aux);
ci->top = L->top = func+aux; /* correct top */
@@ -658,9 +661,9 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
}
case OP_FORLOOP: {
lua_Number step = nvalue(ra+2);
- lua_Number idx = nvalue(ra) + step; /* increment index */
+ lua_Number idx = num_add(nvalue(ra), step); /* increment index */
lua_Number limit = nvalue(ra+1);
- if (step > 0 ? idx <= limit : idx >= limit) {
+ if (step > 0 ? num_le(idx, limit) : num_le(limit, idx)) {
dojump(L, pc, GETARG_sBx(i)); /* jump back */
setnvalue(ra, idx); /* update internal index... */
setnvalue(ra+3, idx); /* ...and external index */
@@ -678,7 +681,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
luaG_runerror(L, "`for' limit must be a number");
else if (!tonumber(pstep, ra+2))
luaG_runerror(L, "`for' step must be a number");
- setnvalue(ra, nvalue(ra) - nvalue(pstep));
+ setnvalue(ra, num_sub(nvalue(ra), nvalue(pstep)));
dojump(L, pc, GETARG_sBx(i));
continue;
}
@@ -723,7 +726,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
if (c == 0) c = cast(int, *pc++);
last = ((c-1)*LFIELDS_PER_FLUSH) + n + LUA_FIRSTINDEX - 1;
if (last > h->sizearray) /* needs more space? */
- luaH_resize(L, h, last, h->lsizenode); /* pre-alloc it at once */
+ luaH_resizearray(L, h, last); /* pre-alloc it at once */
for (; n > 0; n--) {
TValue *val = ra+n;
setobj2t(L, luaH_setnum(L, h, last--), val);
@@ -741,7 +744,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
int nup, j;
p = cl->p->p[GETARG_Bx(i)];
nup = p->nups;
- ncl = luaF_newLclosure(L, nup, &cl->g);
+ ncl = luaF_newLclosure(L, nup, cl->env);
ncl->l.p = p;
for (j=0; j<nup; j++, pc++) {
if (GET_OPCODE(*pc) == OP_GETUPVAL)