Skip to content

Commit 0624d3d

Browse files
authored
fix: address 4 critical bugs in core utilities and singer logic (sugarlabs#5679)
* fix: address 4 critical bugs in core utilities and singer logic 1. Fix numberOfNotes() crash (ReferenceError) — removed dead code referencing undefined variables left over from an incomplete refactor to saveState. (turtle-singer.js) 2. Fix rationalSum() silently mutating its input arrays — replaced in-place array assignments with local const variables to prevent corrupting the caller's note-timing state. (utils.js) 3. Fix safeSVG() only escaping the first special character — added the global /g flag to all three regex patterns. (utils.js) 4. Fix doUseCamera() using deprecated navigator.getUserMedia API — updated to modern navigator.mediaDevices.getUserMedia() with proper promise-based flow and feature detection. (utils.js) * refactor: adopt CameraManager from upstream PR sugarlabs#5662 Replaced bare hasSetupCamera variable with the CameraManager module introduced in upstream PR sugarlabs#5662. All internal references updated from hasSetupCamera to CameraManager.isSetup while preserving the modern avigator.mediaDevices.getUserMedia() fix. Ran prettier on both changed files.
1 parent 1e11bf2 commit 0624d3d

File tree

2 files changed

+19
-41
lines changed

2 files changed

+19
-41
lines changed

js/turtle-singer.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -660,18 +660,6 @@ class Singer {
660660
tur.painter.doPenUp();
661661
tur.painter.doSetXY(saveState.x, saveState.y);
662662
tur.painter.doSetHeading(saveState.orientation);
663-
tur.painter.doSetXY(saveX, saveY);
664-
tur.painter.color = saveColor;
665-
tur.painter.value = saveValue;
666-
tur.painter.chroma = saveChroma;
667-
tur.painter.stroke = saveStroke;
668-
tur.painter.canvasAlpha = saveCanvasAlpha;
669-
tur.painter.doSetHeading(saveOrientation);
670-
tur.painter.penState = savePenState;
671-
tur.singer.suppressOutput = saveSuppressStatus;
672-
tur.singer.previousTurtleTime = savePrevTurtleTime;
673-
tur.singer.turtleTime = saveTurtleTime;
674-
tur.singer.whichNoteToCount = saveWhichNoteToCount;
675663
tur.singer.justCounting.pop();
676664
tur.butNotThese = {};
677665

js/utils/utils.js

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,14 +1093,6 @@ let doUseCamera = (args, turtles, turtle, isVideo, cameraID, setCameraID, errorM
10931093
let streaming = false;
10941094
const video = document.querySelector("#camVideo");
10951095
const canvas = document.querySelector("#camCanvas");
1096-
navigator.getMedia =
1097-
navigator.getUserMedia ||
1098-
navigator.mozGetUserMedia ||
1099-
navigator.webkitGetUserMedia ||
1100-
navigator.msGetUserMedia;
1101-
if (navigator.getMedia === undefined) {
1102-
errorMsg("Your browser does not support the webcam");
1103-
}
11041096

11051097
function draw() {
11061098
canvas.width = w;
@@ -1111,24 +1103,23 @@ let doUseCamera = (args, turtles, turtle, isVideo, cameraID, setCameraID, errorM
11111103
}
11121104

11131105
if (!CameraManager.isSetup) {
1114-
navigator.getMedia(
1115-
{ video: true, audio: false },
1116-
stream => {
1117-
if (navigator.mozGetUserMedia) {
1118-
video.mozSrcObject = stream;
1119-
} else {
1120-
video.srcObject = stream;
1121-
}
1106+
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
1107+
errorMsg("Your browser does not support the webcam");
1108+
return;
1109+
}
11221110

1111+
navigator.mediaDevices
1112+
.getUserMedia({ video: true, audio: false })
1113+
.then(stream => {
1114+
video.srcObject = stream;
11231115
video.play();
11241116
CameraManager.isSetup = true;
1125-
},
1126-
error => {
1117+
})
1118+
.catch(error => {
11271119
errorMsg("Could not connect to camera");
11281120
// eslint-disable-next-line no-console
11291121
console.debug(error);
1130-
}
1131-
);
1122+
});
11321123
} else {
11331124
streaming = true;
11341125
video.play();
@@ -1219,7 +1210,7 @@ function displayMsg(/*blocks, text*/) {
12191210
*/
12201211
function safeSVG(label) {
12211212
if (typeof label === "string") {
1222-
return label.replace(/&/, "&amp;").replace(/</, "&lt;").replace(/>/, "&gt;");
1213+
return label.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
12231214
} else {
12241215
return label;
12251216
}
@@ -1414,16 +1405,15 @@ let rationalSum = (a, b) => {
14141405
} else {
14151406
objb1 = [b[1], 1];
14161407
}
1417-
1418-
a[0] = obja0[0] * obja1[1];
1419-
a[1] = obja0[1] * obja1[0];
1420-
b[0] = objb0[0] * objb1[1];
1421-
b[1] = objb0[1] * objb1[0];
1408+
// Use local variables to avoid mutating the caller's arrays
1409+
const a0 = obja0[0] * obja1[1];
1410+
const a1 = obja0[1] * obja1[0];
1411+
const b0 = objb0[0] * objb1[1];
1412+
const b1 = objb0[1] * objb1[0];
14221413

14231414
// Find the least common denomenator
1424-
const lcd = LCD(a[1], b[1]);
1425-
// const c0 = (a[0] * lcd) / a[1] + (b[0] * lcd) / b[1];
1426-
return [(a[0] * lcd) / a[1] + (b[0] * lcd) / b[1], lcd];
1415+
const lcd = LCD(a1, b1);
1416+
return [(a0 * lcd) / a1 + (b0 * lcd) / b1, lcd];
14271417
};
14281418

14291419
/**

0 commit comments

Comments
 (0)