Skip to content

Commit cd549fd

Browse files
committed
cleanup
1 parent d74b998 commit cd549fd

File tree

2 files changed

+76
-53
lines changed

2 files changed

+76
-53
lines changed

packages/typegpu/src/tgsl/wgslGenerator.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,12 +1054,11 @@ ${this.ctx.pre}else ${alternate}`;
10541054
}
10551055

10561056
const iterableDataType = iterableSnippet.dataType;
1057-
10581057
let elementCount: number;
1059-
let elementType: wgsl.BaseData;
1058+
let elementType: wgsl.AnyWgslData;
10601059
if (wgsl.isWgslArray(iterableDataType)) {
10611060
elementCount = iterableDataType.elementCount;
1062-
elementType = iterableDataType.elementType;
1061+
elementType = iterableDataType.elementType as wgsl.AnyWgslData;
10631062
} else if (wgsl.isVec(iterableDataType)) {
10641063
elementType = iterableDataType.primitive;
10651064
elementCount = Number(iterableDataType.type.match(/\d/));
@@ -1068,29 +1067,28 @@ ${this.ctx.pre}else ${alternate}`;
10681067
'`for ... of ...` loops only support array or vector iterables',
10691068
);
10701069
}
1071-
const loopVarKind = loopVar[0] === tinyest.NodeTypeCatalog.const &&
1072-
wgsl.isNaturallyEphemeral(elementType)
1073-
? 'let'
1074-
: 'var';
10751070

10761071
/*
10771072
* if user defines a variable named 'i', it will be scoped to a new block,
10781073
* shadowing our 'i'
10791074
*/
10801075
const index = this.ctx.makeNameValid('i');
10811076

1077+
const loopVarKind = loopVar[0] === tinyest.NodeTypeCatalog.const &&
1078+
wgsl.isNaturallyEphemeral(elementType)
1079+
? 'let'
1080+
: 'var';
10821081
const loopVarName = this.ctx.makeNameValid(loopVar[1]);
10831082
const loopVarSnippet = snip(
10841083
loopVarName,
1085-
elementType as AnyData,
1084+
elementType,
10861085
'runtime',
10871086
);
10881087
this.ctx.defineVariable(loopVarName, loopVarSnippet);
10891088

10901089
const iterableStr = this.ctx.resolve(
10911090
iterableSnippet.value,
1092-
// it's vector or wgslArray
1093-
iterableSnippet.dataType as AnyData,
1091+
iterableDataType,
10941092
).value;
10951093

10961094
const forStr =

packages/typegpu/tests/tgsl/wgslGenerator.test.ts

Lines changed: 68 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)