mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 06:23:37 -05:00
Merge pull request #1586 from mzgoddard/variable-id-type
conflict workaround for variables and lists of the same name
This commit is contained in:
commit
7a28d5e41d
5 changed files with 17 additions and 12 deletions
|
@ -198,18 +198,18 @@ const parseScripts = function (scripts, blocks, addBroadcastMsg, getVariableId,
|
|||
*/
|
||||
const generateVariableIdGetter = (function () {
|
||||
let globalVariableNameMap = {};
|
||||
const namer = (targetId, name) => `${targetId}-${name}`;
|
||||
const namer = (targetId, name, type) => `${targetId}-${name}-${type}`;
|
||||
return function (targetId, topLevel) {
|
||||
// Reset the global variable map if topLevel
|
||||
if (topLevel) globalVariableNameMap = {};
|
||||
return function (name) {
|
||||
return function (name, type) {
|
||||
if (topLevel) { // Store the name/id pair in the globalVariableNameMap
|
||||
globalVariableNameMap[name] = namer(targetId, name);
|
||||
return globalVariableNameMap[name];
|
||||
globalVariableNameMap[`${name}-${type}`] = namer(targetId, name, type);
|
||||
return globalVariableNameMap[`${name}-${type}`];
|
||||
}
|
||||
// Not top-level, so first check the global name map
|
||||
if (globalVariableNameMap[name]) return globalVariableNameMap[name];
|
||||
return namer(targetId, name);
|
||||
if (globalVariableNameMap[`${name}-${type}`]) return globalVariableNameMap[`${name}-${type}`];
|
||||
return namer(targetId, name, type);
|
||||
};
|
||||
};
|
||||
}());
|
||||
|
@ -282,8 +282,10 @@ const parseMonitorObject = (object, runtime, targets, extensions) => {
|
|||
// Monitor blocks have special IDs to match the toolbox obtained from the getId
|
||||
// function in the runtime.monitorBlocksInfo. Variable monitors, however,
|
||||
// get their IDs from the variable id they reference.
|
||||
if (object.cmd === 'getVar:' || object.cmd === 'contentsOfList:') {
|
||||
block.id = getVariableId(object.param);
|
||||
if (object.cmd === 'getVar:') {
|
||||
block.id = getVariableId(object.param, Variable.SCALAR_TYPE);
|
||||
} else if (object.cmd === 'contentsOfList:') {
|
||||
block.id = getVariableId(object.param, Variable.LIST_TYPE);
|
||||
} else if (runtime.monitorBlockInfo.hasOwnProperty(block.opcode)) {
|
||||
block.id = runtime.monitorBlockInfo[block.opcode].getId(target.id, object.param);
|
||||
}
|
||||
|
@ -452,7 +454,7 @@ const parseScratchObject = function (object, runtime, extensions, topLevel, zip)
|
|||
for (let j = 0; j < object.variables.length; j++) {
|
||||
const variable = object.variables[j];
|
||||
const newVariable = new Variable(
|
||||
getVariableId(variable.name),
|
||||
getVariableId(variable.name, Variable.SCALAR_TYPE),
|
||||
variable.name,
|
||||
Variable.SCALAR_TYPE,
|
||||
variable.isPersistent
|
||||
|
@ -544,7 +546,7 @@ const parseScratchObject = function (object, runtime, extensions, topLevel, zip)
|
|||
for (let k = 0; k < object.lists.length; k++) {
|
||||
const list = object.lists[k];
|
||||
const newVariable = new Variable(
|
||||
getVariableId(list.listName),
|
||||
getVariableId(list.listName, Variable.LIST_TYPE),
|
||||
list.listName,
|
||||
Variable.LIST_TYPE,
|
||||
false
|
||||
|
@ -982,9 +984,12 @@ const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extension
|
|||
value: providedArg
|
||||
};
|
||||
|
||||
if (expectedArg.fieldName === 'VARIABLE' || expectedArg.fieldName === 'LIST') {
|
||||
if (expectedArg.fieldName === 'VARIABLE') {
|
||||
// Add `id` property to variable fields
|
||||
activeBlock.fields[expectedArg.fieldName].id = getVariableId(providedArg);
|
||||
activeBlock.fields[expectedArg.fieldName].id = getVariableId(providedArg, Variable.SCALAR_TYPE);
|
||||
} else if (expectedArg.fieldName === 'LIST') {
|
||||
// Add `id` property to variable fields
|
||||
activeBlock.fields[expectedArg.fieldName].id = getVariableId(providedArg, Variable.LIST_TYPE);
|
||||
} else if (expectedArg.fieldName === 'BROADCAST_OPTION') {
|
||||
// Add the name in this field to the broadcast msg name map.
|
||||
// Also need to provide the fields[fieldName] object,
|
||||
|
|
BIN
test/fixtures/execute/data-operators-global.sb2
vendored
Normal file
BIN
test/fixtures/execute/data-operators-global.sb2
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/execute/data-operators-local.sb2
vendored
Normal file
BIN
test/fixtures/execute/data-operators-local.sb2
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/execute/data-reporter-contents-global.sb2
vendored
Normal file
BIN
test/fixtures/execute/data-reporter-contents-global.sb2
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/execute/data-reporter-contents-local.sb2
vendored
Normal file
BIN
test/fixtures/execute/data-reporter-contents-local.sb2
vendored
Normal file
Binary file not shown.
Loading…
Reference in a new issue