@@ -312,56 +312,8 @@ void Engine::compile()
312312 // Compile monitor blocks to bytecode
313313 std::cout << " Compiling stage monitors..." << std::endl;
314314
315- for (auto monitor : m_monitors) {
316- Target *target = monitor->sprite () ? dynamic_cast <Target *>(monitor->sprite ()) : stage ();
317- Compiler compiler (this , target);
318- auto block = monitor->block ();
319- auto section = blockSection (block->opcode ());
320- auto container = blockSectionContainer (block->opcode ());
321-
322- if (container) {
323- MonitorNameFunc nameFunc = container->resolveMonitorNameFunc (block->opcode ());
324-
325- if (nameFunc)
326- monitor->setName (nameFunc (block.get ()));
327-
328- MonitorChangeFunc changeFunc = container->resolveMonitorChangeFunc (block->opcode ());
329- monitor->setValueChangeFunction (changeFunc);
330- }
331-
332- if (section) {
333- auto script = std::make_shared<Script>(target, block, this );
334- monitor->setScript (script);
335- compiler.init ();
336- compiler.setBlock (block);
337-
338- if (block->compileFunction ())
339- block->compile (&compiler);
340- else
341- std::cout << " warning: monitor block doesn't have a compile function: " << block->opcode () << std::endl;
342-
343- // Workaround for register leak warning spam: pause the script after getting the monitor value
344- compiler.addFunctionCall ([](VirtualMachine *vm) -> unsigned int {
345- vm->stop (false , false , false );
346- return 0 ;
347- });
348-
349- compiler.end ();
350-
351- script->setBytecode (compiler.bytecode ());
352- script->setConstValues (compiler.constValues ());
353- script->setVariables (compiler.variables ());
354- script->setLists (compiler.lists ());
355- } else {
356- std::cout << " warning: unsupported monitor block: " << block->opcode () << std::endl;
357- m_unsupportedBlocks.insert (block->opcode ());
358- }
359-
360- const auto &unsupportedBlocks = compiler.unsupportedBlocks ();
361-
362- for (const std::string &opcode : unsupportedBlocks)
363- m_unsupportedBlocks.insert (opcode);
364- }
315+ for (auto monitor : m_monitors)
316+ compileMonitor (monitor);
365317}
366318
367319void Engine::start ()
@@ -1398,7 +1350,7 @@ void Engine::setMonitors(const std::vector<std::shared_ptr<Monitor>> &newMonitor
13981350 }
13991351}
14001352
1401- Monitor *Engine::createVariableMonitor (std::shared_ptr<Variable> var, const std::string &opcode, const std::string &varFieldName, int varFieldId)
1353+ Monitor *Engine::createVariableMonitor (std::shared_ptr<Variable> var, const std::string &opcode, const std::string &varFieldName, int varFieldId, BlockComp compileFunction )
14021354{
14031355 if (var->monitor ())
14041356 return var->monitor ();
@@ -1407,14 +1359,16 @@ Monitor *Engine::createVariableMonitor(std::shared_ptr<Variable> var, const std:
14071359 auto field = std::make_shared<Field>(varFieldName, var->name (), var);
14081360 field->setFieldId (varFieldId);
14091361 monitor->block ()->addField (field);
1362+ monitor->block ()->setCompileFunction (compileFunction);
14101363
14111364 addVarOrListMonitor (monitor, var->target ());
14121365 var->setMonitor (monitor.get ());
1366+ compileMonitor (monitor);
14131367 return monitor.get ();
14141368 }
14151369}
14161370
1417- Monitor *Engine::createListMonitor (std::shared_ptr<List> list, const std::string &opcode, const std::string &listFieldName, int listFieldId)
1371+ Monitor *Engine::createListMonitor (std::shared_ptr<List> list, const std::string &opcode, const std::string &listFieldName, int listFieldId, BlockComp compileFunction )
14181372{
14191373 if (list->monitor ())
14201374 return list->monitor ();
@@ -1423,9 +1377,11 @@ Monitor *Engine::createListMonitor(std::shared_ptr<List> list, const std::string
14231377 auto field = std::make_shared<Field>(listFieldName, list->name (), list);
14241378 field->setFieldId (listFieldId);
14251379 monitor->block ()->addField (field);
1426- list-> setMonitor (monitor. get () );
1380+ monitor-> block ()-> setCompileFunction (compileFunction );
14271381
14281382 addVarOrListMonitor (monitor, list->target ());
1383+ list->setMonitor (monitor.get ());
1384+ compileMonitor (monitor);
14291385 return monitor.get ();
14301386 }
14311387}
@@ -1783,6 +1739,58 @@ const std::unordered_set<std::string> &Engine::unsupportedBlocks() const
17831739 return m_unsupportedBlocks;
17841740}
17851741
1742+ void Engine::compileMonitor (std::shared_ptr<Monitor> monitor)
1743+ {
1744+ Target *target = monitor->sprite () ? dynamic_cast <Target *>(monitor->sprite ()) : stage ();
1745+ Compiler compiler (this , target);
1746+ auto block = monitor->block ();
1747+ auto section = blockSection (block->opcode ());
1748+ auto container = blockSectionContainer (block->opcode ());
1749+
1750+ if (container) {
1751+ MonitorNameFunc nameFunc = container->resolveMonitorNameFunc (block->opcode ());
1752+
1753+ if (nameFunc)
1754+ monitor->setName (nameFunc (block.get ()));
1755+
1756+ MonitorChangeFunc changeFunc = container->resolveMonitorChangeFunc (block->opcode ());
1757+ monitor->setValueChangeFunction (changeFunc);
1758+ }
1759+
1760+ if (section) {
1761+ auto script = std::make_shared<Script>(target, block, this );
1762+ monitor->setScript (script);
1763+ compiler.init ();
1764+ compiler.setBlock (block);
1765+
1766+ if (block->compileFunction ())
1767+ block->compile (&compiler);
1768+ else
1769+ std::cout << " warning: monitor block doesn't have a compile function: " << block->opcode () << std::endl;
1770+
1771+ // Workaround for register leak warning spam: pause the script after getting the monitor value
1772+ compiler.addFunctionCall ([](VirtualMachine *vm) -> unsigned int {
1773+ vm->stop (false , false , false );
1774+ return 0 ;
1775+ });
1776+
1777+ compiler.end ();
1778+
1779+ script->setBytecode (compiler.bytecode ());
1780+ script->setConstValues (compiler.constValues ());
1781+ script->setVariables (compiler.variables ());
1782+ script->setLists (compiler.lists ());
1783+ } else {
1784+ std::cout << " warning: unsupported monitor block: " << block->opcode () << std::endl;
1785+ m_unsupportedBlocks.insert (block->opcode ());
1786+ }
1787+
1788+ const auto &unsupportedBlocks = compiler.unsupportedBlocks ();
1789+
1790+ for (const std::string &opcode : unsupportedBlocks)
1791+ m_unsupportedBlocks.insert (opcode);
1792+ }
1793+
17861794void Engine::finalize ()
17871795{
17881796 m_eventLoopMutex.lock ();
0 commit comments