Add procedures to name DB in init

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.
This commit is contained in:
Neil Fraser
2021-05-22 00:01:32 -07:00
committed by Neil Fraser
parent 17f9209f2f
commit 9869269ce2
7 changed files with 47 additions and 2 deletions

View File

@@ -513,8 +513,8 @@ Blockly.Generator.prototype.scrub_ = function(_block, code, _opt_thisOnly) {
Blockly.Generator.prototype.finish = function(code) { Blockly.Generator.prototype.finish = function(code) {
// Optionally override // Optionally override
// Clean up temporary data. // Clean up temporary data.
delete Blockly.JavaScript.definitions_; delete this.definitions_;
delete Blockly.JavaScript.functionNames_; delete this.functionNames_;
return code; return code;
}; };

View File

@@ -10,6 +10,10 @@
*/ */
'use strict'; 'use strict';
/**
* @name Blockly.utils.Metrics
* @namespace
*/
goog.provide('Blockly.utils.Metrics'); goog.provide('Blockly.utils.Metrics');

View File

@@ -109,6 +109,14 @@ Blockly.Dart.init = function(workspace) {
Blockly.VARIABLE_CATEGORY_NAME)); 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. // Declare all of the variables.
if (defvars.length) { if (defvars.length) {
this.definitions_['variables'] = this.definitions_['variables'] =

View File

@@ -152,6 +152,14 @@ Blockly.JavaScript.init = function(workspace) {
Blockly.VARIABLE_CATEGORY_NAME)); 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. // Declare all of the variables.
if (defvars.length) { if (defvars.length) {
this.definitions_['variables'] = 'var ' + defvars.join(', ') + ';'; this.definitions_['variables'] = 'var ' + defvars.join(', ') + ';';

View File

@@ -101,6 +101,15 @@ Blockly.Lua.init = function(workspace) {
this.nameDB_.reset(); this.nameDB_.reset();
} }
this.nameDB_.setVariableMap(workspace.getVariableMap()); 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; this.isInitialized = true;
}; };

View File

@@ -156,6 +156,14 @@ Blockly.PHP.init = function(workspace) {
Blockly.VARIABLE_CATEGORY_NAME) + ';'); 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. // Declare all of the variables.
this.definitions_['variables'] = defvars.join('\n'); this.definitions_['variables'] = defvars.join('\n');
this.isInitialized = true; this.isInitialized = true;

View File

@@ -169,6 +169,14 @@ Blockly.Python.init = function(workspace) {
Blockly.VARIABLE_CATEGORY_NAME) + ' = None'); 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.definitions_['variables'] = defvars.join('\n');
this.isInitialized = true; this.isInitialized = true;
}; };