Skip to content

Commit 9343e7c

Browse files
committed
Tidy up stable logic a little
1 parent c0da560 commit 9343e7c

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/flitter/engine/control.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .. import setproctitle
1414
from ..cache import SharedCache
1515
from ..clock import BeatCounter, system_clock
16-
from ..language.vm import log_vm_stats, ProgramInvalid
16+
from ..language.vm import log_vm_stats, StableChanged
1717
from ..model import Vector, StateDict, Context, null, numbers_cache_counts, empty_numbers_cache
1818
from ..plugins import get_plugin
1919
from ..render.window.models import Model
@@ -195,20 +195,27 @@ async def run(self):
195195
run_program = current_program = program
196196
self.handle_pragmas(program.pragmas, frame_time)
197197
errors = logs = None
198+
stables = set()
199+
stable_cache = {}
198200

199201
now = system_clock()
200202
housekeeping += now
201203
execution -= now
202204

203205
if run_program is not None:
204-
context = Context(names={key: Vector.coerce(value) for key, value in dynamic.items()},
205-
state=self.state, references=self._references, stables=stables, stable_cache=stable_cache)
206-
try:
207-
run_program.run(context, record_stats=self.vm_stats)
208-
except ProgramInvalid:
209-
logger.debug("Simplified program invalidated due to change of stable value")
210-
run_program = current_program
211-
run_program.run(context, record_stats=self.vm_stats)
206+
while True:
207+
context = Context(names={key: Vector.coerce(value) for key, value in dynamic.items()},
208+
state=self.state, references=self._references, stables=stables, stable_cache=stable_cache)
209+
try:
210+
run_program.run(context, record_stats=self.vm_stats)
211+
except StableChanged as exc:
212+
if run_program is not current_program:
213+
logger.debug("Simplified program invalidated due to change of stable value")
214+
run_program = current_program
215+
else:
216+
raise RuntimeError("Compiled program contains unexpected StableAssert") from exc
217+
else:
218+
break
212219
else:
213220
context = Context()
214221

src/flitter/language/vm.pyx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ class ProgramInvalid(Exception):
251251
pass
252252

253253

254+
class StableChanged(ProgramInvalid):
255+
pass
256+
257+
254258
cdef class VectorStack:
255259
def __cinit__(self, int64_t size=DEFAULT_STACK_SIZE, int64_t max_size=MAX_STACK_SIZE):
256260
self.vectors = <PyObject**>PyMem_Malloc(sizeof(PyObject*) * size)
@@ -1485,7 +1489,7 @@ cdef class Program:
14851489
if objptr == NULL or r1.eq(<Vector>objptr) is false_:
14861490
PyDict_SetItem(context.stable_cache, key, r1)
14871491
context.stables.remove(key)
1488-
raise ProgramInvalid("Stable assertion failed")
1492+
raise StableChanged(key)
14891493

14901494
elif instruction.code == OpCode.StableTest:
14911495
r1 = peek(stack)

0 commit comments

Comments
 (0)