@@ -26,7 +26,7 @@ import { safeStringify } from '../shared/stringify.ts';
2626import { $internal } from '../shared/symbols.ts' ;
2727import { pow } from '../std/numeric.ts' ;
2828import { add , div , mul , neg , sub } from '../std/operators.ts' ;
29- import type { FnArgsConversionHint } from '../types.ts' ;
29+ import { type FnArgsConversionHint , isKnownAtComptime } from '../types.ts' ;
3030import {
3131 convertStructValues ,
3232 convertToCommonType ,
@@ -50,6 +50,8 @@ const { NodeTypeCatalog: NODE } = tinyest;
5050const parenthesizedOps = [
5151 '==' ,
5252 '!=' ,
53+ '===' ,
54+ '!==' ,
5355 '<' ,
5456 '<=' ,
5557 '>' ,
@@ -68,7 +70,64 @@ const parenthesizedOps = [
6870 '||' ,
6971] ;
7072
71- const binaryLogicalOps = [ '&&' , '||' , '==' , '!=' , '<' , '<=' , '>' , '>=' ] ;
73+ const binaryLogicalOps = [
74+ '&&' ,
75+ '||' ,
76+ '==' ,
77+ '!=' ,
78+ '===' ,
79+ '!==' ,
80+ '<' ,
81+ '<=' ,
82+ '>' ,
83+ '>=' ,
84+ ] ;
85+
86+ const OP_MAP = {
87+ //
88+ // binary
89+ //
90+ '===' : '==' ,
91+ '!==' : '!=' ,
92+ get '>>>' ( ) : never {
93+ throw new Error ( 'The `>>>` operator is unsupported in TypeGPU functions.' ) ;
94+ } ,
95+ get in ( ) : never {
96+ throw new Error ( 'The `in` operator is unsupported in TypeGPU functions.' ) ;
97+ } ,
98+ get instanceof ( ) : never {
99+ throw new Error (
100+ 'The `instanceof` operator is unsupported in TypeGPU functions.' ,
101+ ) ;
102+ } ,
103+ get '|>' ( ) : never {
104+ throw new Error ( 'The `|>` operator is unsupported in TypeGPU functions.' ) ;
105+ } ,
106+ //
107+ // logical
108+ //
109+ get '??' ( ) : never {
110+ throw new Error ( 'The `??` operator is unsupported in TypeGPU functions.' ) ;
111+ } ,
112+ //
113+ // assignment
114+ //
115+ get '>>>=' ( ) : never {
116+ throw new Error ( 'The `>>>=` operator is unsupported in TypeGPU functions.' ) ;
117+ } ,
118+ get '**=' ( ) : never {
119+ throw new Error ( 'The `**=` operator is unsupported in TypeGPU functions.' ) ;
120+ } ,
121+ get '??=' ( ) : never {
122+ throw new Error ( 'The `??=` operator is unsupported in TypeGPU functions.' ) ;
123+ } ,
124+ get '&&=' ( ) : never {
125+ throw new Error ( 'The `&&=` operator is unsupported in TypeGPU functions.' ) ;
126+ } ,
127+ get '||=' ( ) : never {
128+ throw new Error ( 'The `||=` operator is unsupported in TypeGPU functions.' ) ;
129+ } ,
130+ } as Record < string , string > ;
72131
73132type Operator =
74133 | tinyest . BinaryOperator
@@ -261,6 +320,16 @@ ${this.ctx.pre}}`;
261320 ) ;
262321 }
263322
323+ if ( op === '==' ) {
324+ throw new Error ( 'Please use the === operator instead of ==' ) ;
325+ }
326+
327+ if (
328+ op === '===' && isKnownAtComptime ( lhsExpr ) && isKnownAtComptime ( rhsExpr )
329+ ) {
330+ return snip ( lhsExpr . value === rhsExpr . value , bool , 'constant' ) ;
331+ }
332+
264333 if ( lhsExpr . dataType . type === 'unknown' ) {
265334 throw new WgslTypeError ( `Left-hand side of '${ op } ' is of unknown type` ) ;
266335 }
@@ -328,8 +397,8 @@ ${this.ctx.pre}}`;
328397
329398 return snip (
330399 parenthesizedOps . includes ( op )
331- ? `(${ lhsStr } ${ op } ${ rhsStr } )`
332- : `${ lhsStr } ${ op } ${ rhsStr } ` ,
400+ ? `(${ lhsStr } ${ OP_MAP [ op ] ?? op } ${ rhsStr } )`
401+ : `${ lhsStr } ${ OP_MAP [ op ] ?? op } ${ rhsStr } ` ,
333402 type ,
334403 // Result of an operation, so not a reference to anything
335404 /* origin */ 'runtime' ,
0 commit comments