compileModuleJs()
함수, CPU 오라클 분류
Compile a module for the CPU by generating JavaScript, returning the same CpuModule shape compileModule returns and the same results bit for bit.
import { compileModuleJs } from 'typeshade'
시그니처, 설명, 예제는 커밋 c66579bf의 컴파일러 소스에서 그대로 가져온 영어 원문입니다.
구문
function compileModuleJs( m: ModuleDecl, opts?: { gpuStubs?: boolean; precision?: CpuPrecision; consoleSink?: ConsoleSink },): CpuModule매개변수
mModuleDeclthe module to evaluate.
opts선택 사항{ gpuStubs?: boolean; precision?: CpuPrecision; consoleSink?: ConsoleSink }the same
precisionandgpuStubscompileModuletakes.
반환값
the compiled module: fns by name, and setBinding.
예외
ValidationErrorErrorwhen the host forbidsnew Function, which is the case to catch and fall back tocompileModulefor. It also throwsValidationErrorwhen the module fails a core rule, and, once a function runs, the same errorscompileModuledocuments for a GPU-only intrinsic called withgpuStubsoff or a raw statement reached.
설명
Prefer it on any hot path.
Bit identity holds by construction: every operation calls the exact runtime helper the
interpreter calls, or, for a vector operand the IR types say it can and that nothing the
module does can leave missing (a helper’s discard, an element read past an array’s end),
the scalar operation that helper applies to each component, so the two cannot drift apart.
What differs is when the work happens. Instead of walking the IR node by node on every
invocation, this walks each function body once, emits a JavaScript source string and builds
it with new Function, so each call runs straight-line code with real local variables.
The interpreter is the reference and the fallback. A function body holding a shape this
generator cannot emit bit-identically (a raw statement, a placeholder statement, an
assignment target with no expression form) falls back to the interpreter for that function
alone, so a returned module can be part compiled and part interpreted with nothing to do at
the call site. Where new Function itself is unavailable, as on a host whose content
security policy forbids unsafe-eval, construction throws, and the caller catches it and
calls compileModule instead. Reach for the interpreter directly when debugging, too,
since it puts no generated source between you and the IR.
예제
예제
import { compileModuleJs, compileModule } from 'typeshade'
const cpu = (() => { try { return compileModuleJs(MODULE) } catch { return compileModule(MODULE) // no new Function on this host }})()대상별 지원
| 대상 | 지원 | 비고 |
|---|---|---|
| WGSL (WebGPU) | 해당 없음 | |
| GLSL ES 3.00 (WebGL2) | 해당 없음 | |
| CPU 오라클 | 지원 | Compiles the module to a JavaScript function that runs in f64. |
관련 가이드
함께 보기
소스
src/core/cpu-codegen.ts, 1959행, 커밋 c66579bf 기준