Skip to content

Conversation

@Aayushdev18
Copy link

@Aayushdev18 Aayushdev18 commented Oct 24, 2025

Resolves #8172

Changes:

  • Added detection for outside variable references in p5.strands uniform functions
  • Implemented simple string checking for common problematic variables (mouseX, windowWidth, state_, pixelate)
  • Integrated error detection into the existing setDefaultUniforms() method in p5.Shader.js
  • Added helpful error message using p5._friendlyError() when outside variables are referenced

The implementation detects when users reference variables not defined within the shader's scope in p5.strands callbacks and provides clear feedback, improving the debugging experience for users working with shader uniforms.

Screenshots of the change:

N/A - This is a backend error detection feature

PR Checklist

  • npm run lint passes
  • [Inline reference] is included / updated
  • [Unit tests] are included / updated

- Detect common problematic variables (mouseX, windowWidth, state_, pixelate) in uniform functions
- Provide helpful error message when outside variables are referenced
- Addresses GSoC issue processing#8172 for better user experience
@welcome
Copy link

welcome bot commented Oct 24, 2025

🎉 Thanks for opening this pull request! For guidance on contributing, check out our contributor guidelines and other resources for contributors!
🤔 Please ensure that your PR links to an issue, which has been approved for work by a maintainer; otherwise, there might already be someone working on it, or still ongoing discussion about implementation. You are welcome to join the discussion in an Issue if you're not sure!
🌸 Once your PR is merged, be sure to add yourself to the list of contributors on the readme page !

Thank You!

if (initializer instanceof Function) {
// Check for common outside variable references
const funcString = initializer.toString();
if (funcString.includes('mouseX') || funcString.includes('windowWidth') ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll want to go with a different approach than manually trying to make a list of variable names to exclude. A user could create a variable named anything they want, so we need something more robust to capture that. The test code in the issue was just an example.

We may need to detect variable declarations in the transpilation phase (https://github.com/processing/p5.js/blob/dev-2.0/src/strands/strands_transpiler.js) and then see if there are references to variables not included in that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @davepagurek for the feedback! You're absolutely right - checking for hardcoded variable names isn't robust. I've removed that implementation.

Following your suggestion, I need to implement this during the transpilation phase in strands_transpiler.js. The approach should:

  1. Track variable declarations during transpilation to build a scope
  2. Analyze uniform initializer functions to see which variables they reference
  3. Check if any referenced variables aren't in the strand's scope
  4. Provide helpful errors

Could you provide some guidance on how to best integrate this with the existing transpiler? Should I add a new callback to the ASTCallbacks object, or would it be better to analyze the AST separately before/after transpilation?
Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since nothing actually needs to be rewritten, this could be a new phase (maybe run before actual transpilation), but feel free to reuse a lot of the structure of the transpiler, since acorn will be a valuable tool in detecting these. You can use AST Explorer and change the AST to Acorn in the header to see how Acorn parses code, which can help you figure out what structures to look for when detecting declarations.

This is also the kind of thing that would really benefit from unit tests so we can see what's covered.

Following reviewer feedback, removed the approach of checking for specific
variable names (mouseX, windowWidth, etc.) as it's not robust.

Added TODO indicating this should be implemented in strands_transpiler.js
during the transpilation phase by tracking variable declarations and
detecting references to variables outside the strand's scope.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[p5.strands] Detect references to outside variables and give helpful feedback

2 participants