Skip to content

Commit 01517db

Browse files
committed
Pass p5/FES into Vector and decorators
1 parent b2a0972 commit 01517db

5 files changed

Lines changed: 67 additions & 50 deletions

File tree

src/core/main.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class p5 {
5050
constructor(sketch, node) {
5151
// Apply addon defined decorations
5252
if(p5.decorations.size > 0){
53-
decorateClass(p5, p5.decorations, 'p5');
53+
decorateClass(p5, p5.decorations, 'p5', p5);
5454
p5.decorations.clear();
5555
}
5656

@@ -520,7 +520,7 @@ function createBindGlobal(instance) {
520520
}
521521

522522
// Generic function to decorate classes
523-
function decorateClass(Target, decorations, path){
523+
function decorateClass(Target, decorations, path, p5){
524524
path ??= Target.name;
525525
// Static properties
526526
for(const key in Target){
@@ -532,7 +532,8 @@ function decorateClass(Target, decorations, path){
532532
const result = decorator(Target[key], {
533533
kind: 'method',
534534
name: key,
535-
static: true
535+
static: true,
536+
p5
536537
});
537538
if(result){
538539
Object.defineProperty(Target, key, {
@@ -545,7 +546,8 @@ function decorateClass(Target, decorations, path){
545546
const result = decorator(undefined, {
546547
kind: 'field',
547548
name: key,
548-
static: true
549+
static: true,
550+
p5
549551
});
550552
if(result && typeof result === 'function'){
551553
Target[key] = result(Target[key]);
@@ -555,7 +557,7 @@ function decorateClass(Target, decorations, path){
555557
}
556558

557559
if(typeof Target[key] === 'function' && Target[key].prototype){
558-
decorateClass(Target[key], decorations, `${path}.${key}`);
560+
decorateClass(Target[key], decorations, `${path}.${key}`, p5);
559561
}
560562
}
561563
}
@@ -570,7 +572,8 @@ function decorateClass(Target, decorations, path){
570572
const result = decorator(Target.prototype[member], {
571573
kind: 'method',
572574
name: member,
573-
static: false
575+
static: false,
576+
p5
574577
});
575578
if(result) {
576579
Object.defineProperty(Target.prototype, member, {
@@ -588,7 +591,8 @@ function decorateClass(Target, decorations, path){
588591
const result = decorator(undefined, {
589592
kind: 'field',
590593
name: member,
591-
static: false
594+
static: false,
595+
p5
592596
});
593597
Object.defineProperty(Target.prototype, member, {
594598
enumerable: true,
@@ -602,12 +606,14 @@ function decorateClass(Target, decorations, path){
602606
const getterResult = decorator(get, {
603607
kind: 'getter',
604608
name: member,
605-
static: false
609+
static: false,
610+
p5
606611
});
607612
const setterResult = decorator(set, {
608613
kind: 'setter',
609614
name: member,
610-
static: false
615+
static: false,
616+
p5
611617
});
612618
Object.defineProperty(Target.prototype, member, {
613619
enumerable: true,

src/image/p5.Image.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class Image {
3131
this.pixels = [];
3232
}
3333

34+
// This will get overwritten when exported as part of p5.
35+
_friendlyError(_e) {}
36+
3437
/**
3538
* Gets or sets the pixel density for high pixel density displays.
3639
*
@@ -1605,7 +1608,7 @@ class Image {
16051608
props.displayIndex = index;
16061609
this.drawingContext.putImageData(props.frames[index].image, 0, 0);
16071610
} else {
1608-
p5._friendlyError(
1611+
this._friendlyError(
16091612
'Cannot set GIF to a frame number that is higher than total number of frames or below zero.',
16101613
'setFrame'
16111614
);
@@ -2105,6 +2108,8 @@ function image(p5, fn){
21052108
*/
21062109
p5.Image = Image;
21072110

2111+
Image.prototype._friendlyError = p5._friendlyError;
2112+
21082113
/**
21092114
* The image's width in pixels.
21102115
*

src/math/p5.Vector.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import * as constants from '../core/constants';
77

8-
/**
8+
/**
99
* This function is used by binary vector operations to prioritize shorter vectors,
1010
* and to emit a warning when lengths do not match.
1111
*/
@@ -14,7 +14,7 @@ const prioritizeSmallerDimension = function(currentVectorDimension, args) {
1414

1515
//if (args.length !== currentVectorDimension && args.length !== 1) {
1616
// TODO how to suppress for valid solo arguments?
17-
// p5._friendlyError(
17+
// this._friendlyError(
1818
// `Operating on two vectors of different sizes, the smaller dimension is used. In this operation, both vector will be treated as ${minDimension}D vectors, and any additional values of the longer vector will be ignored.`, 'p5.Vector'
1919
//);
2020
//}
@@ -55,7 +55,7 @@ class Vector {
5555
constructor(...args) {
5656

5757
if (args.length === 0) {
58-
p5._friendlyError(
58+
this._friendlyError(
5959
'Requires valid arguments.', 'p5.Vector'
6060
);
6161
}
@@ -69,7 +69,7 @@ class Vector {
6969

7070
this.values = [];
7171
if(Array.isArray(args) && !args.every(v => typeof v === 'number' && Number.isFinite(v))){
72-
p5._friendlyError(
72+
this._friendlyError(
7373
'Arguments contain non-finite numbers',
7474
target.name
7575
);
@@ -85,6 +85,9 @@ class Vector {
8585
this.isVector = true;
8686
}
8787

88+
// This will get overwritten when exported as part of p5.
89+
_friendlyError(_e) {}
90+
8891
get dimensions(){
8992
return this.values.length;
9093
}
@@ -123,7 +126,7 @@ class Vector {
123126
if (index < this.values.length) {
124127
return this.values[index];
125128
} else {
126-
p5._friendlyError(
129+
this._friendlyError(
127130
'The index parameter is trying to set a value outside the bounds of the vector',
128131
'p5.Vector.setValue'
129132
);
@@ -148,7 +151,7 @@ class Vector {
148151
if (index < this.values.length) {
149152
this.values[index] = value;
150153
} else {
151-
p5._friendlyError(
154+
this._friendlyError(
152155
'The index parameter is trying to set a value outside the bounds of the vector',
153156
'p5.Vector.setValue'
154157
);
@@ -3051,7 +3054,7 @@ class Vector {
30513054
if (!target) {
30523055
target = v1.copy();
30533056
if (arguments.length === 3) {
3054-
p5._friendlyError(
3057+
this._friendlyError(
30553058
'The target parameter is undefined, it should be of type p5.Vector',
30563059
'p5.Vector.add'
30573060
);
@@ -3098,7 +3101,7 @@ class Vector {
30983101
if (!target) {
30993102
target = v1.copy();
31003103
if (arguments.length === 3) {
3101-
p5._friendlyError(
3104+
this._friendlyError(
31023105
'The target parameter is undefined, it should be of type p5.Vector',
31033106
'p5.Vector.sub'
31043107
);
@@ -3142,7 +3145,7 @@ class Vector {
31423145
if (!target) {
31433146
target = v.copy();
31443147
if (arguments.length === 3) {
3145-
p5._friendlyError(
3148+
this._friendlyError(
31463149
'The target parameter is undefined, it should be of type p5.Vector',
31473150
'p5.Vector.mult'
31483151
);
@@ -3168,7 +3171,7 @@ class Vector {
31683171
target = v.copy();
31693172
} else {
31703173
if (!(target instanceof Vector)) {
3171-
p5._friendlyError(
3174+
this._friendlyError(
31723175
'The target parameter should be of type p5.Vector',
31733176
'p5.Vector.rotate'
31743177
);
@@ -3212,7 +3215,7 @@ class Vector {
32123215
target = v.copy();
32133216

32143217
if (arguments.length === 3) {
3215-
p5._friendlyError(
3218+
this._friendlyError(
32163219
'The target parameter is undefined, it should be of type p5.Vector',
32173220
'p5.Vector.div'
32183221
);
@@ -3280,7 +3283,7 @@ class Vector {
32803283
if (!target) {
32813284
target = v1.copy();
32823285
if (arguments.length === 4) {
3283-
p5._friendlyError(
3286+
this._friendlyError(
32843287
'The target parameter is undefined, it should be of type p5.Vector',
32853288
'p5.Vector.lerp'
32863289
);
@@ -3310,7 +3313,7 @@ class Vector {
33103313
if (!target) {
33113314
target = v1.copy();
33123315
if (arguments.length === 4) {
3313-
p5._friendlyError(
3316+
this._friendlyError(
33143317
'The target parameter is undefined, it should be of type p5.Vector',
33153318
'p5.Vector.slerp'
33163319
);
@@ -3364,7 +3367,7 @@ class Vector {
33643367
target = v.copy();
33653368
} else {
33663369
if (!(target instanceof Vector)) {
3367-
p5._friendlyError(
3370+
this._friendlyError(
33683371
'The target parameter should be of type p5.Vector',
33693372
'p5.Vector.normalize'
33703373
);
@@ -3390,7 +3393,7 @@ class Vector {
33903393
target = v.copy();
33913394
} else {
33923395
if (!(target instanceof Vector)) {
3393-
p5._friendlyError(
3396+
this._friendlyError(
33943397
'The target parameter should be of type p5.Vector',
33953398
'p5.Vector.limit'
33963399
);
@@ -3416,7 +3419,7 @@ class Vector {
34163419
target = v.copy();
34173420
} else {
34183421
if (!(target instanceof Vector)) {
3419-
p5._friendlyError(
3422+
this._friendlyError(
34203423
'The target parameter should be of type p5.Vector',
34213424
'p5.Vector.setMag'
34223425
);
@@ -3472,7 +3475,7 @@ class Vector {
34723475
target = incidentVector.copy();
34733476
} else {
34743477
if (!(target instanceof Vector)) {
3475-
p5._friendlyError(
3478+
this._friendlyError(
34763479
'The target parameter should be of type p5.Vector',
34773480
'p5.Vector.reflect'
34783481
);
@@ -3513,7 +3516,7 @@ class Vector {
35133516
} else if (v1 instanceof Array) {
35143517
v = new Vector().set(v1);
35153518
} else {
3516-
p5._friendlyError(
3519+
this._friendlyError(
35173520
'The v1 parameter should be of type Array or p5.Vector',
35183521
'p5.Vector.equals'
35193522
);
@@ -3601,6 +3604,8 @@ function vector(p5, fn) {
36013604
*/
36023605
p5.Vector = Vector;
36033606

3607+
Vector.prototype._friendlyError = p5._friendlyError;
3608+
36043609
/**
36053610
* The x component of the vector
36063611
* @type {Number}

src/math/patch-vector.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Vector } from './p5.Vector.js';
44
* @private
55
* @internal
66
*/
7-
export function _defaultEmptyVector(target){
7+
export function _defaultEmptyVector(target, { p5 }){
88
return function(...args){
99
if(args.length === 0){
1010
p5._friendlyError(
@@ -27,7 +27,7 @@ export function _defaultEmptyVector(target){
2727
* @internal
2828
*/
2929
export function _validatedVectorOperation(expectsSoloNumberArgument){
30-
return function(target){
30+
return function(target, { p5 }){
3131
return function(...args){
3232
if (args.length === 0) {
3333
// No arguments? No action

test/unit/math/p5.Vector.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Vector } from '../../../src/math/p5.Vector.js';
1+
import { default as vector, Vector } from '../../../src/math/p5.Vector.js';
22
import { default as math } from '../../../src/math/math.js';
33
import { _defaultEmptyVector, _validatedVectorOperation } from '../../../src/math/patch-vector.js';
44
import { vi } from 'vitest';
@@ -7,33 +7,34 @@ import { vi } from 'vitest';
77
suite('p5.Vector', function () {
88
var v;
99

10-
const mockP5 = {};
10+
let FESCalled = false;
11+
const mockP5 = {
12+
_friendlyError: function(msg, func) {
13+
FESCalled = true;
14+
console.warn(msg);
15+
}
16+
};
17+
const options = { p5: mockP5 };
1118
const mockP5Prototype = {};
1219

1320
beforeAll(async function () {
1421
// Makes createVector available
1522
mockP5.Vector = Vector;
1623
math(mockP5, mockP5Prototype);
24+
vector(mockP5, mockP5Prototype);
1725

1826
// Ensures all decorators are used by unit tests
1927
mockP5Prototype.createVector = _defaultEmptyVector(
20-
mockP5Prototype.createVector
28+
mockP5Prototype.createVector,
29+
options
2130
);
2231

2332
// The following mocks simulate the validation decorator
24-
Vector.prototype.add = _validatedVectorOperation(false)(Vector.prototype.add);
25-
Vector.prototype.sub = _validatedVectorOperation(false)(Vector.prototype.sub);
26-
Vector.prototype.mult = _validatedVectorOperation(true)(Vector.prototype.mult);
27-
Vector.prototype.rem = _validatedVectorOperation(true)(Vector.prototype.rem);
28-
Vector.prototype.div = _validatedVectorOperation(true)(Vector.prototype.div);
29-
30-
globalThis.FESCalled = false;
31-
globalThis.p5 = {
32-
_friendlyError: function(msg, func) {
33-
globalThis.FESCalled = true;
34-
console.warn(msg);
35-
}
36-
};
33+
Vector.prototype.add = _validatedVectorOperation(false)(Vector.prototype.add, options);
34+
Vector.prototype.sub = _validatedVectorOperation(false)(Vector.prototype.sub, options);
35+
Vector.prototype.mult = _validatedVectorOperation(true)(Vector.prototype.mult, options);
36+
Vector.prototype.rem = _validatedVectorOperation(true)(Vector.prototype.rem, options);
37+
Vector.prototype.div = _validatedVectorOperation(true)(Vector.prototype.div, options);
3738
});
3839

3940
afterEach(function () {});
@@ -2091,9 +2092,9 @@ suite('p5.Vector', function () {
20912092
test('should throw friendly error if attempting to get element outside length',
20922093
function () {
20932094
let vect = new Vector(1, 2, 3, 4);
2094-
globalThis.FESCalled = false;
2095+
FESCalled = false;
20952096
assert.equal(vect.getValue(5), undefined);
2096-
assert.equal(globalThis.FESCalled, true);
2097+
assert.equal(FESCalled, true);
20972098
}
20982099
);
20992100
});
@@ -2111,9 +2112,9 @@ suite('p5.Vector', function () {
21112112
test('should throw friendly error if attempting to set element outside lenght',
21122113
function () {
21132114
let vect = new Vector(1, 2, 3, 4);
2114-
globalThis.FESCalled = false;
2115+
FESCalled = false;
21152116
vect.setValue(100, 7);
2116-
assert.equal(globalThis.FESCalled, true);
2117+
assert.equal(FESCalled, true);
21172118
}
21182119
);
21192120
});

0 commit comments

Comments
 (0)