Skip to content

Commit

Permalink
#225 #172 fix type assertion inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyendumajumdar committed May 22, 2021
1 parent aace2f4 commit d31d447
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 32 deletions.
4 changes: 3 additions & 1 deletion ravicomp/src/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,10 @@ static const char Lua_header[] =
" (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))\n"
"#define tointeger(o,i) \\\n"
" (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I))\n"
"#define tointegerns(o, i) (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointegerns(o, i, LUA_FLOORN2I))\n"
"extern int luaV_tonumber_(const TValue *obj, lua_Number *n);\n"
"extern int luaV_tointeger(const TValue *obj, lua_Integer *p, int mode);\n"
"extern int luaV_tointegerns(const TValue *obj, lua_Integer *p, int mode);\n"
#ifdef RAVI_DEFER_STATEMENT
"extern int luaF_close (lua_State *L, StkId level, int status);\n"
#else
Expand Down Expand Up @@ -2198,7 +2200,7 @@ static int emit_op_movfi(struct function *fn, Instruction *insn)
emit_reg_accessor(fn, operand, 0);
raviX_buffer_add_string(&fn->body, ";\n");
raviX_buffer_add_string(&fn->body, " lua_Integer i = 0;\n");
raviX_buffer_add_string(&fn->body, " if (!tointeger(rb, &i)) {\n");
raviX_buffer_add_string(&fn->body, " if (!tointegerns(rb, &i)) {\n");
raviX_buffer_add_fstring(&fn->body, " error_code = %d;\n", Error_integer_expected);
raviX_buffer_add_string(&fn->body, " goto Lraise_error;\n");
raviX_buffer_add_string(&fn->body, " }\n");
Expand Down
56 changes: 28 additions & 28 deletions src/lvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
} \
else { \
lua_Number d = 0.0; \
if (luaV_tonumber_(val, &d)) { \
if (tonumberns(val, d)) { \
raviH_set_float_inline(L, h, ivalue(key), d); \
} \
else \
Expand All @@ -418,7 +418,7 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
} \
else { \
lua_Integer i = 0; \
if (luaV_tointeger_(val, &i)) { \
if (tointegerns(val, &i)) { \
raviH_set_int_inline(L, h, ivalue(key), i); \
} \
else \
Expand Down Expand Up @@ -459,7 +459,7 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
} \
else { \
lua_Number d = 0.0; \
if (luaV_tonumber_(val, &d)) { \
if (tonumberns(val, d)) { \
raviH_set_float_inline(L, h, ivalue(key), d); \
} \
else \
Expand All @@ -473,7 +473,7 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
} \
else { \
lua_Integer i = 0; \
if (luaV_tointeger_(val, &i)) { \
if (tointegerns(val, &i)) { \
raviH_set_int_inline(L, h, ivalue(key), i); \
} \
else \
Expand Down Expand Up @@ -1998,7 +1998,7 @@ int luaV_execute (lua_State *L) {
}
else {
lua_Integer i = 0;
if (luaV_tointeger_(val, &i)) {
if (tointegerns(val, &i)) {
raviH_set_int_inline(L, h, u, i);
}
else
Expand All @@ -2014,7 +2014,7 @@ int luaV_execute (lua_State *L) {
}
else {
lua_Number d = 0.0;
if (luaV_tonumber_(val, &d)) {
if (tonumberns(val, d)) {
raviH_set_float_inline(L, h, u, d);
}
else
Expand Down Expand Up @@ -2316,7 +2316,7 @@ int luaV_execute (lua_State *L) {
}
else {
lua_Integer j;
if (tointeger(rc, &j)) { raviH_set_int_inline(L, t, idx, j); }
if (tointegerns(rc, &j)) { raviH_set_int_inline(L, t, idx, j); }
else
luaG_runerror(L, "integer expected");
}
Expand All @@ -2341,7 +2341,7 @@ int luaV_execute (lua_State *L) {
}
else {
lua_Number j;
if (tonumber(rc, &j)) { raviH_set_float_inline(L, t, idx, j); }
if (tonumberns(rc, j)) { raviH_set_float_inline(L, t, idx, j); }
else
luaG_runerror(L, "number expected");
}
Expand All @@ -2357,7 +2357,7 @@ int luaV_execute (lua_State *L) {
}
vmcase(OP_RAVI_SETUPVALI) {
lua_Integer ia;
if (tointeger(ra, &ia)) {
if (tointegerns(ra, &ia)) {
UpVal *uv = cl->upvals[GETARG_B(i)];
setivalue(uv->v, ia);
luaC_upvalbarrier(L, uv, ra);
Expand All @@ -2369,7 +2369,7 @@ int luaV_execute (lua_State *L) {
}
vmcase(OP_RAVI_SETUPVALF) {
lua_Number na;
if (tonumber(ra, &na)) {
if (tonumberns(ra, na)) {
UpVal *uv = cl->upvals[GETARG_B(i)];
setfltvalue(uv->v, na);
luaC_upvalbarrier(L, uv, ra);
Expand Down Expand Up @@ -2515,15 +2515,15 @@ int luaV_execute (lua_State *L) {
vmcase(OP_RAVI_MOVEI) {
TValue *rb = RB(i);
lua_Integer j;
if (RAVI_LIKELY(tointeger(rb, &j))) { setivalue(ra, j); }
if (RAVI_LIKELY(tointegerns(rb, &j))) { setivalue(ra, j); }
else
luaG_runerror(L, "MOVEI: integer expected");
vmbreak;
}
vmcase(OP_RAVI_MOVEF) {
TValue *rb = RB(i);
lua_Number j;
if (RAVI_LIKELY(tonumber(rb, &j))) { setfltvalue(ra, j); }
if (RAVI_LIKELY(tonumberns(rb, j))) { setfltvalue(ra, j); }
else
luaG_runerror(L, "MOVEF: number expected");
vmbreak;
Expand Down Expand Up @@ -2557,14 +2557,14 @@ int luaV_execute (lua_State *L) {
}
vmcase(OP_RAVI_TOINT) {
lua_Integer j;
if (RAVI_LIKELY(tointeger(ra, &j))) { setivalue(ra, j); }
if (RAVI_LIKELY(tointegerns(ra, &j))) { setivalue(ra, j); }
else
luaG_runerror(L, "TOINT: integer expected");
vmbreak;
}
vmcase(OP_RAVI_TOFLT) {
lua_Number j;
if (RAVI_LIKELY(tonumber(ra, &j))) { setfltvalue(ra, j); }
if (RAVI_LIKELY(tonumberns(ra, j))) { setfltvalue(ra, j); }
else
luaG_runerror(L, "TOFLT: number expected");
vmbreak;
Expand Down Expand Up @@ -2832,7 +2832,7 @@ void raviV_op_setlist(lua_State *L, CallInfo *ci, TValue *ra, int b, int c) {
}
else {
lua_Integer i = 0;
if (luaV_tointeger_(val, &i)) {
if (tointegerns(val, &i)) {
raviH_set_int_inline(L, h, u, i);
}
else
Expand All @@ -2848,7 +2848,7 @@ void raviV_op_setlist(lua_State *L, CallInfo *ci, TValue *ra, int b, int c) {
}
else {
lua_Number d = 0.0;
if (luaV_tonumber_(val, &d)) {
if (tonumberns(val, d)) {
raviH_set_float_inline(L, h, u, d);
}
else
Expand Down Expand Up @@ -2919,7 +2919,7 @@ void raviV_op_loadnil(CallInfo *ci, int a, int b) {

void raviV_op_setupvali(lua_State *L, LClosure *cl, TValue *ra, int b) {
lua_Integer ia;
if (tointeger(ra, &ia)) {
if (tointegerns(ra, &ia)) {
UpVal *uv = cl->upvals[b];
setivalue(uv->v, ia);
luaC_upvalbarrier(L, uv, ra);
Expand All @@ -2931,7 +2931,7 @@ void raviV_op_setupvali(lua_State *L, LClosure *cl, TValue *ra, int b) {

void raviV_op_setupvalf(lua_State *L, LClosure *cl, TValue *ra, int b) {
lua_Number na;
if (tonumber(ra, &na)) {
if (tonumberns(ra, na)) {
UpVal *uv = cl->upvals[b];
setfltvalue(uv->v, na);
luaC_upvalbarrier(L, uv, ra);
Expand Down Expand Up @@ -2981,7 +2981,7 @@ void raviV_op_add(lua_State *L, TValue *ra, TValue *rb, TValue *rc) {
lua_Integer ic = ivalue(rc);
setivalue(ra, intop(+, ib, ic));
}
else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
setfltvalue(ra, luai_numadd(L, nb, nc));
}
else {
Expand All @@ -2997,7 +2997,7 @@ void raviV_op_sub(lua_State *L, TValue *ra, TValue *rb, TValue *rc) {
lua_Integer ic = ivalue(rc);
setivalue(ra, intop(-, ib, ic));
}
else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
setfltvalue(ra, luai_numsub(L, nb, nc));
}
else {
Expand All @@ -3013,7 +3013,7 @@ void raviV_op_mul(lua_State *L, TValue *ra, TValue *rb, TValue *rc) {
lua_Integer ic = ivalue(rc);
setivalue(ra, intop(*, ib, ic));
}
else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
setfltvalue(ra, luai_nummul(L, nb, nc));
}
else {
Expand All @@ -3024,7 +3024,7 @@ void raviV_op_mul(lua_State *L, TValue *ra, TValue *rb, TValue *rc) {
void raviV_op_div(lua_State *L, TValue *ra, TValue *rb, TValue *rc) {
lua_Number nb;
lua_Number nc;
if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
setfltvalue(ra, luai_numdiv(L, nb, nc));
}
else {
Expand All @@ -3035,7 +3035,7 @@ void raviV_op_div(lua_State *L, TValue *ra, TValue *rb, TValue *rc) {
void raviV_op_shl(lua_State *L, TValue *ra, TValue *rb, TValue *rc) {
lua_Integer ib;
lua_Integer ic;
if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) {
setivalue(ra, luaV_shiftl(ib, ic));
}
else {
Expand All @@ -3046,7 +3046,7 @@ void raviV_op_shl(lua_State *L, TValue *ra, TValue *rb, TValue *rc) {
void raviV_op_shr(lua_State *L, TValue *ra, TValue *rb, TValue *rc) {
lua_Integer ib;
lua_Integer ic;
if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) {
setivalue(ra, luaV_shiftl(ib, -ic));
}
else {
Expand All @@ -3057,7 +3057,7 @@ void raviV_op_shr(lua_State *L, TValue *ra, TValue *rb, TValue *rc) {
void raviV_op_band(lua_State *L, TValue *ra, TValue *rb, TValue *rc) {
lua_Integer ib;
lua_Integer ic;
if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) {
setivalue(ra, intop(&, ib, ic));
}
else {
Expand All @@ -3068,7 +3068,7 @@ void raviV_op_band(lua_State *L, TValue *ra, TValue *rb, TValue *rc) {
void raviV_op_bor(lua_State *L, TValue *ra, TValue *rb, TValue *rc) {
lua_Integer ib;
lua_Integer ic;
if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) {
setivalue(ra, intop(|, ib, ic));
}
else {
Expand All @@ -3079,7 +3079,7 @@ void raviV_op_bor(lua_State *L, TValue *ra, TValue *rb, TValue *rc) {
void raviV_op_bxor(lua_State *L, TValue *ra, TValue *rb, TValue *rc) {
lua_Integer ib;
lua_Integer ic;
if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) {
setivalue(ra, intop (^, ib, ic));
}
else {
Expand All @@ -3089,7 +3089,7 @@ void raviV_op_bxor(lua_State *L, TValue *ra, TValue *rb, TValue *rc) {

void raviV_op_bnot(lua_State *L, TValue *ra, TValue *rb) {
lua_Integer ib;
if (tointeger(rb, &ib)) { setivalue(ra, intop (^, ~l_castS2U(0), ib)); }
if (tointegerns(rb, &ib)) { setivalue(ra, intop (^, ~l_castS2U(0), ib)); }
else {
luaT_trybinTM(L, rb, rb, ra, TM_BNOT);
}
Expand Down
8 changes: 5 additions & 3 deletions src/ravi_jitshared.c
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,10 @@ static const char Lua_header[] =
" (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))\n"
"#define tointeger(o,i) \\\n"
" (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I))\n"
"#define tointegerns(o, i) (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointegerns(o, i, LUA_FLOORN2I))\n"
"extern int luaV_tonumber_(const TValue *obj, lua_Number *n);\n"
"extern int luaV_tointeger(const TValue *obj, lua_Integer *p, int mode);\n"
"extern int luaV_tointegerns(const TValue *obj, lua_Integer *p, int mode);\n"
#ifdef RAVI_DEFER_STATEMENT
"extern int luaF_close (lua_State *L, StkId level, int status);\n"
#else
Expand Down Expand Up @@ -1566,7 +1568,7 @@ static void emit_op_toint(struct function *fn, int A, int pc) {
(void)pc;
emit_reg(fn, "ra", A);
membuff_add_string(&fn->body, "i = 0;\n");
membuff_add_string(&fn->body, "if (ttisinteger(ra)) {i = ivalue(ra); setivalue(ra, i); }\n");
membuff_add_string(&fn->body, "if (tointegerns(ra, &i)) { setivalue(ra, i); }\n");
membuff_add_string(&fn->body, "else {\n");
#if GOTO_ON_ERROR
membuff_add_fstring(&fn->body, " error_code = %d;\n", Error_integer_expected);
Expand Down Expand Up @@ -1636,7 +1638,7 @@ static void emit_op_totab(struct function *fn, int A, int pc) {
static void emit_op_toclosure(struct function *fn, int A, int pc) {
(void)pc;
emit_reg(fn, "ra", A);
membuff_add_string(&fn->body, "if (!ttisclosure(ra)) {\n");
membuff_add_string(&fn->body, "if (!ttisnil(ra) && !ttisclosure(ra)) {\n");
#if GOTO_ON_ERROR
membuff_add_fstring(&fn->body, " error_code = %d;\n", Error_closure_expected);
membuff_add_string(&fn->body, " goto Lraise_error;\n");
Expand All @@ -1649,7 +1651,7 @@ static void emit_op_toclosure(struct function *fn, int A, int pc) {
static void emit_op_tostring(struct function *fn, int A, int pc) {
(void)pc;
emit_reg(fn, "ra", A);
membuff_add_string(&fn->body, "if (!ttisstring(ra)) {\n");
membuff_add_string(&fn->body, "if (!ttisnil(ra) && !ttisstring(ra)) {\n");
#if GOTO_ON_ERROR
membuff_add_fstring(&fn->body, " error_code = %d;\n", Error_string_expected);
membuff_add_string(&fn->body, " goto Lraise_error;\n");
Expand Down
1 change: 1 addition & 0 deletions src/ravi_mirjit.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static LuaFunc Lua_functions[] = {
{ "raviV_raise_error_with_info", raviV_raise_error_with_info },
{ "luaV_tonumber_", luaV_tonumber_ },
{ "luaV_tointeger", luaV_tointeger },
{ "luaV_tointegerns", luaV_tointegerns },
{ "luaV_shiftl", luaV_shiftl },
{ "luaD_poscall", luaD_poscall },
{ "luaV_equalobj", luaV_equalobj },
Expand Down
57 changes: 57 additions & 0 deletions tests/language/ravi_tests1.ravi
Original file line number Diff line number Diff line change
Expand Up @@ -1879,6 +1879,63 @@ do
end
print 'Test 88 OK'

do
local data = { "a", 5.5, false }
for i = 1,4 do -- check nil too
local y = data[i]
local function x()
return @integer y
end
assert(not pcall(x))
compile(x)
assert(not pcall(x))
end
end
print 'Test 89 OK'

do
local y = 56.0
local function x()
return @integer y
end
local _,z = pcall(x)
assert(_)
assert(math.type(z) == 'integer')
compile(x)
_,z = pcall(x)
assert(_)
assert(math.type(z) == 'integer')
end
print 'Test 90 OK'

do
local data = { "a", 5.5, false }
for i = 1,4 do -- check nil too
local y = data[i]
local function x(y: integer)
return y
end
assert(not pcall(x))
compile(x)
assert(not pcall(x))
end
end
print 'Test 91 OK'

do
local function x(y: integer)
return y
end
local _,z = pcall(x, 56.0)
assert(_)
assert(math.type(z) == 'integer')
compile(x)
_,z = pcall(x, 56.0)
assert(_)
assert(math.type(z) == 'integer')
end
print 'Test 92 OK'

for k,v in pairs(opcodes_coverage)
do
print(k, v)
Expand Down

0 comments on commit d31d447

Please sign in to comment.