@ -379,15 +379,24 @@ module.exports = function(ast, options) {
thenLength = bc [ ip + baseLength - 2 ] ,
thenLength = bc [ ip + baseLength - 2 ] ,
elseLength = bc [ ip + baseLength - 1 ] ,
elseLength = bc [ ip + baseLength - 1 ] ,
baseSp = stack . sp ,
baseSp = stack . sp ,
thenCode , elseCode ;
thenCode , elseCode , thenSp , elseSp ;
ip += baseLength ;
ip += baseLength ;
thenCode = compile ( bc . slice ( ip , ip + thenLength ) ) ;
thenCode = compile ( bc . slice ( ip , ip + thenLength ) ) ;
thenSp = stack . sp ;
ip += thenLength ;
ip += thenLength ;
if ( elseLength > 0 ) {
if ( elseLength > 0 ) {
stack . sp = baseSp ;
stack . sp = baseSp ;
elseCode = compile ( bc . slice ( ip , ip + elseLength ) ) ;
elseCode = compile ( bc . slice ( ip , ip + elseLength ) ) ;
elseSp = stack . sp ;
ip += elseLength ;
ip += elseLength ;
if ( thenSp !== elseSp ) {
throw new Error (
"Branches of a condition must move the stack pointer in the same way."
) ;
}
}
}
parts . push ( 'if (' + cond + ') {' ) ;
parts . push ( 'if (' + cond + ') {' ) ;
@ -402,12 +411,18 @@ module.exports = function(ast, options) {
function compileLoop ( cond ) {
function compileLoop ( cond ) {
var baseLength = 2 ,
var baseLength = 2 ,
bodyLength = bc [ ip + baseLength - 1 ] ,
bodyLength = bc [ ip + baseLength - 1 ] ,
bodyCode ;
baseSp = stack . sp ,
bodyCode , bodySp ;
ip += baseLength ;
ip += baseLength ;
bodyCode = compile ( bc . slice ( ip , ip + bodyLength ) ) ;
bodyCode = compile ( bc . slice ( ip , ip + bodyLength ) ) ;
bodySp = stack . sp ;
ip += bodyLength ;
ip += bodyLength ;
if ( bodySp !== baseSp ) {
throw new Error ( "Body of a loop can't move the stack pointer." ) ;
}
parts . push ( 'while (' + cond + ') {' ) ;
parts . push ( 'while (' + cond + ') {' ) ;
parts . push ( indent2 ( bodyCode ) ) ;
parts . push ( indent2 ( bodyCode ) ) ;
parts . push ( '}' ) ;
parts . push ( '}' ) ;