Skip to content

Fix inline anonymous functions causing a parsing error in p5.strands callbacks #7956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/webgl/ShaderGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function shadergenerator(p5, fn) {
if (shaderModifier instanceof Function) {
let generatorFunction;
if (options.parser) {
const sourceString = shaderModifier.toString()
const sourceString = `(${shaderModifier.toString()})`;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mind adding a comment here to explain to other contributors why the brackets are necessary?

Copy link
Author

Choose a reason for hiding this comment

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

Would something like this work?

// #7955 Wrap function declaration code in brackets so anonymous functions are not top level statements, which causes an error in acorn when parsing

Copy link
Contributor

Choose a reason for hiding this comment

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

looks great, thanks!

Copy link
Author

Choose a reason for hiding this comment

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

Nice! I will be able to make a commit later today

const ast = parse(sourceString, {
ecmaVersion: 2021,
locations: options.srcLocations
Expand Down
5 changes: 5 additions & 0 deletions test/unit/webgl/p5.Shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ suite('p5.Shader', function() {
expect(modified.fragSrc()).not.to.match(/#define AUGMENTED_HOOK_getVertexColor/);
});

test('anonymous function shaderModifier does not throw when parsed', function() {
const callModify = () => myShader.modify(function() {});
expect(callModify).not.toThrowError();
});

test('filled hooks do have an AUGMENTED_HOOK define', function() {
const modified = myShader.modify({
'vec4 getVertexColor': `(vec4 c) {
Expand Down
Loading