Skip to content

Commit 25d5e48

Browse files
author
Miyo Sho
committed
Fix cubesters implementation of some blocks
1 parent d571363 commit 25d5e48

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

src/blocks/scratch3_json.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class Scratch3JSONBlocks {
8585
mergeObject (args) {
8686
args.OBJ1 = Cast.toObject(args.OBJ1);
8787
args.OBJ2 = Cast.toObject(args.OBJ2);
88-
return {...args.OBJ1, ...args.OBJ2};
88+
return Object.fromEntries(Object.entries(args.OBJ1).concat(Object.entries(args.OBJ2)));
8989
}
9090

9191
hasKey (args) {
@@ -151,7 +151,7 @@ class Scratch3JSONBlocks {
151151
mergeArray (args) {
152152
args.ARR1 = Cast.toArray(args.ARR1);
153153
args.ARR2 = Cast.toArray(args.ARR2);
154-
return [...args.ARR1, ...args.ARR2];
154+
return args.ARR1.concat(args.ARR2);
155155
}
156156

157157
hasItem (args) {

src/compiler/jsexecute.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,24 @@ const globalState = {
2020
let baseRuntime = '';
2121
const runtimeFunctions = {};
2222

23-
/** Null coalshing function */
23+
/**
24+
* Null coalescing just incase it's not supported by the browser.
25+
* @param {any} n
26+
* @param {any} v
27+
* @returns {!any}
28+
*/
2429
baseRuntime += `const nullCoalsh = (n, v) => ((n === null || n === (void 0)) ? v : n);`;
2530

31+
/**
32+
* Merges 2 objects. (use this to get around spread overflow)
33+
* @param {object} a
34+
* @param {object} b
35+
* @returns {object}
36+
*/
37+
runtimeFunctions.mergeObjects = `const mergeObjects = (a, b) => {
38+
return Object.fromEntries(Object.entries(a).concat(Object.entries(b)));
39+
};`;
40+
2641
/**
2742
* Determine whether the current tick is likely stuck.
2843
* This implements similar functionality to the warp timer found in Scratch.

src/compiler/jsgen.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const stringify = (object, type) => {
3434
if (type === 'array' && !Array.isArray(object)) return '[]';
3535
if (type === 'object' && Array.isArray(object)) return '{}';
3636
}
37-
console.log(object, type);
3837
return ((typeof object === 'string') ? object : JSON.stringify(object ?? (
3938
type === 'object' ? (new Object()) : (new Array())
4039
)));
@@ -536,11 +535,11 @@ class JSGenerator {
536535
case 'json.valueOfKey':
537536
return new TypedInput(`(${this.descendInput(node.object).asObject()}[${this.descendInput(node.key).asString()}] ?? "")`, TYPE_STRING);
538537
case 'json.setKey':
539-
return new TypedInput(`(object = {...${this.descendInput(node.object).asObject()}}, object[${this.descendInput(node.key).asString()}] = ${this.descendInput(node.value).asUnknown()}, object)`, TYPE_OBJECT);
538+
return new TypedInput(`(object = Object.assign({}, ${this.descendInput(node.object).asObject()}), object[${this.descendInput(node.key).asString()}] = ${this.descendInput(node.value).asUnknown()}, object)`, TYPE_OBJECT);
540539
case 'json.deleteKey':
541-
return new TypedInput(`(object = {...${this.descendInput(node.object).asObject()}}, delete object[${this.descendInput(node.key).asString()}], object)`, TYPE_OBJECT);
540+
return new TypedInput(`(object = Object.assign({}, ${this.descendInput(node.object).asObject()}), delete object[${this.descendInput(node.key).asString()}], object)`, TYPE_OBJECT);
542541
case 'json.mergeObject':
543-
return new TypedInput(`{...${this.descendInput(node.object1).asObject()}, ...${this.descendInput(node.object2).asObject()}}`, TYPE_OBJECT);
542+
return new TypedInput(`mergeObjects(${this.descendInput(node.object1).asObject()}, ${this.descendInput(node.object2).asObject()})`, TYPE_OBJECT);
544543
case 'json.hasKey':
545544
return new TypedInput(`${this.descendInput(node.object).asObject()}.hasOwnProperty(${this.descendInput(node.key).asString()})`, TYPE_BOOLEAN);
546545
case 'json.newArray':
@@ -552,15 +551,15 @@ class JSGenerator {
552551
case 'json.indexOfValue':
553552
return new TypedInput(`(${this.descendInput(node.array).asArray()}.indexOf(${this.descendInput(node.value).asUnknown()}) !== -1 ? ${this.descendInput(node.array).asArray()}.indexOf(${this.descendInput(node.value).asUnknown()}) : "")`, TYPE_NUMBER);
554553
case 'json.addItem':
555-
return new TypedInput(`(array = [...${this.descendInput(node.array).asArray()}], array.push(${this.descendInput(node.item).asUnknown()}), array)`, TYPE_ARRAY);
554+
return new TypedInput(`(array = ${this.descendInput(node.array).asArray()}.slice(0), array.push(${this.descendInput(node.item).asUnknown()}), array)`, TYPE_ARRAY);
556555
case 'json.replaceIndex':
557556
return new TypedInput(`(${this.descendInput(node.index).asNumber()} >= 0 && ${this.descendInput(node.index).asNumber()} < ${this.descendInput(node.array).asArray()}.length ? (array = [...${this.descendInput(node.array).asArray()}], array[${this.descendInput(node.index).asNumber()}] = ${this.descendInput(node.item).asUnknown()}, array) : new Array())`, TYPE_ARRAY);
558557
case 'json.deleteIndex':
559558
return new TypedInput(`(${this.descendInput(node.index).asNumber()} >= 0 && ${this.descendInput(node.index).asNumber()} < ${this.descendInput(node.array).asArray()}.length ? (array = [...${this.descendInput(node.array).asArray()}], array.splice(${this.descendInput(node.index).asNumber()}, 1), array) : new Array())`, TYPE_ARRAY);
560559
case 'json.deleteAllOccurrences':
561560
return new TypedInput(`${this.descendInput(node.array).asArray()}.filter((item) => item !== ${this.descendInput(node.item).asString()})`, TYPE_ARRAY);
562561
case 'json.mergeArray':
563-
return new TypedInput(`[...${this.descendInput(node.array1).asArray()}, ...${this.descendInput(node.array2).asArray()}]`, TYPE_ARRAY);
562+
return new TypedInput(`${this.descendInput(node.array1).asArray()}.concat(${this.descendInput(node.array2).asArray()})`, TYPE_ARRAY);
564563
case 'json.hasItem':
565564
return new TypedInput(`${this.descendInput(node.array).asArray()}.includes(${this.descendInput(node.item).asUnknown()})`, TYPE_BOOLEAN);
566565

0 commit comments

Comments
 (0)