@@ -312,56 +312,8 @@ void Engine::compile()
312
312
// Compile monitor blocks to bytecode
313
313
std::cout << " Compiling stage monitors..." << std::endl;
314
314
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);
365
317
}
366
318
367
319
void Engine::start ()
@@ -1398,7 +1350,7 @@ void Engine::setMonitors(const std::vector<std::shared_ptr<Monitor>> &newMonitor
1398
1350
}
1399
1351
}
1400
1352
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 )
1402
1354
{
1403
1355
if (var->monitor ())
1404
1356
return var->monitor ();
@@ -1407,14 +1359,16 @@ Monitor *Engine::createVariableMonitor(std::shared_ptr<Variable> var, const std:
1407
1359
auto field = std::make_shared<Field>(varFieldName, var->name (), var);
1408
1360
field->setFieldId (varFieldId);
1409
1361
monitor->block ()->addField (field);
1362
+ monitor->block ()->setCompileFunction (compileFunction);
1410
1363
1411
1364
addVarOrListMonitor (monitor, var->target ());
1412
1365
var->setMonitor (monitor.get ());
1366
+ compileMonitor (monitor);
1413
1367
return monitor.get ();
1414
1368
}
1415
1369
}
1416
1370
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 )
1418
1372
{
1419
1373
if (list->monitor ())
1420
1374
return list->monitor ();
@@ -1423,9 +1377,11 @@ Monitor *Engine::createListMonitor(std::shared_ptr<List> list, const std::string
1423
1377
auto field = std::make_shared<Field>(listFieldName, list->name (), list);
1424
1378
field->setFieldId (listFieldId);
1425
1379
monitor->block ()->addField (field);
1426
- list-> setMonitor (monitor. get () );
1380
+ monitor-> block ()-> setCompileFunction (compileFunction );
1427
1381
1428
1382
addVarOrListMonitor (monitor, list->target ());
1383
+ list->setMonitor (monitor.get ());
1384
+ compileMonitor (monitor);
1429
1385
return monitor.get ();
1430
1386
}
1431
1387
}
@@ -1783,6 +1739,58 @@ const std::unordered_set<std::string> &Engine::unsupportedBlocks() const
1783
1739
return m_unsupportedBlocks;
1784
1740
}
1785
1741
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
+
1786
1794
void Engine::finalize ()
1787
1795
{
1788
1796
m_eventLoopMutex.lock ();
0 commit comments