Skip to content

[p5.js 2.0+ Bug Report]: strands not handling comma operator (common in minified js) #9178

Description

@davepagurek

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • WebGPU
  • p5.strands
  • Build process
  • Unit testing
  • Internationalization
  • Friendly errors
  • Other (specify if possible)

p5.js version

2.3.2

Web browser and version

All

Operating system

All

Steps to reproduce this

A common thing you'll see in some JavaScript minifiers is to collapse multiple statements (things separated by semicolons generally) into one expression (something that evaluates to a single value) via the comma operator. E.g. something like this:

let a
if (something) {
  someThingIsTrue();
  a = 1;
} else {
  somethingElse();
  a = 2;
}

...might get compressed into this:

let a = something ? (somethingIsTrue(), 1) : (somethingElse(), 2);

Anyway it seems p5.strands does not handle comma operators well. In the example below, simulating something a minifier produced while using strands at work, transpiles to invalid javascript:

let gradientShader

function setup() {
  createCanvas(400, 400, WEBGL)
  gradientShader = buildMaterialShader(() => {
    let pos = sharedVec4()

    worldInputs.begin(), pos = [worldInputs.position/200, 1], worldInputs.end()
    
    pixelInputs.begin(), pixelInputs.color = pos, pixelInputs.end()
  })
}

function draw() {
  clear()
  shader(gradientShader)
  noStroke()
  plane(width, height)
}

Live: https://editor.p5js.org/davepagurek/sketches/zNjhdJCGO

This transpiles to:

let pos = sharedVec4('pos');
    worldInputs.begin(), pos.bridge(__p5.strandsNode([
        __p5.strandsNode(worldInputs.position).div(200),
        1
    ]));, worldInputs.end();
    pixelInputs.begin(), pixelInputs.color = pos.getValue(), pixelInputs.end();

Note that there's a ;, in there. I think a few spots in our transpiler code have the assumption that each line is its own statement and is emitting semicolons where it shouldn't. We should do a little audit for those and create some test cases and fixes.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions