diff --git a/src/serialization/sb3.js b/src/serialization/sb3.js index df367cf1b..21c5c5f85 100644 --- a/src/serialization/sb3.js +++ b/src/serialization/sb3.js @@ -325,7 +325,7 @@ const serializeBlocks = function (blocks) { // a shadow block, and there are no blocks that reference it, otherwise // they would have been compressed in the last pass) if (Array.isArray(serializedBlock) && - [VAR_PRIMITIVE, LIST_PRIMITIVE].indexOf(serializedBlock) < 0) { + [VAR_PRIMITIVE, LIST_PRIMITIVE].indexOf(serializedBlock[0]) < 0) { log.warn(`Found an unexpected top level primitive with block ID: ${ blockID}; deleting it from serialized blocks.`); delete obj[blockID]; diff --git a/test/fixtures/top-level-variable-reporter.sb2 b/test/fixtures/top-level-variable-reporter.sb2 new file mode 100644 index 000000000..5c5040217 Binary files /dev/null and b/test/fixtures/top-level-variable-reporter.sb2 differ diff --git a/test/unit/serialization_sb3.js b/test/unit/serialization_sb3.js index caf31e2a8..01da2d75f 100644 --- a/test/unit/serialization_sb3.js +++ b/test/unit/serialization_sb3.js @@ -7,6 +7,7 @@ const exampleProjectPath = path.resolve(__dirname, '../fixtures/clone-cleanup.sb const commentsSB2ProjectPath = path.resolve(__dirname, '../fixtures/comments.sb2'); const commentsSB3ProjectPath = path.resolve(__dirname, '../fixtures/comments.sb3'); const commentsSB3NoDupeIds = path.resolve(__dirname, '../fixtures/comments_no_duplicate_id_serialization.sb3'); +const variableReporterSB2ProjectPath = path.resolve(__dirname, '../fixtures/top-level-variable-reporter.sb2'); const FakeRenderer = require('../fixtures/fake-renderer'); test('serialize', t => { @@ -236,3 +237,16 @@ test('getExtensionIdForOpcode', t => { t.end(); }); + +test('(#1608) serializeBlocks maintains top level variable reporters', t => { + const vm = new VirtualMachine(); + vm.loadProject(readFileToBuffer(variableReporterSB2ProjectPath)) + .then(() => { + const blocks = vm.runtime.targets[0].blocks._blocks; + const result = sb3.serialize(vm.runtime).targets[0].blocks; + // Project should have 1 block, a top-level variable reporter + t.equal(Object.keys(blocks).length, 1); + t.equal(Object.keys(result).length, 1); + t.end(); + }); +});