11import type { DualFn } from '../../data/dualFn.ts' ;
2- import {
3- type MapValueToSnippet ,
4- snip ,
5- type Snippet ,
6- } from '../../data/snippet.ts' ;
7- import { inCodegenMode } from '../../execMode.ts' ;
8- import { type FnArgsConversionHint , isKnownAtComptime } from '../../types.ts' ;
2+ import { type MapValueToSnippet , snip } from '../../data/snippet.ts' ;
3+ import { getResolutionCtx , inCodegenMode } from '../../execMode.ts' ;
4+ import { isKnownAtComptime , NormalState } from '../../types.ts' ;
95import { setName } from '../../shared/meta.ts' ;
106import { $internal } from '../../shared/symbols.ts' ;
117import { tryConvertSnippet } from '../../tgsl/conversion.ts' ;
128import type { AnyData } from '../../data/dataTypes.ts' ;
139
14- export function createDualImpl < T extends ( ...args : never [ ] ) => unknown > (
15- jsImpl : T ,
16- gpuImpl : ( ...args : MapValueToSnippet < Parameters < T > > ) => Snippet ,
17- name : string ,
18- argConversionHint : FnArgsConversionHint = 'keep' ,
19- ) : DualFn < T > {
20- const impl = ( ( ...args : Parameters < T > ) => {
21- if ( inCodegenMode ( ) ) {
22- return gpuImpl ( ...( args as MapValueToSnippet < Parameters < T > > ) ) as Snippet ;
23- }
24- return jsImpl ( ...args ) ;
25- } ) as T ;
26-
27- setName ( impl , name ) ;
28- impl . toString = ( ) => name ;
29- Object . defineProperty ( impl , $internal , {
30- value : {
31- jsImpl,
32- gpuImpl,
33- argConversionHint,
34- } ,
35- } ) ;
36-
37- return impl as DualFn < T > ;
38- }
39-
4010type MapValueToDataType < T > = { [ K in keyof T ] : AnyData } ;
4111
4212interface DualImplOptions < T extends ( ...args : never [ ] ) => unknown > {
@@ -68,6 +38,8 @@ export function dualImpl<T extends (...args: never[]) => unknown>(
6838 options : DualImplOptions < T > ,
6939) : DualFn < T > {
7040 const gpuImpl = ( ...args : MapValueToSnippet < Parameters < T > > ) => {
41+ // biome-ignore lint/style/noNonNullAssertion: it's there
42+ const ctx = getResolutionCtx ( ) ! ;
7143 const { argTypes, returnType } = typeof options . signature === 'function'
7244 ? options . signature (
7345 ...args . map ( ( s ) => {
@@ -94,6 +66,7 @@ export function dualImpl<T extends (...args: never[]) => unknown>(
9466 converted . every ( ( s ) => isKnownAtComptime ( s ) ) &&
9567 typeof options . normalImpl === 'function'
9668 ) {
69+ ctx . pushMode ( new NormalState ( ) ) ;
9770 try {
9871 return snip (
9972 options . normalImpl ( ...converted . map ( ( s ) => s . value ) as never [ ] ) ,
@@ -108,6 +81,8 @@ export function dualImpl<T extends (...args: never[]) => unknown>(
10881 if ( ! ( e instanceof MissingCpuImplError ) ) {
10982 throw e ;
11083 }
84+ } finally {
85+ ctx . popMode ( 'normal' ) ;
11186 }
11287 }
11388
@@ -135,9 +110,11 @@ export function dualImpl<T extends (...args: never[]) => unknown>(
135110 value : {
136111 jsImpl : options . normalImpl ,
137112 gpuImpl,
138- strictSignature : typeof options . signature !== 'function'
139- ? options . signature
140- : undefined ,
113+ get strictSignature ( ) {
114+ return typeof options . signature !== 'function'
115+ ? options . signature
116+ : undefined ;
117+ } ,
141118 argConversionHint : 'keep' ,
142119 } ,
143120 } ) ;
0 commit comments