Skip to content

Commit b8520cc

Browse files
committed
Fix visual report error when thread target does not match editing target
Fixes #252
1 parent 1da38da commit b8520cc

File tree

5 files changed

+73
-4
lines changed

5 files changed

+73
-4
lines changed

src/compiler/jsgen.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ class JSGenerator {
11601160
const value = this.localVariables.next();
11611161
this.source += `const ${value} = ${this.descendInput(node.input).asUnknown()};`;
11621162
// blocks like legacy no-ops can return a literal `undefined`
1163-
this.source += `if (${value} !== undefined) runtime.visualReport("${sanitize(this.script.topBlockId)}", ${value});\n`;
1163+
this.source += `if (${value} !== undefined) runtime.visualReport(target, "${sanitize(this.script.topBlockId)}", ${value});\n`;
11641164
break;
11651165
}
11661166

src/engine/execute.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const handleReport = function (resolvedValue, sequencer, thread, blockCached, la
9292
// at the top of the thread stack.
9393
if (lastOperation && typeof resolvedValue !== 'undefined' && thread.atStackTop()) {
9494
if (thread.stackClick) {
95-
sequencer.runtime.visualReport(currentBlockId, resolvedValue);
95+
sequencer.runtime.visualReport(thread.target, currentBlockId, resolvedValue);
9696
}
9797
if (thread.updateMonitor) {
9898
const targetId = sequencer.runtime.monitorBlocks.getBlock(currentBlockId).targetId;

src/engine/runtime.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -3073,11 +3073,14 @@ class Runtime extends EventEmitter {
30733073

30743074
/**
30753075
* Emit value for reporter to show in the blocks.
3076+
* @param {Target} target The target that the block was run in.
30763077
* @param {string} blockId ID for the block.
30773078
* @param {string} value Value to show associated with the block.
30783079
*/
3079-
visualReport (blockId, value) {
3080-
this.emit(Runtime.VISUAL_REPORT, {id: blockId, value: String(value)});
3080+
visualReport (target, blockId, value) {
3081+
if (target === this.getEditingTarget()) {
3082+
this.emit(Runtime.VISUAL_REPORT, {id: blockId, value: String(value)});
3083+
}
30813084
}
30823085

30833086
/**
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const {test} = require('tap');
2+
const fs = require('fs');
3+
const path = require('path');
4+
const VirtualMachine = require('../../src/virtual-machine');
5+
6+
const fixtureData = fs.readFileSync(path.join(__dirname, '../fixtures/tw-slow-custom-reporter-stack-click.sb3'));
7+
8+
// After starting this block, first step will yield, second step will report value
9+
const procedureCallBlockId = 'e';
10+
11+
test('when thread target is editing target', t => {
12+
const vm = new VirtualMachine();
13+
vm.loadProject(fixtureData).then(() => {
14+
const visualReports = [];
15+
vm.on('VISUAL_REPORT', visualReport => {
16+
visualReports.push(visualReport);
17+
});
18+
19+
vm.setEditingTarget(vm.runtime.getSpriteTargetByName('Sprite1').id);
20+
vm.runtime.toggleScript(procedureCallBlockId, {
21+
stackClick: true
22+
});
23+
24+
t.same(visualReports, []);
25+
26+
vm.runtime._step();
27+
t.same(visualReports, []);
28+
29+
vm.runtime._step();
30+
t.same(visualReports, [
31+
{
32+
id: 'e',
33+
value: 'return value'
34+
}
35+
]);
36+
37+
t.end();
38+
});
39+
});
40+
41+
test('when thread target is not editing target', t => {
42+
const vm = new VirtualMachine();
43+
vm.loadProject(fixtureData).then(() => {
44+
const visualReports = [];
45+
vm.on('VISUAL_REPORT', visualReport => {
46+
visualReports.push(visualReport);
47+
});
48+
49+
vm.setEditingTarget(vm.runtime.getSpriteTargetByName('Sprite1').id);
50+
vm.runtime.toggleScript(procedureCallBlockId, {
51+
stackClick: true
52+
});
53+
54+
t.same(visualReports, []);
55+
56+
vm.runtime._step();
57+
t.same(visualReports, []);
58+
59+
vm.setEditingTarget(vm.runtime.getSpriteTargetByName('Sprite2').id);
60+
61+
vm.runtime._step();
62+
t.same(visualReports, []);
63+
64+
t.end();
65+
});
66+
});

0 commit comments

Comments
 (0)