mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 23:12:24 -05:00
generate sb2 variable ids with variable type
Make variables and lists with the same name have unique IDs so they don't clobber the other in the variables map on the target.
This commit is contained in:
parent
64a1d3e02b
commit
a5151a0446
1 changed files with 17 additions and 12 deletions
|
@ -193,18 +193,18 @@ const parseScripts = function (scripts, blocks, addBroadcastMsg, getVariableId,
|
||||||
*/
|
*/
|
||||||
const generateVariableIdGetter = (function () {
|
const generateVariableIdGetter = (function () {
|
||||||
let globalVariableNameMap = {};
|
let globalVariableNameMap = {};
|
||||||
const namer = (targetId, name) => `${targetId}-${name}`;
|
const namer = (targetId, name, type) => `${targetId}-${name}-${type}`;
|
||||||
return function (targetId, topLevel) {
|
return function (targetId, topLevel) {
|
||||||
// Reset the global variable map if topLevel
|
// Reset the global variable map if topLevel
|
||||||
if (topLevel) globalVariableNameMap = {};
|
if (topLevel) globalVariableNameMap = {};
|
||||||
return function (name) {
|
return function (name, type) {
|
||||||
if (topLevel) { // Store the name/id pair in the globalVariableNameMap
|
if (topLevel) { // Store the name/id pair in the globalVariableNameMap
|
||||||
globalVariableNameMap[name] = namer(targetId, name);
|
globalVariableNameMap[`${name}-${type}`] = namer(targetId, name, type);
|
||||||
return globalVariableNameMap[name];
|
return globalVariableNameMap[`${name}-${type}`];
|
||||||
}
|
}
|
||||||
// Not top-level, so first check the global name map
|
// Not top-level, so first check the global name map
|
||||||
if (globalVariableNameMap[name]) return globalVariableNameMap[name];
|
if (globalVariableNameMap[`${name}-${type}`]) return globalVariableNameMap[`${name}-${type}`];
|
||||||
return namer(targetId, name);
|
return namer(targetId, name, type);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}());
|
}());
|
||||||
|
@ -276,8 +276,10 @@ const parseMonitorObject = (object, runtime, targets, extensions) => {
|
||||||
// Monitor blocks have special IDs to match the toolbox obtained from the getId
|
// Monitor blocks have special IDs to match the toolbox obtained from the getId
|
||||||
// function in the runtime.monitorBlocksInfo. Variable monitors, however,
|
// function in the runtime.monitorBlocksInfo. Variable monitors, however,
|
||||||
// get their IDs from the variable id they reference.
|
// get their IDs from the variable id they reference.
|
||||||
if (object.cmd === 'getVar:' || object.cmd === 'contentsOfList:') {
|
if (object.cmd === 'getVar:') {
|
||||||
block.id = getVariableId(object.param);
|
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)) {
|
} else if (runtime.monitorBlockInfo.hasOwnProperty(block.opcode)) {
|
||||||
block.id = runtime.monitorBlockInfo[block.opcode].getId(target.id, object.param);
|
block.id = runtime.monitorBlockInfo[block.opcode].getId(target.id, object.param);
|
||||||
}
|
}
|
||||||
|
@ -446,7 +448,7 @@ const parseScratchObject = function (object, runtime, extensions, topLevel, zip)
|
||||||
for (let j = 0; j < object.variables.length; j++) {
|
for (let j = 0; j < object.variables.length; j++) {
|
||||||
const variable = object.variables[j];
|
const variable = object.variables[j];
|
||||||
const newVariable = new Variable(
|
const newVariable = new Variable(
|
||||||
getVariableId(variable.name),
|
getVariableId(variable.name, Variable.SCALAR_TYPE),
|
||||||
variable.name,
|
variable.name,
|
||||||
Variable.SCALAR_TYPE,
|
Variable.SCALAR_TYPE,
|
||||||
variable.isPersistent
|
variable.isPersistent
|
||||||
|
@ -538,7 +540,7 @@ const parseScratchObject = function (object, runtime, extensions, topLevel, zip)
|
||||||
for (let k = 0; k < object.lists.length; k++) {
|
for (let k = 0; k < object.lists.length; k++) {
|
||||||
const list = object.lists[k];
|
const list = object.lists[k];
|
||||||
const newVariable = new Variable(
|
const newVariable = new Variable(
|
||||||
getVariableId(list.listName),
|
getVariableId(list.listName, Variable.LIST_TYPE),
|
||||||
list.listName,
|
list.listName,
|
||||||
Variable.LIST_TYPE,
|
Variable.LIST_TYPE,
|
||||||
false
|
false
|
||||||
|
@ -966,9 +968,12 @@ const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extension
|
||||||
value: providedArg
|
value: providedArg
|
||||||
};
|
};
|
||||||
|
|
||||||
if (expectedArg.fieldName === 'VARIABLE' || expectedArg.fieldName === 'LIST') {
|
if (expectedArg.fieldName === 'VARIABLE') {
|
||||||
// Add `id` property to variable fields
|
// 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') {
|
} else if (expectedArg.fieldName === 'BROADCAST_OPTION') {
|
||||||
// Add the name in this field to the broadcast msg name map.
|
// Add the name in this field to the broadcast msg name map.
|
||||||
// Also need to provide the fields[fieldName] object,
|
// Also need to provide the fields[fieldName] object,
|
||||||
|
|
Loading…
Reference in a new issue