mirror of
https://github.com/google/blockly.git
synced 2025-12-16 14:20:10 +01:00
* 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.
130 lines
4.4 KiB
JavaScript
130 lines
4.4 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2012 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Generating Python for procedure blocks.
|
|
*/
|
|
'use strict';
|
|
|
|
goog.module('Blockly.Python.procedures');
|
|
|
|
const Variables = goog.require('Blockly.Variables');
|
|
const {NameType} = goog.require('Blockly.Names');
|
|
const {pythonGenerator: Python} = goog.require('Blockly.Python');
|
|
|
|
|
|
Python['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(Python.nameDB_.getName(varName, NameType.VARIABLE));
|
|
}
|
|
}
|
|
// Add developer variables.
|
|
const devVarList = Variables.allDeveloperVariables(workspace);
|
|
for (let i = 0; i < devVarList.length; i++) {
|
|
globals.push(
|
|
Python.nameDB_.getName(devVarList[i], NameType.DEVELOPER_VARIABLE));
|
|
}
|
|
|
|
const globalString = globals.length ?
|
|
Python.INDENT + 'global ' + globals.join(', ') + '\n' :
|
|
'';
|
|
const funcName =
|
|
Python.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE);
|
|
let xfix1 = '';
|
|
if (Python.STATEMENT_PREFIX) {
|
|
xfix1 += Python.injectId(Python.STATEMENT_PREFIX, block);
|
|
}
|
|
if (Python.STATEMENT_SUFFIX) {
|
|
xfix1 += Python.injectId(Python.STATEMENT_SUFFIX, block);
|
|
}
|
|
if (xfix1) {
|
|
xfix1 = Python.prefixLines(xfix1, Python.INDENT);
|
|
}
|
|
let loopTrap = '';
|
|
if (Python.INFINITE_LOOP_TRAP) {
|
|
loopTrap = Python.prefixLines(
|
|
Python.injectId(Python.INFINITE_LOOP_TRAP, block), Python.INDENT);
|
|
}
|
|
let branch = Python.statementToCode(block, 'STACK');
|
|
let returnValue =
|
|
Python.valueToCode(block, 'RETURN', Python.ORDER_NONE) || '';
|
|
let xfix2 = '';
|
|
if (branch && returnValue) {
|
|
// After executing the function body, revisit this block for the return.
|
|
xfix2 = xfix1;
|
|
}
|
|
if (returnValue) {
|
|
returnValue = Python.INDENT + 'return ' + returnValue + '\n';
|
|
} else if (!branch) {
|
|
branch = Python.PASS;
|
|
}
|
|
const args = [];
|
|
const variables = block.getVars();
|
|
for (let i = 0; i < variables.length; i++) {
|
|
args[i] = Python.nameDB_.getName(variables[i], NameType.VARIABLE);
|
|
}
|
|
let code = 'def ' + funcName + '(' + args.join(', ') + '):\n' + globalString +
|
|
xfix1 + loopTrap + branch + xfix2 + returnValue;
|
|
code = Python.scrub_(block, code);
|
|
// Add % so as not to collide with helper functions in definitions list.
|
|
Python.definitions_['%' + funcName] = code;
|
|
return null;
|
|
};
|
|
|
|
// Defining a procedure without a return value uses the same generator as
|
|
// a procedure with a return value.
|
|
Python['procedures_defnoreturn'] = Python['procedures_defreturn'];
|
|
|
|
Python['procedures_callreturn'] = function(block) {
|
|
// Call a procedure with a return value.
|
|
const funcName =
|
|
Python.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE);
|
|
const args = [];
|
|
const variables = block.getVars();
|
|
for (let i = 0; i < variables.length; i++) {
|
|
args[i] = Python.valueToCode(block, 'ARG' + i, Python.ORDER_NONE) || 'None';
|
|
}
|
|
const code = funcName + '(' + args.join(', ') + ')';
|
|
return [code, Python.ORDER_FUNCTION_CALL];
|
|
};
|
|
|
|
Python['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 = Python['procedures_callreturn'](block);
|
|
return tuple[0] + '\n';
|
|
};
|
|
|
|
Python['procedures_ifreturn'] = function(block) {
|
|
// Conditionally return value from a procedure.
|
|
const condition =
|
|
Python.valueToCode(block, 'CONDITION', Python.ORDER_NONE) || 'False';
|
|
let code = 'if ' + condition + ':\n';
|
|
if (Python.STATEMENT_SUFFIX) {
|
|
// Inject any statement suffix here since the regular one at the end
|
|
// will not get executed if the return is triggered.
|
|
code += Python.prefixLines(
|
|
Python.injectId(Python.STATEMENT_SUFFIX, block), Python.INDENT);
|
|
}
|
|
if (block.hasReturnValue_) {
|
|
const value =
|
|
Python.valueToCode(block, 'VALUE', Python.ORDER_NONE) || 'None';
|
|
code += Python.INDENT + 'return ' + value + '\n';
|
|
} else {
|
|
code += Python.INDENT + 'return\n';
|
|
}
|
|
return code;
|
|
};
|