@@ -322,6 +322,7 @@ function handleAlloc(init, typeAnnotation, name) {
322
322
if ( init . type === 'CallExpression' ) {
323
323
324
324
switch ( typeAnnotation ) {
325
+ // BuiltIn info!
325
326
case 'int' :
326
327
case 'float' :
327
328
case 'bool' :
@@ -336,21 +337,29 @@ function handleAlloc(init, typeAnnotation, name) {
336
337
allocation = '' ;
337
338
}
338
339
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' ) ) {
341
344
if ( init . arguments . length === 1 ) {
342
- const [ first ] = init . arguments ;
345
+
343
346
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 ( ', ' ) ;
345
349
allocation = ` = ${ typeAnnotation } (${ els } );` ;
346
350
} else {
347
351
allocation = ` = ${ handleNode ( init . arguments [ 0 ] ) } ` ;
348
352
}
353
+ } else if ( ! init . arguments . length ) {
354
+ allocation = '' ;
349
355
} else {
350
356
throwError ( `classes dont support init calls yet ${ typeAnnotation } ` , init ) ;
351
357
}
358
+ } else {
359
+ allocation = ` = ${ handleNode ( init ) } ;` ;
352
360
}
353
361
break ;
362
+ }
354
363
}
355
364
} else if ( init . type === 'NewExpression' ) {
356
365
if ( init . arguments . length ) {
@@ -376,30 +385,10 @@ function handleBody(body, tabCount = 0) {
376
385
. join ( '\n' ) ;
377
386
}
378
387
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
-
398
388
function handeAst ( node ) {
399
389
const { body } = node . body [ 0 ] . expression ;
400
390
401
391
body . body = body . body . filter ( ( { type } ) => ( type !== 'ReturnStatement' ) ) ;
402
- // body.body = handleGenTypes(body.body);
403
392
let sh = handleBody ( body ) ;
404
393
405
394
sh = sh . split ( '\n' ) . map ( ( s ) => {
@@ -424,24 +413,26 @@ export function buildGLSL(fun, { glsl = true, js = undefined, ast = undefined }
424
413
let node ;
425
414
let code ;
426
415
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
+ }
427
425
try {
428
426
if ( glsl || ast ) {
429
427
str = fun . toString ( ) ;
430
- node = parse ( str , TREE_SETTINGS ) ;
428
+ node = parse ( str , { ... TREE_SETTINGS } ) ;
431
429
}
432
430
433
431
if ( glsl ) {
434
432
text = handeAst ( node ) ;
435
433
}
436
434
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 ] } ;
445
436
} catch ( e ) {
446
437
if ( e [ LINE ] ) {
447
438
const allLines = str . split ( '\n' ) ;
@@ -459,28 +450,39 @@ ${e.message}`);
459
450
}
460
451
}
461
452
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
+
463
461
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 ) {
466
465
originals . forEach ( ( original ) => {
467
- mem . js = sim ( original , { BuiltIn } , mem . keys ) ;
468
466
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
+ } ) ;
472
473
} ) ;
473
474
}
474
475
if ( glslOn || astOn ) {
476
+ const opt = { ...options } ;
475
477
originals . forEach ( ( fun ) => {
476
478
const str = fun . toString ( ) ;
477
- const ast = parse ( str , options ) ;
479
+ const ast = parse ( str , opt ) ;
478
480
mem . asts . push ( ast ) ;
479
481
} ) ;
480
482
}
481
483
mem . originals . push ( ...originals ) ;
482
484
return mem ;
483
- } , { asts : [ ] , js : undefined , keys : { } , originals : [ ] } ) ;
485
+ } , { asts : [ ] , js, keys : { } , originals : [ ] } ) ;
484
486
485
487
let glsl ;
486
488
if ( glslOn ) {
@@ -511,14 +513,13 @@ export function joinGLSL(args, { glsl: glslOn = true, js: jsOn = false, ast: ast
511
513
glsl = handeAst ( { body : [ { expression : { body : { body } } } ] } ) ;
512
514
}
513
515
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 } ;
522
523
}
523
524
524
525
export function addErrorHandling ( glsl ) {
0 commit comments