Files
blockly/generators/javascript/procedures.js
Christopher Allen f9c865b1b3 refactor(generators)!: CodeGenerator per-block-type generator function dictionary (#7150)
* feat(generators): Add block generator function dictionary

  Add a dictionary of block generator functions, provisionally
  called .forBlock.  Look up generator functions there first, but
  fall back to looking up on 'this' (with deprecation notice)
  for backwards compatibility.

  Also tweak error message generation to use template literal.

* refactor(generators)!: Update generator definitions to use dictionary

* fix(tests): Have blockToCodeTest clean up after itself

  Have the blockToCodeTest helper function delete the block generator
  functions it adds to generator once the test is done.

* refactor(tests): Use generator dictionary in insertion marker test

  The use of generators in insertion_marker_test.js was overlooked
  in the earlier commit making such updates, and some test here
  were failing due to lack of cleanup in
  cleanup in the generator_test.js.

BREAKING CHANGE: this PR moves the generator functions we provide
from their previous location directly on the CodeGenerator instances
to the new .forBlock dictionary on each instance. This does not oblige
external developers to do the same for their custom generators, but
they will need to update any code that references the generator
functions we provide (in generators/*/*, i.e. on javascriptGenerator,
dartGenerator etc.) e.g. to replace the implementation or reuse the
implementation for a different block type.
2023-06-13 20:41:14 +01:00

111 lines
3.9 KiB
JavaScript

/**
* @license
* Copyright 2012 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Generating JavaScript for procedure blocks.
*/
import * as goog from '../../closure/goog/goog.js';
goog.declareModuleId('Blockly.JavaScript.procedures');
import {NameType} from '../../core/names.js';
import {javascriptGenerator as JavaScript} from '../javascript.js';
JavaScript.forBlock['procedures_defreturn'] = function(block) {
// Define a procedure with a return value.
const funcName = JavaScript.nameDB_.getName(
block.getFieldValue('NAME'), NameType.PROCEDURE);
let xfix1 = '';
if (JavaScript.STATEMENT_PREFIX) {
xfix1 += JavaScript.injectId(JavaScript.STATEMENT_PREFIX, block);
}
if (JavaScript.STATEMENT_SUFFIX) {
xfix1 += JavaScript.injectId(JavaScript.STATEMENT_SUFFIX, block);
}
if (xfix1) {
xfix1 = JavaScript.prefixLines(xfix1, JavaScript.INDENT);
}
let loopTrap = '';
if (JavaScript.INFINITE_LOOP_TRAP) {
loopTrap = JavaScript.prefixLines(
JavaScript.injectId(JavaScript.INFINITE_LOOP_TRAP, block),
JavaScript.INDENT);
}
const branch = JavaScript.statementToCode(block, 'STACK');
let returnValue =
JavaScript.valueToCode(block, 'RETURN', JavaScript.ORDER_NONE) || '';
let xfix2 = '';
if (branch && returnValue) {
// After executing the function body, revisit this block for the return.
xfix2 = xfix1;
}
if (returnValue) {
returnValue = JavaScript.INDENT + 'return ' + returnValue + ';\n';
}
const args = [];
const variables = block.getVars();
for (let i = 0; i < variables.length; i++) {
args[i] = JavaScript.nameDB_.getName(variables[i], NameType.VARIABLE);
}
let code = 'function ' + funcName + '(' + args.join(', ') + ') {\n' + xfix1 +
loopTrap + branch + xfix2 + returnValue + '}';
code = JavaScript.scrub_(block, code);
// Add % so as not to collide with helper functions in definitions list.
JavaScript.definitions_['%' + funcName] = code;
return null;
};
// Defining a procedure without a return value uses the same generator as
// a procedure with a return value.
JavaScript.forBlock['procedures_defnoreturn'] = JavaScript.forBlock['procedures_defreturn'];
JavaScript.forBlock['procedures_callreturn'] = function(block) {
// Call a procedure with a return value.
const funcName = JavaScript.nameDB_.getName(
block.getFieldValue('NAME'), NameType.PROCEDURE);
const args = [];
const variables = block.getVars();
for (let i = 0; i < variables.length; i++) {
args[i] = JavaScript.valueToCode(block, 'ARG' + i, JavaScript.ORDER_NONE) ||
'null';
}
const code = funcName + '(' + args.join(', ') + ')';
return [code, JavaScript.ORDER_FUNCTION_CALL];
};
JavaScript.forBlock['procedures_callnoreturn'] = function(block) {
// Call a procedure with no return value.
// Generated code is for a function call as a statement is the same as a
// function call as a value, with the addition of line ending.
const tuple = JavaScript.forBlock['procedures_callreturn'](block);
return tuple[0] + ';\n';
};
JavaScript.forBlock['procedures_ifreturn'] = function(block) {
// Conditionally return value from a procedure.
const condition =
JavaScript.valueToCode(block, 'CONDITION', JavaScript.ORDER_NONE) ||
'false';
let code = 'if (' + condition + ') {\n';
if (JavaScript.STATEMENT_SUFFIX) {
// Inject any statement suffix here since the regular one at the end
// will not get executed if the return is triggered.
code += JavaScript.prefixLines(
JavaScript.injectId(JavaScript.STATEMENT_SUFFIX, block),
JavaScript.INDENT);
}
if (block.hasReturnValue_) {
const value =
JavaScript.valueToCode(block, 'VALUE', JavaScript.ORDER_NONE) || 'null';
code += JavaScript.INDENT + 'return ' + value + ';\n';
} else {
code += JavaScript.INDENT + 'return;\n';
}
code += '}\n';
return code;
};