-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add friendly FES error for dimension mismatch on shared variable assi… #8823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
04d1123
76ec529
19462ad
70ada89
e88f62e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,24 @@ | ||
| import p5 from '../../../src/app.js'; | ||
| import { vi } from 'vitest'; | ||
| import { beforeEach, vi } from 'vitest'; | ||
|
|
||
| const mockUserError = vi.fn(); | ||
| vi.mock('../../../src/strands/strands_FES', () => ({ | ||
| userError: (...args) => { | ||
| vi.mock('../../../src/strands/strands_FES', () => { | ||
| const userError = (...args) => { | ||
| mockUserError(...args); | ||
| const prefixedMessage = `[p5.strands ${args[0]}]: ${args[1]}`; | ||
| throw new Error(prefixedMessage); | ||
| }, | ||
| internalError: (msg) => { throw new Error(`[p5.strands internal error]: ${msg}`); } | ||
| })); | ||
| }; | ||
| return { | ||
| userError, | ||
| internalError: (msg) => { throw new Error(`[p5.strands internal error]: ${msg}`); }, | ||
| dimensionMismatchError: (declaredDim, actualDim, varName) => { | ||
| userError( | ||
| 'dimension mismatch', | ||
| `Cannot assign a value of dimension ${actualDim} to \`${varName}\`, which expects dimension ${declaredDim}.` | ||
| ); | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| suite('p5.Shader', function() { | ||
| var myp5; | ||
|
|
@@ -2631,6 +2640,62 @@ test('returns numbers for builtin globals outside hooks and a strandNode when ca | |
| assert.approximately(pixelColor[1], 0, 5); | ||
| assert.approximately(pixelColor[2], 0, 5); | ||
| }); | ||
|
|
||
| test('allows scalar broadcast when assigning a scalar to a sharedVec3 (bridge)', async () => { | ||
| await myp5.createCanvas(5, 5, myp5.WEBGL); | ||
|
|
||
| expect(() => { | ||
| myp5.baseMaterialShader().modify(() => { | ||
| let worldPosX = myp5.sharedVec3(); | ||
| myp5.getWorldInputs(inputs => { | ||
| worldPosX = inputs.position.x; // scalar → vec3, valid broadcast | ||
| return inputs; | ||
| }); | ||
| },{myp5}); | ||
| }).not.toThrow(); | ||
| }); | ||
|
|
||
| test('reports a friendly error when assigning a vec2 to a sharedVec3 (bridge)', async () => { | ||
| await myp5.createCanvas(5, 5, myp5.WEBGL); | ||
|
|
||
| expect(() => { | ||
| myp5.baseMaterialShader().modify(() => { | ||
| let myVec = myp5.sharedVec3(); | ||
| myp5.getWorldInputs(inputs => { | ||
| myVec = inputs.position.xy; // vec2 → vec3 mismatch | ||
| return inputs; | ||
| }); | ||
| },{myp5}); | ||
| }).toThrow(/dimension mismatch/); | ||
| }); | ||
|
|
||
| test('reports a friendly error on dimension mismatch via swizzle write (bridgeSwizzle)', async () => { | ||
| await myp5.createCanvas(5, 5, myp5.WEBGL); | ||
|
|
||
| expect(() => { | ||
| myp5.baseMaterialShader().modify(() => { | ||
| let myVec = myp5.sharedVec3(); | ||
| myp5.getWorldInputs(inputs => { | ||
| myVec.xy = inputs.position; // vec3 → 2-component swizzle mismatch | ||
| return inputs; | ||
| }); | ||
| },{myp5}); | ||
| }).toThrow(/dimension mismatch/); | ||
| }); | ||
|
|
||
| test('does not error when shared variable assignment dimensions match', async () => { | ||
| await myp5.createCanvas(5, 5, myp5.WEBGL); | ||
|
|
||
| expect(() => { | ||
| myp5.baseMaterialShader().modify(() => { | ||
| let myVec = myp5.sharedVec3(); | ||
| myp5.getWorldInputs(inputs => { | ||
| myVec = inputs.position; // vec3 → vec3, OK | ||
| return inputs; | ||
| }); | ||
| },{myp5}); | ||
| }).not.toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| suite('p5.strands error messages', () => { | ||
|
|
@@ -2648,7 +2713,7 @@ test('returns numbers for builtin globals outside hooks and a strandNode when ca | |
| assert.include(err.message, '// noprotect'); | ||
| }; | ||
|
|
||
| afterEach(() => { | ||
| beforeEach(() => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this breaking something?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah — this fixes a pre-existing isolation issue that my new tests exposed. |
||
| mockUserError.mockClear(); | ||
| }); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1361,6 +1361,62 @@ suite('WebGPU p5.Shader', function() { | |
| myp5.compute(s4, 4); | ||
| }).not.toThrow(); | ||
| }); | ||
|
|
||
| test('allows scalar broadcast when assigning a scalar to a sharedVec3 (bridge)', async () => { | ||
| await myp5.createCanvas(5, 5, myp5.WEBGPU); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mind fixing the indentation in this file?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed, re-indented the new tests to match the suite. |
||
|
|
||
| expect(() => { | ||
| myp5.baseMaterialShader().modify(() => { | ||
| let worldPosX = myp5.sharedVec3(); | ||
| myp5.getWorldInputs(inputs => { | ||
| worldPosX = inputs.position.x; // scalar → vec3, valid broadcast | ||
| return inputs; | ||
| }); | ||
| },{myp5}); | ||
| }).not.toThrow(); | ||
| }); | ||
|
|
||
| test('reports a friendly error when assigning a vec2 to a sharedVec3 (bridge)', async () => { | ||
| await myp5.createCanvas(5, 5, myp5.WEBGPU); | ||
|
|
||
| expect(() => { | ||
| myp5.baseMaterialShader().modify(() => { | ||
| let myVec = myp5.sharedVec3(); | ||
| myp5.getWorldInputs(inputs => { | ||
| myVec = inputs.position.xy; // vec2 → vec3 mismatch | ||
| return inputs; | ||
| }); | ||
| },{myp5}); | ||
| }).toThrow(/dimension mismatch/); | ||
| }); | ||
|
|
||
| test('reports a friendly error on dimension mismatch via swizzle write (bridgeSwizzle)', async () => { | ||
| await myp5.createCanvas(5, 5, myp5.WEBGPU); | ||
|
|
||
| expect(() => { | ||
| myp5.baseMaterialShader().modify(() => { | ||
| let myVec = myp5.sharedVec3(); | ||
| myp5.getWorldInputs(inputs => { | ||
| myVec.xy = inputs.position; // vec3 → 2-component swizzle mismatch | ||
| return inputs; | ||
| }); | ||
| },{myp5}); | ||
| }).toThrow(/dimension mismatch/); | ||
| }); | ||
|
|
||
| test('does not error when shared variable assignment dimensions match', async () => { | ||
| await myp5.createCanvas(5, 5, myp5.WEBGPU); | ||
|
|
||
| expect(() => { | ||
| myp5.baseMaterialShader().modify(() => { | ||
| let myVec = myp5.sharedVec3(); | ||
| myp5.getWorldInputs(inputs => { | ||
| myVec = inputs.position; // vec3 → vec3, OK | ||
| return inputs; | ||
| }); | ||
| },{myp5}); | ||
| }).not.toThrow(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely minor, but it might be a bit easier to read to space this out a bit, like:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, spaced it out across all three sites (bridge, bridgeSwizzle, and the setter).