Skip to content

Commit d5d4be5

Browse files
committed
refactor: Naming cleanup
1 parent 6a12e46 commit d5d4be5

File tree

17 files changed

+187
-313
lines changed

17 files changed

+187
-313
lines changed

packages/typegpu/src/core/buffer/bufferUsage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class TgpuFixedBufferImpl<TData extends BaseData, TUsage extends BindableBufferU
118118

119119
[$resolve](ctx: ResolutionCtx): ResolvedSnippet {
120120
const dataType = this.buffer.dataType;
121-
const id = ctx.getUniqueName(this);
121+
const id = ctx.makeUniqueIdentifier(getName(this), 'global');
122122
const { group, binding } = ctx.allocateFixedEntry(
123123
this.usage === 'uniform' ? { uniform: dataType } : { storage: dataType, access: this.usage },
124124
this.buffer,
@@ -241,7 +241,7 @@ export class TgpuLaidOutBufferImpl<TData extends BaseData, TUsage extends Bindab
241241
}
242242

243243
[$resolve](ctx: ResolutionCtx): ResolvedSnippet {
244-
const id = ctx.getUniqueName(this);
244+
const id = ctx.makeUniqueIdentifier(getName(this), 'global');
245245
const group = ctx.allocateLayoutEntry(this.#membership.layout);
246246
const usage = usageToVarTemplateMap[this.usage];
247247

packages/typegpu/src/core/constant/tgpuConstant.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class TgpuConstImpl<TDataType extends BaseData> implements TgpuConst<TDataType>,
113113
}
114114

115115
[$resolve](ctx: ResolutionCtx): ResolvedSnippet {
116-
const id = ctx.getUniqueName(this);
116+
const id = ctx.makeUniqueIdentifier(getName(this), 'global');
117117
const resolvedDataType = ctx.resolve(this.dataType).value;
118118
const resolvedValue = ctx.resolve(this.#value, this.dataType).value;
119119

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +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 { isValidIdentifier } from '../../nameUtils.ts';
67
import { getMetaData, getName } from '../../shared/meta.ts';
78
import { $getNameForward } from '../../shared/symbols.ts';
89
import type { ResolutionCtx, TgpuShaderStage } from '../../types.ts';
@@ -65,37 +66,41 @@ export function createFnCore(
6566
applyExternals(externalMap, externals);
6667
}
6768

68-
const id = ctx.getUniqueName(this);
69+
const id = ctx.makeUniqueIdentifier(getName(this), 'global');
6970

7071
if (typeof implementation === 'string') {
7172
if (!returnType) {
7273
throw new Error('Explicit return type is required for string implementation');
7374
}
7475

75-
const validArgNames = entryInput
76-
? Object.fromEntries(
77-
entryInput.positionalArgs.map((a) => [a.schemaKey, ctx.makeNameValid(a.schemaKey)]),
78-
)
79-
: undefined;
76+
if (entryInput) {
77+
for (const arg of entryInput.positionalArgs) {
78+
if (!isValidIdentifier(arg.schemaKey)) {
79+
throw new Error(`Invalid argument name: ${arg.schemaKey}`);
80+
}
81+
}
8082

81-
if (validArgNames && Object.keys(validArgNames).length > 0) {
82-
applyExternals(externalMap, { in: validArgNames });
83+
applyExternals(externalMap, {
84+
in: Object.fromEntries(
85+
entryInput.positionalArgs.map((a) => [a.schemaKey, a.schemaKey]),
86+
),
87+
});
8388
}
8489

8590
const replacedImpl = replaceExternalsInWgsl(ctx, externalMap, implementation);
8691

8792
let header = '';
8893
let body = '';
8994

90-
if (functionType !== 'normal' && entryInput && validArgNames) {
95+
if (functionType !== 'normal' && entryInput) {
9196
const { dataSchema, positionalArgs } = entryInput;
9297
const parts: string[] = [];
9398
if (dataSchema && isArgUsedInBody('in', replacedImpl)) {
9499
parts.push(`in: ${ctx.resolve(dataSchema).value}`);
95100
}
96101
for (const a of positionalArgs) {
97-
const argName = validArgNames[a.schemaKey] ?? '';
98-
if (argName !== '' && isArgUsedInBody(argName, replacedImpl)) {
102+
const argName = a.schemaKey;
103+
if (isArgUsedInBody(argName, replacedImpl)) {
99104
parts.push(`${getAttributesString(a.type)}${argName}: ${ctx.resolve(a.type).value}`);
100105
}
101106
}
Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import type { ResolvedSnippet } from '../../data/snippet.ts';
2-
import { type NameRegistry, RandomNameRegistry, StrictNameRegistry } from '../../nameRegistry.ts';
3-
import { getName } from '../../shared/meta.ts';
2+
import { bannedTokens, builtins } from '../../nameUtils.ts';
43
import { $internal } from '../../shared/symbols.ts';
54
import { ShelllessRepository } from '../../tgsl/shellless.ts';
65
import type { TgpuLazy, TgpuSlot } from '../slot/slotTypes.ts';
76

87
type SlotToValueMap = Map<TgpuSlot<unknown>, unknown>;
98

109
export interface NamespaceInternal {
11-
readonly nameRegistry: NameRegistry;
10+
readonly takenGlobalIdentifiers: Set<string>;
1211
readonly shelllessRepo: ShelllessRepository;
12+
readonly strategy: 'random' | 'strict';
1313

1414
memoizedResolves: WeakMap<
1515
// WeakMap because if the item does not exist anymore,
@@ -24,73 +24,32 @@ export interface NamespaceInternal {
2424
TgpuLazy<unknown>,
2525
{ slotToValueMap: SlotToValueMap; result: unknown }[]
2626
>;
27-
28-
listeners: {
29-
[K in keyof NamespaceEventMap]: Set<(event: NamespaceEventMap[K]) => void>;
30-
};
3127
}
3228

33-
type NamespaceEventMap = {
34-
name: { target: object; name: string };
35-
};
36-
37-
type DetachListener = () => void;
38-
3929
export interface Namespace {
4030
readonly [$internal]: NamespaceInternal;
41-
42-
on<TEvent extends keyof NamespaceEventMap>(
43-
event: TEvent,
44-
listener: (event: NamespaceEventMap[TEvent]) => void,
45-
): DetachListener;
4631
}
4732

4833
class NamespaceImpl implements Namespace {
4934
readonly [$internal]: NamespaceInternal;
5035

51-
constructor(nameRegistry: NameRegistry) {
36+
constructor(strategy: 'random' | 'strict') {
5237
this[$internal] = {
53-
nameRegistry,
38+
strategy,
39+
takenGlobalIdentifiers: new Set([...bannedTokens, ...builtins]),
5440
shelllessRepo: new ShelllessRepository(),
5541
memoizedResolves: new WeakMap(),
5642
memoizedLazy: new WeakMap(),
57-
listeners: {
58-
name: new Set(),
59-
},
6043
};
6144
}
62-
63-
on<TEvent extends keyof NamespaceEventMap>(
64-
event: TEvent,
65-
listener: (event: NamespaceEventMap[TEvent]) => void,
66-
): DetachListener {
67-
if (event === 'name') {
68-
const listeners = this[$internal].listeners.name;
69-
listeners.add(listener);
70-
71-
return () => listeners.delete(listener);
72-
}
73-
74-
throw new Error(`Unsupported event: ${event}`);
75-
}
7645
}
7746

7847
export interface NamespaceOptions {
7948
names?: 'random' | 'strict' | undefined;
8049
}
8150

82-
export function getUniqueName(namespace: NamespaceInternal, resource: object): string {
83-
const name = namespace.nameRegistry.makeUnique(getName(resource), true);
84-
for (const listener of namespace.listeners.name) {
85-
listener({ target: resource, name });
86-
}
87-
return name;
88-
}
89-
9051
export function namespace(options?: NamespaceOptions): Namespace {
9152
const { names = 'strict' } = options ?? {};
9253

93-
return new NamespaceImpl(
94-
names === 'strict' ? new StrictNameRegistry() : new RandomNameRegistry(),
95-
);
54+
return new NamespaceImpl(names);
9655
}

packages/typegpu/src/core/resolve/resolveData.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import type {
3838
WgslArray,
3939
WgslStruct,
4040
} from '../../data/wgslTypes.ts';
41+
import { getName } from '../../shared/meta.ts';
4142
import { $internal } from '../../shared/symbols.ts';
4243
import { assertExhaustive } from '../../shared/utilityTypes.ts';
4344
import type { ResolutionCtx } from '../../types.ts';
@@ -127,7 +128,7 @@ function resolveStruct(ctx: ResolutionCtx, struct: WgslStruct) {
127128
if (struct[$internal].isAbstruct) {
128129
throw new Error('Cannot resolve abstract struct types to WGSL.');
129130
}
130-
const id = ctx.getUniqueName(struct);
131+
const id = ctx.makeUniqueIdentifier(getName(struct), 'global');
131132

132133
ctx.addDeclaration(`\
133134
struct ${id} {
@@ -155,7 +156,7 @@ ${Object.entries(struct.propTypes)
155156
* ```
156157
*/
157158
function resolveUnstruct(ctx: ResolutionCtx, unstruct: Unstruct) {
158-
const id = ctx.getUniqueName(unstruct);
159+
const id = ctx.makeUniqueIdentifier(getName(unstruct), 'global');
159160

160161
ctx.addDeclaration(`\
161162
struct ${id} {

packages/typegpu/src/core/sampler/sampler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export class TgpuLaidOutSamplerImpl<
9999
}
100100

101101
[$resolve](ctx: ResolutionCtx): ResolvedSnippet {
102-
const id = ctx.getUniqueName(this);
102+
const id = ctx.makeUniqueIdentifier(getName(this), 'global');
103103
const group = ctx.allocateLayoutEntry(this.#membership.layout);
104104

105105
ctx.addDeclaration(
@@ -186,7 +186,7 @@ class TgpuFixedSamplerImpl<T extends WgslSampler | WgslComparisonSampler>
186186
}
187187

188188
[$resolve](ctx: ResolutionCtx): ResolvedSnippet {
189-
const id = ctx.getUniqueName(this);
189+
const id = ctx.makeUniqueIdentifier(getName(this), 'global');
190190

191191
const { group, binding } = ctx.allocateFixedEntry(
192192
this.schema.type === 'sampler_comparison'

packages/typegpu/src/core/texture/externalTexture.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class TgpuExternalTextureImpl implements TgpuExternalTexture, SelfResolva
3737
}
3838

3939
[$resolve](ctx: ResolutionCtx): ResolvedSnippet {
40-
const id = ctx.getUniqueName(this);
40+
const id = ctx.makeUniqueIdentifier(getName(this), 'global');
4141
const group = ctx.allocateLayoutEntry(this.#membership.layout);
4242

4343
ctx.addDeclaration(

packages/typegpu/src/core/texture/texture.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ class TgpuFixedTextureViewImpl<T extends WgslTexture | WgslStorageTexture>
600600
}
601601

602602
[$resolve](ctx: ResolutionCtx): ResolvedSnippet {
603-
const id = ctx.getUniqueName(this);
603+
const id = ctx.makeUniqueIdentifier(getName(this), 'global');
604604
const { group, binding } = ctx.allocateFixedEntry(
605605
isWgslStorageTexture(this.schema)
606606
? {
@@ -642,7 +642,7 @@ export class TgpuLaidOutTextureViewImpl<T extends WgslTexture | WgslStorageTextu
642642
}
643643

644644
[$resolve](ctx: ResolutionCtx): ResolvedSnippet {
645-
const id = ctx.getUniqueName(this);
645+
const id = ctx.makeUniqueIdentifier(getName(this), 'global');
646646
const group = ctx.allocateLayoutEntry(this.#membership.layout);
647647

648648
ctx.addDeclaration(

packages/typegpu/src/core/variable/tgpuVariable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class TgpuVarImpl<TScope extends VariableScope, TDataType extends BaseData>
8787
}
8888

8989
[$resolve](ctx: ResolutionCtx): ResolvedSnippet {
90-
const id = ctx.getUniqueName(this);
90+
const id = ctx.makeUniqueIdentifier(getName(this), 'global');
9191
const pre = `var<${this.#scope}> ${id}: ${ctx.resolve(this.#dataType).value}`;
9292

9393
if (this.#initialValue) {

packages/typegpu/src/data/autoStruct.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createIoSchema } from '../core/function/ioSchema.ts';
2-
import { isValidProp } from '../nameRegistry.ts';
2+
import { isValidProp } from '../nameUtils.ts';
33
import { getName, setName } from '../shared/meta.ts';
44
import { $internal, $repr, $resolve } from '../shared/symbols.ts';
55
import type { ResolutionCtx, SelfResolvable } from '../types.ts';

0 commit comments

Comments
 (0)