Skip to content

Commit ae67b9b

Browse files
gonfunkocwillisf
authored andcommitted
fix: fix bug that could result in the VM's representation of shadow blocks getting into a bad state (#9)
1 parent ced972e commit ae67b9b

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

packages/scratch-vm/src/engine/blocks.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -886,23 +886,32 @@ class Blocks {
886886
typeof e.oldInput !== "undefined" &&
887887
oldParent.inputs[e.oldInput].block === e.id
888888
) {
889-
// This block was connected to the old parent's input. We either
890-
// want to restore the shadow block that previously occupied
891-
// this input, or set it to null (which `.shadow` will be if
892-
// there was no shadow previously)
893-
oldParent.inputs[e.oldInput].block =
894-
oldParent.inputs[e.oldInput].shadow;
889+
// This block was connected to an input. We either want to
890+
// restore the shadow block that previously occupied
891+
// this input, or null out the input's block.
892+
const shadow = oldParent.inputs[e.oldInput].shadow;
893+
if (shadow && e.id !== shadow) {
894+
oldParent.inputs[e.oldInput].block = shadow;
895+
this._blocks[shadow].parent = oldParent.id;
896+
} else {
897+
oldParent.inputs[e.oldInput].block = null;
898+
if (e.id !== shadow) {
899+
this._blocks[e.id].parent = null;
900+
}
901+
}
895902
} else if (oldParent.next === e.id) {
896903
// This block was connected to the old parent's next connection.
897904
oldParent.next = null;
905+
this._blocks[e.id].parent = null;
898906
}
899-
this._blocks[e.id].parent = null;
900907
didChange = true;
901908
}
902909

903910
// Is this block a top-level block?
904911
if (typeof e.newParent === "undefined") {
905-
this._addScript(e.id);
912+
if (!this._blocks[e.id].shadow) {
913+
this._addScript(e.id);
914+
}
906915
} else {
907916
// Remove script, if one exists.
908917
this._deleteScript(e.id);

0 commit comments

Comments
 (0)