@@ -130,22 +130,19 @@ const ASTCallbacks = {
130130 if ( ancestors . some ( nodeIsUniform ) ) { return ; }
131131 if ( _state . varyings [ node . name ]
132132 && ! ancestors . some ( a => a . type === 'AssignmentExpression' && a . left === node ) ) {
133- node . type = 'ExpressionStatement' ;
134- node . expression = {
135- type : 'CallExpression' ,
136- callee : {
137- type : 'MemberExpression' ,
138- object : {
139- type : 'Identifier' ,
140- name : node . name
141- } ,
142- property : {
143- type : 'Identifier' ,
144- name : 'getValue'
145- } ,
133+ node . type = 'CallExpression' ;
134+ node . callee = {
135+ type : 'MemberExpression' ,
136+ object : {
137+ type : 'Identifier' ,
138+ name : node . name
146139 } ,
147- arguments : [ ] ,
148- }
140+ property : {
141+ type : 'Identifier' ,
142+ name : 'getValue'
143+ } ,
144+ } ;
145+ node . arguments = [ ] ;
149146 }
150147 } ,
151148 // The callbacks for AssignmentExpression and BinaryExpression handle
@@ -208,13 +205,12 @@ const ASTCallbacks = {
208205 varyingName = node . left . object . name ;
209206 }
210207 // Check if it's a getValue() call: myVarying.getValue().xyz
211- else if ( node . left . object . type === 'ExpressionStatement' &&
212- node . left . object . expression ?. type === 'CallExpression' &&
213- node . left . object . expression . callee ?. type === 'MemberExpression' &&
214- node . left . object . expression . callee . property ?. name === 'getValue' &&
215- node . left . object . expression . callee . object ?. type === 'Identifier' &&
216- _state . varyings [ node . left . object . expression . callee . object . name ] ) {
217- varyingName = node . left . object . expression . callee . object . name ;
208+ else if ( node . left . object . type === 'CallExpression' &&
209+ node . left . object . callee ?. type === 'MemberExpression' &&
210+ node . left . object . callee . property ?. name === 'getValue' &&
211+ node . left . object . callee . object ?. type === 'Identifier' &&
212+ _state . varyings [ node . left . object . callee . object . name ] ) {
213+ varyingName = node . left . object . callee . object . name ;
218214 }
219215
220216 if ( varyingName ) {
@@ -564,7 +560,7 @@ const ASTCallbacks = {
564560
565561 // Transform for statement into strandsFor() call
566562 // for (init; test; update) body -> strandsFor(initCb, conditionCb, updateCb, bodyCb, initialVars)
567-
563+
568564 // Generate unique loop variable name
569565 const uniqueLoopVar = `loopVar${ loopVarCounter ++ } ` ;
570566
@@ -928,7 +924,7 @@ const ASTCallbacks = {
928924 // Reset counters at the start of each transpilation
929925 blockVarCounter = 0 ;
930926 loopVarCounter = 0 ;
931-
927+
932928 const ast = parse ( sourceString , {
933929 ecmaVersion : 2021 ,
934930 locations : srcLocations
@@ -961,18 +957,37 @@ const ASTCallbacks = {
961957 recursive ( ast , { varyings : { } } , postOrderControlFlowTransform ) ;
962958 const transpiledSource = escodegen . generate ( ast ) ;
963959 const scopeKeys = Object . keys ( scope ) ;
964- const internalStrandsCallback = new Function (
965- // Create a parameter called __p5, not just p5, because users of instance mode
966- // may pass in a variable called p5 as a scope variable. If we rely on a variable called
967- // p5, then the scope variable called p5 might accidentally override internal function
968- // calls to p5 static methods.
969- '__p5' ,
970- ...scopeKeys ,
971- transpiledSource
972- . slice (
973- transpiledSource . indexOf ( '{' ) + 1 ,
974- transpiledSource . lastIndexOf ( '}' )
975- ) . replaceAll ( ';' , '' )
976- ) ;
977- return ( ) => internalStrandsCallback ( p5 , ...scopeKeys . map ( key => scope [ key ] ) ) ;
960+ const match = / \( ? \s * (?: f u n c t i o n ) ? \s * \( ( [ ^ ) ] * ) \) \s * (?: = > ) ? \s * { ( (?: .| \n ) * ) } \s * ; ? \s * \) ? /
961+ . exec ( transpiledSource ) ;
962+ if ( ! match ) {
963+ console . log ( transpiledSource ) ;
964+ throw new Error ( 'Could not parse p5.strands function!' ) ;
965+ }
966+ const params = match [ 1 ] . split ( / , \s * / ) . filter ( param => ! ! param . trim ( ) ) ;
967+ let paramVals , paramNames ;
968+ if ( params . length > 0 ) {
969+ paramNames = params ;
970+ paramVals = [ scope ] ;
971+ } else {
972+ paramNames = scopeKeys ;
973+ paramVals = scopeKeys . map ( key => scope [ key ] ) ;
974+ }
975+ const body = match [ 2 ] ;
976+ try {
977+ const internalStrandsCallback = new Function (
978+ // Create a parameter called __p5, not just p5, because users of instance mode
979+ // may pass in a variable called p5 as a scope variable. If we rely on a variable called
980+ // p5, then the scope variable called p5 might accidentally override internal function
981+ // calls to p5 static methods.
982+ '__p5' ,
983+ ...paramNames ,
984+ body ,
985+ ) ;
986+ return ( ) => internalStrandsCallback ( p5 , ...paramVals ) ;
987+ } catch ( e ) {
988+ console . error ( e ) ;
989+ console . log ( paramNames ) ;
990+ console . log ( body ) ;
991+ throw new Error ( 'Error transpiling p5.strands callback!' ) ;
992+ }
978993 }
0 commit comments