Skip to content

Commit e717fae

Browse files
authored
Proposed way to proceed with tgpu.fn (#2030)
1 parent 3d71e24 commit e717fae

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

packages/typegpu/src/resolutionCtx.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,10 @@ export class ResolutionCtxImpl implements ResolutionCtx {
607607
}
608608

609609
withSlots<T>(pairs: SlotValuePair<unknown>[], callback: () => T): T {
610+
if (pairs.length === 0) {
611+
return callback();
612+
}
613+
610614
this._itemStateStack.pushSlotBindings(pairs);
611615

612616
try {

packages/typegpu/src/tgsl/wgslGenerator.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -549,27 +549,36 @@ ${this.ctx.pre}}`;
549549
return callee.value.operator(callee.value.lhs, rhs);
550550
}
551551

552-
if (!isMarkedInternal(callee.value)) {
553-
const args = argNodes.map((arg) => this.expression(arg));
554-
const shellless = this.ctx.shelllessRepo.get(
555-
callee.value as (...args: never[]) => unknown,
556-
args,
557-
);
558-
if (shellless) {
559-
const converted = args.map((s, idx) => {
560-
const argType = shellless.argTypes[idx] as AnyData;
561-
return tryConvertSnippet(s, argType, /* verbose */ false);
562-
});
563-
564-
return this.ctx.withResetIndentLevel(() => {
565-
const snippet = this.ctx.resolve(shellless);
566-
return snip(
567-
stitch`${snippet.value}(${converted})`,
568-
snippet.dataType,
569-
/* origin */ 'runtime',
570-
);
571-
});
572-
}
552+
if (!isMarkedInternal(callee.value) || isGenericFn(callee.value)) {
553+
const slotPairs = isGenericFn(callee.value)
554+
? callee.value[$providing]?.pairs
555+
: [];
556+
const callback = isGenericFn(callee.value)
557+
? callee.value.callback
558+
: (callee.value as (...args: never[]) => unknown);
559+
560+
this.ctx.withSlots(slotPairs, () => {
561+
const args = argNodes.map((arg) => this.expression(arg));
562+
const shellless = this.ctx.shelllessRepo.get(
563+
callback,
564+
args,
565+
);
566+
if (shellless) {
567+
const converted = args.map((s, idx) => {
568+
const argType = shellless.argTypes[idx] as AnyData;
569+
return tryConvertSnippet(s, argType, /* verbose */ false);
570+
});
571+
572+
return this.ctx.withResetIndentLevel(() => {
573+
const snippet = this.ctx.resolve(shellless);
574+
return snip(
575+
stitch`${snippet.value}(${converted})`,
576+
snippet.dataType,
577+
/* origin */ 'runtime',
578+
);
579+
});
580+
}
581+
});
573582

574583
throw new Error(
575584
`Function '${

0 commit comments

Comments
 (0)