@@ -401,6 +401,28 @@ function handleBody(body, tabCount = 0) {
401
401
// }).flat(1);
402
402
// }
403
403
404
+ function handeAst ( node ) {
405
+ const { body } = node . body [ 0 ] . expression ;
406
+
407
+ body . body = body . body . filter ( ( { type } ) => ( type !== 'ReturnStatement' ) ) ;
408
+ // body.body = handleGenTypes(body.body);
409
+ let sh = handleBody ( body ) ;
410
+
411
+ sh = sh . split ( '\n' ) . map ( ( s ) => {
412
+ const last = s [ s . length - 1 ] ;
413
+ if ( ! s . length ) {
414
+ return s ;
415
+ }
416
+ if ( last === '{' || last === '}' || last === ';' || last === '/' ) {
417
+ return s ;
418
+ }
419
+ return `${ s } ;` ;
420
+ } ) . join ( '\n' ) ;
421
+
422
+ // console.log('\n' + sh + '\n');
423
+ return `${ sh } \n` ;
424
+ }
425
+
404
426
export function buildGLSL ( fun , { glsl = true , js = undefined , ast = undefined } = { } ) {
405
427
// console.log('fun', fun.toString());
406
428
@@ -415,25 +437,7 @@ export function buildGLSL(fun, { glsl = true, js = undefined, ast = undefined }
415
437
}
416
438
417
439
if ( glsl ) {
418
- const { body } = node . body [ 0 ] . expression ;
419
-
420
- body . body = body . body . filter ( ( { type } ) => ( type !== 'ReturnStatement' ) ) ;
421
- // body.body = handleGenTypes(body.body);
422
- let sh = handleBody ( body ) ;
423
-
424
- sh = sh . split ( '\n' ) . map ( ( s ) => {
425
- const last = s [ s . length - 1 ] ;
426
- if ( ! s . length ) {
427
- return s ;
428
- }
429
- if ( last === '{' || last === '}' || last === ';' || last === '/' ) {
430
- return s ;
431
- }
432
- return `${ s } ;` ;
433
- } ) . join ( '\n' ) ;
434
-
435
- // console.log('\n' + sh + '\n');
436
- text = `${ sh } \n` ;
440
+ text = handeAst ( node ) ;
437
441
}
438
442
439
443
if ( js ) {
@@ -462,17 +466,7 @@ ${e.message}`);
462
466
}
463
467
464
468
export function joinGLSL ( args , { glsl : glslOn = true , js : jsOn = false } = { } ) {
465
- const { glsls, js, originals, keys } = args . reduce ( ( mem , { glsl, [ ORIGINALS ] : originals } ) => {
466
- if ( ! glsl && glslOn ) {
467
- if ( originals . length === 1 ) {
468
- glsl = buildGLSL ( originals [ 0 ] , { glsl : true } ) . glsl ;
469
- } else {
470
- glsl = joinGLSL ( originals , { glsl : true } ) . glsl ;
471
- }
472
- }
473
- if ( glsl ) {
474
- mem . glsls . push ( glsl ) ;
475
- }
469
+ const { asts, js, originals, keys } = args . reduce ( ( mem , { [ ORIGINALS ] : originals , ast } ) => {
476
470
if ( jsOn ) {
477
471
originals . forEach ( ( original ) => {
478
472
mem . js = sim ( original , { BuiltIn } , mem . keys ) ;
@@ -483,10 +477,39 @@ export function joinGLSL(args, { glsl: glslOn = true, js: jsOn = false } = {}) {
483
477
} ) ;
484
478
}
485
479
mem . originals . push ( ...originals ) ;
480
+ mem . asts . push ( ast ) ;
486
481
return mem ;
487
- } , { glsls : [ ] , js : undefined , keys : { } , originals : [ ] } ) ;
482
+ } , { asts : [ ] , js : undefined , keys : { } , originals : [ ] } ) ;
483
+
484
+ let glsl ;
485
+ if ( glslOn ) {
486
+ const { body, mains } = asts . reduce ( ( mem , node ) => {
487
+ let { body : { body } } = node . body [ 0 ] . expression ;
488
+ let main = body [ 0 ] . declarations . find ( ( { id } ) => ( id && id . name === 'main' ) ) ;
489
+ body [ 0 ] . declarations = body [ 0 ] . declarations . filter ( ( { id } ) => ( ! id || id . name !== 'main' ) ) ;
490
+
491
+ mem . body . push ( ...body ) ;
492
+ if ( main ) {
493
+ mem . mains . push ( main ) ;
494
+ }
495
+ return mem ;
496
+ } , { body : [ ] , mains : [ ] } ) ;
497
+
498
+ if ( mains . length ) {
499
+ const initBody = mains . reduce ( ( res , { init : { body } } ) => {
500
+ res . push ( ...body . body ) ;
501
+ return res ;
502
+ } , [ ] ) ;
503
+
504
+ const main = mains [ 0 ] ;
505
+ main . init . body . body = initBody ;
506
+
507
+ body . push ( { declarations : [ main ] , type : 'VariableDeclaration' , kind : 'let' } ) ;
508
+ }
509
+
510
+ glsl = handeAst ( { body : [ { expression : { body : { body } } } ] } ) ;
511
+ }
488
512
489
- const glsl = glsls . length ? glsls . join ( '\n' ) : undefined ;
490
513
if ( js ) {
491
514
Object . entries ( keys ) . forEach ( ( [ key , value ] ) => {
492
515
if ( ! js [ key ] ) {
0 commit comments