Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: convert generators/javascript/procedures.js to goog.module #5754

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 45 additions & 48 deletions generators/javascript/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,107 +6,104 @@

/**
* @fileoverview Generating JavaScript for procedure blocks.
* @suppress {missingRequire}
*/
'use strict';

goog.provide('Blockly.JavaScript.procedures');
goog.module('Blockly.JavaScript.procedures');

goog.require('Blockly.JavaScript');
const JavaScript = goog.require('Blockly.JavaScript');
const {NameType} = goog.require('Blockly.Names');


Blockly.JavaScript['procedures_defreturn'] = function(block) {
JavaScript['procedures_defreturn'] = function(block) {
// Define a procedure with a return value.
const funcName = Blockly.JavaScript.nameDB_.getName(
block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME);
const funcName = JavaScript.nameDB_.getName(
block.getFieldValue('NAME'), NameType.PROCEDURE);
let xfix1 = '';
if (Blockly.JavaScript.STATEMENT_PREFIX) {
xfix1 += Blockly.JavaScript.injectId(Blockly.JavaScript.STATEMENT_PREFIX,
block);
if (JavaScript.STATEMENT_PREFIX) {
xfix1 += JavaScript.injectId(JavaScript.STATEMENT_PREFIX, block);
}
if (Blockly.JavaScript.STATEMENT_SUFFIX) {
xfix1 += Blockly.JavaScript.injectId(Blockly.JavaScript.STATEMENT_SUFFIX,
block);
if (JavaScript.STATEMENT_SUFFIX) {
xfix1 += JavaScript.injectId(JavaScript.STATEMENT_SUFFIX, block);
}
if (xfix1) {
xfix1 = Blockly.JavaScript.prefixLines(xfix1, Blockly.JavaScript.INDENT);
xfix1 = JavaScript.prefixLines(xfix1, JavaScript.INDENT);
}
let loopTrap = '';
if (Blockly.JavaScript.INFINITE_LOOP_TRAP) {
loopTrap = Blockly.JavaScript.prefixLines(
Blockly.JavaScript.injectId(Blockly.JavaScript.INFINITE_LOOP_TRAP,
block), Blockly.JavaScript.INDENT);
if (JavaScript.INFINITE_LOOP_TRAP) {
loopTrap = JavaScript.prefixLines(
JavaScript.injectId(JavaScript.INFINITE_LOOP_TRAP, block),
JavaScript.INDENT);
}
const branch = Blockly.JavaScript.statementToCode(block, 'STACK');
let returnValue = Blockly.JavaScript.valueToCode(block, 'RETURN',
Blockly.JavaScript.ORDER_NONE) || '';
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 = Blockly.JavaScript.INDENT + 'return ' + returnValue + ';\n';
returnValue = JavaScript.INDENT + 'return ' + returnValue + ';\n';
}
const args = [];
const variables = block.getVars();
for (let i = 0; i < variables.length; i++) {
args[i] = Blockly.JavaScript.nameDB_.getName(variables[i],
Blockly.VARIABLE_CATEGORY_NAME);
args[i] = JavaScript.nameDB_.getName(variables[i], NameType.VARIABLE);
}
let code = 'function ' + funcName + '(' + args.join(', ') + ') {\n' +
xfix1 + loopTrap + branch + xfix2 + returnValue + '}';
code = Blockly.JavaScript.scrub_(block, code);
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.
Blockly.JavaScript.definitions_['%' + funcName] = code;
JavaScript.definitions_['%' + funcName] = code;
return null;
};

// Defining a procedure without a return value uses the same generator as
// a procedure with a return value.
Blockly.JavaScript['procedures_defnoreturn'] =
Blockly.JavaScript['procedures_defreturn'];
JavaScript['procedures_defnoreturn'] = JavaScript['procedures_defreturn'];

Blockly.JavaScript['procedures_callreturn'] = function(block) {
JavaScript['procedures_callreturn'] = function(block) {
// Call a procedure with a return value.
const funcName = Blockly.JavaScript.nameDB_.getName(
block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME);
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] = Blockly.JavaScript.valueToCode(block, 'ARG' + i,
Blockly.JavaScript.ORDER_NONE) || 'null';
args[i] = JavaScript.valueToCode(block, 'ARG' + i, JavaScript.ORDER_NONE) ||
'null';
}
const code = funcName + '(' + args.join(', ') + ')';
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
return [code, JavaScript.ORDER_FUNCTION_CALL];
};

Blockly.JavaScript['procedures_callnoreturn'] = function(block) {
JavaScript['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 = Blockly.JavaScript['procedures_callreturn'](block);
const tuple = JavaScript['procedures_callreturn'](block);
return tuple[0] + ';\n';
};

Blockly.JavaScript['procedures_ifreturn'] = function(block) {
JavaScript['procedures_ifreturn'] = function(block) {
// Conditionally return value from a procedure.
const condition = Blockly.JavaScript.valueToCode(block, 'CONDITION',
Blockly.JavaScript.ORDER_NONE) || 'false';
const condition =
JavaScript.valueToCode(block, 'CONDITION', JavaScript.ORDER_NONE) ||
'false';
let code = 'if (' + condition + ') {\n';
if (Blockly.JavaScript.STATEMENT_SUFFIX) {
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 += Blockly.JavaScript.prefixLines(
Blockly.JavaScript.injectId(Blockly.JavaScript.STATEMENT_SUFFIX, block),
Blockly.JavaScript.INDENT);
code += JavaScript.prefixLines(
JavaScript.injectId(JavaScript.STATEMENT_SUFFIX, block),
JavaScript.INDENT);
}
if (block.hasReturnValue_) {
const value = Blockly.JavaScript.valueToCode(block, 'VALUE',
Blockly.JavaScript.ORDER_NONE) || 'null';
code += Blockly.JavaScript.INDENT + 'return ' + value + ';\n';
const value =
JavaScript.valueToCode(block, 'VALUE', JavaScript.ORDER_NONE) || 'null';
code += JavaScript.INDENT + 'return ' + value + ';\n';
} else {
code += Blockly.JavaScript.INDENT + 'return;\n';
code += JavaScript.INDENT + 'return;\n';
}
code += '}\n';
return code;
Expand Down
2 changes: 1 addition & 1 deletion tests/deps.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.