@@ -322,6 +322,7 @@ function handleAlloc(init, typeAnnotation, name) {
322322 if ( init . type === 'CallExpression' ) {
323323
324324 switch ( typeAnnotation ) {
325+ // BuiltIn info!
325326 case 'int' :
326327 case 'float' :
327328 case 'bool' :
@@ -336,21 +337,29 @@ function handleAlloc(init, typeAnnotation, name) {
336337 allocation = '' ;
337338 }
338339 break ;
339- default :
340- if ( init . arguments . length ) {
340+ default : {
341+ const [ first ] = init . arguments ;
342+
343+ if ( typeAnnotation === init . callee . name || ( first && first . type === 'ArrayExpression' ) ) {
341344 if ( init . arguments . length === 1 ) {
342- const [ first ] = init . arguments ;
345+
343346 if ( first . type === 'ArrayExpression' ) {
344- const els = first . elements . map ( ( n ) => `${ handleNode ( n ) } ` ) . join ( ', ' ) ;
347+ const els = first . elements . map ( ( n ) => `${ handleNode ( n ) } ` )
348+ . join ( ', ' ) ;
345349 allocation = ` = ${ typeAnnotation } (${ els } );` ;
346350 } else {
347351 allocation = ` = ${ handleNode ( init . arguments [ 0 ] ) } ` ;
348352 }
353+ } else if ( ! init . arguments . length ) {
354+ allocation = '' ;
349355 } else {
350356 throwError ( `classes dont support init calls yet ${ typeAnnotation } ` , init ) ;
351357 }
358+ } else {
359+ allocation = ` = ${ handleNode ( init ) } ;` ;
352360 }
353361 break ;
362+ }
354363 }
355364 } else if ( init . type === 'NewExpression' ) {
356365 if ( init . arguments . length ) {
@@ -376,30 +385,10 @@ function handleBody(body, tabCount = 0) {
376385 . join ( '\n' ) ;
377386}
378387
379- // function replaceGenType(node) {
380- // console.log('replaceGenType', node);
381- // return node;
382- // }
383- //
384- // function handleGenTypes(body) {
385- // return body.map((node) => {
386- // if (node.right && node.right.init && node.right.init.returnType === 'genType') {
387- // return [
388- // replaceGenType(JSON.parse(JSON.stringify(node)), 'float'),
389- // replaceGenType(JSON.parse(JSON.stringify(node)), 'vec2'),
390- // replaceGenType(JSON.parse(JSON.stringify(node)), 'vec3'),
391- // replaceGenType(JSON.parse(JSON.stringify(node)), 'vec4')
392- // ];
393- // }
394- // return [node];
395- // }).flat(1);
396- // }
397-
398388function handeAst ( node ) {
399389 const { body } = node . body [ 0 ] . expression ;
400390
401391 body . body = body . body . filter ( ( { type } ) => ( type !== 'ReturnStatement' ) ) ;
402- // body.body = handleGenTypes(body.body);
403392 let sh = handleBody ( body ) ;
404393
405394 sh = sh . split ( '\n' ) . map ( ( s ) => {
@@ -424,24 +413,26 @@ export function buildGLSL(fun, { glsl = true, js = undefined, ast = undefined }
424413 let node ;
425414 let code ;
426415 let text ;
416+
417+ if ( js ) {
418+ if ( js === true ) {
419+ js = { } ;
420+ }
421+ if ( js ) {
422+ code = readOnlyView ( sim ( fun , { BuiltIn, ...js } ) ) ;
423+ }
424+ }
427425 try {
428426 if ( glsl || ast ) {
429427 str = fun . toString ( ) ;
430- node = parse ( str , TREE_SETTINGS ) ;
428+ node = parse ( str , { ... TREE_SETTINGS } ) ;
431429 }
432430
433431 if ( glsl ) {
434432 text = handeAst ( node ) ;
435433 }
436434
437- if ( js ) {
438- if ( js === true ) {
439- js = { } ;
440- }
441- code = sim ( fun , { BuiltIn, ...js } ) ;
442- }
443-
444- return { glsl : text , ast : node , js : readOnlyView ( code ) , [ ORIGINALS ] : [ fun ] } ;
435+ return { glsl : text , ast : node , js : code , [ ORIGINALS ] : [ fun ] } ;
445436 } catch ( e ) {
446437 if ( e [ LINE ] ) {
447438 const allLines = str . split ( '\n' ) ;
@@ -459,28 +450,39 @@ ${e.message}`);
459450 }
460451}
461452
462- export function joinGLSL ( args , { glsl : glslOn = true , js : jsOn = false , ast : astOn = false } = { } ) {
453+ export function joinGLSL ( args , { glsl : glslOn = true , js = undefined , ast : astOn = false } = { } ) {
454+ if ( js === true ) {
455+ js = { } ;
456+ }
457+ if ( js ) {
458+ js = { BuiltIn, ...js } ;
459+ }
460+
463461 const options = { ...TREE_SETTINGS , scope : { } } ;
464- const { asts, js, originals, keys } = args . reduce ( ( mem , { [ ORIGINALS ] : originals } ) => {
465- if ( jsOn ) {
462+ const { asts, js : newJs , originals, keys } = args . reduce ( ( mem , { [ ORIGINALS ] : originals } ) => {
463+
464+ if ( js ) {
466465 originals . forEach ( ( original ) => {
467- mem . js = sim ( original , { BuiltIn } , mem . keys ) ;
468466
469- Object . entries ( mem . js ) . forEach ( ( [ key , value ] ) => {
470- mem . keys [ key ] = value ;
471- } ) ;
467+ mem . js = sim ( original , mem . js , mem . keys ) ;
468+
469+ Object . entries ( mem . js )
470+ . forEach ( ( [ key , value ] ) => {
471+ mem . keys [ key ] = value ;
472+ } ) ;
472473 } ) ;
473474 }
474475 if ( glslOn || astOn ) {
476+ const opt = { ...options } ;
475477 originals . forEach ( ( fun ) => {
476478 const str = fun . toString ( ) ;
477- const ast = parse ( str , options ) ;
479+ const ast = parse ( str , opt ) ;
478480 mem . asts . push ( ast ) ;
479481 } ) ;
480482 }
481483 mem . originals . push ( ...originals ) ;
482484 return mem ;
483- } , { asts : [ ] , js : undefined , keys : { } , originals : [ ] } ) ;
485+ } , { asts : [ ] , js, keys : { } , originals : [ ] } ) ;
484486
485487 let glsl ;
486488 if ( glslOn ) {
@@ -511,14 +513,13 @@ export function joinGLSL(args, { glsl: glslOn = true, js: jsOn = false, ast: ast
511513 glsl = handeAst ( { body : [ { expression : { body : { body } } } ] } ) ;
512514 }
513515
514- if ( js ) {
515- Object . entries ( keys ) . forEach ( ( [ key , value ] ) => {
516- if ( ! js [ key ] ) {
517- js [ key ] = value ;
518- }
519- } ) ;
520- }
521- return { glsl, js : readOnlyView ( js ) , [ ORIGINALS ] : originals } ;
516+ Object . entries ( keys ) . forEach ( ( [ key , value ] ) => {
517+ if ( ! newJs [ key ] ) {
518+ newJs [ key ] = value ;
519+ }
520+ } ) ;
521+
522+ return { glsl, js : readOnlyView ( newJs ) , [ ORIGINALS ] : originals } ;
522523}
523524
524525export function addErrorHandling ( glsl ) {
0 commit comments