11import { stitch } from '../core/resolve/stitch.ts' ;
2- import { dualImpl } from '../core/function/dualImpl.ts' ;
32import { $internal } from '../shared/symbols.ts' ;
43import type {
54 AbstractFloat ,
@@ -11,6 +10,7 @@ import type {
1110 U16 ,
1211 U32 ,
1312} from './wgslTypes.ts' ;
13+ import { callableSchema } from '../core/function/createCallableSchema.ts' ;
1414
1515export const abstractInt = {
1616 [ $internal ] : { } ,
@@ -28,7 +28,7 @@ export const abstractFloat = {
2828 } ,
2929} as AbstractFloat ;
3030
31- const boolCast = dualImpl ( {
31+ const boolCast = callableSchema ( {
3232 name : 'bool' ,
3333 signature : ( arg ) => ( { argTypes : arg ? [ arg ] : [ ] , returnType : bool } ) ,
3434 normalImpl ( v ?: number | boolean ) {
@@ -66,7 +66,7 @@ export const bool: Bool = Object.assign(boolCast, {
6666 type : 'bool' ,
6767} ) as unknown as Bool ;
6868
69- const u32Cast = dualImpl ( {
69+ const u32Cast = callableSchema ( {
7070 name : 'u32' ,
7171 signature : ( arg ) => ( { argTypes : arg ? [ arg ] : [ ] , returnType : u32 } ) ,
7272 normalImpl ( v ?: number | boolean ) {
@@ -76,6 +76,17 @@ const u32Cast = dualImpl({
7676 if ( typeof v === 'boolean' ) {
7777 return v ? 1 : 0 ;
7878 }
79+ if ( ! Number . isInteger ( v ) ) {
80+ const truncated = Math . trunc ( v ) ;
81+ if ( truncated < 0 ) {
82+ return 0 ;
83+ }
84+ if ( truncated > 0xffffffff ) {
85+ return 0xffffffff ;
86+ }
87+ return truncated ;
88+ }
89+ // Integer input: treat as bit reinterpretation (i32 -> u32)
7990 return ( v & 0xffffffff ) >>> 0 ;
8091 } ,
8192 codegenImpl : ( _ctx , [ arg ] ) =>
@@ -106,7 +117,7 @@ export const u32: U32 = Object.assign(u32Cast, {
106117 type : 'u32' ,
107118} ) as unknown as U32 ;
108119
109- const i32Cast = dualImpl ( {
120+ const i32Cast = callableSchema ( {
110121 name : 'i32' ,
111122 signature : ( arg ) => ( { argTypes : arg ? [ arg ] : [ ] , returnType : i32 } ) ,
112123 normalImpl ( v ?: number | boolean ) {
@@ -149,7 +160,7 @@ export const i32: I32 = Object.assign(i32Cast, {
149160 type : 'i32' ,
150161} ) as unknown as I32 ;
151162
152- const f32Cast = dualImpl ( {
163+ const f32Cast = callableSchema ( {
153164 name : 'f32' ,
154165 signature : ( arg ) => ( { argTypes : arg ? [ arg ] : [ ] , returnType : f32 } ) ,
155166 normalImpl ( v ?: number | boolean ) {
@@ -275,7 +286,7 @@ function roundToF16(x: number): number {
275286 return fromHalfBits ( toHalfBits ( x ) ) ;
276287}
277288
278- const f16Cast = dualImpl ( {
289+ const f16Cast = callableSchema ( {
279290 name : 'f16' ,
280291 signature : ( arg ) => ( { argTypes : arg ? [ arg ] : [ ] , returnType : f16 } ) ,
281292 normalImpl ( v ?: number | boolean ) {
0 commit comments