From b50cb5779f3b2c915e72feeaee3b98676fda9d0f Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Fri, 21 May 2021 10:17:26 -0700 Subject: [PATCH] =?UTF-8?q?Set=20generator=E2=80=99s=20isInitialized=20to?= =?UTF-8?q?=20false=20in=20finish?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously isInitialized would remain true after first generation. Also move common init/finish code to parent class. --- core/generator.js | 9 +++++++++ generators/dart.js | 17 +++++++---------- generators/javascript.js | 14 ++++++-------- generators/lua.js | 17 +++++++---------- generators/php.js | 17 +++++++---------- 5 files changed, 36 insertions(+), 38 deletions(-) diff --git a/core/generator.js b/core/generator.js index 3cea34416..7b6ed3920 100644 --- a/core/generator.js +++ b/core/generator.js @@ -477,6 +477,12 @@ Blockly.Generator.prototype.provideFunction_ = function(desiredName, code) { */ Blockly.Generator.prototype.init = function(_workspace) { // Optionally override + // Create a dictionary of definitions to be printed before the code. + this.definitions_ = Object.create(null); + // Create a dictionary mapping desired developer-defined function names in + // definitions_ to actual function names (to avoid collisions with + // user-defined procedures). + this.functionNames_ = Object.create(null); }; /** @@ -506,6 +512,9 @@ 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_; return code; }; diff --git a/generators/dart.js b/generators/dart.js index 0f57fc71a..26156f636 100644 --- a/generators/dart.js +++ b/generators/dart.js @@ -83,15 +83,11 @@ Blockly.Dart.isInitialized = false; * @param {!Blockly.Workspace} workspace Workspace to generate code from. */ Blockly.Dart.init = function(workspace) { - // Create a dictionary of definitions to be printed before the code. - Blockly.Dart.definitions_ = Object.create(null); - // Create a dictionary mapping desired function names in definitions_ - // to actual function names (to avoid collisions with user functions). - Blockly.Dart.functionNames_ = Object.create(null); + // Call Blockly.Generator's init. + Object.getPrototypeOf(this).init.call(this); if (!Blockly.Dart.nameDB_) { - Blockly.Dart.nameDB_ = - new Blockly.Names(Blockly.Dart.RESERVED_WORDS_); + Blockly.Dart.nameDB_ = new Blockly.Names(Blockly.Dart.RESERVED_WORDS_); } else { Blockly.Dart.nameDB_.reset(); } @@ -144,9 +140,10 @@ Blockly.Dart.finish = function(code) { definitions.push(def); } } - // Clean up temporary data. - delete Blockly.Dart.definitions_; - delete Blockly.Dart.functionNames_; + // Call Blockly.Generator's finish. + code = Object.getPrototypeOf(this).finish.call(this, code); + this.isInitialized = false; + Blockly.Dart.nameDB_.reset(); var allDefs = imports.join('\n') + '\n\n' + definitions.join('\n\n'); return allDefs.replace(/\n\n+/g, '\n\n').replace(/\n*$/, '\n\n\n') + code; diff --git a/generators/javascript.js b/generators/javascript.js index 26367f774..f1cbe4819 100644 --- a/generators/javascript.js +++ b/generators/javascript.js @@ -125,11 +125,8 @@ Blockly.JavaScript.isInitialized = false; * @param {!Blockly.Workspace} workspace Workspace to generate code from. */ Blockly.JavaScript.init = function(workspace) { - // Create a dictionary of definitions to be printed before the code. - Blockly.JavaScript.definitions_ = Object.create(null); - // Create a dictionary mapping desired function names in definitions_ - // to actual function names (to avoid collisions with user functions). - Blockly.JavaScript.functionNames_ = Object.create(null); + // Call Blockly.Generator's init. + Object.getPrototypeOf(this).init.call(this); if (!Blockly.JavaScript.nameDB_) { Blockly.JavaScript.nameDB_ = @@ -174,9 +171,10 @@ Blockly.JavaScript.finish = function(code) { for (var name in Blockly.JavaScript.definitions_) { definitions.push(Blockly.JavaScript.definitions_[name]); } - // Clean up temporary data. - delete Blockly.JavaScript.definitions_; - delete Blockly.JavaScript.functionNames_; + // Call Blockly.Generator's finish. + code = Object.getPrototypeOf(this).finish.call(this, code); + this.isInitialized = false; + Blockly.JavaScript.nameDB_.reset(); return definitions.join('\n\n') + '\n\n\n' + code; }; diff --git a/generators/lua.js b/generators/lua.js index cf1a1173a..383f1c6bc 100644 --- a/generators/lua.js +++ b/generators/lua.js @@ -91,15 +91,11 @@ Blockly.Lua.isInitialized = false; * @param {!Blockly.Workspace} workspace Workspace to generate code from. */ Blockly.Lua.init = function(workspace) { - // Create a dictionary of definitions to be printed before the code. - Blockly.Lua.definitions_ = Object.create(null); - // Create a dictionary mapping desired function names in definitions_ - // to actual function names (to avoid collisions with user functions). - Blockly.Lua.functionNames_ = Object.create(null); + // Call Blockly.Generator's init. + Object.getPrototypeOf(this).init.call(this); if (!Blockly.Lua.nameDB_) { - Blockly.Lua.nameDB_ = - new Blockly.Names(Blockly.Lua.RESERVED_WORDS_); + Blockly.Lua.nameDB_ = new Blockly.Names(Blockly.Lua.RESERVED_WORDS_); } else { Blockly.Lua.nameDB_.reset(); } @@ -118,9 +114,10 @@ Blockly.Lua.finish = function(code) { for (var name in Blockly.Lua.definitions_) { definitions.push(Blockly.Lua.definitions_[name]); } - // Clean up temporary data. - delete Blockly.Lua.definitions_; - delete Blockly.Lua.functionNames_; + // Call Blockly.Generator's finish. + code = Object.getPrototypeOf(this).finish.call(this, code); + this.isInitialized = false; + Blockly.Lua.nameDB_.reset(); return definitions.join('\n\n') + '\n\n\n' + code; }; diff --git a/generators/php.js b/generators/php.js index 03883f744..2e29f4897 100644 --- a/generators/php.js +++ b/generators/php.js @@ -129,15 +129,11 @@ Blockly.PHP.isInitialized = false; * @param {!Blockly.Workspace} workspace Workspace to generate code from. */ Blockly.PHP.init = function(workspace) { - // Create a dictionary of definitions to be printed before the code. - Blockly.PHP.definitions_ = Object.create(null); - // Create a dictionary mapping desired function names in definitions_ - // to actual function names (to avoid collisions with user functions). - Blockly.PHP.functionNames_ = Object.create(null); + // Call Blockly.Generator's init. + Object.getPrototypeOf(this).init.call(this); if (!Blockly.PHP.nameDB_) { - Blockly.PHP.nameDB_ = - new Blockly.Names(Blockly.PHP.RESERVED_WORDS_, '$'); + Blockly.PHP.nameDB_ = new Blockly.Names(Blockly.PHP.RESERVED_WORDS_, '$'); } else { Blockly.PHP.nameDB_.reset(); } @@ -175,9 +171,10 @@ Blockly.PHP.finish = function(code) { for (var name in Blockly.PHP.definitions_) { definitions.push(Blockly.PHP.definitions_[name]); } - // Clean up temporary data. - delete Blockly.PHP.definitions_; - delete Blockly.PHP.functionNames_; + // Call Blockly.Generator's finish. + code = Object.getPrototypeOf(this).finish.call(this, code); + this.isInitialized = false; + Blockly.PHP.nameDB_.reset(); return definitions.join('\n\n') + '\n\n\n' + code; };