composeModule()
함수, 배리언트 분류
Compose a module by filling its placeholder statements.
import { composeModule } from 'typeshade'
시그니처, 설명, 예제는 커밋 c66579bf의 컴파일러 소스에서 그대로 가져온 영어 원문입니다.
구문
function composeModule( m: ModuleDecl, swaps: Record<string, readonly Stmt[]>, opts?: ComposeOptions,): ModuleDecl매개변수
mModuleDeclthe base module carrying the placeholders.
swapsRecord<string, readonly Stmt[]>tag to statement list, one entry per placeholder to fill.
opts선택 사항ComposeOptionsallowUnswappedto let a placeholder stay unfilled.
반환값
a new module with the function bodies rewritten; m is not modified.
예외
ErrorErrorwhen a swap key matches no placeholder, or when a placeholder is left un-swapped andallowUnswappedis not set.
설명
A base module marks each place where
its builds differ with b.placeholder('tag'), and composeModule replaces each one with the
statement list swaps holds under that tag, returning a new module. Function bodies are
rewritten; consts, structs and bindings are copied unchanged.
It descends into if, for and switch bodies, so a placeholder may sit in a nested scope.
It is strict by default. A placeholder that receives no swap is otherwise easy to miss: the
WGSL emitter writes it as a bare // __placeholder: <tag> comment and the shader still
compiles, while the GLSL emitter and compileModule (the same module evaluated on the
CPU in double precision) throw only when they reach it. A swap key that matches no
placeholder, such as a typo, would do nothing at all. Here each case throws at compose time
and names the tags involved.
allowUnswapped is for the deliberate case: a placeholder that is meant to stay empty in
this build. It turns off the first check only; a swap key that matches no placeholder is
still an error.
예제
예제
import { composeModule, fn, module, f32, f32T, type Stmt } from 'typeshade'
const base = module({ funcs: [fn('shade', {}, f32T, (_p, b) => b.placeholder('result'))],})const returnConstant = (v: number): Stmt => ({ s: 'return', expr: f32(v).expr })const composed = composeModule(base, { result: [returnConstant(1)] })관련 가이드
함께 보기
소스
src/core/passes/compose.ts, 123행, 커밋 c66579bf 기준