Skip to content

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

Open
harshiltewari2004 wants to merge 1 commit into
processing:mainfrom
harshiltewari2004:fix-strands-exponent-operator
Open

harshiltewari2004 wants to merge 1 commit 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

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

1 participant