Skip to content

Commit 458ce4d

Browse files
committed
Add comments about duck typing usage
1 parent e4c3a9e commit 458ce4d

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

src/color/p5.Color.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ class Color {
6161
static #colorjsMaxes = {};
6262
static #grayscaleMap = {};
6363

64-
// For duck typing
64+
// This property is here where duck typing (checking if obj.isColor) needs
65+
// to be used over more standard type checking (obj instanceof Color). This
66+
// needs to happen where we are building multiple files, such as in p5.webgpu.js,
67+
// where if we `import { Color }` directly, it will be a separate copy of the
68+
// Color class from the one imported in the main p5.js bundle.
6569
isColor = true;
6670

6771
// Used to add additional color modes to p5.js

src/math/p5.Vector.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ class Vector {
4848
this.dimensions = dimensions;
4949
this._values = values;
5050
}
51+
52+
// This property is here where duck typing (checking if obj.isVector) needs
53+
// to be used over more standard type checking (obj instanceof Vector). This
54+
// needs to happen where we are building multiple files, such as in p5.webgpu.js,
55+
// where if we `import { Vector }` directly, it will be a separate copy of the
56+
// Vector class from the one imported in the main p5.js bundle.
5157
this.isVector = true;
5258
}
5359

src/webgl/p5.Shader.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,10 @@ class Shader {
10991099
return;
11001100
}
11011101

1102+
// In p5.strands-related code, where some of the code may be in
1103+
// p5.webgpu.js instead of the main p5.js build, we generally use
1104+
// duck typing instead of instanceof to avoid accidentally importing
1105+
// and comparing against a separate copy of p5 classes
11021106
if (data?.isVector) {
11031107
data = data.values.length !== data.dimensions ? data.values.slice(0, data.dimensions) : data.values;
11041108
} else if (data?.isColor) {

src/webgpu/p5.RendererWebGPU.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,6 +1869,9 @@ function rendererWebGPU(p5, fn) {
18691869
// value: number or number[] - the data to write
18701870
_packField(field, value, floatView, dataView, baseOffset) {
18711871
if (value === undefined) return;
1872+
1873+
// Duck typing instead of instanceof to avoid importing a separate
1874+
// copy of the Color/Vector classes
18721875
if (value?.isVector) {
18731876
value = value.values.length !== value.dimensions ? value.values.slice(0, value.dimensions) : value.values;
18741877
} else if (value?.isColor) {
@@ -3113,6 +3116,8 @@ ${hookUniformFields}}
31133116
// Maps a plain JS value to the WGSL type string that represents it in a struct.
31143117
_jsValueToWgslType(value) {
31153118
if (typeof value === 'number') return 'f32';
3119+
// Duck typing instead of instanceof to avoid importing a separate
3120+
// copy of the Color/Vector classes
31163121
if (value?.isVector) {
31173122
if (value.dimensions === 2) return 'vec2f';
31183123
if (value.dimensions === 3) return 'vec3f';
@@ -3148,6 +3153,8 @@ ${hookUniformFields}}
31483153
value !== null &&
31493154
typeof value === 'object' &&
31503155
!Array.isArray(value) &&
3156+
// Duck typing instead of instanceof to avoid importing a separate
3157+
// copy of the Color/Vector classes
31513158
!value?.isVector &&
31523159
!value?.isColor
31533160
) {

0 commit comments

Comments
 (0)