Fix build*Shader methods in instance mode by forwarding optional scope#8523
Conversation
|
Thanks for taking this on! It's looking good so far, but can we add a unit test for one of these methods to make sure it continues to work going forward? Also, can we add the optional context parameter to the doc comments, mirroring what is found in the |
|
Hii @davepagurek I have added the unit test for scope forwarding and updated the JSDoc comments as suggested. All tests pass locally. Thanks ❤️ |
test/unit/webgl/p5.Shader.scope.js
Outdated
| @@ -0,0 +1,25 @@ | |||
| import p5 from '../../../src/app.js'; | |||
|
|
|||
| suite('p5.Shader', function() { | |||
There was a problem hiding this comment.
Hi! The test looks good, is there a reason why this is in its own file as opposed to the existing p5.Shader unit tests file? It might be better in there.
Also, generally the tests in that file don't create a new instance, they reuse a myp5 instance. To have less moving parts, they also omit a setup function, they just create a canvas, initialize whatever they need to, and then make their assertions. This means you wouldn't need a done function.
There was a problem hiding this comment.
Thanks, that's right! I will move the test into p5.Shader.js and update it to reuse myp5 like the other tests, without creating a new instance or using done.
test/unit/webgl/p5.Shader.scope.js
Outdated
| let receivedSketch = null; | ||
|
|
||
| sketch.buildMaterialShader(function({ sketch: s }) { | ||
| receivedSketch = s; |
There was a problem hiding this comment.
p5.strands shaders don't have the ability to assign to variables outside of their functions -- since we transpile them, they lose all external scope, which is the reason why we have to manually pass scope in.
Rather than doing something like this in the test, we can maybe just use scope the way we use it in the .modify calls in the other tests, e.g. this call, before your changes, wouldn't work with buildMaterialShader:
p5.js/test/unit/webgl/p5.Shader.js
Lines 555 to 565 in 3a7992c
We can just wrap the creation of the shader in an expect(() => { /* ... */ }).not.toThrow() call maybe?
There was a problem hiding this comment.
Thanks for explaining, I will update the test to follow the existing .modify() pattern and wrap the shader creation in an expect(() => {}).not.toThrowError() call instead of assigning to external variables.
478e147 to
bec8391
Compare
|
Hi @davepagurek . I have moved the scope test into p5.Shader.js under the existing p5.strands suite and aligned it with the surrounding test style. All tests have passed locally. Thanks 🙂 |
davepagurek
left a comment
There was a problem hiding this comment.
Awesome! thanks again!
Thanks a lot for the review and guidance, I really appreciate the time you took to explain the test structure & strands behavior. Learned a lot from this one 🙂 |
Resolves #8520
Changes
The five build*Shader methods in
src/webgl/material.jswere calling.modify(callback)without forwarding the optionalscopeparameter.In instance mode, users pass { p5 } or { sketch } as scope so the callback receives the correct instance. Since this argument was not forwarded, the callback didn’t receive the expected context.
This PR updates these methods to accept an optional
scopeparameter and pass it to.modify(callback, scope)No changes are made to .modify() itself, and existing code without scope continues to work.
I have also added a unit test under test/unit/webgl/p5.Shader.js (inside the existing p5.strands suite) to verify that buildMaterialShader correctly forwards the optional scope to .modify(). JSDoc comments were updated to document the optional scope parameter.
PR Checklist
npm run lintpasses