Skip to content

Commit

Permalink
Replace WGSLPlaceholder with WGSLSlot
Browse files Browse the repository at this point in the history
  • Loading branch information
mhawryluk committed Jul 9, 2024
1 parent f01dcc4 commit 1675c3f
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 52 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
48 changes: 0 additions & 48 deletions packages/wigsill/src/wgslPlaceholder.ts

This file was deleted.

74 changes: 74 additions & 0 deletions packages/wigsill/src/wgslSlot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {
IResolutionCtx,
WGSLBindableTrait,
WGSLCompoundTrait,
WGSLItem,
WGSLSegment,
hasCompoundTrait,
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>, WGSLCompoundTrait
{
__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);
}

getChildren(ctx: IResolutionCtx): WGSLItem[] {
const segment = this.getValue(ctx);

if (hasCompoundTrait(segment)) {
return segment.getChildren(ctx);
}

return [];
}

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 1675c3f

Please sign in to comment.