@@ -83,13 +83,13 @@ void Engine::compile()
8383 variables = compiler.variables ();
8484 lists = compiler.lists ();
8585 constInputValues = compiler.constInputValues ();
86- m_scripts[block] = VirtualMachine (target.get (), this );
87- VirtualMachine &vm = m_scripts[block] ;
88- vm. setFunctions (m_functions );
89- vm. setConstValues (compiler.constValues ());
90- vm. setVariables (compiler. variablePtrs () );
91- vm. setLists (lists );
92- vm. setBytecode (compiler. bytecode ()) ;
86+ auto vm = std::make_shared< VirtualMachine> (target.get (), this );
87+ vm-> setFunctions (m_functions) ;
88+ vm-> setConstValues (compiler. constValues () );
89+ vm-> setVariables (compiler.variablePtrs ());
90+ vm-> setLists (lists );
91+ vm-> setBytecode (compiler. bytecode () );
92+ m_scripts[block] = vm ;
9393 } else
9494 std::cout << " warning: unsupported top level block: " << block->opcode () << std::endl;
9595 }
@@ -106,19 +106,22 @@ void Engine::compile()
106106 */
107107void Engine::frame ()
108108{
109- for (VirtualMachine &script : m_runningScripts) {
109+ for (int i = 0 ; i < m_runningScripts.size (); i++) {
110+ auto script = m_runningScripts[i];
110111 m_breakFrame = false ;
111112
112113 do {
113- script.run ();
114- } while (script.atEnd () && !m_breakFrame);
114+ m_scriptPositions[i] = script->run ();
115+ if (script->atEnd ())
116+ m_scriptsToRemove.push_back (script);
117+ } while (!script->atEnd () && !m_breakFrame);
115118 }
116119
117120 for (auto script : m_scriptsToRemove) {
118- auto it = std::find (m_runningScriptPtrs .begin (), m_runningScriptPtrs .end (), script);
119- auto index = it - m_runningScriptPtrs .begin ();
121+ auto it = std::find (m_runningScripts .begin (), m_runningScripts .end (), script);
122+ auto index = it - m_runningScripts .begin ();
120123 m_runningScripts.erase (m_runningScripts.begin () + index);
121- m_runningScriptPtrs .erase (m_runningScriptPtrs .begin () + index);
124+ m_scriptPositions .erase (m_scriptPositions .begin () + index);
122125 scriptCount--;
123126 }
124127 m_scriptsToRemove.clear ();
@@ -160,8 +163,9 @@ void Engine::startScript(std::shared_ptr<Block> topLevelBlock, std::shared_ptr<T
160163 }
161164
162165 if (topLevelBlock->next ()) {
163- m_runningScripts.push_back (m_scripts[topLevelBlock]);
164- m_runningScriptPtrs.push_back (&m_runningScripts.back ());
166+ auto vm = m_scripts[topLevelBlock].get ();
167+ m_runningScripts.push_back (vm);
168+ m_scriptPositions.push_back (vm->bytecode ());
165169 scriptCount++;
166170 }
167171}
@@ -181,9 +185,9 @@ void Engine::stopScript(VirtualMachine *vm)
181185void Engine::stopTarget (Target *target, VirtualMachine *exceptScript)
182186{
183187 std::vector<VirtualMachine *> scripts;
184- for (VirtualMachine & script : m_runningScripts) {
185- if ((script. target () == target) && (& script != exceptScript))
186- scripts.push_back (& script);
188+ for (auto script : m_runningScripts) {
189+ if ((script-> target () == target) && (script != exceptScript))
190+ scripts.push_back (script);
187191 }
188192
189193 for (auto script : scripts)
0 commit comments