---
title: "compileModuleJs(): function in CPU oracle, TypeShade API"
description: "compileModuleJs, a function in CPU oracle. Compile a module for the CPU by generating JavaScript…"
url: https://typeshade.dev/api/compilemodulejs/
language: en
---

# `compileModuleJs()`

Function in CPU oracle

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'`

The signature, the description and the examples come from the compiler's own source at commit af54ddad5.

## Syntax

```ts
function compileModuleJs(
  m: ModuleDecl,
  opts?: {
    gpuStubs?: boolean;
    precision?: CpuPrecision;
    consoleSink?: ConsoleSink;
    overrides?: Readonly<Record<string, CpuValue>>;
  },
): CpuModule
```

## Parameters

**`m` `ModuleDecl`**

the module to evaluate.

**`opts` optional `{ gpuStubs?: boolean; precision?: CpuPrecision; consoleSink?: ConsoleSink; overrides?: Readonly<Record<string, CpuValue>>; }`**

the same `precision` and `gpuStubs` [`compileModule`](https://typeshade.dev/api/compilemodule/) takes.

## Return value

`CpuModule`

the compiled module: `fns` by name, and `setBinding`.

## Exceptions

**`ValidationError`**

`Error` when the host forbids `new Function`, which is the case to catch and fall back to [`compileModule`](https://typeshade.dev/api/compilemodule/) for. It also throws [`ValidationError`](https://typeshade.dev/api/validationerror/) when the module fails a core rule, and, once a function runs, the same errors [`compileModule`](https://typeshade.dev/api/compilemodule/) documents for a GPU-only intrinsic called with `gpuStubs` off or a raw statement reached.

## Description

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`](https://typeshade.dev/api/compilemodule/) instead. Reach for the interpreter directly when debugging, too, since it puts no generated source between you and the IR.

## Examples

### Example

```ts
import { compileModuleJs, compileModule } from 'typeshade'

const cpu = (() => {
  try {
    return compileModuleJs(MODULE)
  } catch {
    return compileModule(MODULE) // no new Function on this host
  }
})()
```

## Targets

| Target | Support | Notes |
| --- | --- | --- |
| WGSL (WebGPU) | Does not apply | |
| GLSL ES 3.00 (WebGL2) | Does not apply | |
| CPU oracle | Supported | Compiles the module to a JavaScript function that runs in `f64`. |

## In the guide

- [The CPU oracle](https://typeshade.dev/guide/internals/the-cpu-oracle/)

## See also

- [`CpuModule`](https://typeshade.dev/api/cpumodule/)
- [`compileModule`](https://typeshade.dev/api/compilemodule/)
- [`ValidationError`](https://typeshade.dev/api/validationerror/)

## Source

[`src/core/cpu-codegen.ts`, line 1986](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/cpu-codegen.ts#L1986), at commit af54ddad5
