@@ -74,54 +74,56 @@ function strands(p5, fn) {
7474 const oldModify = p5 . Shader . prototype . modify ;
7575
7676 p5 . Shader . prototype . modify = function ( shaderModifier , scope = { } ) {
77- if ( shaderModifier instanceof Function ) {
78- // Reset the context object every time modify is called;
79- // const backend = glslBackend;
80- initStrandsContext ( strandsContext , this . _renderer . strandsBackend , {
81- active : true ,
82- renderer : this . _renderer ,
83- baseShader : this ,
84- } ) ;
85- createShaderHooksFunctions ( strandsContext , fn , this ) ;
86- // TODO: expose this, is internal for debugging for now.
87- const options = { parser : true , srcLocations : false } ;
77+ try {
78+ if ( shaderModifier instanceof Function ) {
79+ // Reset the context object every time modify is called;
80+ // const backend = glslBackend;
81+ initStrandsContext ( strandsContext , this . _renderer . strandsBackend , {
82+ active : true ,
83+ renderer : this . _renderer ,
84+ baseShader : this ,
85+ } ) ;
86+ createShaderHooksFunctions ( strandsContext , fn , this ) ;
87+ // TODO: expose this, is internal for debugging for now.
88+ const options = { parser : true , srcLocations : false } ;
8889
89- // 1. Transpile from strands DSL to JS
90- let strandsCallback ;
91- if ( options . parser ) {
92- // #7955 Wrap function declaration code in brackets so anonymous functions are not top level statements, which causes an error in acorn when parsing
93- // https://github.com/acornjs/acorn/issues/1385
94- const sourceString = `(${ shaderModifier . toString ( ) } )` ;
95- strandsCallback = transpileStrandsToJS (
96- p5 ,
97- sourceString ,
98- options . srcLocations ,
99- scope ,
100- ) ;
101- } else {
102- strandsCallback = shaderModifier ;
103- }
90+ // 1. Transpile from strands DSL to JS
91+ let strandsCallback ;
92+ if ( options . parser ) {
93+ // #7955 Wrap function declaration code in brackets so anonymous functions are not top level statements, which causes an error in acorn when parsing
94+ // https://github.com/acornjs/acorn/issues/1385
95+ const sourceString = `(${ shaderModifier . toString ( ) } )` ;
96+ strandsCallback = transpileStrandsToJS (
97+ p5 ,
98+ sourceString ,
99+ options . srcLocations ,
100+ scope ,
101+ ) ;
102+ } else {
103+ strandsCallback = shaderModifier ;
104+ }
104105
105- // 2. Build the IR from JavaScript API
106- const globalScope = createBasicBlock (
107- strandsContext . cfg ,
108- BlockType . GLOBAL ,
109- ) ;
110- pushBlock ( strandsContext . cfg , globalScope ) ;
111- strandsCallback ( ) ;
112- popBlock ( strandsContext . cfg ) ;
106+ // 2. Build the IR from JavaScript API
107+ const globalScope = createBasicBlock (
108+ strandsContext . cfg ,
109+ BlockType . GLOBAL ,
110+ ) ;
111+ pushBlock ( strandsContext . cfg , globalScope ) ;
112+ strandsCallback ( ) ;
113+ popBlock ( strandsContext . cfg ) ;
113114
114- // 3. Generate shader code hooks object from the IR
115- // .......
116- const hooksObject = generateShaderCode ( strandsContext ) ;
115+ // 3. Generate shader code hooks object from the IR
116+ // .......
117+ const hooksObject = generateShaderCode ( strandsContext ) ;
117118
119+ // Call modify with the generated hooks object
120+ return oldModify . call ( this , hooksObject ) ;
121+ } else {
122+ return oldModify . call ( this , shaderModifier ) ;
123+ }
124+ } finally {
118125 // Reset the strands runtime context
119126 deinitStrandsContext ( strandsContext ) ;
120-
121- // Call modify with the generated hooks object
122- return oldModify . call ( this , hooksObject ) ;
123- } else {
124- return oldModify . call ( this , shaderModifier ) ;
125127 }
126128 } ;
127129}
0 commit comments