Skip to content

Commit 7558847

Browse files
committed
General bug fixing & make code more robust
1 parent 09d050d commit 7558847

4 files changed

Lines changed: 43 additions & 40 deletions

File tree

dist/bundle.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,10 @@
478478
case "multiply":
479479
return val1 * val2;
480480
case "divide":
481+
if (val2 === 0) return "Error: Division by zero";
481482
return val1 / val2;
482483
case "modulo":
484+
if (val2 === 0) return "Error: Division by zero";
483485
return val1 % val2;
484486
case "power":
485487
return val1 ** val2;
@@ -1949,12 +1951,14 @@
19491951
num1: Argument("string", "200000000000000000000"),
19501952
num2: Argument("string", "2")
19511953
}, ({ num1, num2 }) => {
1954+
if (BigInt(num2) === 0n) return "Error: Division by zero";
19521955
return BigInt(num1) / BigInt(num2);
19531956
}),
19541957
Block(BlockType.REPORTER, "bignumModulo", "[num1] mod [num2]", {
19551958
num1: Argument("string", "100000000000000000001"),
19561959
num2: Argument("string", "100000000000000000000")
19571960
}, ({ num1, num2 }) => {
1961+
if (BigInt(num2) === 0n) return "Error: Division by zero";
19581962
return BigInt(num1) % BigInt(num2);
19591963
}),
19601964
Block(BlockType.REPORTER, "bignumPower", "[num1] ^ [num2]", {

scratchjs.js

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ See more about ScratchJS at https://ironbill25.github.io/projects/scratchjs/`);
1313
window.sjs_extensionBlocks = [];
1414

1515
function chkKey(obj, key) {
16-
if (!obj) return "";
17-
return Object.keys(obj).includes(key) ? obj[key] : "";
16+
if (!obj) return "No object provided";
17+
return Object.keys(obj).includes(key) ? obj[key] : "Key not found";
1818
}
1919

2020
window.sjs_applyViewMode = () => {
@@ -350,7 +350,7 @@ You can get the official bookmarklet here: https://scratch.mit.edu/projects/1316
350350
const timestamp = Date.now();
351351
const response = await fetch(
352352
"https://raw.githubusercontent.com/Ironbill25/JavaScript-For-Scratch/main/dist/bundle.js?t=" +
353-
timestamp, { cache: "no-store" }
353+
timestamp, { cache: "no-store" }
354354
);
355355
if (response.ok) {
356356
const code = await response.text();
@@ -449,9 +449,9 @@ You can get the official bookmarklet here: https://scratch.mit.edu/projects/1316
449449
* @param {Function} fun - The function to execute when the block is run.
450450
* @returns {Object} - The block object.
451451
*/
452-
window.Block = (blockType, opcode, text, args = {}, fun = () => {}, othersettings = {}) => {
452+
window.Block = (blockType, opcode, text, args = {}, fun = () => { }, othersettings = {}) => {
453453

454-
const wrappedFunction = function (...args) {
454+
const wrappedFunction = function (...args) {
455455
try {
456456
return fun.apply(this, args);
457457
} catch (error) {
@@ -579,13 +579,13 @@ const wrappedFunction = function (...args) {
579579
STAGE: "stage",
580580
};
581581

582-
window.addExtensionBlocks = function(blocks) {
582+
window.addExtensionBlocks = function (blocks) {
583583
window.allBlocks = [...window.allBlocks, Spacer, ...blocks];
584584
};
585585

586586
await loadBlockFiles();
587587

588-
588+
589589

590590
window.ScratchJS = class {
591591
constructor(runtime) {
@@ -779,46 +779,41 @@ const wrappedFunction = function (...args) {
779779
localStorage.setItem("scratchjs_devMode", "true");
780780
}
781781

782-
let retryCount = 0;
783-
const maxRetries = 3;
784782

785-
while (retryCount < maxRetries) {
786-
if (retryCount === maxRetries - 1) {
787-
const allBlocks = categories.flatMap(
788-
(category) => window[`sjs_${category}`] || [],
789-
);
790-
allBlocks.push(...(window.sjs_extensionBlocks || []));
783+
const allBlocks = categories.flatMap(
784+
(category) => window[`sjs_${category}`] || [],
785+
);
786+
allBlocks.push(...(window.sjs_extensionBlocks || []));
791787

792-
console.log(allBlocks);
788+
console.log(allBlocks);
793789

794-
const missingFunctions = allBlocks.filter(
795-
(block) =>
796-
block.opcode && !(block.opcode in (window.allFunctions || {})),
797-
);
798-
if (missingFunctions.length > 0) console.warn(
799-
"Missing functions for blocks:",
800-
missingFunctions.map((b) => b.opcode),
801-
);
802-
break;
803-
}
790+
const missingFunctions = allBlocks.filter(
791+
(block) =>
792+
block.opcode && !(block.opcode in (window.allFunctions || {})),
793+
);
794+
if (missingFunctions.length > 0) console.warn(
795+
"Missing functions for blocks:",
796+
missingFunctions.map((b) => b.opcode),
797+
);
804798

805-
await new Promise((resolve) => setTimeout(resolve, 500));
806-
retryCount++;
807-
}
808799

809800
var extensionInstance = new ScratchJS(vm.extensionManager.runtime);
810801

811-
if (viewmode) { for (const [opcode, func] of Object.entries(
812-
window.allFunctions || {},
813-
)) {
814-
extensionInstance[opcode] = () => "This block is disabled in view mode";
815-
}};
816-
817-
if (!viewmode) {for (const [opcode, func] of Object.entries(
818-
window.allFunctions || {},
819-
)) {
820-
extensionInstance[opcode] = func;
821-
}};
802+
if (viewmode) {
803+
for (const [opcode, func] of Object.entries(
804+
window.allFunctions || {},
805+
)) {
806+
extensionInstance[opcode] = () => "This block is disabled in view mode";
807+
}
808+
};
809+
810+
if (!viewmode) {
811+
for (const [opcode, func] of Object.entries(
812+
window.allFunctions || {},
813+
)) {
814+
extensionInstance[opcode] = func;
815+
}
816+
};
822817
var serviceName =
823818
vm.extensionManager._registerInternalExtension(extensionInstance);
824819
vm.extensionManager._loadedExtensions.set(

scratchjsblocks/bignum.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@ window.sjs_bignum = [
4949
num1: Argument("string", "200000000000000000000"),
5050
num2: Argument("string", "2")
5151
}, ({ num1, num2 }) => {
52+
if (BigInt(num2) === 0n) return "Error: Division by zero";
5253
return (BigInt(num1) / BigInt(num2));
5354
}),
5455

5556
Block(BlockType.REPORTER, "bignumModulo", "[num1] mod [num2]", {
5657
num1: Argument("string", "100000000000000000001"),
5758
num2: Argument("string", "100000000000000000000")
5859
}, ({ num1, num2 }) => {
60+
if (BigInt(num2) === 0n) return "Error: Division by zero";
5961
return (BigInt(num1) % BigInt(num2));
6062
}),
6163

scratchjsblocks/booleans.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ window.sjs_booleans = [
5858
case "multiply":
5959
return val1 * val2;
6060
case "divide":
61+
if (val2 === 0) return "Error: Division by zero";
6162
return val1 / val2;
6263
case "modulo":
64+
if (val2 === 0) return "Error: Division by zero";
6365
return val1 % val2;
6466
case "power":
6567
return val1 ** val2;

0 commit comments

Comments
 (0)