forked from scratchfoundation/scratch-vm
-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix stopping a thread from inside of its blocks
Works in command or reporter after yielding or not. These snippets are covered by the tests: thread.status = Thread.STATUS_DONE; thread.status = 4; (indirectly) sequencer.retireThread(thread);
- Loading branch information
1 parent
73d71c0
commit f548d1b
Showing
5 changed files
with
206 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
const {test} = require('tap'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const VM = require('../../src/virtual-machine'); | ||
const Thread = require('../../src/engine/thread'); | ||
const Clone = require('../../src/util/clone'); | ||
|
||
const fixturePath = path.join(__dirname, '../fixtures/tw-block-stop-thread.sb3'); | ||
const fixtureData = fs.readFileSync(fixturePath); | ||
|
||
const stopByRetireThread = thread => { | ||
thread.target.runtime.sequencer.retireThread(thread); | ||
}; | ||
|
||
const stopBySetStatus = thread => { | ||
thread.status = Thread.STATUS_DONE; | ||
}; | ||
|
||
test('constants', t => { | ||
t.equal(Thread.STATUS_DONE, 4); | ||
t.end(); | ||
}); | ||
|
||
for (const [enableCompiler, stopFunction] of [ | ||
[true, stopByRetireThread], | ||
[true, stopBySetStatus], | ||
[false, stopByRetireThread], | ||
[false, stopBySetStatus] | ||
]) { | ||
const subtestName = `${enableCompiler ? 'compiler' : 'interpreter'} - ${stopFunction.name}`; | ||
|
||
test(`${subtestName} - stop in command block`, t => { | ||
const vm = new VM(); | ||
vm.setCompilerOptions({enabled: enableCompiler}); | ||
t.equal(vm.runtime.compilerOptions.enabled, enableCompiler); | ||
|
||
const callOrder = []; | ||
vm.addAddonBlock({ | ||
procedureCode: 'first block %s', | ||
arguments: ['number or text'], | ||
callback: (args, util) => { | ||
callOrder.push(['first block', Clone.simple(args)]); | ||
stopFunction(util.thread); | ||
} | ||
}); | ||
vm.addAddonBlock({ | ||
procedureCode: 'inner block', | ||
return: 1, | ||
callback: args => { | ||
callOrder.push(['input block', Clone.simple(args)]); | ||
return callOrder.length; | ||
} | ||
}); | ||
vm.addAddonBlock({ | ||
procedureCode: 'second block', | ||
callback: args => { | ||
callOrder.push(['second block', Clone.simple(args)]); | ||
} | ||
}); | ||
|
||
vm.loadProject(fixtureData).then(() => { | ||
vm.greenFlag(); | ||
vm.runtime._step(); | ||
t.same(callOrder, [ | ||
['input block', {}], | ||
['first block', {'number or text': 1}] | ||
]); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test(`${subtestName} - stop in command block after yielding once`, t => { | ||
const vm = new VM(); | ||
vm.setCompilerOptions({enabled: enableCompiler}); | ||
t.equal(vm.runtime.compilerOptions.enabled, enableCompiler); | ||
|
||
const callOrder = []; | ||
vm.addAddonBlock({ | ||
procedureCode: 'first block %s', | ||
arguments: ['number or text'], | ||
callback: (args, util) => { | ||
callOrder.push(['first block', Clone.simple(args)]); | ||
if (callOrder.length === 1) { | ||
util.yield(); | ||
} else { | ||
stopFunction(util.thread); | ||
} | ||
} | ||
}); | ||
vm.addAddonBlock({ | ||
procedureCode: 'inner block', | ||
return: 1, | ||
// Ignore calls because interpreter will re-evaluate on yield but compiler won't | ||
callback: () => 5 | ||
}); | ||
vm.addAddonBlock({ | ||
procedureCode: 'second block', | ||
callback: args => { | ||
callOrder.push(['second block', Clone.simple(args)]); | ||
} | ||
}); | ||
|
||
vm.loadProject(fixtureData).then(() => { | ||
vm.greenFlag(); | ||
vm.runtime._step(); | ||
t.same(callOrder, [ | ||
['first block', {'number or text': 5}], | ||
['first block', {'number or text': 5}] | ||
]); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test(`${subtestName} - stop in reporter block`, t => { | ||
const vm = new VM(); | ||
vm.setCompilerOptions({enabled: enableCompiler}); | ||
t.equal(vm.runtime.compilerOptions.enabled, enableCompiler); | ||
|
||
const callOrder = []; | ||
vm.addAddonBlock({ | ||
procedureCode: 'first block %s', | ||
arguments: ['number or text'], | ||
callback: args => { | ||
callOrder.push(['first block', Clone.simple(args)]); | ||
} | ||
}); | ||
vm.addAddonBlock({ | ||
procedureCode: 'inner block', | ||
return: 1, | ||
callback: (args, util) => { | ||
callOrder.push(['input block', Clone.simple(args)]); | ||
stopFunction(util.thread); | ||
return callOrder.length; | ||
} | ||
}); | ||
vm.addAddonBlock({ | ||
procedureCode: 'second block', | ||
callback: args => { | ||
callOrder.push(['second block', Clone.simple(args)]); | ||
} | ||
}); | ||
|
||
vm.loadProject(fixtureData).then(() => { | ||
vm.greenFlag(); | ||
vm.runtime._step(); | ||
t.same(callOrder, [ | ||
['input block', {}] | ||
]); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test(`${subtestName} - stop in reporter block after yielding once`, t => { | ||
const vm = new VM(); | ||
vm.setCompilerOptions({enabled: enableCompiler}); | ||
t.equal(vm.runtime.compilerOptions.enabled, enableCompiler); | ||
|
||
const callOrder = []; | ||
vm.addAddonBlock({ | ||
procedureCode: 'first block %s', | ||
arguments: ['number or text'], | ||
callback: args => { | ||
callOrder.push(['first block', Clone.simple(args)]); | ||
} | ||
}); | ||
vm.addAddonBlock({ | ||
procedureCode: 'inner block', | ||
return: 1, | ||
callback: (args, util) => { | ||
callOrder.push(['input block', Clone.simple(args)]); | ||
if (callOrder.length === 1) { | ||
util.yield(); | ||
// TODO: interpreter bug, should not need to do this... | ||
util.thread.peekStackFrame().waitingReporter = true; | ||
} else { | ||
stopFunction(util.thread); | ||
} | ||
return callOrder.length; | ||
} | ||
}); | ||
vm.addAddonBlock({ | ||
procedureCode: 'second block', | ||
callback: args => { | ||
callOrder.push(['second block', Clone.simple(args)]); | ||
} | ||
}); | ||
|
||
vm.loadProject(fixtureData).then(() => { | ||
vm.greenFlag(); | ||
vm.runtime._step(); | ||
t.same(callOrder, [ | ||
['input block', {}], | ||
['input block', {}] | ||
]); | ||
t.end(); | ||
}); | ||
}); | ||
} |