@@ -2,7 +2,7 @@ import { stitch } from '../core/resolve/stitch.ts';
22import { invariant } from '../errors.ts' ;
33import { inCodegenMode } from '../execMode.ts' ;
44import { setName } from '../shared/meta.ts' ;
5- import { $internal , $isRef , $ ownSnippet, $resolve } from '../shared/symbols.ts' ;
5+ import { $internal , $ownSnippet , $resolve } from '../shared/symbols.ts' ;
66import type { ResolutionCtx , SelfResolvable } from '../types.ts' ;
77import { UnknownData } from './dataTypes.ts' ;
88import type { DualFn } from './dualFn.ts' ;
@@ -19,9 +19,15 @@ import {
1919// Public API
2020// ----------
2121
22+ /**
23+ * A reference to a value `T`. Can be passed to other functions to give them
24+ * mutable access to the underlying value.
25+ *
26+ * Conceptually, it represents a WGSL pointer.
27+ */
2228export interface ref < T > {
2329 readonly [ $internal ] : unknown ;
24- readonly [ $isRef ] : true ;
30+ readonly type : 'ref' ;
2531
2632 /**
2733 * Derefences the reference, and gives access to the underlying value.
@@ -38,7 +44,6 @@ export interface ref<T> {
3844 $ : T ;
3945}
4046
41- // TODO: Restrict calls to this function only from within TypeGPU functions
4247export const ref : DualFn < < T > ( value : T ) => ref < T > > = ( ( ) => {
4348 const gpuImpl = ( value : Snippet ) => {
4449 /**
@@ -84,19 +89,23 @@ export const ref: DualFn<<T>(value: T) => ref<T>> = (() => {
8489 return impl as unknown as DualFn << T > ( value : T ) => ref < T >> ;
8590} ) ( ) ;
8691
92+ export function isRef < T > ( value : unknown | ref < T > ) : value is ref < T > {
93+ return value instanceof refImpl ;
94+ }
95+
8796// --------------
8897// Implementation
8998// --------------
9099
91100class refImpl < T > implements ref < T > {
92- #value: T ;
93101 readonly [ $internal ] : true ;
94- readonly [ $isRef ] : true ;
102+ readonly type : 'ref' ;
103+ #value: T ;
95104
96105 constructor ( value : T ) {
97- this . #value = value ;
98106 this [ $internal ] = true ;
99- this [ $isRef ] = true ;
107+ this . type = 'ref' ;
108+ this . #value = value ;
100109 }
101110
102111 get $ ( ) : T {
0 commit comments