Files
blockly/generators/php/procedures.js
Christopher Allen f947b3f4f6 refactor!: Remove remaining use of goog.module.declareLegacyNamespace. (#6254)
* fix(build): Minor corrections to build_tasks.js

  - Use TSC_OUTPUT_DIR to find goog/goog.js when suppressing warnings.
  - Remove unnecessary trailing semicolons.

* refactor(blocks): Remove declareLegacyNamespace

  Remove the call to goog.module.declareLegacyNamespace from
  Blockly.libraryBlocks.  This entails:

  - Changes to the UMD wrapper to be able to find the exports object.
  - Changes to tests/bootstrap_helper.js to save the exports object
    in the libraryBlocks global variable.
  - As a precaution, renaming the tests/compile/test_blocks.js module
    so that goog.provide does not touch Blockly or
    Blockly.libraryBlocks, which may not exist / be writable.

  * feat(build): Add support named exports from chunks

  We need to convert the generators to named exports.  For backwards
  compatibility we still want e.g. Blockly.JavaScript to point at
  the generator object when the chunk is loaded using a script tag.

  Modify chunkWrapper to honour a .reexportOnly property in the
  chunks table and generate suitable additional code in the UMD
  wrapper.

* refactor(generators): Migrate JavaScript generator to named export

  - Export the JavaScript generator object as javascriptGenerator
    from the Blockly.JavaScript module(generators/javascript.js).

  - Modify the Blockly.JavaScript.all module
    (generators/javascript/all.js) to reexport the exports from
    Blockly.JavaScript.

  - Update chunk configuration so the generator object remains
    available as Blockly.JavaScript when loading
    javascript_compressed.js via a <script> tag.

    (N.B. it is otherwise necessary to destructure the require
    / import.)

  - Modify bootstrap_helper.js to store that export as
    window.javascriptGenerator for use in test code.

  - Modify test code to use javascriptGenerator instead of
    Blockly.JavaScript.

  - Modify .eslintrc.json so that javascriptGenerator is allowed
    as a global in test/.  (Also restrict use of Blockly global
    to test/.)

  N.B. that demo code in demos/code/code.js uses <script> tag
  loading and so will continue to access Blockly.JavaScript.

* refactor(generators): Migrate Lua generator to named export

* refactor(generators): Migrate PHP generator to named export

* refactor(generators): Migrate Python generator to named export

* refactor(generators): Remove declareLegacyNamespace calls

  Remove the goog.module.declareLegacyNamespace calls from the
  generators.

  This turns out to have the unexpected side-effect of causing the
  compiler to rename the core/blockly.js exports object from
  $.Blockly to just Blockly in blockly_compressed.js - presumably
  because it no longer needs to be accessed in any subsequent chunk
  because they no longer add properties to it.  This requires
  some changes (mainly simplification) to the chunkWrapper function
  in build_tasks.js.

* refactor(core): Remove declareLegacyNamespace from blockly.js

  So easy to do _now_: just need to:

  - Make sure the UMD wrapper for the first chunk knows where the
    exports object is.
  - Use that same value to set the Blockly.VERSION @define.
  - Have bootstrap_helper.js set window.Blockly to the exports
    object.
  - Fix tests/compile/test_blocks.js to not assume a Blockly
    global variable, by converting it to a goog.module so we
    can use a named require.
2022-06-30 19:53:32 +01:00

126 lines
4.2 KiB
JavaScript

/**
* @license
* Copyright 2015 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Generating PHP for procedure blocks.
*/
'use strict';
goog.module('Blockly.PHP.procedures');
const Variables = goog.require('Blockly.Variables');
const {NameType} = goog.require('Blockly.Names');
const {phpGenerator: PHP} = goog.require('Blockly.PHP');
PHP['procedures_defreturn'] = function(block) {
// Define a procedure with a return value.
// First, add a 'global' statement for every variable that is not shadowed by
// a local parameter.
const globals = [];
const workspace = block.workspace;
const usedVariables = Variables.allUsedVarModels(workspace) || [];
for (let i = 0, variable; variable = usedVariables[i]; i++) {
const varName = variable.name;
if (block.getVars().indexOf(varName) === -1) {
globals.push(PHP.nameDB_.getName(varName, NameType.VARIABLE));
}
}
// Add developer variables.
const devVarList = Variables.allDeveloperVariables(workspace);
for (let i = 0; i < devVarList.length; i++) {
globals.push(
PHP.nameDB_.getName(devVarList[i], NameType.DEVELOPER_VARIABLE));
}
const globalStr =
globals.length ? PHP.INDENT + 'global ' + globals.join(', ') + ';\n' : '';
const funcName =
PHP.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE);
let xfix1 = '';
if (PHP.STATEMENT_PREFIX) {
xfix1 += PHP.injectId(PHP.STATEMENT_PREFIX, block);
}
if (PHP.STATEMENT_SUFFIX) {
xfix1 += PHP.injectId(PHP.STATEMENT_SUFFIX, block);
}
if (xfix1) {
xfix1 = PHP.prefixLines(xfix1, PHP.INDENT);
}
let loopTrap = '';
if (PHP.INFINITE_LOOP_TRAP) {
loopTrap = PHP.prefixLines(
PHP.injectId(PHP.INFINITE_LOOP_TRAP, block), PHP.INDENT);
}
const branch = PHP.statementToCode(block, 'STACK');
let returnValue = PHP.valueToCode(block, 'RETURN', PHP.ORDER_NONE) || '';
let xfix2 = '';
if (branch && returnValue) {
// After executing the function body, revisit this block for the return.
xfix2 = xfix1;
}
if (returnValue) {
returnValue = PHP.INDENT + 'return ' + returnValue + ';\n';
}
const args = [];
const variables = block.getVars();
for (let i = 0; i < variables.length; i++) {
args[i] = PHP.nameDB_.getName(variables[i], NameType.VARIABLE);
}
let code = 'function ' + funcName + '(' + args.join(', ') + ') {\n' +
globalStr + xfix1 + loopTrap + branch + xfix2 + returnValue + '}';
code = PHP.scrub_(block, code);
// Add % so as not to collide with helper functions in definitions list.
PHP.definitions_['%' + funcName] = code;
return null;
};
// Defining a procedure without a return value uses the same generator as
// a procedure with a return value.
PHP['procedures_defnoreturn'] = PHP['procedures_defreturn'];
PHP['procedures_callreturn'] = function(block) {
// Call a procedure with a return value.
const funcName =
PHP.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE);
const args = [];
const variables = block.getVars();
for (let i = 0; i < variables.length; i++) {
args[i] = PHP.valueToCode(block, 'ARG' + i, PHP.ORDER_NONE) || 'null';
}
const code = funcName + '(' + args.join(', ') + ')';
return [code, PHP.ORDER_FUNCTION_CALL];
};
PHP['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 = PHP['procedures_callreturn'](block);
return tuple[0] + ';\n';
};
PHP['procedures_ifreturn'] = function(block) {
// Conditionally return value from a procedure.
const condition =
PHP.valueToCode(block, 'CONDITION', PHP.ORDER_NONE) || 'false';
let code = 'if (' + condition + ') {\n';
if (PHP.STATEMENT_SUFFIX) {
// Inject any statement suffix here since the regular one at the end
// will not get executed if the return is triggered.
code +=
PHP.prefixLines(PHP.injectId(PHP.STATEMENT_SUFFIX, block), PHP.INDENT);
}
if (block.hasReturnValue_) {
const value = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || 'null';
code += PHP.INDENT + 'return ' + value + ';\n';
} else {
code += PHP.INDENT + 'return;\n';
}
code += '}\n';
return code;
};