** Try to "constant-fold" an operation; return 1 iff successful */
| 764 | ** Try to "constant-fold" an operation; return 1 iff successful |
| 765 | */ |
| 766 | static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) { |
| 767 | TValue v1, v2, res; |
| 768 | if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2)) |
| 769 | return 0; /* non-numeric operands or not safe to fold */ |
| 770 | luaO_arith(fs->ls->L, op, &v1, &v2, &res); /* does operation */ |
| 771 | if (ttisinteger(&res)) { |
| 772 | e1->k = VKINT; |
| 773 | e1->u.ival = ivalue(&res); |
| 774 | } |
| 775 | else { /* folds neither NaN nor 0.0 (to avoid collapsing with -0.0) */ |
| 776 | lua_Number n = fltvalue(&res); |
| 777 | if (luai_numisnan(n) || n == 0) |
| 778 | return 0; |
| 779 | e1->k = VKFLT; |
| 780 | e1->u.nval = n; |
| 781 | } |
| 782 | return 1; |
| 783 | } |
| 784 | |
| 785 | |
| 786 | /* |
no test coverage detected