@@ -41,7 +41,7 @@ export interface ref<T> {
4141// TODO: Restrict calls to this function only from within TypeGPU functions
4242export const ref : DualFn < < T > ( value : T ) => ref < T > > = ( ( ) => {
4343 const gpuImpl = ( value : Snippet ) => {
44- return snip ( new RefOnGPU ( value ) , UnknownData , /* origin */ 'runtime' ) ;
44+ return snip ( new RefOperator ( value ) , UnknownData , /* origin */ 'runtime' ) ;
4545 } ;
4646
4747 const jsImpl = < T > ( value : T ) => new refImpl ( value ) ;
@@ -99,66 +99,43 @@ class refImpl<T> implements ref<T> {
9999 }
100100}
101101
102- export class RefOnGPU {
102+ export class RefOperator implements SelfResolvable {
103103 readonly [ $internal ] : true ;
104-
105104 readonly snippet : Snippet ;
105+
106106 /**
107107 * Pointer params only exist if the ref was created from a reference (buttery-butter).
108+ *
109+ * @example
110+ * ```ts
111+ * const life = ref(42); // created from a value
112+ * const boid = ref(layout.$.boids[0]); // created from a reference
113+ * ```
108114 */
109115 readonly ptrType : Ptr | undefined ;
110116
111117 constructor ( snippet : Snippet ) {
112118 this [ $internal ] = true ;
113119 this . snippet = snippet ;
114- this . ptrType = createPtrFromOrigin (
115- snippet . origin ,
116- snippet . dataType as StorableData ,
117- ) ;
118- }
119-
120- toString ( ) : string {
121- return `ref:${ this . snippet . value } ` ;
122- }
123120
124- [ $resolve ] ( ctx : ResolutionCtx ) : ResolvedSnippet {
125- invariant (
126- ! ! this . ptrType ,
127- 'RefOnGPU must have a pointer type when resolved' ,
128- ) ;
129- return snip ( stitch `(&${ this . snippet } )` , this . ptrType , this . snippet . origin ) ;
130- }
131- }
132-
133- export class RefOperator implements SelfResolvable {
134- readonly [ $internal ] : true ;
135- readonly snippet : Snippet ;
136- readonly #ptrType: Ptr ;
137-
138- constructor ( snippet : Snippet ) {
139- this [ $internal ] = true ;
140- this . snippet = snippet ;
141-
142- const ptrType = createPtrFromOrigin (
121+ this . ptrType = createPtrFromOrigin (
143122 snippet . origin ,
144123 snippet . dataType as StorableData ,
145124 ) ;
146-
147- if ( ! ptrType ) {
148- throw new Error (
149- `Cannot take a reference of a value with origin ${ this . snippet . origin } ` ,
150- ) ;
151- }
152-
153- this . #ptrType = ptrType ;
154125 }
155126
156127 get [ $ownSnippet ] ( ) : Snippet {
157- return snip ( this , this . #ptrType, this . snippet . origin ) ;
128+ if ( ! this . ptrType ) {
129+ throw new Error ( stitch `Cannot take a reference of ${ this . snippet } ` ) ;
130+ }
131+ return snip ( this , this . ptrType , this . snippet . origin ) ;
158132 }
159133
160134 [ $resolve ] ( ctx : ResolutionCtx ) : ResolvedSnippet {
161- return snip ( stitch `(&${ this . snippet } )` , this . #ptrType, this . snippet . origin ) ;
135+ if ( ! this . ptrType ) {
136+ throw new Error ( stitch `Cannot take a reference of ${ this . snippet } ` ) ;
137+ }
138+ return snip ( stitch `(&${ this . snippet } )` , this . ptrType , this . snippet . origin ) ;
162139 }
163140}
164141
0 commit comments