-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace WGSLPlaceholder with WGSLSlot
- Loading branch information
Showing
5 changed files
with
86 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |