* @desc Parses the abstract syntax tree for *for-loop* expression * @param {Object} forNode - An ast Node * @param {Array} retArr - return array string * @returns {Array} the parsed webgl string
(forNode, retArr)
| 629 | * @returns {Array} the parsed webgl string |
| 630 | */ |
| 631 | astForStatement(forNode, retArr) { |
| 632 | if (forNode.type !== 'ForStatement') { |
| 633 | throw this.astErrorOutput('Invalid for statement', forNode); |
| 634 | } |
| 635 | |
| 636 | const initArr = []; |
| 637 | const testArr = []; |
| 638 | const updateArr = []; |
| 639 | const bodyArr = []; |
| 640 | let isSafe = null; |
| 641 | |
| 642 | if (forNode.init) { |
| 643 | const { declarations } = forNode.init; |
| 644 | if (declarations.length > 1) { |
| 645 | isSafe = false; |
| 646 | } |
| 647 | this.astGeneric(forNode.init, initArr); |
| 648 | for (let i = 0; i < declarations.length; i++) { |
| 649 | if (declarations[i].init && declarations[i].init.type !== 'Literal') { |
| 650 | isSafe = false; |
| 651 | } |
| 652 | } |
| 653 | } else { |
| 654 | isSafe = false; |
| 655 | } |
| 656 | |
| 657 | if (forNode.test) { |
| 658 | this.astGeneric(forNode.test, testArr); |
| 659 | } else { |
| 660 | isSafe = false; |
| 661 | } |
| 662 | |
| 663 | if (forNode.update) { |
| 664 | this.astGeneric(forNode.update, updateArr); |
| 665 | } else { |
| 666 | isSafe = false; |
| 667 | } |
| 668 | |
| 669 | if (forNode.body) { |
| 670 | this.pushState('loop-body'); |
| 671 | this.astGeneric(forNode.body, bodyArr); |
| 672 | this.popState('loop-body'); |
| 673 | } |
| 674 | |
| 675 | // have all parts, now make them safe |
| 676 | if (isSafe === null) { |
| 677 | isSafe = this.isSafe(forNode.init) && this.isSafe(forNode.test); |
| 678 | } |
| 679 | |
| 680 | if (isSafe) { |
| 681 | const initString = initArr.join(''); |
| 682 | const initNeedsSemiColon = initString[initString.length - 1] !== ';'; |
| 683 | retArr.push(`for (${initString}${initNeedsSemiColon ? ';' : ''}${testArr.join('')};${updateArr.join('')}){\n`); |
| 684 | retArr.push(bodyArr.join('')); |
| 685 | retArr.push('}\n'); |
| 686 | } else { |
| 687 | const iVariableName = this.getInternalVariableName('safeI'); |
| 688 | if (initArr.length > 0) { |
nothing calls this directly
no test coverage detected