From 1675c3f057cdd73b5cad9df5338e2efe54595741 Mon Sep 17 00:00:00 2001 From: mhawryluk Date: Tue, 9 Jul 2024 16:38:51 +0200 Subject: [PATCH] Replace WGSLPlaceholder with WGSLSlot --- packages/wigsill/src/index.ts | 2 +- packages/wigsill/src/types.ts | 10 +++- packages/wigsill/src/wgsl.ts | 4 +- packages/wigsill/src/wgslPlaceholder.ts | 48 ---------------- packages/wigsill/src/wgslSlot.ts | 74 +++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 52 deletions(-) delete mode 100644 packages/wigsill/src/wgslPlaceholder.ts create mode 100644 packages/wigsill/src/wgslSlot.ts diff --git a/packages/wigsill/src/index.ts b/packages/wigsill/src/index.ts index b3ae3aca..2abd9d5e 100644 --- a/packages/wigsill/src/index.ts +++ b/packages/wigsill/src/index.ts @@ -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'; diff --git a/packages/wigsill/src/types.ts b/packages/wigsill/src/types.ts index d6020b76..b39e82eb 100644 --- a/packages/wigsill/src/types.ts +++ b/packages/wigsill/src/types.ts @@ -27,9 +27,17 @@ export function isWGSLItem(value: unknown): value is WGSLItem { ); } -export interface WGSLBindableTrait extends WGSLItem { +export function isWGSLSegment(value: unknown): value is WGSLSegment { + return ( + typeof value === 'number' || typeof value === 'string' || isWGSLItem(value) + ); +} + +export interface WGSLBindableTrait { /** type-token, not available at runtime */ readonly __bindingType: TBinding; + + readonly debugLabel?: string | undefined; } export type WGSLBindPair = [WGSLBindableTrait, T]; diff --git a/packages/wigsill/src/wgsl.ts b/packages/wigsill/src/wgsl.ts index f344233e..1bce0014 100644 --- a/packages/wigsill/src/wgsl.ts +++ b/packages/wigsill/src/wgsl.ts @@ -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, { @@ -14,7 +14,7 @@ export default Object.assign(code, { fun, identifier, memory, - placeholder, + slot, constant, require, var: variable, diff --git a/packages/wigsill/src/wgslPlaceholder.ts b/packages/wigsill/src/wgslPlaceholder.ts deleted file mode 100644 index 09c6530e..00000000 --- a/packages/wigsill/src/wgslPlaceholder.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { - IResolutionCtx, - WGSLBindableTrait, - WGSLCompoundTrait, - WGSLItem, - WGSLSegment, - hasCompoundTrait, -} from './types'; - -export class WGSLPlaceholder - implements WGSLItem, WGSLBindableTrait, WGSLCompoundTrait -{ - __bindingType!: WGSLSegment; - public debugLabel?: string | undefined; - - constructor(public defaultSegment?: WGSLSegment) {} - - public alias(debugLabel: string) { - this.debugLabel = debugLabel; - return this; - } - - private getSegment(ctx: IResolutionCtx) { - if (this.defaultSegment) { - return ctx.tryBinding(this, this.defaultSegment); - } - - return ctx.requireBinding(this); - } - - getChildren(ctx: IResolutionCtx): WGSLItem[] { - const segment = this.getSegment(ctx); - - if (hasCompoundTrait(segment)) { - return segment.getChildren(ctx); - } - - return []; - } - - resolve(ctx: IResolutionCtx): string { - return ctx.resolve(this.getSegment(ctx)); - } -} - -export function placeholder(defaultSegment?: WGSLSegment): WGSLPlaceholder { - return new WGSLPlaceholder(defaultSegment); -} diff --git a/packages/wigsill/src/wgslSlot.ts b/packages/wigsill/src/wgslSlot.ts new file mode 100644 index 00000000..35df6bae --- /dev/null +++ b/packages/wigsill/src/wgslSlot.ts @@ -0,0 +1,74 @@ +import { + IResolutionCtx, + WGSLBindableTrait, + WGSLCompoundTrait, + WGSLItem, + WGSLSegment, + hasCompoundTrait, + isWGSLSegment, +} from './types'; + +export interface Slot { + __brand: 'Slot'; + /** type-token, not available at runtime */ + __bindingType: T; +} + +export interface ResolvableSlot extends WGSLItem { + __brand: 'Slot'; + /** type-token, not available at runtime */ + __bindingType: T; +} + +export class WGSLSlot + implements WGSLItem, WGSLBindableTrait, 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 ?? ''}, type WGSLSegment required`, + ); + } + return ctx.resolve(value); + } +} + +export function slot( + defaultValue?: T, +): ResolvableSlot; + +export function slot(defaultValue?: T): Slot; + +export function slot(defaultValue?: T): Slot { + return new WGSLSlot(defaultValue); +}