diff options
-rw-r--r-- | apps/plugins/lua/lcode.c | 13 | ||||
-rw-r--r-- | apps/plugins/lua/lvm.c | 28 |
2 files changed, 33 insertions, 8 deletions
diff --git a/apps/plugins/lua/lcode.c b/apps/plugins/lua/lcode.c index cff626b7fa..cc26ac7521 100644 --- a/apps/plugins/lua/lcode.c +++ b/apps/plugins/lua/lcode.c | |||
@@ -642,26 +642,27 @@ static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { | |||
642 | case OP_SUB: r = luai_numsub(v1, v2); break; | 642 | case OP_SUB: r = luai_numsub(v1, v2); break; |
643 | case OP_MUL: r = luai_nummul(v1, v2); break; | 643 | case OP_MUL: r = luai_nummul(v1, v2); break; |
644 | case OP_DIV: | 644 | case OP_DIV: |
645 | if (v2 == 0) return 0; /* do not attempt to divide by 0 */ | 645 | if (v2 == 0) return -1; /* do not attempt to divide by 0 */ |
646 | r = luai_numdiv(v1, v2); break; | 646 | r = luai_numdiv(v1, v2); break; |
647 | case OP_MOD: | 647 | case OP_MOD: |
648 | if (v2 == 0) return 0; /* do not attempt to divide by 0 */ | 648 | if (v2 == 0) return -1; /* do not attempt to divide by 0 */ |
649 | r = luai_nummod(v1, v2); break; | 649 | r = luai_nummod(v1, v2); break; |
650 | case OP_POW: r = luai_numpow(v1, v2); break; | 650 | case OP_POW: r = luai_numpow(v1, v2); break; |
651 | case OP_UNM: r = luai_numunm(v1); break; | 651 | case OP_UNM: r = luai_numunm(v1); break; |
652 | case OP_LEN: return 0; /* no constant folding for 'len' */ | 652 | case OP_LEN: return 0; /* no constant folding for 'len' */ |
653 | default: lua_assert(0); r = 0; break; | 653 | default: lua_assert(0); r = 0; break; |
654 | } | 654 | } |
655 | if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */ | 655 | if (luai_numisnan(r)) return -2; /* do not attempt to produce NaN */ |
656 | e1->u.nval = r; | 656 | e1->u.nval = r; |
657 | return 1; | 657 | return 1; |
658 | } | 658 | } |
659 | 659 | ||
660 | 660 | ||
661 | static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { | 661 | static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { |
662 | if (constfolding(op, e1, e2)) | 662 | int resf = constfolding(op, e1, e2); |
663 | if (resf > 0) | ||
663 | return; | 664 | return; |
664 | else { | 665 | else if (resf == 0) { |
665 | int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0; | 666 | int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0; |
666 | int o1 = luaK_exp2RK(fs, e1); | 667 | int o1 = luaK_exp2RK(fs, e1); |
667 | if (o1 > o2) { | 668 | if (o1 > o2) { |
@@ -675,6 +676,8 @@ static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { | |||
675 | e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2); | 676 | e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2); |
676 | e1->k = VRELOCABLE; | 677 | e1->k = VRELOCABLE; |
677 | } | 678 | } |
679 | else | ||
680 | luaX_syntaxerror(fs->ls, "Inf or NaN"); | ||
678 | } | 681 | } |
679 | 682 | ||
680 | 683 | ||
diff --git a/apps/plugins/lua/lvm.c b/apps/plugins/lua/lvm.c index ee3256ab94..acce53a858 100644 --- a/apps/plugins/lua/lvm.c +++ b/apps/plugins/lua/lvm.c | |||
@@ -480,13 +480,35 @@ void luaV_execute (lua_State *L, int nexeccalls) { | |||
480 | continue; | 480 | continue; |
481 | } | 481 | } |
482 | case OP_DIV: { | 482 | case OP_DIV: { |
483 | arith_op(luai_numdiv, TM_DIV); | 483 | TValue *rb = RKB(i); |
484 | TValue *rc = RKC(i); | ||
485 | if (ttisnumber(rb) && ttisnumber(rc)) { | ||
486 | lua_Number nb = nvalue(rb), nc = nvalue(rc); | ||
487 | if (nc == 0) | ||
488 | luaG_typeerror(L, rc, "divide by zero"); | ||
489 | |||
490 | setnvalue(ra, luai_numdiv(nb, nc)); | ||
491 | } | ||
492 | else | ||
493 | Protect(Arith(L, ra, rb, rc, TM_DIV)); | ||
494 | |||
484 | continue; | 495 | continue; |
485 | } | 496 | } |
486 | case OP_MOD: { | 497 | case OP_MOD: { |
487 | arith_op(luai_nummod, TM_MOD); | 498 | TValue *rb = RKB(i); |
499 | TValue *rc = RKC(i); | ||
500 | if (ttisnumber(rb) && ttisnumber(rc)) { | ||
501 | lua_Number nb = nvalue(rb), nc = nvalue(rc); | ||
502 | if (nc == 0) | ||
503 | luaG_typeerror(L, rc, "perform 'n%0'"); | ||
504 | |||
505 | setnvalue(ra, luai_nummod(nb, nc)); | ||
506 | } | ||
507 | else | ||
508 | Protect(Arith(L, ra, rb, rc, TM_MOD)); | ||
509 | |||
488 | continue; | 510 | continue; |
489 | } | 511 | } |
490 | case OP_POW: { | 512 | case OP_POW: { |
491 | arith_op(luai_numpow, TM_POW); | 513 | arith_op(luai_numpow, TM_POW); |
492 | continue; | 514 | continue; |