@@ -512,7 +512,7 @@ describe('shellless', () => {
512512 ` ) ;
513513 } ) ;
514514
515- it ( 'inlines comptime-known default values of omitted parameters' , ( ) => {
515+ it ( 'declares default values of omitted parameters at the top of the function body ' , ( ) => {
516516 const scale = ( v : d . v2f , factor : number = 2 ) => {
517517 'use gpu' ;
518518 return std . mul ( v , d . f32 ( factor ) ) ;
@@ -525,7 +525,8 @@ describe('shellless', () => {
525525
526526 expect ( tgpu . resolve ( [ foo ] ) ) . toMatchInlineSnapshot ( `
527527 "fn scale(v: vec2f) -> vec2f {
528- return (v * 2f);
528+ const factor = 2;
529+ return (v * f32(factor));
529530 }
530531
531532 fn scale_1(v: vec2f, factor: f32) -> vec2f {
@@ -538,7 +539,7 @@ describe('shellless', () => {
538539 ` ) ;
539540 } ) ;
540541
541- it ( 'prunes branches based on comptime-known default values ' , ( ) => {
542+ it ( 'does not treat default values as comptime-known' , ( ) => {
542543 const interpolate = ( t : number , cyclic : boolean = false ) => {
543544 'use gpu' ;
544545 if ( cyclic ) {
@@ -554,6 +555,10 @@ describe('shellless', () => {
554555
555556 expect ( tgpu . resolve ( [ foo ] ) ) . toMatchInlineSnapshot ( `
556557 "fn interpolate(t: f32) -> f32 {
558+ const cyclic = false;
559+ if (cyclic) {
560+ return fract(t);
561+ }
557562 return clamp(t, 0f, 1f);
558563 }
559564
@@ -591,11 +596,16 @@ describe('shellless', () => {
591596
592597 expect ( tgpu . resolve ( [ foo ] ) ) . toMatchInlineSnapshot ( `
593598 "fn interpolate(t: f32) -> f32 {
599+ const cyclic = false;
600+ if (cyclic) {
601+ return fract(t);
602+ }
594603 return clamp(t, 0f, 1f);
595604 }
596605
597606 fn offset(x: i32) -> i32 {
598- return (10i + x);
607+ const base = 10;
608+ return (base + x);
599609 }
600610
601611 fn foo() -> f32 {
@@ -604,7 +614,7 @@ describe('shellless', () => {
604614 ` ) ;
605615 } ) ;
606616
607- it ( 'inlines comptime-known default values referencing other defaults ' , ( ) => {
617+ it ( 'supports default values referencing other parameters ' , ( ) => {
608618 const foo = ( a : number = 2 , b : number = a * 3 ) => {
609619 'use gpu' ;
610620 return d . f32 ( a + b ) ;
@@ -617,7 +627,9 @@ describe('shellless', () => {
617627
618628 expect ( tgpu . resolve ( [ main ] ) ) . toMatchInlineSnapshot ( `
619629 "fn foo() -> f32 {
620- return 8f;
630+ const a = 2;
631+ let b = (a * 3i);
632+ return f32((a + b));
621633 }
622634
623635 fn foo_1(a: i32) -> f32 {
@@ -677,7 +689,8 @@ describe('shellless', () => {
677689
678690 expect ( tgpu . resolve ( [ main ] ) ) . toMatchInlineSnapshot ( `
679691 "fn addBias(x: f32) -> f32 {
680- return (x + 10f);
692+ const bias = 10;
693+ return (x + f32(bias));
681694 }
682695
683696 fn main() -> f32 {
@@ -694,27 +707,87 @@ describe('shellless', () => {
694707
695708 expect ( tgpu . resolve ( [ foo ] ) ) . toMatchInlineSnapshot ( `
696709 "fn foo() -> f32 {
697- return 1f;
710+ const a = 1;
711+ const b = true;
712+ return select(0f, f32(a), b);
713+ }"
714+ ` ) ;
715+ } ) ;
716+
717+ it ( 'treats omitted optional parameters as comptime-known undefined' , ( ) => {
718+ const interpolate = ( t : number , period ?: number ) => {
719+ 'use gpu' ;
720+ if ( period !== undefined ) {
721+ return std . fract ( t / period ) ;
722+ }
723+ return std . saturate ( t ) ;
724+ } ;
725+
726+ const main = ( ) => {
727+ 'use gpu' ;
728+ return interpolate ( 0.5 ) + interpolate ( 1.5 , 2 ) + interpolate ( 2.5 , undefined ) ;
729+ } ;
730+
731+ expect ( tgpu . resolve ( [ main ] ) ) . toMatchInlineSnapshot ( `
732+ "fn interpolate(t: f32) -> f32 {
733+ return saturate(t);
734+ }
735+
736+ fn interpolate_1(t: f32, period: i32) -> f32 {
737+ return fract((t / f32(period)));
738+ }
739+
740+ fn main() -> f32 {
741+ return ((interpolate(0.5f) + interpolate_1(1.5f, 2i)) + interpolate(2.5f));
742+ }"
743+ ` ) ;
744+ } ) ;
745+
746+ it ( 'folds comparisons of runtime values against undefined' , ( ) => {
747+ const foo = ( a : number , b ?: number ) => {
748+ 'use gpu' ;
749+ if ( a === undefined || b === undefined ) {
750+ return a ;
751+ }
752+ return a + b ;
753+ } ;
754+
755+ const main = ( ) => {
756+ 'use gpu' ;
757+ return foo ( 1 ) + foo ( 1 , 2 ) ;
758+ } ;
759+
760+ expect ( tgpu . resolve ( [ main ] ) ) . toMatchInlineSnapshot ( `
761+ "fn foo(a: i32) -> i32 {
762+ return a;
763+ }
764+
765+ fn foo_1(a: i32, b: i32) -> i32 {
766+ return (a + b);
767+ }
768+
769+ fn main() -> i32 {
770+ return (foo(1i) + foo_1(1i, 2i));
698771 }"
699772 ` ) ;
700773 } ) ;
701774
702- it ( 'throws when passing undefined to a parameter without a default value ' , ( ) => {
703- const foo = ( a : number | undefined ) => {
775+ it ( 'throws when passing undefined to a destructured parameter ' , ( ) => {
776+ const foo = ( { a } : { a : number } ) => {
704777 'use gpu' ;
705778 return a ;
706779 } ;
707780
708781 const main = ( ) => {
709782 'use gpu' ;
710- return foo ( undefined ) ;
783+ return foo ( undefined as unknown as { a : number } ) ;
711784 } ;
712785
713786 expect ( ( ) => tgpu . resolve ( [ main ] ) ) . toThrowErrorMatchingInlineSnapshot ( `
714787 [Error: Resolution of the following tree failed:
715788 - <root>
716789 - fn*:main
717- - fn*:main(): Passed undefined as the #0 argument to foo(...), but the parameter has no default value .]
790+ - fn*:main(): Passed undefined as the #0 argument to foo(...), but only identifier parameters can be omitted .]
718791 ` ) ;
719792 } ) ;
720793
0 commit comments