module
On this page

module()

Function in Authoring

Assemble a module from its declarations.

import { module } from 'typeshade'

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

Syntax

function module(parts: ModuleParts): ModuleDecl

Parameters

parts ModuleParts

the declaration arrays, the optional funcs record, and uses.

Return value

ModuleDecl

the assembled module.

Exceptions

Error

Error when a funcs record key renames a declaration that was already assembled under a different name, which would corrupt the earlier module’s re-emit.

Description

consts, structs, bindings and funcs are the four arrays a module is made of, and each defaults to empty; overrides, externs, enables and uses are the optional rest. The result is a plain ModuleDecl, the value emitModule, emitGlslModule, reflect and compileModule all take.

The order of funcs is the emit order, and two things depend on it. GLSL ES 3.00 requires declare-before-use: the backend topologically sorts its own function section and emits a forward prototype where the call graph forces one, and a callee-first list keeps working without them. A fixed order also keeps the emitted bytes deterministic across rebuilds. A function reached only through a handle call is collected transitively and prepended callee-first, so listing the entry points is usually enough.

funcs also accepts a record. Each key renames the handle it holds, an anonymous fn(params, body) handle included, and key order is the emit order, since JavaScript preserves string-key insertion order. The record therefore names every function deterministically and no auto-generated _fn0 name reaches the output, which matters when a snapshot test compares the output or another declaration refers to a function by name. Keep the array form when the list is spread across sources or post-processed as data.

uses takes declarator handles, uniformStruct, storageBuffer, resource, ioStruct, structDecl, constDecl and fp64Guard among them, and derives the struct, binding and const entries from what each handle already knows. Explicit arrays still work and merge with the derived ones.

Examples

Example

import { module } from 'typeshade'
// Array form: the order is the emit order, callees first.
const m = module({ structs: [VsOut.decl], bindings: [U.binding], funcs: [wrap, vs, fs] })
// Record form: each key is the emitted name, and key order is the emit order.
const n = module({ uses: [U, VsOut], funcs: { wrap, vs_main: vs, fs_main: fs } })

In the guide

See also

Source

src/core/ir/builder.ts, line 1264, at commit 26de7be8

Edit this page Report a problem