From 9869269ce29ade08948a66adab74a0a86cd882d9 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Sat, 22 May 2021 00:01:32 -0700 Subject: [PATCH] Add procedures to name DB in init MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This enables the generator for any block to see all variable names and procedure names in the whole program, including those that haven’t generated yet. --- core/generator.js | 4 ++-- core/utils/metrics.js | 4 ++++ generators/dart.js | 8 ++++++++ generators/javascript.js | 8 ++++++++ generators/lua.js | 9 +++++++++ generators/php.js | 8 ++++++++ generators/python.js | 8 ++++++++ 7 files changed, 47 insertions(+), 2 deletions(-) diff --git a/core/generator.js b/core/generator.js index 8f91b33b9..f06682271 100644 --- a/core/generator.js +++ b/core/generator.js @@ -513,8 +513,8 @@ Blockly.Generator.prototype.scrub_ = function(_block, code, _opt_thisOnly) { Blockly.Generator.prototype.finish = function(code) { // Optionally override // Clean up temporary data. - delete Blockly.JavaScript.definitions_; - delete Blockly.JavaScript.functionNames_; + delete this.definitions_; + delete this.functionNames_; return code; }; diff --git a/core/utils/metrics.js b/core/utils/metrics.js index dda790f4f..226ef0734 100644 --- a/core/utils/metrics.js +++ b/core/utils/metrics.js @@ -10,6 +10,10 @@ */ 'use strict'; +/** + * @name Blockly.utils.Metrics + * @namespace + */ goog.provide('Blockly.utils.Metrics'); diff --git a/generators/dart.js b/generators/dart.js index 640a11b9b..cc9368a02 100644 --- a/generators/dart.js +++ b/generators/dart.js @@ -109,6 +109,14 @@ Blockly.Dart.init = function(workspace) { Blockly.VARIABLE_CATEGORY_NAME)); } + // Add user procedures. + var procedures = Blockly.Procedures.allProcedures(workspace); + // Flatten the return vs no-return procedure lists. + procedures = procedures[0].concat(procedures[1]); + for (var i = 0; i < procedures.length; i++) { + this.nameDB_.getName(procedures[i][0], Blockly.PROCEDURE_CATEGORY_NAME); + } + // Declare all of the variables. if (defvars.length) { this.definitions_['variables'] = diff --git a/generators/javascript.js b/generators/javascript.js index c15ebd3bc..122171cf5 100644 --- a/generators/javascript.js +++ b/generators/javascript.js @@ -152,6 +152,14 @@ Blockly.JavaScript.init = function(workspace) { Blockly.VARIABLE_CATEGORY_NAME)); } + // Add user procedures. + var procedures = Blockly.Procedures.allProcedures(workspace); + // Flatten the return vs no-return procedure lists. + procedures = procedures[0].concat(procedures[1]); + for (var i = 0; i < procedures.length; i++) { + this.nameDB_.getName(procedures[i][0], Blockly.PROCEDURE_CATEGORY_NAME); + } + // Declare all of the variables. if (defvars.length) { this.definitions_['variables'] = 'var ' + defvars.join(', ') + ';'; diff --git a/generators/lua.js b/generators/lua.js index e204d205b..5033307b1 100644 --- a/generators/lua.js +++ b/generators/lua.js @@ -101,6 +101,15 @@ Blockly.Lua.init = function(workspace) { this.nameDB_.reset(); } this.nameDB_.setVariableMap(workspace.getVariableMap()); + + // Add user procedures. + var procedures = Blockly.Procedures.allProcedures(workspace); + // Flatten the return vs no-return procedure lists. + procedures = procedures[0].concat(procedures[1]); + for (var i = 0; i < procedures.length; i++) { + this.nameDB_.getName(procedures[i][0], Blockly.PROCEDURE_CATEGORY_NAME); + } + this.isInitialized = true; }; diff --git a/generators/php.js b/generators/php.js index 3ee1b0967..8c8703f61 100644 --- a/generators/php.js +++ b/generators/php.js @@ -156,6 +156,14 @@ Blockly.PHP.init = function(workspace) { Blockly.VARIABLE_CATEGORY_NAME) + ';'); } + // Add user procedures. + var procedures = Blockly.Procedures.allProcedures(workspace); + // Flatten the return vs no-return procedure lists. + procedures = procedures[0].concat(procedures[1]); + for (var i = 0; i < procedures.length; i++) { + this.nameDB_.getName(procedures[i][0], Blockly.PROCEDURE_CATEGORY_NAME); + } + // Declare all of the variables. this.definitions_['variables'] = defvars.join('\n'); this.isInitialized = true; diff --git a/generators/python.js b/generators/python.js index 9b22c5e4d..b8125691a 100644 --- a/generators/python.js +++ b/generators/python.js @@ -169,6 +169,14 @@ Blockly.Python.init = function(workspace) { Blockly.VARIABLE_CATEGORY_NAME) + ' = None'); } + // Add user procedures. + var procedures = Blockly.Procedures.allProcedures(workspace); + // Flatten the return vs no-return procedure lists. + procedures = procedures[0].concat(procedures[1]); + for (var i = 0; i < procedures.length; i++) { + this.nameDB_.getName(procedures[i][0], Blockly.PROCEDURE_CATEGORY_NAME); + } + this.definitions_['variables'] = defvars.join('\n'); this.isInitialized = true; };