Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions packages/typegpu/src/shared/meta.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// The version is inlined during build-time 🎉
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉🎉

import { version } from '../../package.json';
import type { Block, FuncParameter } from 'tinyest';
import { DEV, TEST } from './env.ts';
import { $getNameForward, isMarkedInternal } from './symbols.ts';
Expand Down Expand Up @@ -28,23 +30,27 @@ export interface MetaData {
* @internal
*/
export type INTERNAL_GlobalExt = typeof globalThis & {
__TYPEGPU_VERSION__: string | undefined;
__TYPEGPU_META__: WeakMap<object, MetaData>;
__TYPEGPU_AUTONAME__: <T>(exp: T, label: string) => T;
__TYPEGPU_MEASURE_PERF__?: boolean | undefined;
__TYPEGPU_PERF_RECORDS__?: Map<string, unknown[]> | undefined;
};

Object.assign(globalThis, {
'__TYPEGPU_AUTONAME__': <T>(exp: T, label: string): T => {
if (isNamable(exp) && isMarkedInternal(exp) && !getName(exp)) {
exp.$name(label);
}
return exp;
},
});

const globalWithMeta = globalThis as INTERNAL_GlobalExt;

if (globalWithMeta.__TYPEGPU_VERSION__ !== undefined) {
console.warn(
`Found duplicate TypeGPU version. First was ${globalWithMeta.__TYPEGPU_VERSION__}, this one is ${version}. This may cause unexpected behavior.`,
);
}

globalWithMeta.__TYPEGPU_VERSION__ = version;
globalWithMeta.__TYPEGPU_AUTONAME__ = <T>(exp: T, label: string): T =>
isNamable(exp) && isMarkedInternal(exp) && !getName(exp)
? exp.$name(label)
: exp;

/**
* Performance measurements are only enabled in dev & test environments for now
*/
Expand Down