mirror of
https://github.com/google/blockly.git
synced 2026-01-10 02:17:09 +01:00
* chore(build): Add "all" modules for blocks & generators
These modules (Blockly.blocks.all and Blockly.<Generator>.all) will
be the entry points for the corresponding chunks.
They also make it easier to pull in all the modules in each package
(e.g. for playground and tests).
It is necessary to set the Closure Compiler dependency_mode to
SORT_ONLY as otherwise it tries to compile the "all" modules before
their dependencies, which fails.
The only impact on the _compressed.js files is the addition of a short
string to the very end of each file, e.g.:
var module$exports$Blockly$JavaScript$all={};
* chore(deps): Add devDependency on closure-calculate-chunks
* feat(build): First pass at chunked complation
Add a new buildCompiled gulp target (npm run build:compiled) that
uses closure-calculate-chunks to do chunked compliation of core/,
blocks/ and generators/ all in a single pass.
This work is incomplete: the resulting *_compressed.js files don't
(yet) have UMD wrappers.
* chore(build): Generate chunk wrappers
A first pass; this does not have support for a namespace object yet.
* refactor(build): Use chunked compilation by default
Remove old "compressed" gulp tasks in favour of new "compiled" task.
* chore(build): Remove cruft from buildCompiled
Remove unneeded `done` parameter and commented-out options that had
been cargo-culted from the old build pipeline.
* fix(build): Fix test failures caused by new build pipeline
- Exclude closure/goog/base.js from compiler input; use
externs/goog-externs.js instead.
- Have the build:debug and build:strict targets only build the first
chunk (blockly_compressed.js).
- Fix namespace entries for blocks and generators.
* fix(build): Fix build failures on node v12
closure-calculate-chunks requires node.js v14 or later.
When running on node.js v14 or later have getChunkOptions save
the output of closure-calculate-chunks to
scripts/gulpfiles/chunks.json. When running on older versions of
node.js have it use this checked-in, cached output instead of
attempting to run closure-calculate-chunks.
* chore(build): enable --rename_prefix_namespace
This will allow modules in blocks/ and generators/ to use
goog.require to obtain the exports object of goog.modules from
core/.
* fix(build): Always build all chunks
The previous commit enabled --rename_prefix_namespace option to
Closure Compiler, and this causes the buildCompressed target to
work fine when run without --debug or --strict, but adding either
of those flags (as for example when `npm test` runs
`npm run build:debug`) causes an issue:
- Because of many compiler errors in blocks/ and generators/,
a previous commit added a hack to only build the first chunk
when doing debug/strict builds.
- When asked to build only one chunk, Closure Compiler ignores the
--rename_prefix_namespace flag, because it 'correctly' infers
that there are no later chunks that will need to access global
variables from the first chunk.
- This causes a test failure, because `npm test` first runs
`npm run build`, which generates a valid blockly_compressed.js,
but this is then overrwritten by an invalid one when it next runs
`npm run build:debug`.
(The invalid one is missing all `$.` prefixes on 'global' variables,
including on Blockly, so the wrapper's last two lines -
"$.Blockly.internal_ = $;" and "return $.Blockly" - fail.)
The fix is to add appropriate @suppress annotations to blocks/*.js and
generators/**/*.js and then remove the first-chunk-only hack.
* refactor(build): Just build once
Since the previous commit caused `npm run build:debug` to do
everything that `... build:compressed` does - and to produce
byte-for-byte identical output - it doesn't make sense to run
both when testing. To that end:
- Replace the build:debug and build:strict package scripts that
did `gulp buildCompressed --...` with new scripts build-debug
and build-strict that do `gulp build --...` instead.
(The target names are changed so as to extend our existing naming
convention as follows: a target named "foo:bar" does some sub-part
of the job done by target "foo", but a target named "foo-bar" does
all the work of the target "foo" with some extra options.)
- build:debug:log and build:strict:log are similarly replaced with
build-debug-log and build-strict-log.
- Modify run_all_tests.js to just do `npm run build-debug` instead of
doing both `npm run build` and `npm run build:debug`.
- Also remove the 'build:blocks' script that should have been removed
when the buildBlocks gulp task was deleted previously.
* refactor(build): Compile with base_minimal.js instead of base.js
Introduce a (very!) cut-down version of closure/goog/base.js named
base_minimal.js that is used as input to the compiler as an
alternative to using externs/goog-externs.js (which will be deleted
once the buildAdvancedCompilationTest target has been updated).
This will allow use of goog.setTestOnly since it will now exist in
compiled mode, and allows the changes made in 5b112db to filter
base.js out of the files for the first chunk to be reverted.
(It also obliges a change to the compiled-mode check in blockly.js.)
* fix(build): Fix buildAdvanceCompilationTest
- In build_tasks.js:
- Replace the old compile() function with a new one factored out of
buildCompiled().
- Update buildAdvancedCompilationTest to use the new compile()
and other helpers created in the meantime.
- Remove no-longer-used maybeAddClosureLibrary().
- Remove externs/{block,generator,goog}-externs.js, which are no longer
used by any compile pipeline.
- Update core/blockly.js to fix issue with detection of compiled mode
when using ADVANCED_OPTIMISATIONS.
- Update only other use of globalThis, in core/utils/xml.js, to
consistently treat it as a dictionary object.
- Update instructions in tests/compile/index.html.
This commit is sort-of-a-prerequisite to #5602; test:compile:advanced
was previously working but the generated `main_compresed.js` would
throw errors upon loading.
511 lines
19 KiB
JavaScript
511 lines
19 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2015 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Generating PHP for list blocks.
|
|
* @suppress {missingRequire}
|
|
*/
|
|
|
|
/**
|
|
* Lists in PHP are known to break when non-variables are passed into blocks
|
|
* that require a list. PHP, unlike other languages, passes arrays as reference
|
|
* value instead of value so we are unable to support it to the extent we can
|
|
* for the other languages.
|
|
* For example, a ternary operator with two arrays will return the array by
|
|
* value and that cannot be passed into any of the built-in array functions for
|
|
* PHP (because only variables can be passed by reference).
|
|
* ex: end(true ? list1 : list2)
|
|
*/
|
|
'use strict';
|
|
|
|
goog.provide('Blockly.PHP.lists');
|
|
|
|
goog.require('Blockly.PHP');
|
|
|
|
|
|
Blockly.PHP['lists_create_empty'] = function(block) {
|
|
// Create an empty list.
|
|
return ['array()', Blockly.PHP.ORDER_FUNCTION_CALL];
|
|
};
|
|
|
|
Blockly.PHP['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++) {
|
|
code[i] = Blockly.PHP.valueToCode(block, 'ADD' + i,
|
|
Blockly.PHP.ORDER_NONE) || 'null';
|
|
}
|
|
code = 'array(' + code.join(', ') + ')';
|
|
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
|
};
|
|
|
|
Blockly.PHP['lists_repeat'] = function(block) {
|
|
// Create a list with one element repeated.
|
|
const functionName = Blockly.PHP.provideFunction_(
|
|
'lists_repeat',
|
|
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
|
'($value, $count) {',
|
|
' $array = array();',
|
|
' for ($index = 0; $index < $count; $index++) {',
|
|
' $array[] = $value;',
|
|
' }',
|
|
' return $array;',
|
|
'}']);
|
|
const element = Blockly.PHP.valueToCode(block, 'ITEM',
|
|
Blockly.PHP.ORDER_NONE) || 'null';
|
|
const repeatCount = Blockly.PHP.valueToCode(block, 'NUM',
|
|
Blockly.PHP.ORDER_NONE) || '0';
|
|
const code = functionName + '(' + element + ', ' + repeatCount + ')';
|
|
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
|
};
|
|
|
|
Blockly.PHP['lists_length'] = function(block) {
|
|
// String or array length.
|
|
const functionName = Blockly.PHP.provideFunction_(
|
|
'length',
|
|
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '($value) {',
|
|
' if (is_string($value)) {',
|
|
' return strlen($value);',
|
|
' } else {',
|
|
' return count($value);',
|
|
' }',
|
|
'}']);
|
|
const list = Blockly.PHP.valueToCode(block, 'VALUE',
|
|
Blockly.PHP.ORDER_NONE) || '\'\'';
|
|
return [functionName + '(' + list + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
|
|
};
|
|
|
|
Blockly.PHP['lists_isEmpty'] = function(block) {
|
|
// Is the string null or array empty?
|
|
const argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
|
|
Blockly.PHP.ORDER_FUNCTION_CALL) || 'array()';
|
|
return ['empty(' + argument0 + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
|
|
};
|
|
|
|
Blockly.PHP['lists_indexOf'] = function(block) {
|
|
// Find an item in the list.
|
|
const argument0 = Blockly.PHP.valueToCode(block, 'FIND',
|
|
Blockly.PHP.ORDER_NONE) || '\'\'';
|
|
const argument1 = Blockly.PHP.valueToCode(block, 'VALUE',
|
|
Blockly.PHP.ORDER_MEMBER) || '[]';
|
|
let errorIndex = ' -1';
|
|
let indexAdjustment = '';
|
|
if (block.workspace.options.oneBasedIndex) {
|
|
errorIndex = ' 0';
|
|
indexAdjustment = ' + 1';
|
|
}
|
|
let functionName;
|
|
if (block.getFieldValue('END') === 'FIRST') {
|
|
// indexOf
|
|
functionName = Blockly.PHP.provideFunction_(
|
|
'indexOf',
|
|
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
|
'($haystack, $needle) {',
|
|
' for ($index = 0; $index < count($haystack); $index++) {',
|
|
' if ($haystack[$index] == $needle) return $index' +
|
|
indexAdjustment + ';',
|
|
' }',
|
|
' return ' + errorIndex + ';',
|
|
'}']);
|
|
} else {
|
|
// lastIndexOf
|
|
functionName = Blockly.PHP.provideFunction_(
|
|
'lastIndexOf',
|
|
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
|
'($haystack, $needle) {',
|
|
' $last = ' + errorIndex + ';',
|
|
' for ($index = 0; $index < count($haystack); $index++) {',
|
|
' if ($haystack[$index] == $needle) $last = $index' +
|
|
indexAdjustment + ';',
|
|
' }',
|
|
' return $last;',
|
|
'}']);
|
|
}
|
|
|
|
const code = functionName + '(' + argument1 + ', ' + argument0 + ')';
|
|
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
|
};
|
|
|
|
Blockly.PHP['lists_getIndex'] = function(block) {
|
|
// Get element at index.
|
|
const mode = block.getFieldValue('MODE') || 'GET';
|
|
const where = block.getFieldValue('WHERE') || 'FROM_START';
|
|
switch (where) {
|
|
case 'FIRST':
|
|
if (mode === 'GET') {
|
|
const list = Blockly.PHP.valueToCode(block, 'VALUE',
|
|
Blockly.PHP.ORDER_MEMBER) || 'array()';
|
|
const code = list + '[0]';
|
|
return [code, Blockly.PHP.ORDER_MEMBER];
|
|
} else if (mode === 'GET_REMOVE') {
|
|
const list = Blockly.PHP.valueToCode(block, 'VALUE',
|
|
Blockly.PHP.ORDER_NONE) || 'array()';
|
|
const code = 'array_shift(' + list + ')';
|
|
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
|
} else if (mode === 'REMOVE') {
|
|
const list = Blockly.PHP.valueToCode(block, 'VALUE',
|
|
Blockly.PHP.ORDER_NONE) || 'array()';
|
|
return 'array_shift(' + list + ');\n';
|
|
}
|
|
break;
|
|
case 'LAST':
|
|
if (mode === 'GET') {
|
|
const list = Blockly.PHP.valueToCode(block, 'VALUE',
|
|
Blockly.PHP.ORDER_NONE) || 'array()';
|
|
const code = 'end(' + list + ')';
|
|
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
|
} else if (mode === 'GET_REMOVE') {
|
|
const list = Blockly.PHP.valueToCode(block, 'VALUE',
|
|
Blockly.PHP.ORDER_NONE) || 'array()';
|
|
const code = 'array_pop(' + list + ')';
|
|
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
|
} else if (mode === 'REMOVE') {
|
|
const list = Blockly.PHP.valueToCode(block, 'VALUE',
|
|
Blockly.PHP.ORDER_NONE) || 'array()';
|
|
return 'array_pop(' + list + ');\n';
|
|
}
|
|
break;
|
|
case 'FROM_START': {
|
|
const at = Blockly.PHP.getAdjusted(block, 'AT');
|
|
if (mode === 'GET') {
|
|
const list = Blockly.PHP.valueToCode(block, 'VALUE',
|
|
Blockly.PHP.ORDER_MEMBER) || 'array()';
|
|
const code = list + '[' + at + ']';
|
|
return [code, Blockly.PHP.ORDER_MEMBER];
|
|
} else if (mode === 'GET_REMOVE') {
|
|
const list = Blockly.PHP.valueToCode(block, 'VALUE',
|
|
Blockly.PHP.ORDER_NONE) || 'array()';
|
|
const code = 'array_splice(' + list + ', ' + at + ', 1)[0]';
|
|
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
|
} else if (mode === 'REMOVE') {
|
|
const list = Blockly.PHP.valueToCode(block, 'VALUE',
|
|
Blockly.PHP.ORDER_NONE) || 'array()';
|
|
return 'array_splice(' + list + ', ' + at + ', 1);\n';
|
|
}
|
|
break;
|
|
}
|
|
case 'FROM_END':
|
|
if (mode === 'GET') {
|
|
const list = Blockly.PHP.valueToCode(block, 'VALUE',
|
|
Blockly.PHP.ORDER_NONE) || 'array()';
|
|
const at = Blockly.PHP.getAdjusted(block, 'AT', 1, true);
|
|
const code = 'array_slice(' + list + ', ' + at + ', 1)[0]';
|
|
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
|
} else if (mode === 'GET_REMOVE' || mode === 'REMOVE') {
|
|
const list = Blockly.PHP.valueToCode(block, 'VALUE',
|
|
Blockly.PHP.ORDER_NONE) || 'array()';
|
|
const at = Blockly.PHP.getAdjusted(block, 'AT', 1, false,
|
|
Blockly.PHP.ORDER_SUBTRACTION);
|
|
const code = 'array_splice(' + list +
|
|
', count(' + list + ') - ' + at + ', 1)[0]';
|
|
if (mode === 'GET_REMOVE') {
|
|
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
|
} else if (mode === 'REMOVE') {
|
|
return code + ';\n';
|
|
}
|
|
}
|
|
break;
|
|
case 'RANDOM': {
|
|
const list = Blockly.PHP.valueToCode(block, 'VALUE',
|
|
Blockly.PHP.ORDER_NONE) || 'array()';
|
|
if (mode === 'GET') {
|
|
const functionName = Blockly.PHP.provideFunction_(
|
|
'lists_get_random_item',
|
|
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
|
'($list) {',
|
|
' return $list[rand(0,count($list)-1)];',
|
|
'}']);
|
|
const code = functionName + '(' + list + ')';
|
|
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
|
} else if (mode === 'GET_REMOVE') {
|
|
const functionName = Blockly.PHP.provideFunction_(
|
|
'lists_get_remove_random_item',
|
|
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
|
'(&$list) {',
|
|
' $x = rand(0,count($list)-1);',
|
|
' unset($list[$x]);',
|
|
' return array_values($list);',
|
|
'}']);
|
|
const code = functionName + '(' + list + ')';
|
|
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
|
} else if (mode === 'REMOVE') {
|
|
const functionName = Blockly.PHP.provideFunction_(
|
|
'lists_remove_random_item',
|
|
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
|
'(&$list) {',
|
|
' unset($list[rand(0,count($list)-1)]);',
|
|
'}']);
|
|
return functionName + '(' + list + ');\n';
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
throw Error('Unhandled combination (lists_getIndex).');
|
|
};
|
|
|
|
Blockly.PHP['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';
|
|
const where = block.getFieldValue('WHERE') || 'FROM_START';
|
|
const value = Blockly.PHP.valueToCode(block, 'TO',
|
|
Blockly.PHP.ORDER_ASSIGNMENT) || 'null';
|
|
// Cache non-trivial values to variables to prevent repeated look-ups.
|
|
// Closure, which accesses and modifies 'list'.
|
|
let cachedList;
|
|
function cacheList() {
|
|
if (cachedList.match(/^\$\w+$/)) {
|
|
return '';
|
|
}
|
|
const listVar = Blockly.PHP.nameDB_.getDistinctName(
|
|
'tmp_list', Blockly.VARIABLE_CATEGORY_NAME);
|
|
const code = listVar + ' = &' + cachedList + ';\n';
|
|
cachedList = listVar;
|
|
return code;
|
|
}
|
|
switch (where) {
|
|
case 'FIRST':
|
|
if (mode === 'SET') {
|
|
const list = Blockly.PHP.valueToCode(block, 'LIST',
|
|
Blockly.PHP.ORDER_MEMBER) || 'array()';
|
|
return list + '[0] = ' + value + ';\n';
|
|
} else if (mode === 'INSERT') {
|
|
const list = Blockly.PHP.valueToCode(block, 'LIST',
|
|
Blockly.PHP.ORDER_NONE) || 'array()';
|
|
return 'array_unshift(' + list + ', ' + value + ');\n';
|
|
}
|
|
break;
|
|
case 'LAST': {
|
|
const list = Blockly.PHP.valueToCode(block, 'LIST',
|
|
Blockly.PHP.ORDER_NONE) || 'array()';
|
|
if (mode === 'SET') {
|
|
const functionName = Blockly.PHP.provideFunction_(
|
|
'lists_set_last_item',
|
|
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
|
'(&$list, $value) {',
|
|
' $list[count($list) - 1] = $value;',
|
|
'}']);
|
|
return functionName + '(' + list + ', ' + value + ');\n';
|
|
} else if (mode === 'INSERT') {
|
|
return 'array_push(' + list + ', ' + value + ');\n';
|
|
}
|
|
break;
|
|
}
|
|
case 'FROM_START': {
|
|
const at = Blockly.PHP.getAdjusted(block, 'AT');
|
|
if (mode === 'SET') {
|
|
const list = Blockly.PHP.valueToCode(block, 'LIST',
|
|
Blockly.PHP.ORDER_MEMBER) || 'array()';
|
|
return list + '[' + at + '] = ' + value + ';\n';
|
|
} else if (mode === 'INSERT') {
|
|
const list = Blockly.PHP.valueToCode(block, 'LIST',
|
|
Blockly.PHP.ORDER_NONE) || 'array()';
|
|
return 'array_splice(' + list + ', ' + at + ', 0, ' + value + ');\n';
|
|
}
|
|
break;
|
|
}
|
|
case 'FROM_END': {
|
|
const list = Blockly.PHP.valueToCode(block, 'LIST',
|
|
Blockly.PHP.ORDER_NONE) || 'array()';
|
|
const at = Blockly.PHP.getAdjusted(block, 'AT', 1);
|
|
if (mode === 'SET') {
|
|
const functionName = Blockly.PHP.provideFunction_(
|
|
'lists_set_from_end',
|
|
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
|
'(&$list, $at, $value) {',
|
|
' $list[count($list) - $at] = $value;',
|
|
'}']);
|
|
return functionName + '(' + list + ', ' + at + ', ' + value + ');\n';
|
|
} else if (mode === 'INSERT') {
|
|
const functionName = Blockly.PHP.provideFunction_(
|
|
'lists_insert_from_end',
|
|
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
|
'(&$list, $at, $value) {',
|
|
' return array_splice($list, count($list) - $at, 0, $value);',
|
|
'}']);
|
|
return functionName + '(' + list + ', ' + at + ', ' + value + ');\n';
|
|
}
|
|
break;
|
|
}
|
|
case 'RANDOM':
|
|
cachedList = Blockly.PHP.valueToCode(block, 'LIST',
|
|
Blockly.PHP.ORDER_REFERENCE) || 'array()';
|
|
let code = cacheList();
|
|
const list = cachedList;
|
|
const xVar = Blockly.PHP.nameDB_.getDistinctName(
|
|
'tmp_x', Blockly.VARIABLE_CATEGORY_NAME);
|
|
code += xVar + ' = rand(0, count(' + list + ')-1);\n';
|
|
if (mode === 'SET') {
|
|
code += list + '[' + xVar + '] = ' + value + ';\n';
|
|
return code;
|
|
} else if (mode === 'INSERT') {
|
|
code += 'array_splice(' + list + ', ' + xVar + ', 0, ' + value +
|
|
');\n';
|
|
return code;
|
|
}
|
|
break;
|
|
}
|
|
throw Error('Unhandled combination (lists_setIndex).');
|
|
};
|
|
|
|
Blockly.PHP['lists_getSublist'] = function(block) {
|
|
// Get sublist.
|
|
const list = Blockly.PHP.valueToCode(block, 'LIST',
|
|
Blockly.PHP.ORDER_NONE) || 'array()';
|
|
const where1 = block.getFieldValue('WHERE1');
|
|
const where2 = block.getFieldValue('WHERE2');
|
|
let code;
|
|
if (where1 === 'FIRST' && where2 === 'LAST') {
|
|
code = list;
|
|
} else if (list.match(/^\$\w+$/) ||
|
|
(where1 !== 'FROM_END' && where2 === 'FROM_START')) {
|
|
// If the list is a simple value or doesn't require a call for length, don't
|
|
// generate a helper function.
|
|
let at1;
|
|
switch (where1) {
|
|
case 'FROM_START':
|
|
at1 = Blockly.PHP.getAdjusted(block, 'AT1');
|
|
break;
|
|
case 'FROM_END':
|
|
at1 = Blockly.PHP.getAdjusted(block, 'AT1', 1, false,
|
|
Blockly.PHP.ORDER_SUBTRACTION);
|
|
at1 = 'count(' + list + ') - ' + at1;
|
|
break;
|
|
case 'FIRST':
|
|
at1 = '0';
|
|
break;
|
|
default:
|
|
throw Error('Unhandled option (lists_getSublist).');
|
|
}
|
|
let at2;
|
|
let length;
|
|
switch (where2) {
|
|
case 'FROM_START':
|
|
at2 = Blockly.PHP.getAdjusted(block, 'AT2', 0, false,
|
|
Blockly.PHP.ORDER_SUBTRACTION);
|
|
length = at2 + ' - ';
|
|
if (Blockly.isNumber(String(at1)) || String(at1).match(/^\(.+\)$/)) {
|
|
length += at1;
|
|
} else {
|
|
length += '(' + at1 + ')';
|
|
}
|
|
length += ' + 1';
|
|
break;
|
|
case 'FROM_END':
|
|
at2 = Blockly.PHP.getAdjusted(block, 'AT2', 0, false,
|
|
Blockly.PHP.ORDER_SUBTRACTION);
|
|
length = 'count(' + list + ') - ' + at2 + ' - ';
|
|
if (Blockly.isNumber(String(at1)) || String(at1).match(/^\(.+\)$/)) {
|
|
length += at1;
|
|
} else {
|
|
length += '(' + at1 + ')';
|
|
}
|
|
break;
|
|
case 'LAST':
|
|
length = 'count(' + list + ') - ';
|
|
if (Blockly.isNumber(String(at1)) || String(at1).match(/^\(.+\)$/)) {
|
|
length += at1;
|
|
} else {
|
|
length += '(' + at1 + ')';
|
|
}
|
|
break;
|
|
default:
|
|
throw Error('Unhandled option (lists_getSublist).');
|
|
}
|
|
code = 'array_slice(' + list + ', ' + at1 + ', ' + length + ')';
|
|
} else {
|
|
const at1 = Blockly.PHP.getAdjusted(block, 'AT1');
|
|
const at2 = Blockly.PHP.getAdjusted(block, 'AT2');
|
|
const functionName = Blockly.PHP.provideFunction_(
|
|
'lists_get_sublist',
|
|
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
|
'($list, $where1, $at1, $where2, $at2) {',
|
|
' if ($where1 == \'FROM_END\') {',
|
|
' $at1 = count($list) - 1 - $at1;',
|
|
' } else if ($where1 == \'FIRST\') {',
|
|
' $at1 = 0;',
|
|
' } else if ($where1 != \'FROM_START\') {',
|
|
' throw new Exception(\'Unhandled option (lists_get_sublist).\');',
|
|
' }',
|
|
' $length = 0;',
|
|
' if ($where2 == \'FROM_START\') {',
|
|
' $length = $at2 - $at1 + 1;',
|
|
' } else if ($where2 == \'FROM_END\') {',
|
|
' $length = count($list) - $at1 - $at2;',
|
|
' } else if ($where2 == \'LAST\') {',
|
|
' $length = count($list) - $at1;',
|
|
' } else {',
|
|
' throw new Exception(\'Unhandled option (lists_get_sublist).\');',
|
|
' }',
|
|
' return array_slice($list, $at1, $length);',
|
|
'}']);
|
|
code = functionName + '(' + list + ', \'' +
|
|
where1 + '\', ' + at1 + ', \'' + where2 + '\', ' + at2 + ')';
|
|
}
|
|
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
|
};
|
|
|
|
Blockly.PHP['lists_sort'] = function(block) {
|
|
// Block for sorting a list.
|
|
const listCode = Blockly.PHP.valueToCode(block, 'LIST',
|
|
Blockly.PHP.ORDER_NONE) || 'array()';
|
|
const direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1;
|
|
const type = block.getFieldValue('TYPE');
|
|
const functionName = Blockly.PHP.provideFunction_(
|
|
'lists_sort',
|
|
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
|
'($list, $type, $direction) {',
|
|
' $sortCmpFuncs = array(',
|
|
' "NUMERIC" => "strnatcasecmp",',
|
|
' "TEXT" => "strcmp",',
|
|
' "IGNORE_CASE" => "strcasecmp"',
|
|
' );',
|
|
' $sortCmp = $sortCmpFuncs[$type];',
|
|
' $list2 = $list;', // Clone list.
|
|
' usort($list2, $sortCmp);',
|
|
' if ($direction == -1) {',
|
|
' $list2 = array_reverse($list2);',
|
|
' }',
|
|
' return $list2;',
|
|
'}']);
|
|
const sortCode = functionName +
|
|
'(' + listCode + ', "' + type + '", ' + direction + ')';
|
|
return [sortCode, Blockly.PHP.ORDER_FUNCTION_CALL];
|
|
};
|
|
|
|
Blockly.PHP['lists_split'] = function(block) {
|
|
// Block for splitting text into a list, or joining a list into text.
|
|
let value_input = Blockly.PHP.valueToCode(block, 'INPUT',
|
|
Blockly.PHP.ORDER_NONE);
|
|
const value_delim = Blockly.PHP.valueToCode(block, 'DELIM',
|
|
Blockly.PHP.ORDER_NONE) || '\'\'';
|
|
const mode = block.getFieldValue('MODE');
|
|
let functionName;
|
|
if (mode === 'SPLIT') {
|
|
if (!value_input) {
|
|
value_input = '\'\'';
|
|
}
|
|
functionName = 'explode';
|
|
} else if (mode === 'JOIN') {
|
|
if (!value_input) {
|
|
value_input = 'array()';
|
|
}
|
|
functionName = 'implode';
|
|
} else {
|
|
throw Error('Unknown mode: ' + mode);
|
|
}
|
|
const code = functionName + '(' + value_delim + ', ' + value_input + ')';
|
|
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
|
};
|
|
|
|
Blockly.PHP['lists_reverse'] = function(block) {
|
|
// Block for reversing a list.
|
|
const list = Blockly.PHP.valueToCode(block, 'LIST',
|
|
Blockly.PHP.ORDER_NONE) || '[]';
|
|
const code = 'array_reverse(' + list + ')';
|
|
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
|
};
|