Skip to content

Commit 8e03625

Browse files
authored
Merge pull request #8301 from processing/strands-cleanup
Makes sure p5.strands cleans up after an error
2 parents 8d148fb + 3d353ca commit 8e03625

File tree

4 files changed

+49
-30
lines changed

4 files changed

+49
-30
lines changed

src/strands/p5.strands.js

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -65,39 +65,41 @@ function strands(p5, fn) {
6565

6666
p5.Shader.prototype.modify = function(shaderModifier, scope = {}) {
6767
if (shaderModifier instanceof Function) {
68-
// Reset the context object every time modify is called;
69-
// const backend = glslBackend;
70-
initStrandsContext(strandsContext, glslBackend, { active: true });
71-
createShaderHooksFunctions(strandsContext, fn, this);
72-
// TODO: expose this, is internal for debugging for now.
73-
const options = { parser: true, srcLocations: false };
68+
try {
69+
// Reset the context object every time modify is called;
70+
// const backend = glslBackend;
71+
initStrandsContext(strandsContext, glslBackend, { active: true });
72+
createShaderHooksFunctions(strandsContext, fn, this);
73+
// TODO: expose this, is internal for debugging for now.
74+
const options = { parser: true, srcLocations: false };
7475

75-
// 1. Transpile from strands DSL to JS
76-
let strandsCallback;
77-
if (options.parser) {
78-
// #7955 Wrap function declaration code in brackets so anonymous functions are not top level statements, which causes an error in acorn when parsing
79-
// https://github.com/acornjs/acorn/issues/1385
80-
const sourceString = `(${shaderModifier.toString()})`;
81-
strandsCallback = transpileStrandsToJS(p5, sourceString, options.srcLocations, scope);
82-
} else {
83-
strandsCallback = shaderModifier;
84-
}
85-
86-
// 2. Build the IR from JavaScript API
87-
const globalScope = createBasicBlock(strandsContext.cfg, BlockType.GLOBAL);
88-
pushBlock(strandsContext.cfg, globalScope);
89-
strandsCallback();
90-
popBlock(strandsContext.cfg);
76+
// 1. Transpile from strands DSL to JS
77+
let strandsCallback;
78+
if (options.parser) {
79+
// #7955 Wrap function declaration code in brackets so anonymous functions are not top level statements, which causes an error in acorn when parsing
80+
// https://github.com/acornjs/acorn/issues/1385
81+
const sourceString = `(${shaderModifier.toString()})`;
82+
strandsCallback = transpileStrandsToJS(p5, sourceString, options.srcLocations, scope);
83+
} else {
84+
strandsCallback = shaderModifier;
85+
}
9186

92-
// 3. Generate shader code hooks object from the IR
93-
// .......
94-
const hooksObject = generateShaderCode(strandsContext);
87+
// 2. Build the IR from JavaScript API
88+
const globalScope = createBasicBlock(strandsContext.cfg, BlockType.GLOBAL);
89+
pushBlock(strandsContext.cfg, globalScope);
90+
strandsCallback();
91+
popBlock(strandsContext.cfg);
9592

96-
// Reset the strands runtime context
97-
deinitStrandsContext(strandsContext);
93+
// 3. Generate shader code hooks object from the IR
94+
// .......
95+
const hooksObject = generateShaderCode(strandsContext);
9896

99-
// Call modify with the generated hooks object
100-
return oldModify.call(this, hooksObject);
97+
// Call modify with the generated hooks object
98+
return oldModify.call(this, hooksObject);
99+
} finally {
100+
// Reset the strands runtime context
101+
deinitStrandsContext(strandsContext);
102+
}
101103
}
102104
else {
103105
return oldModify.call(this, shaderModifier)

test/unit/visual/cases/webgl.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ visualSuite('WebGL', function() {
692692
});
693693
});
694694

695-
visualSuite('textures in p5.strands', async () => {
695+
visualSuite('textures in p5.strands', () => {
696696
visualTest('uniformTexture() works', async (p5, screenshot) => {
697697
p5.createCanvas(50, 50, p5.WEBGL);
698698
const tex = await p5.loadImage('/unit/assets/cat.jpg');
@@ -708,4 +708,18 @@ visualSuite('WebGL', function() {
708708
screenshot();
709709
});
710710
});
711+
712+
visualSuite('p5.strands', () => {
713+
visualTest('it recovers from p5.strands errors', (p5, screenshot) => {
714+
p5.createCanvas(50, 50, p5.WEBGL);
715+
try {
716+
p5.baseMaterialShader().modify(() => {
717+
undefined.someMethod(); // This will throw an error
718+
});
719+
} catch (e) {}
720+
p5.background('red');
721+
p5.circle(p5.noise(0), p5.noise(0), 20);
722+
screenshot();
723+
});
724+
});
711725
});
591 Bytes
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"numScreenshots": 1
3+
}

0 commit comments

Comments
 (0)