mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-03-13 17:04:39 -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;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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
|
||||
* currently being serialized.)
|
||||
|
@ -285,10 +298,9 @@ const serializeBlocks = function (blocks) {
|
|||
for (const blockID in blocks) {
|
||||
if (!blocks.hasOwnProperty(blockID)) continue;
|
||||
obj[blockID] = serializeBlock(blocks[blockID], blocks);
|
||||
const index = blocks[blockID].opcode.indexOf('_');
|
||||
const prefix = blocks[blockID].opcode.substring(0, index);
|
||||
if (CORE_EXTENSIONS.indexOf(prefix) === -1) {
|
||||
if (prefix !== '') extensionIDs.add(prefix);
|
||||
const extensionID = getExtensionIdForOpcode(blocks[blockID].opcode);
|
||||
if (extensionID) {
|
||||
extensionIDs.add(extensionID);
|
||||
}
|
||||
}
|
||||
// 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);
|
||||
|
||||
// If the block is from an extension, record it.
|
||||
const index = blockJSON.opcode.indexOf('_');
|
||||
const prefix = blockJSON.opcode.substring(0, index);
|
||||
if (CORE_EXTENSIONS.indexOf(prefix) === -1) {
|
||||
if (prefix !== '') extensions.extensionIDs.add(prefix);
|
||||
const extensionID = getExtensionIdForOpcode(blockJSON.opcode);
|
||||
if (extensionID) {
|
||||
extensions.extensionIDs.add(extensionID);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1049,5 +1060,6 @@ module.exports = {
|
|||
serialize: serialize,
|
||||
deserialize: deserialize,
|
||||
deserializeBlocks: deserializeBlocks,
|
||||
serializeBlocks: serializeBlocks
|
||||
serializeBlocks: serializeBlocks,
|
||||
getExtensionIdForOpcode: getExtensionIdForOpcode
|
||||
};
|
||||
|
|
|
@ -221,3 +221,14 @@ test('deserializeBlocks on already deserialized input', t => {
|
|||
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