Skip to content

Commit c65a385

Browse files
committed
More double descent fixes and lint fixes
1 parent 6da1c17 commit c65a385

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

src/compiler/irgen.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -683,22 +683,22 @@ class ScriptTreeGenerator {
683683
case 'comments_reporter':
684684
return new IntermediateInput(InputOpcode.COMMENTS_REPORTER, InputType.ANY, {
685685
value: this.descendInputOfBlock(block, 'VALUE'),
686-
comment: this.descendInputOfBlock(block, 'COMMENT').toType(InputType.STRING),
686+
comment: this.descendInputOfBlock(block, 'COMMENT').toType(InputType.STRING)
687687
});
688688
case 'comments_boolean':
689689
return new IntermediateInput(InputOpcode.COMMENTS_BOOLEAN, InputType.BOOLEAN, {
690690
value: this.descendInputOfBlock(block, 'VALUE').toType(InputType.BOOLEAN),
691-
comment: this.descendInputOfBlock(block, 'COMMENT').toType(InputType.STRING),
691+
comment: this.descendInputOfBlock(block, 'COMMENT').toType(InputType.STRING)
692692
});
693693
case 'comments_object':
694694
return new IntermediateInput(InputOpcode.COMMENTS_OBJECT, InputType.OBJECT, {
695695
value: this.descendInputOfBlock(block, 'VALUE').toType(InputType.OBJECT),
696-
comment: this.descendInputOfBlock(block, 'COMMENT').toType(InputType.STRING),
696+
comment: this.descendInputOfBlock(block, 'COMMENT').toType(InputType.STRING)
697697
});
698698
case 'comments_array':
699699
return new IntermediateInput(InputOpcode.COMMENTS_ARRAY, InputType.ARRAY, {
700700
value: this.descendInputOfBlock(block, 'VALUE').toType(InputType.ARRAY),
701-
comment: this.descendInputOfBlock(block, 'COMMENT').toType(InputType.STRING),
701+
comment: this.descendInputOfBlock(block, 'COMMENT').toType(InputType.STRING)
702702
});
703703

704704
case 'tw_getLastKeyPressed':

src/compiler/jsexecute.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,37 @@ runtimeFunctions.arrayIndexOf = `const arrayIndexOf = (array, item) => {
5252
return index;
5353
}`;
5454

55+
/**
56+
* Replaces an item at the defined index in an array
57+
* @param {Array<*>} array The array
58+
* @param {number} index The index of the item to replace
59+
* @param {*} item The replacement item
60+
* @returns {Array<*>} Updated array with the replaced item
61+
*/
62+
runtimeFunctions.arrayReplaceAtIndex = `const arrayReplaceAtIndex = (array, index, item) => {
63+
if (index >= 0 && index < array.length) {
64+
const newArray = [...array];
65+
newArray[index] = item;
66+
return newArray;
67+
}
68+
return new Array();
69+
}`;
70+
71+
/**
72+
* Deletes an item at the defined index in an array
73+
* @param {Array<*>} array The array
74+
* @param {number} index The index of the item to delete
75+
* @returns {Array<*>} Updated array after deleting the item
76+
*/
77+
runtimeFunctions.arrayDeleteAtIndex = `const arrayDeleteAtIndex = (array, index) => {
78+
if (index >= 0 && index < array.length) {
79+
const newArray = [...array];
80+
newArray.splice(index, 1);
81+
return newArray;
82+
}
83+
return new Array();
84+
}`;
85+
5586
/**
5687
* Determine whether the current tick is likely stuck.
5788
* This implements similar functionality to the warp timer found in Scratch.

src/compiler/jsgen.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const {
1818
IntermediateScript,
1919
IntermediateRepresentation
2020
} = require('./intermediate');
21-
const { Stack } = require('immutable');
21+
const {Stack} = require('immutable');
2222
/* eslint-enable no-unused-vars */
2323

2424
/**
@@ -291,9 +291,9 @@ class JSGenerator {
291291
case InputOpcode.JSON_ADD_ITEM:
292292
return `(array = ${this.descendInput(node.array)}.slice(0), array.push(${this.descendInput(node.item)}), array)`;
293293
case InputOpcode.JSON_REPLACE_INDEX:
294-
return `(${this.descendInput(node.index)} >= 0 && ${this.descendInput(node.index)} < ${this.descendInput(node.array)}.length ? (array = [...${this.descendInput(node.array)}], array[${this.descendInput(node.index)}] = ${this.descendInput(node.item)}, array) : new Array())`;
294+
return `arrayReplaceAtIndex(${this.descendInput(node.array)}, ${this.descendInput(node.index)}, ${this.descendInput(node.item)})`;
295295
case InputOpcode.JSON_DELETE_INDEX:
296-
return `(${this.descendInput(node.index)} >= 0 && ${this.descendInput(node.index)} < ${this.descendInput(node.array)}.length ? (array = [...${this.descendInput(node.array)}], array.splice(${this.descendInput(node.index)}, 1), array) : new Array())`;
296+
return `arrayDeleteAtIndex(${this.descendInput(node.array)}, ${this.descendInput(node.index)})`;
297297
case InputOpcode.JSON_DELETE_ALL_OCCURRENCES:
298298
return `${this.descendInput(node.array)}.filter((item) => item !== ${this.descendInput(node.item)})`;
299299
case InputOpcode.JSON_MERGE_ARRAY:

0 commit comments

Comments
 (0)