Skip to content

Commit e4ad5ee

Browse files
authored
Merge pull request #7956 from nking07049925/7955_strands_AnonymousFunctionCallbackFix
Fix inline anonymous functions causing a parsing error in p5.strands callbacks
2 parents f3ab0ce + dcc31b7 commit e4ad5ee

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/webgl/ShaderGenerator.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ function shadergenerator(p5, fn) {
2020
if (shaderModifier instanceof Function) {
2121
let generatorFunction;
2222
if (options.parser) {
23-
const sourceString = shaderModifier.toString()
23+
// #7955 Wrap function declaration code in brackets so anonymous functions are not top level statements, which causes an error in acorn when parsing
24+
// https://github.com/acornjs/acorn/issues/1385
25+
const sourceString = `(${shaderModifier.toString()})`;
2426
const ast = parse(sourceString, {
2527
ecmaVersion: 2021,
2628
locations: options.srcLocations

test/unit/webgl/p5.Shader.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ suite('p5.Shader', function() {
324324
expect(modified.fragSrc()).not.to.match(/#define AUGMENTED_HOOK_getVertexColor/);
325325
});
326326

327+
test('anonymous function shaderModifier does not throw when parsed', function() {
328+
const callModify = () => myShader.modify(function() {});
329+
expect(callModify).not.toThrowError();
330+
});
331+
327332
test('filled hooks do have an AUGMENTED_HOOK define', function() {
328333
const modified = myShader.modify({
329334
'vec4 getVertexColor': `(vec4 c) {

0 commit comments

Comments
 (0)