@@ -36,6 +36,7 @@ import {
3636} from './conversion.ts' ;
3737import {
3838 ArrayExpression ,
39+ coerceToSnippet ,
3940 concretize ,
4041 type GenerationCtx ,
4142 numericLiteralToSnippet ,
@@ -51,6 +52,7 @@ import type { AnyFn } from '../core/function/fnTypes.ts';
5152import { arrayLength } from '../std/array.ts' ;
5253import { AutoStruct } from '../data/autoStruct.ts' ;
5354import { mathToStd } from './math.ts' ;
55+ import type { ExternalMap } from '../core/resolve/externals.ts' ;
5456
5557const { NodeTypeCatalog : NODE } = tinyest ;
5658
@@ -200,8 +202,19 @@ class WgslGenerator implements ShaderGenerator {
200202
201203 public block (
202204 [ _ , statements ] : tinyest . Block ,
205+ externalMap ?: ExternalMap ,
203206 ) : string {
204207 this . ctx . pushBlockScope ( ) ;
208+
209+ if ( externalMap ) {
210+ const externals = Object . fromEntries (
211+ Object . entries ( externalMap ) . map ( (
212+ [ id , value ] ,
213+ ) => [ id , coerceToSnippet ( value ) ] ) ,
214+ ) ;
215+ this . ctx . setBlockExternals ( externals ) ;
216+ }
217+
205218 try {
206219 this . ctx . indent ( ) ;
207220 const body = statements . map ( ( statement ) => this . statement ( statement ) )
@@ -1233,8 +1246,6 @@ ${this.ctx.pre}else ${alternate}`;
12331246 // If it's ephemeral, it's a value that cannot change. If it's a reference, we take
12341247 // an implicit pointer to it
12351248 let loopVarKind = 'let' ;
1236- const loopVarName = this . ctx . makeNameValid ( loopVar [ 1 ] ) ;
1237-
12381249 if ( ! isEphemeralSnippet ( elementSnippet ) ) {
12391250 if ( elementSnippet . origin === 'constant-tgpu-const-ref' ) {
12401251 loopVarKind = 'const' ;
@@ -1260,21 +1271,13 @@ ${this.ctx.pre}else ${alternate}`;
12601271 }
12611272 }
12621273
1263- const loopVarSnippet = snip (
1264- loopVarName ,
1265- elementType ,
1266- elementSnippet . origin ,
1267- ) ;
1268-
1269- this . ctx . defineVariable ( loopVar [ 1 ] , loopVarSnippet ) ;
1270-
12711274 const forStr =
12721275 stitch `${ this . ctx . pre } for (var ${ index } = 0u; ${ index } < ${
12731276 tryConvertSnippet ( this . ctx , elementCountSnippet , u32 , false )
12741277 } ; ${ index } ++) {`;
1275-
12761278 this . ctx . indent ( ) ;
12771279
1280+ const loopVarName = this . ctx . makeNameValid ( loopVar [ 1 ] ) ;
12781281 const loopVarDeclStr =
12791282 stitch `${ this . ctx . pre } ${ loopVarKind } ${ loopVarName } = ${
12801283 tryConvertSnippet (
@@ -1286,7 +1289,9 @@ ${this.ctx.pre}else ${alternate}`;
12861289 } ;`;
12871290
12881291 const bodyStr = `${ this . ctx . pre } ${
1289- this . block ( blockifySingleStatement ( body ) )
1292+ this . block ( blockifySingleStatement ( body ) , {
1293+ [ loopVar [ 1 ] ] : snip ( loopVarName , elementType , elementSnippet . origin ) ,
1294+ } )
12901295 } `;
12911296
12921297 this . ctx . dedent ( ) ;
0 commit comments