@@ -997,6 +997,7 @@ class QAST::OperationsJS {
997
997
" \}\n " ], : node($ node ));
998
998
});
999
999
1000
+
1000
1001
for <if unless > -> $ op_name {
1001
1002
add_op($ op_name , sub ($ comp , $ node , : $ want ) {
1002
1003
unless nqp :: defined ($ want ) {
@@ -1007,8 +1008,17 @@ class QAST::OperationsJS {
1007
1008
nqp ::die(" Operation '" ~ $ node . op~ " ' needs either 2 or 3 operands" )
1008
1009
if $ operands < 2 || $operands > 3 ;
1009
1010
1011
+ my sub needs_cond_passed ($ n ) {
1012
+ nqp ::istype($ n , QAST ::Block) && $ n . arity > 0 &&
1013
+ ($ n . blocktype eq ' immediate' || $ n . blocktype eq ' immediate_static' )
1014
+ }
1015
+
1016
+ my $ cond_type := (needs_cond_passed($ node [1 ]) || needs_cond_passed($ node [2 ]))
1017
+ ?? $ T_OBJ
1018
+ !! (($ operands == 3 || $ want == $ T_VOID ) ?? $ T_BOOL !! $ want );
1019
+
1010
1020
# The 2 operand form of if in a non-void context also uses the cond as the return value
1011
- my $ cond := $ comp . as_js($ node [0 ], : want( ( $ operands == 3 || $ want == $ T_VOID ) ?? $ T_BOOL !! $ want ));
1021
+ my $ cond := $ comp . as_js($ node [0 ], : want($ cond_type ));
1012
1022
my $ then ;
1013
1023
my $ else ;
1014
1024
@@ -1033,21 +1043,32 @@ class QAST::OperationsJS {
1033
1043
1034
1044
my $ cond_without_sideeffects := Chunk. new ($ cond . type, $ cond . expr, []);
1035
1045
1046
+
1047
+ my sub compile_block ($ node ) {
1048
+ if needs_cond_passed($ node ) {
1049
+ my $ block := try $ * BLOCK ;
1050
+ my $ loop := try $ * LOOP ;
1051
+ $ comp . compile_block($ node , $ block , $ loop , : $ want , : extra_args([$ cond_without_sideeffects ]));
1052
+ } else {
1053
+ $ comp . as_js($ node , : $ want );
1054
+ }
1055
+ }
1056
+
1036
1057
if $ node . op eq ' if' {
1037
- $ then := $ comp . as_js ($ node [1 ], : $ want );
1058
+ $ then := compile_block ($ node [1 ]);
1038
1059
1039
1060
if $ operands == 3 {
1040
- $ else := $ comp . as_js ($ node [2 ], : $ want );
1061
+ $ else := compile_block ($ node [2 ]);
1041
1062
} else {
1042
1063
$ else := $ comp . coerce($ cond_without_sideeffects , $ want );
1043
1064
}
1044
1065
} else {
1045
1066
if $ operands == 3 {
1046
- $ then := $ comp . as_js ($ node [2 ], : $ want );
1067
+ $ then := compile_block ($ node [2 ]);
1047
1068
} else {
1048
1069
$ then := $ comp . coerce($ cond_without_sideeffects , $ want );
1049
1070
}
1050
- $ else := $ comp . as_js ($ node [1 ], : $ want );
1071
+ $ else := compile_block ($ node [1 ]);
1051
1072
}
1052
1073
1053
1074
0 commit comments