From f96497397addca22f22a6ba6eeabc906be43f16b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 29 Jun 2017 12:06:44 -0300 Subject: new type 'StackValue' for stack elements (we may want to put extra info there in the future) --- lobject.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) (limited to 'lobject.c') diff --git a/lobject.c b/lobject.c index 43938c9c..9ad833c9 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.114 2017/04/19 16:34:35 roberto Exp roberto $ +** $Id: lobject.c,v 2.115 2017/05/24 13:47:11 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -120,8 +120,8 @@ static lua_Number numarith (lua_State *L, int op, lua_Number v1, } -void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, - TValue *res) { +int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2, + TValue *res) { switch (op) { case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR: @@ -129,33 +129,40 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, lua_Integer i1; lua_Integer i2; if (tointeger(p1, &i1) && tointeger(p2, &i2)) { setivalue(res, intarith(L, op, i1, i2)); - return; + return 1; } - else break; /* go to the end */ + else return 0; /* fail */ } case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */ lua_Number n1; lua_Number n2; if (tonumber(p1, &n1) && tonumber(p2, &n2)) { setfltvalue(res, numarith(L, op, n1, n2)); - return; + return 1; } - else break; /* go to the end */ + else return 0; /* fail */ } default: { /* other operations */ lua_Number n1; lua_Number n2; if (ttisinteger(p1) && ttisinteger(p2)) { setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); - return; + return 1; } else if (tonumber(p1, &n1) && tonumber(p2, &n2)) { setfltvalue(res, numarith(L, op, n1, n2)); - return; + return 1; } - else break; /* go to the end */ + else return 0; /* fail */ } } - /* could not perform raw operation; try metamethod */ - luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); +} + + +void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, + StkId res) { + if (!luaO_rawarith(L, op, p1, p2, s2v(res))) { + /* could not perform raw operation; try metamethod */ + luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); + } } @@ -367,7 +374,7 @@ int luaO_utf8esc (char *buff, unsigned long x) { /* ** Convert a number object to a string */ -void luaO_tostring (lua_State *L, StkId obj) { +void luaO_tostring (lua_State *L, TValue *obj) { char buff[MAXNUMBER2STR]; size_t len; lua_assert(ttisnumber(obj)); @@ -382,7 +389,7 @@ void luaO_tostring (lua_State *L, StkId obj) { } #endif } - setsvalue2s(L, obj, luaS_newlstr(L, buff, len)); + setsvalue(L, obj, luaS_newlstr(L, buff, len)); } @@ -418,18 +425,18 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { break; } case 'd': { /* an 'int' */ - setivalue(L->top, va_arg(argp, int)); + setivalue(s2v(L->top), va_arg(argp, int)); goto top2str; } case 'I': { /* a 'lua_Integer' */ - setivalue(L->top, cast(lua_Integer, va_arg(argp, l_uacInt))); + setivalue(s2v(L->top), cast(lua_Integer, va_arg(argp, l_uacInt))); goto top2str; } case 'f': { /* a 'lua_Number' */ - setfltvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); + setfltvalue(s2v(L->top), cast_num(va_arg(argp, l_uacNumber))); top2str: /* convert the top element to a string */ luaD_inctop(L); - luaO_tostring(L, L->top - 1); + luaO_tostring(L, s2v(L->top - 1)); break; } case 'p': { /* a pointer */ @@ -460,7 +467,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { luaD_checkstack(L, 1); pushstr(L, fmt, strlen(fmt)); if (n > 0) luaV_concat(L, n + 1); - return svalue(L->top - 1); + return svalue(s2v(L->top - 1)); } -- cgit v1.2.1