@@ -449,7 +449,38 @@ describe('wgslGenerator', () => {
449449 ` ) ;
450450 } ) ;
451451
452- it ( 'creates correct code for for ... of ... statement with array of primitives' , ( ) => {
452+ it ( 'parses correctly "for ... of ..." statements' , ( ) => {
453+ const main1 = ( ) => {
454+ 'use gpu' ;
455+ const arr = [ 1 , 2 , 3 ] ;
456+ for ( const foo of arr ) {
457+ // biome-ignore lint/complexity/noUselessContinue: it's a part of the test
458+ continue ;
459+ }
460+ } ;
461+
462+ const main2 = ( ) => {
463+ 'use gpu' ;
464+ const arr = [ 1 , 2 , 3 ] ;
465+ // biome-ignore lint/style/useConst: it's a part of the test
466+ for ( let foo of arr ) {
467+ // biome-ignore lint/complexity/noUselessContinue: it's a part of the test
468+ continue ;
469+ }
470+ } ;
471+
472+ const parsed1 = getMetaData ( main1 ) ?. ast ?. body ;
473+ expect ( JSON . stringify ( parsed1 ) ) . toMatchInlineSnapshot (
474+ `"[0,[[13,"arr",[100,[[5,"1"],[5,"2"],[5,"3"]]]],[18,[13,"foo"],"arr",[0,[[16]]]]]]"` ,
475+ ) ;
476+
477+ const parsed2 = getMetaData ( main2 ) ?. ast ?. body ;
478+ expect ( JSON . stringify ( parsed2 ) ) . toMatchInlineSnapshot (
479+ `"[0,[[13,"arr",[100,[[5,"1"],[5,"2"],[5,"3"]]]],[18,[12,"foo"],"arr",[0,[[16]]]]]]"` ,
480+ ) ;
481+ } ) ;
482+
483+ it ( 'creates correct code for "for ... of ..." statement using array of primitives' , ( ) => {
453484 const main = ( ) => {
454485 'use gpu' ;
455486 const arr = d . arrayOf ( d . f32 , 3 ) ( [ 1 , 2 , 3 ] ) ;
@@ -460,28 +491,54 @@ describe('wgslGenerator', () => {
460491 }
461492 } ;
462493
494+ expect ( tgpu . resolve ( [ main ] ) ) . toMatchInlineSnapshot ( `
495+ "fn main() {
496+ var arr = array<f32, 3>(1f, 2f, 3f);
497+ var res = 0f;
498+ for (var i = 0; i < 3; i++) {
499+ let foo = arr[i];
500+ {
501+ res += foo;
502+ let i = res;
503+ }
504+ }
505+ }"
506+ ` ) ;
507+ } ) ;
508+
509+ it ( 'creates correct code for "for ... of ..." statement using array of non-primitives' , ( ) => {
510+ const main = ( ) => {
511+ 'use gpu' ;
512+ const arr = d . arrayOf ( d . vec2f , 3 ) ( [ d . vec2f ( 1 ) , d . vec2f ( 2 ) , d . vec2f ( 3 ) ] ) ;
513+ let res = 0 ;
514+ for ( const foo of arr ) {
515+ res += foo . x ;
516+ const i = res ;
517+ }
518+ } ;
519+
463520 const parsed = getMetaData ( main ) ?. ast ?. body ;
464521
465522 expect ( JSON . stringify ( parsed ) ) . toMatchInlineSnapshot (
466- `"[0,[[13,"arr",[6,[6,[7,"d","arrayOf"],[[7,"d","f32 "],[5,"3"]]],[[100,[[5,"1" ],[5,"2"],[5,"3"]]]]]],[12,"res" ,[6,[7,"d","f32 "],[]]],[ 18,[13,"foo"],"arr",[0,[[2,"res","+=","foo"],[13,"i","res"]]]]]]"` ,
523+ `"[0,[[13,"arr",[6,[6,[7,"d","arrayOf"],[[7,"d","vec2f "],[5,"3"]]],[[100,[[6,[7,"d","vec2f" ],[[ 5,"1"]]],[6,[7,"d","vec2f"],[[5,"2"]]] ,[6,[7,"d","vec2f "],[[5,"3" ]]]]]]]],[12,"res",[5,"0"]],[ 18,[13,"foo"],"arr",[0,[[2,"res","+=",[7, "foo","x"] ],[13,"i","res"]]]]]]"` ,
467524 ) ;
468525
469526 expect ( tgpu . resolve ( [ main ] ) ) . toMatchInlineSnapshot ( `
470527 "fn main() {
471- var arr = array<f32 , 3>(1f, 2f, 3f );
472- var res = 0f ;
528+ var arr = array<vec2f , 3>(vec2f(1), vec2f(2), vec2f(3) );
529+ var res = 0 ;
473530 for (var i = 0; i < 3; i++) {
474- let foo = arr[i];
531+ var foo = arr[i];
475532 {
476- res += foo;
533+ res += i32( foo.x) ;
477534 let i = res;
478535 }
479536 }
480537 }"
481538 ` ) ;
482539 } ) ;
483540
484- it ( 'creates correct code for for ... of ... statement derived, comptime iterables' , ( ) => {
541+ it ( 'creates correct code for " for ... of ..." statement using derived and comptime iterables' , ( ) => {
485542 const comptimeVec = tgpu [ '~unstable' ] . comptime ( ( ) => d . vec4f ( 1 , 8 , 8 , 2 ) ) ;
486543
487544 const main = ( ) => {
@@ -519,39 +576,7 @@ describe('wgslGenerator', () => {
519576 ` ) ;
520577 } ) ;
521578
522- it ( 'creates correct code for for ... of ... statement with array of non-primitives' , ( ) => {
523- const main = ( ) => {
524- 'use gpu' ;
525- const arr = d . arrayOf ( d . vec2f , 3 ) ( [ d . vec2f ( 1 ) , d . vec2f ( 2 ) , d . vec2f ( 3 ) ] ) ;
526- let res = 0 ;
527- for ( const foo of arr ) {
528- res += foo . x ;
529- const i = res ;
530- }
531- } ;
532-
533- const parsed = getMetaData ( main ) ?. ast ?. body ;
534-
535- expect ( JSON . stringify ( parsed ) ) . toMatchInlineSnapshot (
536- `"[0,[[13,"arr",[6,[6,[7,"d","arrayOf"],[[7,"d","vec2f"],[5,"3"]]],[[100,[[6,[7,"d","vec2f"],[[5,"1"]]],[6,[7,"d","vec2f"],[[5,"2"]]],[6,[7,"d","vec2f"],[[5,"3"]]]]]]]],[12,"res",[5,"0"]],[18,[13,"foo"],"arr",[0,[[2,"res","+=",[7,"foo","x"]],[13,"i","res"]]]]]]"` ,
537- ) ;
538-
539- expect ( tgpu . resolve ( [ main ] ) ) . toMatchInlineSnapshot ( `
540- "fn main() {
541- var arr = array<vec2f, 3>(vec2f(1), vec2f(2), vec2f(3));
542- var res = 0;
543- for (var i = 0; i < 3; i++) {
544- var foo = arr[i];
545- {
546- res += i32(foo.x);
547- let i = res;
548- }
549- }
550- }"
551- ` ) ;
552- } ) ;
553-
554- it ( 'creates correct code for for ... of ... statement with vector iterables' , ( ) => {
579+ it ( 'creates correct code for "for ... of ..." statement using vector iterables' , ( ) => {
555580 const main = ( ) => {
556581 'use gpu' ;
557582 const v1 = d . vec4f ( 1 , 2 , 3 , 4 ) ;
@@ -605,7 +630,7 @@ describe('wgslGenerator', () => {
605630 ` ) ;
606631 } ) ;
607632
608- it ( 'creates correct code for for ... of ... statement with struct member iterable' , ( ) => {
633+ it ( 'creates correct code for " for ... of ..." statement using a struct member iterable' , ( ) => {
609634 const TestStruct = d . struct ( {
610635 arr : d . arrayOf ( d . f32 , 4 ) ,
611636 } ) ;
@@ -636,7 +661,7 @@ describe('wgslGenerator', () => {
636661 ` ) ;
637662 } ) ;
638663
639- it ( 'throws error when for ... of ... iterable is ephemeral' , ( ) => {
664+ it ( 'throws error when " for ... of ..." statement uses an ephemeral iterable ' , ( ) => {
640665 const main = ( ) => {
641666 'use gpu' ;
642667 for ( const foo of [ 1 , 2 , 3 ] ) {
@@ -653,7 +678,7 @@ describe('wgslGenerator', () => {
653678 ` ) ;
654679 } ) ;
655680
656- it ( 'throws error when for ... of ... iterable is not an array or a vector' , ( ) => {
681+ it ( 'throws error when " for ... of ..." statement uses iterable that is not an array or a vector' , ( ) => {
657682 const TestStruct = d . struct ( {
658683 x : d . u32 ,
659684 y : d . f32 ,
0 commit comments