Skip to content

Commit

Permalink
Replace WGSLPlaceholder with WGSLSlot (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhawryluk authored Jul 10, 2024
1 parent 3b9eced commit 913fe1d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 40 deletions.
2 changes: 1 addition & 1 deletion packages/wigsill/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export { WGSLConstant } from './wgslConstant';
export { WGSLFunction } from './wgslFunction';
export { WGSLIdentifier } from './wgslIdentifier';
export { WGSLMemory } from './wgslMemory';
export { WGSLPlaceholder } from './wgslPlaceholder';
export { WGSLSlot } from './wgslSlot';
export { WGSLRequire } from './wgslRequire';
10 changes: 9 additions & 1 deletion packages/wigsill/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,17 @@ export function isWGSLItem(value: unknown): value is WGSLItem {
);
}

export interface WGSLBindableTrait<TBinding> extends WGSLItem {
export function isWGSLSegment(value: unknown): value is WGSLSegment {
return (
typeof value === 'number' || typeof value === 'string' || isWGSLItem(value)
);
}

export interface WGSLBindableTrait<TBinding> {
/** type-token, not available at runtime */
readonly __bindingType: TBinding;

readonly debugLabel?: string | undefined;
}

export type WGSLBindPair<T> = [WGSLBindableTrait<T>, T];
Expand Down
4 changes: 2 additions & 2 deletions packages/wigsill/src/wgsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { fn as fun } from './wgslFunction';
import { fn } from './wgslFunctionOld';
import { identifier } from './wgslIdentifier';
import { memory } from './wgslMemory';
import { placeholder } from './wgslPlaceholder';
import { require } from './wgslRequire';
import { slot } from './wgslSlot';
import { variable } from './wgslVariable';

export default Object.assign(code, {
Expand All @@ -14,7 +14,7 @@ export default Object.assign(code, {
fun,
identifier,
memory,
placeholder,
slot,
constant,
require,
var: variable,
Expand Down
36 changes: 0 additions & 36 deletions packages/wigsill/src/wgslPlaceholder.ts

This file was deleted.

60 changes: 60 additions & 0 deletions packages/wigsill/src/wgslSlot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {
IResolutionCtx,
WGSLBindableTrait,
WGSLItem,
WGSLSegment,
isWGSLSegment,
} from './types';

export interface Slot<T> {
__brand: 'Slot';
/** type-token, not available at runtime */
__bindingType: T;
}

export interface ResolvableSlot<T extends WGSLSegment> extends WGSLItem {
__brand: 'Slot';
/** type-token, not available at runtime */
__bindingType: T;
}

export class WGSLSlot<T> implements WGSLItem, WGSLBindableTrait<T> {
__bindingType!: T;
__brand = 'Slot' as const;
public debugLabel?: string | undefined;

constructor(public defaultValue?: T) {}

public alias(debugLabel: string) {
this.debugLabel = debugLabel;
return this;
}

private getValue(ctx: IResolutionCtx) {
if (this.defaultValue) {
return ctx.tryBinding(this, this.defaultValue);
}

return ctx.requireBinding(this);
}

resolve(ctx: IResolutionCtx): string {
const value = this.getValue(ctx);
if (!isWGSLSegment(value)) {
throw new Error(
`Cannot resolve value of type ${typeof value} for slot: ${this.debugLabel ?? '<unnamed>'}, type WGSLSegment required`,
);
}
return ctx.resolve(value);
}
}

export function slot<T extends WGSLSegment>(
defaultValue?: T,
): ResolvableSlot<T>;

export function slot<T>(defaultValue?: T): Slot<T>;

export function slot<T>(defaultValue?: T): Slot<T> {
return new WGSLSlot(defaultValue);
}

0 comments on commit 913fe1d

Please sign in to comment.