shithub: libmujs

Download patch

ref: dceb42044791bacd56bcf5017c865ff13bb68d7c
parent: 7f3673fee2f872d78c3ffad3c50ee7196f2a5862
author: Tor Andersson <[email protected]>
date: Sun Jan 12 09:18:51 EST 2014

Use emitname() for identifiers, and emitstring() for string literals.

--- a/jscompile.c
+++ b/jscompile.c
@@ -68,6 +68,15 @@
 	emit(J, F, addconst(J, F, v));
 }
 
+static void emitname(JF, int opcode, const char *s)
+{
+	js_Value v;
+	v.type = JS_TSTRING;
+	v.u.string = s;
+	emit(J, F, opcode);
+	emit(J, F, addconst(J, F, v));
+}
+
 static int jump(JF, int opcode)
 {
 	int addr = F->len + 1;
@@ -114,6 +123,8 @@
 			js_Ast *prop = kv->a;
 			cexp(J, F, kv->b);
 			if (prop->type == AST_IDENTIFIER || prop->type == AST_STRING)
+				emitname(J, F, OP_OBJECTPUT, prop->string);
+			else if (prop->type == AST_STRING)
 				emitstring(J, F, OP_OBJECTPUT, prop->string);
 			else if (prop->type == AST_NUMBER)
 				emitnumber(J, F, OP_OBJECTPUT, prop->number);
@@ -140,7 +151,7 @@
 {
 	switch (exp->type) {
 	case AST_IDENTIFIER:
-		emitstring(J, F, OP_AVAR, exp->string);
+		emitname(J, F, OP_AVAR, exp->string);
 		break;
 	case EXP_INDEX:
 		cexp(J, F, exp->a);
@@ -149,7 +160,7 @@
 		break;
 	case EXP_MEMBER:
 		cexp(J, F, exp->a);
-		emitstring(J, F, OP_AMEMBER, exp->b->string);
+		emitname(J, F, OP_AMEMBER, exp->b->string);
 		break;
 	default:
 		jsC_error(J, exp, "invalid l-value in assignment");
@@ -172,10 +183,7 @@
 	int n;
 
 	switch (exp->type) {
-	case AST_IDENTIFIER:
-		emitstring(J, F, OP_LOADVAR, exp->string);
-		break;
-
+	case AST_IDENTIFIER: emitname(J, F, OP_LOADVAR, exp->string); break;
 	case AST_NUMBER: emitnumber(J, F, OP_CONST, exp->number); break;
 	case AST_STRING: emitstring(J, F, OP_CONST, exp->string); break;
 	case EXP_UNDEF: emit(J, F, OP_UNDEF); break;
@@ -202,7 +210,7 @@
 
 	case EXP_MEMBER:
 		cexp(J, F, exp->a);
-		emitstring(J, F, OP_LOADMEMBER, exp->b->string);
+		emitname(J, F, OP_LOADMEMBER, exp->b->string);
 		break;
 
 	case EXP_CALL:
@@ -209,7 +217,7 @@
 		if (exp->a->type == EXP_MEMBER) {
 			cexp(J, F, exp->a->a);
 			emit(J, F, OP_DUP);
-			emitstring(J, F, OP_LOADMEMBER, exp->a->b->string);
+			emitname(J, F, OP_LOADMEMBER, exp->a->b->string);
 			n = cargs(J, F, exp->b);
 			emit(J, F, OP_TCALL);
 			emit(J, F, n);
@@ -337,7 +345,7 @@
 		cexp(J, F, vardec->b);
 	else
 		emit(J, F, OP_UNDEF);
-	emitstring(J, F, OP_VARDEC, vardec->a->string);
+	emitname(J, F, OP_VARDEC, vardec->a->string);
 }
 
 static void cvardeclist(JF, js_Ast *list)