diff --git a/src/webgl/ShaderGenerator.js b/src/webgl/ShaderGenerator.js index d029ed44ef..a4ecb7c9ae 100644 --- a/src/webgl/ShaderGenerator.js +++ b/src/webgl/ShaderGenerator.js @@ -19,7 +19,9 @@ function shadergenerator(p5, fn) { if (shaderModifier instanceof Function) { let generatorFunction; if (options.parser) { - const sourceString = shaderModifier.toString() + // #7955 Wrap function declaration code in brackets so anonymous functions are not top level statements, which causes an error in acorn when parsing + // https://github.com/acornjs/acorn/issues/1385 + const sourceString = `(${shaderModifier.toString()})`; const ast = parse(sourceString, { ecmaVersion: 2021, locations: options.srcLocations diff --git a/test/unit/webgl/p5.Shader.js b/test/unit/webgl/p5.Shader.js index 00d4d00847..8567280e38 100644 --- a/test/unit/webgl/p5.Shader.js +++ b/test/unit/webgl/p5.Shader.js @@ -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) {