Skip to content

Support ** in p5.strands and error on unsupported operators - #9375

Open
harshiltewari2004 wants to merge 3 commits into
processing:mainfrom
harshiltewari2004:fix-strands-exponent-operator
Open

harshiltewari2004 wants to merge 3 commits into
processing:mainfrom
harshiltewari2004:fix-strands-exponent-operator

Conversation

@harshiltewari2004

Copy link
Copy Markdown
Contributor

Resolves #9252

Changes:

  • replaceBinaryOperator (src/strands/strands_transpiler.js) now maps ** to pow, so a ** b becomes a.pow(b). This also covers **=, since compound assignments go through the same function.
  • Adds StrandsNode.prototype.pow (src/strands/strands_api.js), which builds a pow() function call node rather than a binary operator, since GLSL and WGSL have no ** operator. This resolves the TODO.
  • Adds a default case that throws a strands error naming the operator (e.g. &, <<), instead of crashing with .undefined is not a function / reading 'startsWith' inside the transpiler. This was the second option in the issue; happy to drop it into a separate PR if you'd rather keep this one to ** only.

Tests (new exponentiation operator (#9252) suite in test/unit/webgl/p5.Shader.js):

  • v ** 2 renders the same as pow(v, 2)
  • v **= 2 works
  • an unsupported operator throws unsupported operator

All three failed before the source change and pass after. The change is in the backend-independent transpiler, so I only added WebGL tests; happy to add WebGPU ones too.

npx vitest run: everything passes except test/unit/webgpu/p5.Shader.js → "handle passing a value from a vertex hook to a fragment hook with swizzle assignment", which also fails on a clean main.

PR Checklist

  • npm run lint passes
  • [Inline reference] is included / updated — N/A, no public API change
  • [Unit tests] are included / updated

@p5-bot

p5-bot Bot commented Nov 14, 2026

Copy link
Copy Markdown

Continuous Release

CDN link

Published Packages

Commit hash: 5971e8c

Previous deployments

This is an automated message.

Comment thread test/unit/webgl/p5.Shader.js Outdated
}).not.toThrow();
});

suite('exponentiation operator (#9252)', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's probably not super important to put the PR number here; the idea is to have the code be understandable to other contributors without extra context from github.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, I'll drop it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in a29df88.

myp5.background(0);
myp5.filter(testShader);
const c = myp5.get(25, 25);
assert.approximately(c[0], 64, 5); // 0.5 ** 2 = 0.25 -> ~64

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For completeness, can we add similar tests to the webgpu module too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure, adding them now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added in a29df88, same three cases using the material shader pattern, since filter shader tests are still todo on WebGPU.

Comment thread test/unit/webgpu/p5.Shader.js Outdated
});


suite('exponentiation operator', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

minor, but it looks like indentation is off here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 2509c41,thanks!!

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.js 2.0+ Bug Report]: p5.strands: the ** operator crashes the transpiler with an error pointing at p5.js internals

2 participants