Skip to content

Commit 0b8ff6c

Browse files
committed
Added protected properties before second bindGlobal loop, exclude protected properties from being bound.
1 parent 214e7b3 commit 0b8ff6c

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

src/core/friendly_errors/fes_core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ function fesCore(p5, fn){
348348
// actual name with correct capitalization doesnt exist in context,
349349
// and if the user-defined symbol is of the type function
350350
if (
351-
fxns[lowercase] &&
351+
fxns.hasOwnProperty(lowercase) &&
352352
!context[fxns[lowercase]] &&
353353
typeof context[prop] === 'function'
354354
) {

src/core/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ class p5 {
137137
bindGlobal(p);
138138
}
139139

140+
const protectedProperties = ['constructor', 'length', 'print'];
140141
// Attach its properties to the window
141142
for (const p in this) {
142143
if (this.hasOwnProperty(p)) {
143-
if(p[0] === '_') continue;
144+
if(p[0] === '_' || protectedProperties.includes(p)) continue;
144145
bindGlobal(p);
145146
}
146147
}

test.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script src="/lib/p5.js"></script>
5+
</head>
6+
<body>
7+
<script>
8+
new p5();
9+
</script>
10+
</body>
11+
</html>

test2.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script src="lib/p5.js"></script>
5+
</head>
6+
<body>
7+
<script>
8+
// Instance mode test
9+
new p5((p) => {
10+
p.setup = function() {
11+
p.createCanvas(400, 400);
12+
console.log("Instance mode setup called");
13+
}
14+
15+
// Intentionally miscapitalized function to test error detection
16+
p.mousePressed = function() {
17+
console.log("correct capitalization");
18+
}
19+
20+
p.MousePressed = function() {
21+
console.log("wrong capitalization");
22+
}
23+
24+
// Test constructor-related behavior
25+
p.Constructor = function() {
26+
console.log("shouldn't trigger error");
27+
}
28+
});
29+
</script>
30+
</body>
31+
</html>

0 commit comments

Comments
 (0)