Skip to content

Commit 2b209d3

Browse files
committed
Omit needless setjmp guard over catch block in try/catch statement.
Make the catch opcodes only set the scope chain, and add explicit try/endtry opcodes for the catch block in try/catch/finally statements.
1 parent 4057217 commit 2b209d3

4 files changed

Lines changed: 24 additions & 33 deletions

File tree

jscompile.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -666,8 +666,14 @@ static void cexit(JF, js_AstType T, js_Ast *node, js_Ast *target)
666666
}
667667
/* came from catch block */
668668
if (prev == node->c) {
669-
emit(J, F, OP_ENDCATCH);
670-
if (node->d) cstm(J, F, node->d); /* finally */
669+
/* ... with finally */
670+
if (node->d) {
671+
emit(J, F, OP_ENDCATCH);
672+
emit(J, F, OP_ENDTRY);
673+
cstm(J, F, node->d); /* finally */
674+
} else {
675+
emit(J, F, OP_ENDCATCH);
676+
}
671677
}
672678
break;
673679
}
@@ -693,25 +699,19 @@ static void ctryfinally(JF, js_Ast *trystm, js_Ast *finallystm)
693699

694700
static void ctrycatch(JF, js_Ast *trystm, js_Ast *catchvar, js_Ast *catchstm)
695701
{
696-
int L1, L2, L3;
702+
int L1, L2;
697703
L1 = jump(J, F, OP_TRY);
698704
{
699705
/* if we get here, we have caught an exception in the try block */
700-
L2 = jump(J, F, OP_CATCH);
701-
emitraw(J, F, addstring(J, F, catchvar->string));
702-
{
703-
/* if we get here, we have caught an exception in the catch block */
704-
emit(J, F, OP_THROW); /* rethrow exception */
705-
}
706-
label(J, F, L2);
706+
emitstring(J, F, OP_CATCH, catchvar->string);
707707
cstm(J, F, catchstm);
708708
emit(J, F, OP_ENDCATCH);
709-
L3 = jump(J, F, OP_JUMP); /* skip past the try block */
709+
L2 = jump(J, F, OP_JUMP); /* skip past the try block */
710710
}
711711
label(J, F, L1);
712712
cstm(J, F, trystm);
713713
emit(J, F, OP_ENDTRY);
714-
label(J, F, L3);
714+
label(J, F, L2);
715715
}
716716

717717
static void ctrycatchfinally(JF, js_Ast *trystm, js_Ast *catchvar, js_Ast *catchstm, js_Ast *finallystm)
@@ -720,14 +720,14 @@ static void ctrycatchfinally(JF, js_Ast *trystm, js_Ast *catchvar, js_Ast *catch
720720
L1 = jump(J, F, OP_TRY);
721721
{
722722
/* if we get here, we have caught an exception in the try block */
723-
L2 = jump(J, F, OP_CATCH);
724-
emitraw(J, F, addstring(J, F, catchvar->string));
723+
L2 = jump(J, F, OP_TRY);
725724
{
726725
/* if we get here, we have caught an exception in the catch block */
727726
cstm(J, F, finallystm); /* inline finally block */
728727
emit(J, F, OP_THROW); /* rethrow exception */
729728
}
730729
label(J, F, L2);
730+
emitstring(J, F, OP_CATCH, catchvar->string);
731731
cstm(J, F, catchstm);
732732
emit(J, F, OP_ENDCATCH);
733733
L3 = jump(J, F, OP_JUMP); /* skip past the try block to the finally block */

jscompile.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ enum js_OpCode
8989
OP_TRY, /* -ADDR- /jump/ or -ADDR- <exception> */
9090
OP_ENDTRY,
9191

92-
OP_CATCH, /* -ADDR,S- /jump/ or -ADDR,S- <exception> */
92+
OP_CATCH, /* push scope chain with exception variable */
9393
OP_ENDCATCH,
94+
9495
OP_WITH,
9596
OP_ENDWITH,
9697

jsdump.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ void jsC_dumpfunction(js_State *J, js_Function *F)
651651
case OP_GETPROPS:
652652
case OP_SETPROPS:
653653
case OP_DELPROPS:
654+
case OP_CATCH:
654655
pc(' ');
655656
ps(F->strtab[*p++]);
656657
break;
@@ -664,11 +665,6 @@ void jsC_dumpfunction(js_State *J, js_Function *F)
664665
case OP_TRY:
665666
printf(" %d", *p++);
666667
break;
667-
case OP_CATCH:
668-
printf(" %d", *p++);
669-
pc(' ');
670-
ps(F->strtab[*p++]);
671-
break;
672668
}
673669

674670
nl();
@@ -713,6 +709,7 @@ void js_dumpvalue(js_State *J, js_Value v)
713709
case JS_CNUMBER: printf("[Number %g]", v.u.object->u.number); break;
714710
case JS_CSTRING: printf("[String'%s']", v.u.object->u.string); break;
715711
case JS_CERROR: printf("[Error %s]", v.u.object->u.string); break;
712+
case JS_CITERATOR: printf("[Iterator %p]", v.u.object); break;
716713
default: printf("[Object %p]", v.u.object); break;
717714
}
718715
break;

jsrun.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -822,23 +822,16 @@ static void jsR_run(js_State *J, js_Function *F)
822822
break;
823823

824824
case OP_CATCH:
825-
offset = *pc++;
826825
str = ST[*pc++];
827-
if (js_trypc(J, pc)) {
828-
pc = J->trybuf[J->trylen].pc;
829-
} else {
830-
obj = jsV_newobject(J, JS_COBJECT, NULL);
831-
js_pushobject(J, obj);
832-
js_rot2(J);
833-
js_setproperty(J, -2, str);
834-
J->E = jsR_newenvironment(J, obj, J->E);
835-
js_pop(J, 1);
836-
pc = pcstart + offset;
837-
}
826+
obj = jsV_newobject(J, JS_COBJECT, NULL);
827+
js_pushobject(J, obj);
828+
js_rot2(J);
829+
js_setproperty(J, -2, str);
830+
J->E = jsR_newenvironment(J, obj, J->E);
831+
js_pop(J, 1);
838832
break;
839833

840834
case OP_ENDCATCH:
841-
js_endtry(J);
842835
J->E = J->E->outer;
843836
break;
844837

0 commit comments

Comments
 (0)