11import type * as smol from 'tinyest' ;
22import * as d from '../data' ;
3+ import type { AnyData } from '../data/dataTypes' ;
34import { abstractInt } from '../data/numeric.js' ;
45import * as wgsl from '../data/wgslTypes.js' ;
56import {
67 type ResolutionCtx ,
78 type Resource ,
89 UnknownData ,
9- type Wgsl ,
10+ isMarkedInternal ,
1011 isWgsl ,
1112} from '../types.js' ;
1213import {
@@ -46,8 +47,8 @@ type Operator =
4647 | smol . UnaryOperator ;
4748
4849function operatorToType <
49- TL extends wgsl . AnyWgslData | UnknownData ,
50- TR extends wgsl . AnyWgslData | UnknownData ,
50+ TL extends AnyData | UnknownData ,
51+ TR extends AnyData | UnknownData ,
5152> ( lhs : TL , op : Operator , rhs ?: TR ) : TL | TR | wgsl . Bool {
5253 if ( ! rhs ) {
5354 if ( op === '!' || op === '~' ) {
@@ -190,7 +191,9 @@ export function generateExpression(
190191 if ( typeof target . value === 'string' ) {
191192 return {
192193 value : `${ target . value } .${ property } ` ,
193- dataType : getTypeForPropAccess ( target . dataType as Wgsl , property ) ,
194+ dataType : d . isData ( target . dataType )
195+ ? getTypeForPropAccess ( target . dataType , property )
196+ : UnknownData ,
194197 } ;
195198 }
196199
@@ -214,10 +217,24 @@ export function generateExpression(
214217 // biome-ignore lint/suspicious/noExplicitAny: <sorry TypeScript>
215218 const propValue = ( target . value as any ) [ property ] ;
216219
220+ if ( target . dataType . type !== 'unknown' ) {
221+ if ( wgsl . isMat ( target . dataType ) && property === 'columns' ) {
222+ return {
223+ value : target . value ,
224+ dataType : target . dataType ,
225+ } ;
226+ }
227+
228+ return {
229+ value : propValue ,
230+ dataType : getTypeForPropAccess ( target . dataType , property ) ,
231+ } ;
232+ }
233+
217234 if ( isWgsl ( target . value ) ) {
218235 return {
219236 value : propValue ,
220- dataType : getTypeForPropAccess ( target . value as d . AnyWgslData , property ) ,
237+ dataType : getTypeForPropAccess ( target . value , property ) ,
221238 } ;
222239 }
223240
@@ -245,7 +262,9 @@ export function generateExpression(
245262
246263 return {
247264 value : `${ targetStr } [${ propertyStr } ]` ,
248- dataType : getTypeForIndexAccess ( targetExpr . dataType as d . AnyWgslData ) ,
265+ dataType : d . isData ( targetExpr . dataType )
266+ ? getTypeForIndexAccess ( targetExpr . dataType )
267+ : UnknownData ,
249268 } ;
250269 }
251270
@@ -291,10 +310,21 @@ export function generateExpression(
291310 } ;
292311 }
293312
313+ if ( ! isMarkedInternal ( idValue ) ) {
314+ throw new Error (
315+ `Function ${ String ( idValue ) } has not been created using TypeGPU APIs. Did you mean to wrap the function with tgpu.fn(args, return)(...) ?` ,
316+ ) ;
317+ }
318+
294319 // Assuming that `id` is callable
295- return ( idValue as unknown as ( ...args : unknown [ ] ) => unknown ) (
320+ const fnRes = ( idValue as unknown as ( ...args : unknown [ ] ) => unknown ) (
296321 ...resolvedResources ,
297322 ) as Resource ;
323+
324+ return {
325+ value : resolveRes ( ctx , fnRes ) ,
326+ dataType : fnRes . dataType ,
327+ } ;
298328 }
299329
300330 if ( 'o' in expression ) {
@@ -369,7 +399,7 @@ export function generateExpression(
369399
370400 return {
371401 value : `${ arrayType } ( ${ arrayValues . join ( ', ' ) } )` ,
372- dataType : d . arrayOf ( type as d . AnyWgslData , values . length ) ,
402+ dataType : d . arrayOf ( type , values . length ) as d . AnyWgslData ,
373403 } ;
374404 }
375405
@@ -449,6 +479,10 @@ ${alternate}`;
449479 throw new Error ( 'Cannot create variable without an initial value.' ) ;
450480 }
451481
482+ if ( d . isLooseData ( eq . dataType ) ) {
483+ throw new Error ( 'Cannot create variable with loose data type.' ) ;
484+ }
485+
452486 registerBlockVariable ( ctx , rawId , eq . dataType ) ;
453487 const id = resolveRes ( ctx , generateIdentifier ( ctx , rawId ) ) ;
454488
0 commit comments