@@ -193,6 +193,15 @@ var renderCode = function(exampleName) {
193
193
'touchStarted' , 'touchMoved' , 'touchEnded' ,
194
194
'keyPressed' , 'keyReleased' , 'keyTyped' ] ;
195
195
var _found = [ ] ;
196
+ // p.preload is an empty function created by the p5.sound library in order to use the p5.js preload system
197
+ // to load AudioWorklet modules before a sketch runs, even if that sketch doesn't have its own preload function.
198
+ // However, this causes an error in the eval code below because the _found array will always contain "preload",
199
+ // even if the sketch in question doesn't have a preload function. To get around this, we delete p.preload before
200
+ // eval-ing the sketch and add it back afterwards if the sketch doesn't contain its own preload function.
201
+ // For more info, see: https://github.com/processing/p5.js-sound/blob/master/src/audioWorklet/index.js#L22
202
+ if ( p . preload ) {
203
+ delete p . preload ;
204
+ }
196
205
with ( p ) {
197
206
// Builds a function to detect declared functions via
198
207
// them being hoisted past the return statement. Does
@@ -220,8 +229,9 @@ var renderCode = function(exampleName) {
220
229
] . join ( '\n' ) ) ;
221
230
}
222
231
// If we haven't found any functions we'll assume it's
223
- // just a setup body.
232
+ // just a setup body with an empty preload .
224
233
if ( ! _found . length ) {
234
+ p . preload = function ( ) { } ;
225
235
p . setup = function ( ) {
226
236
p . createCanvas ( 100 , 100 ) ;
227
237
p . background ( 200 ) ;
@@ -237,10 +247,12 @@ var renderCode = function(exampleName) {
237
247
_found . forEach ( function ( name ) {
238
248
p [ name ] = eval ( name ) ;
239
249
} ) ;
250
+ // Ensure p.preload exists even if the sketch doesn't have a preload function.
251
+ p . preload = p . preload || function ( ) { } ;
240
252
p . setup = p . setup || function ( ) {
241
253
p . createCanvas ( 100 , 100 ) ;
242
254
p . background ( 200 ) ;
243
- }
255
+ } ;
244
256
}
245
257
} ;
246
258
}
0 commit comments