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.
This commit is contained in:
Christopher Allen
2023-06-13 20:41:14 +01:00
committed by GitHub
parent f37380969a
commit f9c865b1b3
48 changed files with 386 additions and 371 deletions

View File

@@ -14,13 +14,13 @@ goog.declareModuleId('Blockly.PHP.colour');
import {phpGenerator as PHP} from '../php.js';
PHP['colour_picker'] = function(block) {
PHP.forBlock['colour_picker'] = function(block) {
// Colour picker.
const code = PHP.quote_(block.getFieldValue('COLOUR'));
return [code, PHP.ORDER_ATOMIC];
};
PHP['colour_random'] = function(block) {
PHP.forBlock['colour_random'] = function(block) {
// Generate a random colour.
const functionName = PHP.provideFunction_('colour_random', `
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}() {
@@ -31,7 +31,7 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}() {
return [code, PHP.ORDER_FUNCTION_CALL];
};
PHP['colour_rgb'] = function(block) {
PHP.forBlock['colour_rgb'] = function(block) {
// Compose a colour from RGB components expressed as percentages.
const red = PHP.valueToCode(block, 'RED', PHP.ORDER_NONE) || 0;
const green = PHP.valueToCode(block, 'GREEN', PHP.ORDER_NONE) || 0;
@@ -52,7 +52,7 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($r, $g, $b) {
return [code, PHP.ORDER_FUNCTION_CALL];
};
PHP['colour_blend'] = function(block) {
PHP.forBlock['colour_blend'] = function(block) {
// Blend two colours together.
const c1 = PHP.valueToCode(block, 'COLOUR1', PHP.ORDER_NONE) || "'#000000'";
const c2 = PHP.valueToCode(block, 'COLOUR2', PHP.ORDER_NONE) || "'#000000'";

View File

@@ -26,12 +26,12 @@ import * as stringUtils from '../../core/utils/string.js';
import {NameType} from '../../core/names.js';
import {phpGenerator as PHP} from '../php.js';
PHP['lists_create_empty'] = function(block) {
PHP.forBlock['lists_create_empty'] = function(block) {
// Create an empty list.
return ['array()', PHP.ORDER_FUNCTION_CALL];
};
PHP['lists_create_with'] = function(block) {
PHP.forBlock['lists_create_with'] = function(block) {
// Create a list with any number of elements of any type.
let code = new Array(block.itemCount_);
for (let i = 0; i < block.itemCount_; i++) {
@@ -41,7 +41,7 @@ PHP['lists_create_with'] = function(block) {
return [code, PHP.ORDER_FUNCTION_CALL];
};
PHP['lists_repeat'] = function(block) {
PHP.forBlock['lists_repeat'] = function(block) {
// Create a list with one element repeated.
const functionName = PHP.provideFunction_('lists_repeat', `
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($value, $count) {
@@ -58,7 +58,7 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($value, $count) {
return [code, PHP.ORDER_FUNCTION_CALL];
};
PHP['lists_length'] = function(block) {
PHP.forBlock['lists_length'] = function(block) {
// String or array length.
const functionName = PHP.provideFunction_('length', `
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($value) {
@@ -73,14 +73,14 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($value) {
return [functionName + '(' + list + ')', PHP.ORDER_FUNCTION_CALL];
};
PHP['lists_isEmpty'] = function(block) {
PHP.forBlock['lists_isEmpty'] = function(block) {
// Is the string null or array empty?
const argument0 =
PHP.valueToCode(block, 'VALUE', PHP.ORDER_FUNCTION_CALL) || 'array()';
return ['empty(' + argument0 + ')', PHP.ORDER_FUNCTION_CALL];
};
PHP['lists_indexOf'] = function(block) {
PHP.forBlock['lists_indexOf'] = function(block) {
// Find an item in the list.
const argument0 = PHP.valueToCode(block, 'FIND', PHP.ORDER_NONE) || "''";
const argument1 = PHP.valueToCode(block, 'VALUE', PHP.ORDER_MEMBER) || '[]';
@@ -118,7 +118,7 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($haystack, $needle) {
return [code, PHP.ORDER_FUNCTION_CALL];
};
PHP['lists_getIndex'] = function(block) {
PHP.forBlock['lists_getIndex'] = function(block) {
// Get element at index.
const mode = block.getFieldValue('MODE') || 'GET';
const where = block.getFieldValue('WHERE') || 'FROM_START';
@@ -232,7 +232,7 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}(&$list) {
throw Error('Unhandled combination (lists_getIndex).');
};
PHP['lists_setIndex'] = function(block) {
PHP.forBlock['lists_setIndex'] = function(block) {
// Set element at index.
// Note: Until February 2013 this block did not have MODE or WHERE inputs.
const mode = block.getFieldValue('MODE') || 'GET';
@@ -328,7 +328,7 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}(&$list, $at, $value) {
throw Error('Unhandled combination (lists_setIndex).');
};
PHP['lists_getSublist'] = function(block) {
PHP.forBlock['lists_getSublist'] = function(block) {
// Get sublist.
const list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || 'array()';
const where1 = block.getFieldValue('WHERE1');
@@ -424,7 +424,7 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($list, $where1, $at1, $where2, $at2)
return [code, PHP.ORDER_FUNCTION_CALL];
};
PHP['lists_sort'] = function(block) {
PHP.forBlock['lists_sort'] = function(block) {
// Block for sorting a list.
const listCode = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || 'array()';
const direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1;
@@ -450,7 +450,7 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($list, $type, $direction) {
return [sortCode, PHP.ORDER_FUNCTION_CALL];
};
PHP['lists_split'] = function(block) {
PHP.forBlock['lists_split'] = function(block) {
// Block for splitting text into a list, or joining a list into text.
let value_input = PHP.valueToCode(block, 'INPUT', PHP.ORDER_NONE);
const value_delim = PHP.valueToCode(block, 'DELIM', PHP.ORDER_NONE) || "''";
@@ -473,7 +473,7 @@ PHP['lists_split'] = function(block) {
return [code, PHP.ORDER_FUNCTION_CALL];
};
PHP['lists_reverse'] = function(block) {
PHP.forBlock['lists_reverse'] = function(block) {
// Block for reversing a list.
const list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || '[]';
const code = 'array_reverse(' + list + ')';

View File

@@ -14,7 +14,7 @@ goog.declareModuleId('Blockly.PHP.logic');
import {phpGenerator as PHP} from '../php.js';
PHP['controls_if'] = function(block) {
PHP.forBlock['controls_if'] = function(block) {
// If/elseif/else condition.
let n = 0;
let code = '', branchCode, conditionCode;
@@ -47,9 +47,9 @@ PHP['controls_if'] = function(block) {
return code + '\n';
};
PHP['controls_ifelse'] = PHP['controls_if'];
PHP.forBlock['controls_ifelse'] = PHP.forBlock['controls_if'];
PHP['logic_compare'] = function(block) {
PHP.forBlock['logic_compare'] = function(block) {
// Comparison operator.
const OPERATORS =
{'EQ': '==', 'NEQ': '!=', 'LT': '<', 'LTE': '<=', 'GT': '>', 'GTE': '>='};
@@ -62,7 +62,7 @@ PHP['logic_compare'] = function(block) {
return [code, order];
};
PHP['logic_operation'] = function(block) {
PHP.forBlock['logic_operation'] = function(block) {
// Operations 'and', 'or'.
const operator = (block.getFieldValue('OP') === 'AND') ? '&&' : '||';
const order =
@@ -87,7 +87,7 @@ PHP['logic_operation'] = function(block) {
return [code, order];
};
PHP['logic_negate'] = function(block) {
PHP.forBlock['logic_negate'] = function(block) {
// Negation.
const order = PHP.ORDER_LOGICAL_NOT;
const argument0 = PHP.valueToCode(block, 'BOOL', order) || 'true';
@@ -95,18 +95,18 @@ PHP['logic_negate'] = function(block) {
return [code, order];
};
PHP['logic_boolean'] = function(block) {
PHP.forBlock['logic_boolean'] = function(block) {
// Boolean values true and false.
const code = (block.getFieldValue('BOOL') === 'TRUE') ? 'true' : 'false';
return [code, PHP.ORDER_ATOMIC];
};
PHP['logic_null'] = function(block) {
PHP.forBlock['logic_null'] = function(block) {
// Null data type.
return ['null', PHP.ORDER_ATOMIC];
};
PHP['logic_ternary'] = function(block) {
PHP.forBlock['logic_ternary'] = function(block) {
// Ternary operator.
const value_if =
PHP.valueToCode(block, 'IF', PHP.ORDER_CONDITIONAL) || 'false';

View File

@@ -16,7 +16,7 @@ import {NameType} from '../../core/names.js';
import {phpGenerator as PHP} from '../php.js';
PHP['controls_repeat_ext'] = function(block) {
PHP.forBlock['controls_repeat_ext'] = function(block) {
// Repeat n times.
let repeats;
if (block.getField('TIMES')) {
@@ -40,9 +40,9 @@ PHP['controls_repeat_ext'] = function(block) {
return code;
};
PHP['controls_repeat'] = PHP['controls_repeat_ext'];
PHP.forBlock['controls_repeat'] = PHP.forBlock['controls_repeat_ext'];
PHP['controls_whileUntil'] = function(block) {
PHP.forBlock['controls_whileUntil'] = function(block) {
// Do while/until loop.
const until = block.getFieldValue('MODE') === 'UNTIL';
let argument0 =
@@ -57,7 +57,7 @@ PHP['controls_whileUntil'] = function(block) {
return 'while (' + argument0 + ') {\n' + branch + '}\n';
};
PHP['controls_for'] = function(block) {
PHP.forBlock['controls_for'] = function(block) {
// For loop.
const variable0 =
PHP.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE);
@@ -116,7 +116,7 @@ PHP['controls_for'] = function(block) {
return code;
};
PHP['controls_forEach'] = function(block) {
PHP.forBlock['controls_forEach'] = function(block) {
// For each loop.
const variable0 =
PHP.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE);
@@ -130,7 +130,7 @@ PHP['controls_forEach'] = function(block) {
return code;
};
PHP['controls_flow_statements'] = function(block) {
PHP.forBlock['controls_flow_statements'] = function(block) {
// Flow statements: continue, break.
let xfix = '';
if (PHP.STATEMENT_PREFIX) {

View File

@@ -15,7 +15,7 @@ import {NameType} from '../../core/names.js';
import {phpGenerator as PHP} from '../php.js';
PHP['math_number'] = function(block) {
PHP.forBlock['math_number'] = function(block) {
// Numeric value.
let code = Number(block.getFieldValue('NUM'));
const order = code >= 0 ? PHP.ORDER_ATOMIC : PHP.ORDER_UNARY_NEGATION;
@@ -27,7 +27,7 @@ PHP['math_number'] = function(block) {
return [code, order];
};
PHP['math_arithmetic'] = function(block) {
PHP.forBlock['math_arithmetic'] = function(block) {
// Basic arithmetic operators, and power.
const OPERATORS = {
'ADD': [' + ', PHP.ORDER_ADDITION],
@@ -45,7 +45,7 @@ PHP['math_arithmetic'] = function(block) {
return [code, order];
};
PHP['math_single'] = function(block) {
PHP.forBlock['math_single'] = function(block) {
// Math operators with single operand.
const operator = block.getFieldValue('OP');
let code;
@@ -126,7 +126,7 @@ PHP['math_single'] = function(block) {
return [code, PHP.ORDER_DIVISION];
};
PHP['math_constant'] = function(block) {
PHP.forBlock['math_constant'] = function(block) {
// Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY.
const CONSTANTS = {
'PI': ['M_PI', PHP.ORDER_ATOMIC],
@@ -139,7 +139,7 @@ PHP['math_constant'] = function(block) {
return CONSTANTS[block.getFieldValue('CONSTANT')];
};
PHP['math_number_property'] = function(block) {
PHP.forBlock['math_number_property'] = function(block) {
// Check if a number is even, odd, prime, whole, positive, or negative
// or if it is divisible by certain number. Returns true or false.
const PROPERTIES = {
@@ -193,7 +193,7 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($n) {
return [code, outputOrder];
};
PHP['math_change'] = function(block) {
PHP.forBlock['math_change'] = function(block) {
// Add to a variable in place.
const argument0 = PHP.valueToCode(block, 'DELTA', PHP.ORDER_ADDITION) || '0';
const varName =
@@ -202,11 +202,11 @@ PHP['math_change'] = function(block) {
};
// Rounding functions have a single operand.
PHP['math_round'] = PHP['math_single'];
PHP.forBlock['math_round'] = PHP.forBlock['math_single'];
// Trigonometry functions have a single operand.
PHP['math_trig'] = PHP['math_single'];
PHP.forBlock['math_trig'] = PHP.forBlock['math_single'];
PHP['math_on_list'] = function(block) {
PHP.forBlock['math_on_list'] = function(block) {
// Math functions for lists.
const func = block.getFieldValue('OP');
let list;
@@ -297,7 +297,7 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($list) {
return [code, PHP.ORDER_FUNCTION_CALL];
};
PHP['math_modulo'] = function(block) {
PHP.forBlock['math_modulo'] = function(block) {
// Remainder computation.
const argument0 =
PHP.valueToCode(block, 'DIVIDEND', PHP.ORDER_MODULUS) || '0';
@@ -306,7 +306,7 @@ PHP['math_modulo'] = function(block) {
return [code, PHP.ORDER_MODULUS];
};
PHP['math_constrain'] = function(block) {
PHP.forBlock['math_constrain'] = function(block) {
// Constrain a number between two limits.
const argument0 = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || '0';
const argument1 = PHP.valueToCode(block, 'LOW', PHP.ORDER_NONE) || '0';
@@ -317,7 +317,7 @@ PHP['math_constrain'] = function(block) {
return [code, PHP.ORDER_FUNCTION_CALL];
};
PHP['math_random_int'] = function(block) {
PHP.forBlock['math_random_int'] = function(block) {
// Random integer between [X] and [Y].
const argument0 = PHP.valueToCode(block, 'FROM', PHP.ORDER_NONE) || '0';
const argument1 = PHP.valueToCode(block, 'TO', PHP.ORDER_NONE) || '0';
@@ -333,12 +333,12 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($a, $b) {
return [code, PHP.ORDER_FUNCTION_CALL];
};
PHP['math_random_float'] = function(block) {
PHP.forBlock['math_random_float'] = function(block) {
// Random fraction between 0 and 1.
return ['(float)rand()/(float)getrandmax()', PHP.ORDER_FUNCTION_CALL];
};
PHP['math_atan2'] = function(block) {
PHP.forBlock['math_atan2'] = function(block) {
// Arctangent of point (X, Y) in degrees from -180 to 180.
const argument0 = PHP.valueToCode(block, 'X', PHP.ORDER_NONE) || '0';
const argument1 = PHP.valueToCode(block, 'Y', PHP.ORDER_NONE) || '0';

View File

@@ -16,7 +16,7 @@ import {NameType} from '../../core/names.js';
import {phpGenerator as PHP} from '../php.js';
PHP['procedures_defreturn'] = function(block) {
PHP.forBlock['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.
@@ -80,9 +80,9 @@ PHP['procedures_defreturn'] = function(block) {
// 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.forBlock['procedures_defnoreturn'] = PHP.forBlock['procedures_defreturn'];
PHP['procedures_callreturn'] = function(block) {
PHP.forBlock['procedures_callreturn'] = function(block) {
// Call a procedure with a return value.
const funcName =
PHP.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE);
@@ -95,15 +95,15 @@ PHP['procedures_callreturn'] = function(block) {
return [code, PHP.ORDER_FUNCTION_CALL];
};
PHP['procedures_callnoreturn'] = function(block) {
PHP.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 = PHP['procedures_callreturn'](block);
const tuple = PHP.forBlock['procedures_callreturn'](block);
return tuple[0] + ';\n';
};
PHP['procedures_ifreturn'] = function(block) {
PHP.forBlock['procedures_ifreturn'] = function(block) {
// Conditionally return value from a procedure.
const condition =
PHP.valueToCode(block, 'CONDITION', PHP.ORDER_NONE) || 'false';

View File

@@ -15,13 +15,13 @@ import {NameType} from '../../core/names.js';
import {phpGenerator as PHP} from '../php.js';
PHP['text'] = function(block) {
PHP.forBlock['text'] = function(block) {
// Text value.
const code = PHP.quote_(block.getFieldValue('TEXT'));
return [code, PHP.ORDER_ATOMIC];
};
PHP['text_multiline'] = function(block) {
PHP.forBlock['text_multiline'] = function(block) {
// Text value.
const code = PHP.multiline_quote_(block.getFieldValue('TEXT'));
const order =
@@ -29,7 +29,7 @@ PHP['text_multiline'] = function(block) {
return [code, order];
};
PHP['text_join'] = function(block) {
PHP.forBlock['text_join'] = function(block) {
// Create a string made up of any number of elements of any type.
if (block.itemCount_ === 0) {
return ["''", PHP.ORDER_ATOMIC];
@@ -54,7 +54,7 @@ PHP['text_join'] = function(block) {
}
};
PHP['text_append'] = function(block) {
PHP.forBlock['text_append'] = function(block) {
// Append to a variable in place.
const varName =
PHP.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE);
@@ -62,7 +62,7 @@ PHP['text_append'] = function(block) {
return varName + ' .= ' + value + ';\n';
};
PHP['text_length'] = function(block) {
PHP.forBlock['text_length'] = function(block) {
// String or array length.
const functionName = PHP.provideFunction_('length', `
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($value) {
@@ -76,13 +76,13 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($value) {
return [functionName + '(' + text + ')', PHP.ORDER_FUNCTION_CALL];
};
PHP['text_isEmpty'] = function(block) {
PHP.forBlock['text_isEmpty'] = function(block) {
// Is the string null or array empty?
const text = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || "''";
return ['empty(' + text + ')', PHP.ORDER_FUNCTION_CALL];
};
PHP['text_indexOf'] = function(block) {
PHP.forBlock['text_indexOf'] = function(block) {
// Search the text for a substring.
const operator =
block.getFieldValue('END') === 'FIRST' ? 'strpos' : 'strrpos';
@@ -107,7 +107,7 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($text, $search) {
return [code, PHP.ORDER_FUNCTION_CALL];
};
PHP['text_charAt'] = function(block) {
PHP.forBlock['text_charAt'] = function(block) {
// Get letter at index.
const where = block.getFieldValue('WHERE') || 'FROM_START';
const textOrder = (where === 'RANDOM') ? PHP.ORDER_NONE : PHP.ORDER_NONE;
@@ -144,7 +144,7 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($text) {
throw Error('Unhandled option (text_charAt).');
};
PHP['text_getSubstring'] = function(block) {
PHP.forBlock['text_getSubstring'] = function(block) {
// Get substring.
const where1 = block.getFieldValue('WHERE1');
const where2 = block.getFieldValue('WHERE2');
@@ -183,7 +183,7 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($text, $where1, $at1, $where2, $at2)
}
};
PHP['text_changeCase'] = function(block) {
PHP.forBlock['text_changeCase'] = function(block) {
// Change capitalization.
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
let code;
@@ -197,7 +197,7 @@ PHP['text_changeCase'] = function(block) {
return [code, PHP.ORDER_FUNCTION_CALL];
};
PHP['text_trim'] = function(block) {
PHP.forBlock['text_trim'] = function(block) {
// Trim spaces.
const OPERATORS = {'LEFT': 'ltrim', 'RIGHT': 'rtrim', 'BOTH': 'trim'};
const operator = OPERATORS[block.getFieldValue('MODE')];
@@ -205,13 +205,13 @@ PHP['text_trim'] = function(block) {
return [operator + '(' + text + ')', PHP.ORDER_FUNCTION_CALL];
};
PHP['text_print'] = function(block) {
PHP.forBlock['text_print'] = function(block) {
// Print statement.
const msg = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
return 'print(' + msg + ');\n';
};
PHP['text_prompt_ext'] = function(block) {
PHP.forBlock['text_prompt_ext'] = function(block) {
// Prompt function.
let msg;
if (block.getField('TEXT')) {
@@ -229,9 +229,9 @@ PHP['text_prompt_ext'] = function(block) {
return [code, PHP.ORDER_FUNCTION_CALL];
};
PHP['text_prompt'] = PHP['text_prompt_ext'];
PHP.forBlock['text_prompt'] = PHP.forBlock['text_prompt_ext'];
PHP['text_count'] = function(block) {
PHP.forBlock['text_count'] = function(block) {
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
const sub = PHP.valueToCode(block, 'SUB', PHP.ORDER_NONE) || "''";
const code = 'strlen(' + sub + ') === 0' +
@@ -240,7 +240,7 @@ PHP['text_count'] = function(block) {
return [code, PHP.ORDER_CONDITIONAL];
};
PHP['text_replace'] = function(block) {
PHP.forBlock['text_replace'] = function(block) {
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
const from = PHP.valueToCode(block, 'FROM', PHP.ORDER_NONE) || "''";
const to = PHP.valueToCode(block, 'TO', PHP.ORDER_NONE) || "''";
@@ -248,7 +248,7 @@ PHP['text_replace'] = function(block) {
return [code, PHP.ORDER_FUNCTION_CALL];
};
PHP['text_reverse'] = function(block) {
PHP.forBlock['text_reverse'] = function(block) {
const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || "''";
const code = 'strrev(' + text + ')';
return [code, PHP.ORDER_FUNCTION_CALL];

View File

@@ -15,14 +15,14 @@ import {NameType} from '../../core/names.js';
import {phpGenerator as PHP} from '../php.js';
PHP['variables_get'] = function(block) {
PHP.forBlock['variables_get'] = function(block) {
// Variable getter.
const code =
PHP.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE);
return [code, PHP.ORDER_ATOMIC];
};
PHP['variables_set'] = function(block) {
PHP.forBlock['variables_set'] = function(block) {
// Variable setter.
const argument0 =
PHP.valueToCode(block, 'VALUE', PHP.ORDER_ASSIGNMENT) || '0';

View File

@@ -16,5 +16,5 @@ import './variables.js';
// PHP is dynamically typed.
PHP['variables_get_dynamic'] = PHP['variables_get'];
PHP['variables_set_dynamic'] = PHP['variables_set'];
PHP.forBlock['variables_get_dynamic'] = PHP.forBlock['variables_get'];
PHP.forBlock['variables_set_dynamic'] = PHP.forBlock['variables_set'];