Skip to content

Commit e29acd4

Browse files
Handle externals2 if present
1 parent 7a3756d commit e29acd4

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

packages/typegpu/src/core/function/fnCore.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { undecorate } from '../../data/dataTypes.ts';
33
import { type ResolvedSnippet, snip } from '../../data/snippet.ts';
44
import { type BaseData, isWgslData, isWgslStruct, Void } from '../../data/wgslTypes.ts';
55
import { MissingLinksError } from '../../errors.ts';
6-
import { getMetaData, getName } from '../../shared/meta.ts';
6+
import { getMetaData, getName, type Externals2 } from '../../shared/meta.ts';
77
import { $getNameForward } from '../../shared/symbols.ts';
88
import type { ResolutionCtx } from '../../types.ts';
99
import { applyExternals, type ExternalMap, replaceExternalsInWgsl } from '../resolve/externals.ts';
@@ -148,11 +148,14 @@ export function createFnCore(implementation: Implementation, fnAttribute = ''):
148148
const pluginData = getMetaData(implementation);
149149

150150
// Passing a record happens prior to version 0.9.0
151+
// Passing a function happens prior to version 0.12.0
151152
// TODO: Support for this can be removed down the line
152-
const pluginExternals =
153-
typeof pluginData?.externals === 'function'
154-
? pluginData.externals()
155-
: pluginData?.externals;
153+
let pluginExternals: ExternalMap | Record<string, unknown> | undefined =
154+
pluginData?.externals2
155+
? externals2ToExternalMap(pluginData.externals2)
156+
: typeof pluginData?.externals === 'function'
157+
? pluginData.externals()
158+
: pluginData?.externals;
156159

157160
if (pluginExternals) {
158161
const missing = Object.fromEntries(
@@ -220,6 +223,22 @@ export function createFnCore(implementation: Implementation, fnAttribute = ''):
220223
return core;
221224
}
222225

226+
// TODO: deslopify, document, make sure it works as intended
227+
function externals2ToExternalMap(ext2: Externals2): ExternalMap {
228+
const result: ExternalMap = {};
229+
for (const [key, value] of Object.entries(ext2)) {
230+
if (typeof value === 'function') {
231+
Object.defineProperty(result, key, {
232+
get: value,
233+
enumerable: true,
234+
});
235+
} else {
236+
result[key] = externals2ToExternalMap(value);
237+
}
238+
}
239+
return result;
240+
}
241+
223242
function isArgUsedInBody(argName: string, body: string): boolean {
224243
return new RegExp(`\\b${argName}\\b`).test(body);
225244
}

packages/typegpu/src/shared/meta.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export interface Externals {
88
[key: string]: Externals | string;
99
}
1010

11+
export interface Externals2 {
12+
[key: string]: Externals2 | (() => unknown);
13+
}
14+
1115
export interface MetaData {
1216
v?: number;
1317
name?: string | undefined;
@@ -22,6 +26,7 @@ export interface MetaData {
2226
// Passing a record happens prior to version 0.9.0
2327
// TODO: Support for this can be removed down the line
2428
Record<string, unknown> | (() => Record<string, unknown>) | undefined;
29+
externals2?: Externals2;
2530
}
2631

2732
/**

0 commit comments

Comments
 (0)