Files
blockly/generators/javascript/lists.js
Christopher Allen 985af10f6e chore(build): Use chunked compilation (#5721)
* 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.
2021-11-29 17:50:17 +00:00

399 lines
14 KiB
JavaScript

/**
* @license
* Copyright 2012 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Generating JavaScript for list blocks.
* @suppress {missingRequire}
*/
'use strict';
goog.provide('Blockly.JavaScript.lists');
goog.require('Blockly.JavaScript');
Blockly.JavaScript['lists_create_empty'] = function(block) {
// Create an empty list.
return ['[]', Blockly.JavaScript.ORDER_ATOMIC];
};
Blockly.JavaScript['lists_create_with'] = function(block) {
// Create a list with any number of elements of any type.
const elements = new Array(block.itemCount_);
for (let i = 0; i < block.itemCount_; i++) {
elements[i] = Blockly.JavaScript.valueToCode(block, 'ADD' + i,
Blockly.JavaScript.ORDER_NONE) || 'null';
}
const code = '[' + elements.join(', ') + ']';
return [code, Blockly.JavaScript.ORDER_ATOMIC];
};
Blockly.JavaScript['lists_repeat'] = function(block) {
// Create a list with one element repeated.
const functionName = Blockly.JavaScript.provideFunction_(
'listsRepeat',
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
'(value, n) {',
' var array = [];',
' for (var i = 0; i < n; i++) {',
' array[i] = value;',
' }',
' return array;',
'}']);
const element = Blockly.JavaScript.valueToCode(block, 'ITEM',
Blockly.JavaScript.ORDER_NONE) || 'null';
const repeatCount = Blockly.JavaScript.valueToCode(block, 'NUM',
Blockly.JavaScript.ORDER_NONE) || '0';
const code = functionName + '(' + element + ', ' + repeatCount + ')';
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
};
Blockly.JavaScript['lists_length'] = function(block) {
// String or array length.
const list = Blockly.JavaScript.valueToCode(block, 'VALUE',
Blockly.JavaScript.ORDER_MEMBER) || '[]';
return [list + '.length', Blockly.JavaScript.ORDER_MEMBER];
};
Blockly.JavaScript['lists_isEmpty'] = function(block) {
// Is the string null or array empty?
const list = Blockly.JavaScript.valueToCode(block, 'VALUE',
Blockly.JavaScript.ORDER_MEMBER) || '[]';
return ['!' + list + '.length', Blockly.JavaScript.ORDER_LOGICAL_NOT];
};
Blockly.JavaScript['lists_indexOf'] = function(block) {
// Find an item in the list.
const operator = block.getFieldValue('END') === 'FIRST' ?
'indexOf' : 'lastIndexOf';
const item = Blockly.JavaScript.valueToCode(block, 'FIND',
Blockly.JavaScript.ORDER_NONE) || '\'\'';
const list = Blockly.JavaScript.valueToCode(block, 'VALUE',
Blockly.JavaScript.ORDER_MEMBER) || '[]';
const code = list + '.' + operator + '(' + item + ')';
if (block.workspace.options.oneBasedIndex) {
return [code + ' + 1', Blockly.JavaScript.ORDER_ADDITION];
}
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
};
Blockly.JavaScript['lists_getIndex'] = function(block) {
// Get element at index.
// Note: Until January 2013 this block did not have MODE or WHERE inputs.
const mode = block.getFieldValue('MODE') || 'GET';
const where = block.getFieldValue('WHERE') || 'FROM_START';
const listOrder = (where === 'RANDOM') ? Blockly.JavaScript.ORDER_NONE :
Blockly.JavaScript.ORDER_MEMBER;
const list = Blockly.JavaScript.valueToCode(block, 'VALUE', listOrder) || '[]';
switch (where) {
case ('FIRST'):
if (mode === 'GET') {
const code = list + '[0]';
return [code, Blockly.JavaScript.ORDER_MEMBER];
} else if (mode === 'GET_REMOVE') {
const code = list + '.shift()';
return [code, Blockly.JavaScript.ORDER_MEMBER];
} else if (mode === 'REMOVE') {
return list + '.shift();\n';
}
break;
case ('LAST'):
if (mode === 'GET') {
const code = list + '.slice(-1)[0]';
return [code, Blockly.JavaScript.ORDER_MEMBER];
} else if (mode === 'GET_REMOVE') {
const code = list + '.pop()';
return [code, Blockly.JavaScript.ORDER_MEMBER];
} else if (mode === 'REMOVE') {
return list + '.pop();\n';
}
break;
case ('FROM_START'): {
const at = Blockly.JavaScript.getAdjusted(block, 'AT');
if (mode === 'GET') {
const code = list + '[' + at + ']';
return [code, Blockly.JavaScript.ORDER_MEMBER];
} else if (mode === 'GET_REMOVE') {
const code = list + '.splice(' + at + ', 1)[0]';
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
} else if (mode === 'REMOVE') {
return list + '.splice(' + at + ', 1);\n';
}
break;
}
case ('FROM_END'): {
const at = Blockly.JavaScript.getAdjusted(block, 'AT', 1, true);
if (mode === 'GET') {
const code = list + '.slice(' + at + ')[0]';
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
} else if (mode === 'GET_REMOVE') {
const code = list + '.splice(' + at + ', 1)[0]';
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
} else if (mode === 'REMOVE') {
return list + '.splice(' + at + ', 1);';
}
break;
}
case ('RANDOM'): {
const functionName = Blockly.JavaScript.provideFunction_(
'listsGetRandomItem',
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
'(list, remove) {',
' var x = Math.floor(Math.random() * list.length);',
' if (remove) {',
' return list.splice(x, 1)[0];',
' } else {',
' return list[x];',
' }',
'}']);
const code = functionName + '(' + list + ', ' + (mode !== 'GET') + ')';
if (mode === 'GET' || mode === 'GET_REMOVE') {
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
} else if (mode === 'REMOVE') {
return code + ';\n';
}
break;
}
}
throw Error('Unhandled combination (lists_getIndex).');
};
Blockly.JavaScript['lists_setIndex'] = function(block) {
// Set element at index.
// Note: Until February 2013 this block did not have MODE or WHERE inputs.
let list = Blockly.JavaScript.valueToCode(block, 'LIST',
Blockly.JavaScript.ORDER_MEMBER) || '[]';
const mode = block.getFieldValue('MODE') || 'GET';
const where = block.getFieldValue('WHERE') || 'FROM_START';
const value = Blockly.JavaScript.valueToCode(block, 'TO',
Blockly.JavaScript.ORDER_ASSIGNMENT) || 'null';
// Cache non-trivial values to variables to prevent repeated look-ups.
// Closure, which accesses and modifies 'list'.
function cacheList() {
if (list.match(/^\w+$/)) {
return '';
}
const listVar = Blockly.JavaScript.nameDB_.getDistinctName(
'tmpList', Blockly.VARIABLE_CATEGORY_NAME);
const code = 'var ' + listVar + ' = ' + list + ';\n';
list = listVar;
return code;
}
switch (where) {
case ('FIRST'):
if (mode === 'SET') {
return list + '[0] = ' + value + ';\n';
} else if (mode === 'INSERT') {
return list + '.unshift(' + value + ');\n';
}
break;
case ('LAST'):
if (mode === 'SET') {
let code = cacheList();
code += list + '[' + list + '.length - 1] = ' + value + ';\n';
return code;
} else if (mode === 'INSERT') {
return list + '.push(' + value + ');\n';
}
break;
case ('FROM_START'): {
const at = Blockly.JavaScript.getAdjusted(block, 'AT');
if (mode === 'SET') {
return list + '[' + at + '] = ' + value + ';\n';
} else if (mode === 'INSERT') {
return list + '.splice(' + at + ', 0, ' + value + ');\n';
}
break;
}
case ('FROM_END'): {
const at = Blockly.JavaScript.getAdjusted(block, 'AT', 1, false,
Blockly.JavaScript.ORDER_SUBTRACTION);
let code = cacheList();
if (mode === 'SET') {
code += list + '[' + list + '.length - ' + at + '] = ' + value + ';\n';
return code;
} else if (mode === 'INSERT') {
code += list + '.splice(' + list + '.length - ' + at + ', 0, ' + value +
');\n';
return code;
}
break;
}
case ('RANDOM'): {
let code = cacheList();
const xVar = Blockly.JavaScript.nameDB_.getDistinctName(
'tmpX', Blockly.VARIABLE_CATEGORY_NAME);
code += 'var ' + xVar + ' = Math.floor(Math.random() * ' + list +
'.length);\n';
if (mode === 'SET') {
code += list + '[' + xVar + '] = ' + value + ';\n';
return code;
} else if (mode === 'INSERT') {
code += list + '.splice(' + xVar + ', 0, ' + value + ');\n';
return code;
}
break;
}
}
throw Error('Unhandled combination (lists_setIndex).');
};
/**
* Returns an expression calculating the index into a list.
* @param {string} listName Name of the list, used to calculate length.
* @param {string} where The method of indexing, selected by dropdown in Blockly
* @param {string=} opt_at The optional offset when indexing from start/end.
* @return {string|undefined} Index expression.
* @private
*/
Blockly.JavaScript.lists.getIndex_ = function(listName, where, opt_at) {
if (where === 'FIRST') {
return '0';
} else if (where === 'FROM_END') {
return listName + '.length - 1 - ' + opt_at;
} else if (where === 'LAST') {
return listName + '.length - 1';
} else {
return opt_at;
}
};
Blockly.JavaScript['lists_getSublist'] = function(block) {
// Get sublist.
const list = Blockly.JavaScript.valueToCode(block, 'LIST',
Blockly.JavaScript.ORDER_MEMBER) || '[]';
const where1 = block.getFieldValue('WHERE1');
const where2 = block.getFieldValue('WHERE2');
let code;
if (where1 === 'FIRST' && where2 === 'LAST') {
code = list + '.slice(0)';
} else if (list.match(/^\w+$/) ||
(where1 !== 'FROM_END' && where2 === 'FROM_START')) {
// If the list is a variable or doesn't require a call for length, don't
// generate a helper function.
let at1;
switch (where1) {
case 'FROM_START':
at1 = Blockly.JavaScript.getAdjusted(block, 'AT1');
break;
case 'FROM_END':
at1 = Blockly.JavaScript.getAdjusted(block, 'AT1', 1, false,
Blockly.JavaScript.ORDER_SUBTRACTION);
at1 = list + '.length - ' + at1;
break;
case 'FIRST':
at1 = '0';
break;
default:
throw Error('Unhandled option (lists_getSublist).');
}
let at2;
switch (where2) {
case 'FROM_START':
at2 = Blockly.JavaScript.getAdjusted(block, 'AT2', 1);
break;
case 'FROM_END':
at2 = Blockly.JavaScript.getAdjusted(block, 'AT2', 0, false,
Blockly.JavaScript.ORDER_SUBTRACTION);
at2 = list + '.length - ' + at2;
break;
case 'LAST':
at2 = list + '.length';
break;
default:
throw Error('Unhandled option (lists_getSublist).');
}
code = list + '.slice(' + at1 + ', ' + at2 + ')';
} else {
const at1 = Blockly.JavaScript.getAdjusted(block, 'AT1');
const at2 = Blockly.JavaScript.getAdjusted(block, 'AT2');
const getIndex_ = Blockly.JavaScript.lists.getIndex_;
const wherePascalCase = {'FIRST': 'First', 'LAST': 'Last',
'FROM_START': 'FromStart', 'FROM_END': 'FromEnd'};
const functionName = Blockly.JavaScript.provideFunction_(
'subsequence' + wherePascalCase[where1] + wherePascalCase[where2],
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
'(sequence' +
// The value for 'FROM_END' and'FROM_START' depends on `at` so
// we add it as a parameter.
((where1 === 'FROM_END' || where1 === 'FROM_START') ? ', at1' : '') +
((where2 === 'FROM_END' || where2 === 'FROM_START') ? ', at2' : '') +
') {',
' var start = ' + getIndex_('sequence', where1, 'at1') + ';',
' var end = ' + getIndex_('sequence', where2, 'at2') + ' + 1;',
' return sequence.slice(start, end);',
'}']);
code = functionName + '(' + list +
// The value for 'FROM_END' and 'FROM_START' depends on `at` so we
// pass it.
((where1 === 'FROM_END' || where1 === 'FROM_START') ? ', ' + at1 : '') +
((where2 === 'FROM_END' || where2 === 'FROM_START') ? ', ' + at2 : '') +
')';
}
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
};
Blockly.JavaScript['lists_sort'] = function(block) {
// Block for sorting a list.
const list = Blockly.JavaScript.valueToCode(block, 'LIST',
Blockly.JavaScript.ORDER_FUNCTION_CALL) || '[]';
const direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1;
const type = block.getFieldValue('TYPE');
const getCompareFunctionName = Blockly.JavaScript.provideFunction_(
'listsGetSortCompare',
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
'(type, direction) {',
' var compareFuncs = {',
' "NUMERIC": function(a, b) {',
' return Number(a) - Number(b); },',
' "TEXT": function(a, b) {',
' return a.toString() > b.toString() ? 1 : -1; },',
' "IGNORE_CASE": function(a, b) {',
' return a.toString().toLowerCase() > ' +
'b.toString().toLowerCase() ? 1 : -1; },',
' };',
' var compare = compareFuncs[type];',
' return function(a, b) { return compare(a, b) * direction; }',
'}']);
return [list + '.slice().sort(' +
getCompareFunctionName + '("' + type + '", ' + direction + '))',
Blockly.JavaScript.ORDER_FUNCTION_CALL];
};
Blockly.JavaScript['lists_split'] = function(block) {
// Block for splitting text into a list, or joining a list into text.
let input = Blockly.JavaScript.valueToCode(block, 'INPUT',
Blockly.JavaScript.ORDER_MEMBER);
const delimiter = Blockly.JavaScript.valueToCode(block, 'DELIM',
Blockly.JavaScript.ORDER_NONE) || '\'\'';
const mode = block.getFieldValue('MODE');
let functionName;
if (mode === 'SPLIT') {
if (!input) {
input = '\'\'';
}
functionName = 'split';
} else if (mode === 'JOIN') {
if (!input) {
input = '[]';
}
functionName = 'join';
} else {
throw Error('Unknown mode: ' + mode);
}
const code = input + '.' + functionName + '(' + delimiter + ')';
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
};
Blockly.JavaScript['lists_reverse'] = function(block) {
// Block for reversing a list.
const list = Blockly.JavaScript.valueToCode(block, 'LIST',
Blockly.JavaScript.ORDER_FUNCTION_CALL) || '[]';
const code = list + '.slice().reverse()';
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
};