Skip to content

Commit

Permalink
JSON: "Set list to array" block (VM) (#62)
Browse files Browse the repository at this point in the history
* clone list output before reporting

* clone list output before reporting

* add setList to primitives

* add data_setlist to irgen

* add list.set to jsgen

* Update jsexecute.js

* minor fixes
  • Loading branch information
LilyMakesThings authored Sep 23, 2024
1 parent 96b3934 commit 1a20823
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/blocks/scratch3_data.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Cast = require('../util/cast');
const Clone = require('../util/clone');

class Scratch3DataBlocks {
constructor (runtime) {
Expand Down Expand Up @@ -27,6 +28,7 @@ class Scratch3DataBlocks {
data_deletealloflist: this.deleteAllOfList,
data_insertatlist: this.insertAtList,
data_replaceitemoflist: this.replaceItemOfList,
data_setlist: this.setList,
data_itemoflist: this.getItemOfList,
data_itemnumoflist: this.getItemNumOfList,
data_lengthoflist: this.lengthOfList,
Expand Down Expand Up @@ -105,7 +107,7 @@ class Scratch3DataBlocks {
return list.value.slice();
}

return list.value;
return Clone.structured(list.value);
}

getListContents (args, util) {
Expand Down Expand Up @@ -199,6 +201,15 @@ class Scratch3DataBlocks {
list._monitorUpToDate = false;
}

setList (args, util) {
const array = Cast.toArray(args.ARRAY);
const list = util.target.lookupOrCreateList(
args.LIST.id, args.LIST.name);
if (list.locked) return;
list.value = array;
list._monitorUpToDate = false;
}

getItemOfList (args, util) {
const list = util.target.lookupOrCreateList(
args.LIST.id, args.LIST.name);
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/irgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,12 @@ class ScriptTreeGenerator {
variable: this.descendVariable(block, 'VARIABLE', SCALAR_TYPE),
value: this.descendInputOfBlock(block, 'VALUE')
};
case 'data_setlist':
return {
kind: 'list.set',
list: this.descendVariable(block, 'LIST', LIST_TYPE),
array: this.descendInputOfBlock(block, 'ARRAY')
};
case 'data_showlist':
return {
kind: 'list.show',
Expand Down
13 changes: 12 additions & 1 deletion src/compiler/jsexecute.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
const globalState = {
Timer: require('../util/timer'),
Cast: require('../util/cast'),
Clone: require('../util/clone'),
log: require('../util/log'),
blockUtility: require('./compat-block-utility'),
thread: null
Expand Down Expand Up @@ -469,6 +470,16 @@ runtimeFunctions.listReplace = `const listReplace = (list, idx, value) => {
list._monitorUpToDate = false;
}`;

/**
* Set the contents in a list.
* @param {import('../engine/variable')} list The list
* @param {*} array The new contents.
*/
runtimeFunctions.listSet = `const listSet = (list, array) => {
list.value = globalState.Cast.toArray(array);
list._monitorUpToDate = false;
}`;

/**
* Insert a value in a list.
* @param {import('../engine/variable')} list The list.
Expand Down Expand Up @@ -559,7 +570,7 @@ runtimeFunctions.listContents = `const listContents = list => {
* @returns {string} Stringified form of the list.
*/
runtimeFunctions.listArrayContents = `const listArrayContents = list => {
return list.value;
return globalState.Clone.structured(list.value);
}`;

/**
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/jsgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,11 @@ class JSGenerator {
this.source += `if (!${list}.locked) listReplace(${list}, ${this.descendInput(node.index).asUnknown()}, ${this.descendInput(node.item).asSafe()});\n`;
break;
}
case 'list.set': {
const list = this.referenceVariable(node.list);
this.source += `if (!${list}.locked) listSet(${list}, ${this.descendInput(node.array).asUnknown()});\n`;
break;
}
case 'list.show':
this.source += `runtime.monitorBlocks.changeBlock({ id: "${sanitize(node.list.id)}", element: "checkbox", value: true }, runtime);\n`;
break;
Expand Down

0 comments on commit 1a20823

Please sign in to comment.