shithub: libmujs

Download patch

ref: 7d698ae9230261310d869188737366f1d5939490
parent: 8ef83a3cc1efc47cd8e09c637f9cf6c57df559c1
author: Tor Andersson <[email protected]>
date: Tue Feb 11 09:57:56 EST 2014

Compile pre/post increment/decrement as special opcodes.

They always do addition, never concatenation as strings, so we can't
treat "++a" the same as "a += 1".

--- a/jscompile.c
+++ b/jscompile.c
@@ -500,22 +500,19 @@
 
 	case EXP_PREINC:
 		cassignop1(J, F, exp->a, 0);
-		emit(J, F, OP_NUMBER_1);
-		emit(J, F, OP_ADD);
+		emit(J, F, OP_INC);
 		cassignop2(J, F, exp->a);
 		break;
 
 	case EXP_PREDEC:
 		cassignop1(J, F, exp->a, 0);
-		emit(J, F, OP_NUMBER_1);
-		emit(J, F, OP_SUB);
+		emit(J, F, OP_DEC);
 		cassignop2(J, F, exp->a);
 		break;
 
 	case EXP_POSTINC:
 		cassignop1(J, F, exp->a, 1);
-		emit(J, F, OP_NUMBER_1);
-		emit(J, F, OP_ADD);
+		emit(J, F, OP_INC);
 		cassignop2(J, F, exp->a);
 		emit(J, F, OP_POP);
 		break;
@@ -522,8 +519,7 @@
 
 	case EXP_POSTDEC:
 		cassignop1(J, F, exp->a, 1);
-		emit(J, F, OP_NUMBER_1);
-		emit(J, F, OP_SUB);
+		emit(J, F, OP_DEC);
 		cassignop2(J, F, exp->a);
 		emit(J, F, OP_POP);
 		break;
--- a/jscompile.h
+++ b/jscompile.h
@@ -75,6 +75,8 @@
 	OP_NEG,
 	OP_BITNOT,
 	OP_LOGNOT,
+	OP_INC,
+	OP_DEC,
 
 	OP_MUL,
 	OP_DIV,
--- a/jsrun.c
+++ b/jsrun.c
@@ -1235,6 +1235,18 @@
 			js_pushboolean(J, !b);
 			break;
 
+		case OP_INC:
+			x = js_tonumber(J, -1);
+			js_pop(J, 1);
+			js_pushnumber(J, x + 1);
+			break;
+
+		case OP_DEC:
+			x = js_tonumber(J, -1);
+			js_pop(J, 1);
+			js_pushnumber(J, x - 1);
+			break;
+
 		/* Multiplicative operators */
 
 		case OP_MUL: