mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-03-14 01:09:51 -04:00
Move extension ID parsing into a helper and add test
This commit is contained in:
parent
692b71a737
commit
bc1da9fa44
2 changed files with 32 additions and 9 deletions
|
@ -271,6 +271,19 @@ const compressInputTree = function (block, blocks) {
|
||||||
return block;
|
return block;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get non-core extension ID for a given sb3 opcode.
|
||||||
|
* @param {!string} opcode The opcode to examine for extension.
|
||||||
|
* @return {?string} The extension ID, if it exists/is not a core extension.
|
||||||
|
*/
|
||||||
|
const getExtensionIdForOpcode = function (opcode) {
|
||||||
|
const index = opcode.indexOf('_');
|
||||||
|
const prefix = opcode.substring(0, index);
|
||||||
|
if (CORE_EXTENSIONS.indexOf(prefix) === -1) {
|
||||||
|
if (prefix !== '') return prefix;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Serialize the given blocks object (representing all the blocks for the target
|
* Serialize the given blocks object (representing all the blocks for the target
|
||||||
* currently being serialized.)
|
* currently being serialized.)
|
||||||
|
@ -285,10 +298,9 @@ const serializeBlocks = function (blocks) {
|
||||||
for (const blockID in blocks) {
|
for (const blockID in blocks) {
|
||||||
if (!blocks.hasOwnProperty(blockID)) continue;
|
if (!blocks.hasOwnProperty(blockID)) continue;
|
||||||
obj[blockID] = serializeBlock(blocks[blockID], blocks);
|
obj[blockID] = serializeBlock(blocks[blockID], blocks);
|
||||||
const index = blocks[blockID].opcode.indexOf('_');
|
const extensionID = getExtensionIdForOpcode(blocks[blockID].opcode);
|
||||||
const prefix = blocks[blockID].opcode.substring(0, index);
|
if (extensionID) {
|
||||||
if (CORE_EXTENSIONS.indexOf(prefix) === -1) {
|
extensionIDs.add(extensionID);
|
||||||
if (prefix !== '') extensionIDs.add(prefix);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// once we have completed a first pass, do a second pass on block inputs
|
// once we have completed a first pass, do a second pass on block inputs
|
||||||
|
@ -812,10 +824,9 @@ const parseScratchObject = function (object, runtime, extensions, zip) {
|
||||||
blocks.createBlock(blockJSON);
|
blocks.createBlock(blockJSON);
|
||||||
|
|
||||||
// If the block is from an extension, record it.
|
// If the block is from an extension, record it.
|
||||||
const index = blockJSON.opcode.indexOf('_');
|
const extensionID = getExtensionIdForOpcode(blockJSON.opcode);
|
||||||
const prefix = blockJSON.opcode.substring(0, index);
|
if (extensionID) {
|
||||||
if (CORE_EXTENSIONS.indexOf(prefix) === -1) {
|
extensions.extensionIDs.add(extensionID);
|
||||||
if (prefix !== '') extensions.extensionIDs.add(prefix);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1049,5 +1060,6 @@ module.exports = {
|
||||||
serialize: serialize,
|
serialize: serialize,
|
||||||
deserialize: deserialize,
|
deserialize: deserialize,
|
||||||
deserializeBlocks: deserializeBlocks,
|
deserializeBlocks: deserializeBlocks,
|
||||||
serializeBlocks: serializeBlocks
|
serializeBlocks: serializeBlocks,
|
||||||
|
getExtensionIdForOpcode: getExtensionIdForOpcode
|
||||||
};
|
};
|
||||||
|
|
|
@ -221,3 +221,14 @@ test('deserializeBlocks on already deserialized input', t => {
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('getExtensionIdForOpcode', t => {
|
||||||
|
t.equal(sb3.getExtensionIdForOpcode('wedo_loopy'), 'wedo');
|
||||||
|
|
||||||
|
// does not consider CORE to be extensions
|
||||||
|
t.false(sb3.getExtensionIdForOpcode('control_loopy'));
|
||||||
|
|
||||||
|
// only considers things before the first underscore
|
||||||
|
t.equal(sb3.getExtensionIdForOpcode('hello_there_loopy'), 'hello');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue