If I recall correctly, we add a one-tick delay to the while true
loop if there has been no other blocking action called during that while loop frame, but we don’t do that when the while
loop has some other condition other than a Boolean literal like True
. There might be some other exceptions, like nesting other while
loops inside. This should be the same in all languages, not just Python. Code for that is here:
makeYieldFilter = (aether) -> (engine, evaluator, e) ->
frame_stack = evaluator.frames
#console.log x.type + " " + x.ast?.type for x in frame_stack
#console.log "----"
top = frame_stack[0]
if e? and e.type is 'event' and e.event is 'loopBodyStart'
# Legacy programming languages use 'Literal' whilst C++ and Java use 'BooleanLiteral'.
if top.srcAst.type is 'WhileStatement' and (top.srcAst.test.type is 'Literal' or top.srcAst.test.type is 'BooleanLiteral')
if aether.whileLoopMarker?
currentMark = aether.whileLoopMarker(top)
if currentMark is top.mark
# console.log "[Aether] Frame #{this.world.age}: Forcing while-true loop to yield, repeat #{currentMark}"
top.mark = currentMark + 1
return true
else
# console.log "[Aether] Frame #{this.world.age}: Loop Avoided, mark #{top.mark} isnt #{currentMark}"
top.mark = currentMark
if aether._shouldYield
yieldValue = aether._shouldYield
aether._shouldYield = false
frame_stack[1].didYield = true if frame_stack[1].type is 'loop'
return true
return false