diff --git a/blocks/procedures.js b/blocks/procedures.js index eb42047a7..6532da888 100644 --- a/blocks/procedures.js +++ b/blocks/procedures.js @@ -159,8 +159,8 @@ Blockly.Blocks['procedures_defnoreturn'] = { if (childNode.nodeName.toLowerCase() == 'arg') { var varName = childNode.getAttribute('name'); this.arguments_.push(varName); - var variable = Blockly.Variables.getOrCreateVariable(this.workspace, - null, varName, ''); + var variable = Blockly.Variables.getOrCreateVariablePackage( + this.workspace, null, varName, ''); this.argumentVarModels_.push(variable); } } @@ -217,8 +217,8 @@ Blockly.Blocks['procedures_defnoreturn'] = { while (paramBlock) { var varName = paramBlock.getFieldValue('NAME'); this.arguments_.push(varName); - var variable = Blockly.Variables.getOrCreateVariable(this.workspace, null, - varName, ''); + var variable = Blockly.Variables.getOrCreateVariablePackage( + this.workspace, null, varName, ''); this.argumentVarModels_.push(variable); this.paramIds_.push(paramBlock.id); paramBlock = paramBlock.nextConnection && @@ -636,8 +636,8 @@ Blockly.Blocks['procedures_callnoreturn'] = { // And rebuild the argument model list. this.argumentVarModels_ = []; for (var i = 0; i < this.arguments_.length; i++) { - var variable = Blockly.Variables.getOrCreateVariable(this.workspace, null, - this.arguments_[i], ''); + var variable = Blockly.Variables.getOrCreateVariablePackage( + this.workspace, null, this.arguments_[i], ''); this.argumentVarModels_.push(variable); } diff --git a/core/field_variable.js b/core/field_variable.js index c0e3bb07a..53883e56a 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -88,7 +88,7 @@ Blockly.FieldVariable.prototype.initModel = function() { return; // Initialization already happened. } this.workspace_ = this.sourceBlock_.workspace; - var variable = Blockly.Variables.getOrCreateVariable( + var variable = Blockly.Variables.getOrCreateVariablePackage( this.workspace_, null, this.defaultVariableName, this.defaultType_); // Don't fire a change event for this setValue. It would have null as the diff --git a/core/flyout_base.js b/core/flyout_base.js index 07f4a9096..50c38d5d7 100644 --- a/core/flyout_base.js +++ b/core/flyout_base.js @@ -250,6 +250,8 @@ Blockly.Flyout.prototype.init = function(targetWorkspace) { // Get variables from the main workspace rather than the target workspace. this.workspace_.variableMap_ = this.targetWorkspace_.getVariableMap(); + + this.workspace_.createPotentialVariableMap(); }; /** diff --git a/core/variables.js b/core/variables.js index 224aa20c6..0e2c5214a 100644 --- a/core/variables.js +++ b/core/variables.js @@ -385,7 +385,7 @@ Blockly.Variables.generateVariableFieldXml_ = function(variableModel) { * or name + type combination. * @package */ -Blockly.Variables.getOrCreateVariable = function(workspace, id, opt_name, +Blockly.Variables.getOrCreateVariablePackage = function(workspace, id, opt_name, opt_type) { var variable = Blockly.Variables.getVariable(workspace, id, opt_name, opt_type); @@ -420,14 +420,15 @@ Blockly.Variables.getVariable = function(workspace, id, opt_name, opt_type) { if (!variable && potentialVariableMap) { variable = potentialVariableMap.getVariableById(id); } - } else if (opt_name && (opt_type != undefined)){ + } else if (opt_name) { + if (opt_type == undefined) { + throw new Error('Tried to look up a variable by name without a type'); + } // Otherwise look up by name and type. var variable = workspace.getVariable(opt_name, opt_type); if (!variable && potentialVariableMap) { variable = potentialVariableMap.getVariable(opt_name, opt_type); } - } else { - throw new Error('Tried to look up a variable by name without a type'); } return variable; }; diff --git a/core/workspace.js b/core/workspace.js index d4f9f27ae..a1428220d 100644 --- a/core/workspace.js +++ b/core/workspace.js @@ -95,7 +95,7 @@ Blockly.Workspace = function(opt_options) { * @type {!Blockly.VariableMap} * @private */ - this.potentialVariableMap_ = new Blockly.VariableMap(this); + this.potentialVariableMap_ = null; }; /** @@ -197,7 +197,9 @@ Blockly.Workspace.prototype.clear = function() { Blockly.Events.setGroup(false); } this.variableMap_.clear(); - this.potentialVariableMap_.clear(); + if (this.potentialVariableMap_) { + this.potentialVariableMap_.clear(); + } }; /* Begin functions that are just pass-throughs to the variable map. */ @@ -469,7 +471,15 @@ Blockly.Workspace.prototype.allInputsFilled = function(opt_shadowBlocksAreFilled * @package */ Blockly.Workspace.prototype.getPotentialVariableMap = function() { - return this.isFlyout ? this.potentialVariableMap_ : null; + return this.potentialVariableMap_; +}; + +/** + * Create and store the potential variable map for this workspace. + * @package + */ +Blockly.Workspace.prototype.createPotentialVariableMap = function() { + this.potentialVariableMap_ = new Blockly.VariableMap(this); }; /** diff --git a/core/xml.js b/core/xml.js index 4d5a0a65e..fb695a48f 100644 --- a/core/xml.js +++ b/core/xml.js @@ -758,8 +758,8 @@ Blockly.Xml.domToFieldVariable_ = function(workspace, xml, text, field) { type = ''; } - var variable = - Blockly.Variables.getOrCreateVariable(workspace, xml.id, text, type); + var variable = Blockly.Variables.getOrCreateVariablePackage(workspace, xml.id, + text, type); // This should never happen :) if (type != null && type !== variable.type) {