diff --git a/lib/compiler/passes/generate-javascript.js b/lib/compiler/passes/generate-javascript.js index 41ba34e..6de510c 100644 --- a/lib/compiler/passes/generate-javascript.js +++ b/lib/compiler/passes/generate-javascript.js @@ -379,15 +379,24 @@ module.exports = function(ast, options) { thenLength = bc[ip + baseLength - 2], elseLength = bc[ip + baseLength - 1], baseSp = stack.sp, - thenCode, elseCode; + thenCode, elseCode, thenSp, elseSp; ip += baseLength; thenCode = compile(bc.slice(ip, ip + thenLength)); + thenSp = stack.sp; ip += thenLength; + if (elseLength > 0) { stack.sp = baseSp; elseCode = compile(bc.slice(ip, ip + elseLength)); + elseSp = stack.sp; 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 + ') {'); @@ -402,12 +411,18 @@ module.exports = function(ast, options) { function compileLoop(cond) { var baseLength = 2, bodyLength = bc[ip + baseLength - 1], - bodyCode; + baseSp = stack.sp, + bodyCode, bodySp; ip += baseLength; bodyCode = compile(bc.slice(ip, ip + bodyLength)); + bodySp = stack.sp; ip += bodyLength; + if (bodySp !== baseSp) { + throw new Error("Body of a loop can't move the stack pointer."); + } + parts.push('while (' + cond + ') {'); parts.push(indent2(bodyCode)); parts.push('}');