Skip to content

Commit 9e20591

Browse files
committed
Clean up p5.strands in a finally block
1 parent 424d2e8 commit 9e20591

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
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)

0 commit comments

Comments
 (0)