---
title: "module(): TypeShade 셰이더 API의 작성 분류 함수 참조 문서"
description: "module, TypeShade API의 작성 분류에 속하는 함수입니다. 선언으로 모듈을 조립합니다. 셰이더를 작성할 때 쓰는 빌더 API입니다. 모듈, 함수, 변수와 제어 흐름을 다룹니다."
url: https://typeshade.dev/ko/api/module/
language: ko
---

# `module()`

함수, 작성 분류

선언으로 모듈을 조립합니다.

`import { module } from 'typeshade/core/ir'`

설명은 커밋 af54ddad5의 검토된 컴파일러 문서를 번역한 것입니다. 시그니처와 예제 코드는 원문을 따르며, 필요한 경우 설명에 편집상 수정이 반영됩니다.

## 구문

```ts
function module(parts: ModuleParts): ModuleDecl
```

## 매개변수

**`parts` `ModuleParts`**

선언 배열, 선택 항목인 `funcs` 레코드, 그리고 `uses`입니다.

## 반환값

`ModuleDecl`

조립된 모듈입니다.

## 예외

**`Error`**

`funcs` 레코드의 키가 이미 다른 이름으로 조립된 선언의 이름을 바꾸면 `Error`를 던집니다. 이렇게 되면 앞서 조립한 모듈을 다시 생성할 때 그 출력이 손상됩니다.

## 설명

`consts`, `structs`, `bindings`, `funcs`는 모듈을 이루는 네 배열이며, 각 배열의 기본값은 빈 배열입니다. `overrides`, `externs`, `enables`, `uses`는 선택 항목입니다. 결과는 평범한 [`ModuleDecl`](https://typeshade.dev/ko/api/moduledecl/)이며, 이 값은 [`emitModule`](https://typeshade.dev/ko/api/emitmodule/), [`emitGlslModule`](https://typeshade.dev/ko/api/emitglslmodule/), [`reflect`](https://typeshade.dev/ko/api/reflect/), [`compileModule`](https://typeshade.dev/ko/api/compilemodule/)이 모두 받는 값입니다.

`funcs`의 순서는 생성 순서이며, 두 가지가 이 순서에 달려 있습니다. GLSL ES 3.00은 선언 후 사용을 요구합니다. 백엔드는 자신의 함수 구역을 위상 정렬하고, 호출 그래프상 필요한 자리에 전방 프로토타입을 생성합니다. 호출되는 함수가 먼저 오는 목록이라면 전방 프로토타입 없이도 계속 동작합니다. 고정된 순서는 다시 빌드할 때도 생성 결과를 항상 동일한 바이트로 유지합니다. 핸들 호출을 통해서만 도달하는 함수는 전이적으로 수집되어 호출되는 쪽이 앞에 붙으므로, 진입점만 나열해도 대개 충분합니다.

`funcs`는 레코드도 받습니다. 각 키는 자신이 담고 있는 핸들의 이름을 정하며, 익명 `fn(params, body)` 핸들을 포함합니다. 키의 순서가 생성 순서이고, JavaScript가 문자열 키의 삽입 순서를 보존하기 때문입니다. 따라서 레코드는 모든 함수에 결정적인 이름을 붙이며, 자동 생성된 `_fn0` 이름이 출력에 나타나지 않습니다. 이 점은 스냅샷 테스트가 출력을 비교할 때나 다른 선언이 함수를 이름으로 참조할 때 중요합니다. 함수 목록이 여러 소스에 걸쳐 있거나 데이터로 후처리될 때는 배열 형태를 유지합니다.

`uses`는 선언자 핸들을 받습니다. 여기에는 [`uniformStruct`](https://typeshade.dev/ko/api/uniformstruct/), [`storageBuffer`](https://typeshade.dev/ko/api/storagebuffer/), [`resource`](https://typeshade.dev/ko/api/resource/), [`ioStruct`](https://typeshade.dev/ko/api/iostruct/), [`structDecl`](https://typeshade.dev/ko/api/structdecl/), [`constDecl`](https://typeshade.dev/ko/api/constdecl/), [`fp64Guard`](https://typeshade.dev/ko/api/fp64guard/) 등이 포함되며, 각 핸들이 이미 알고 있는 내용으로 구조체, 바인딩, 상수 항목을 유도합니다. 명시한 배열도 그대로 동작하며, 유도된 항목과 합쳐집니다.

## 예제

### 예제

```ts
import { module } from 'typeshade/core/ir'

// 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 } })
```

## 관련 가이드

- [작성 가이드](https://typeshade.dev/ko/guide/internals/)
- [생성과 리플렉션](https://typeshade.dev/ko/guide/internals/emitting-and-reflection/)
- [CPU 오라클 안내서](https://typeshade.dev/ko/guide/internals/the-cpu-oracle/)

## 함께 보기

- [`ModuleDecl`](https://typeshade.dev/ko/api/moduledecl/)
- [`emitModule`](https://typeshade.dev/ko/api/emitmodule/)
- [`emitGlslModule`](https://typeshade.dev/ko/api/emitglslmodule/)
- [`reflect`](https://typeshade.dev/ko/api/reflect/)
- [`compileModule`](https://typeshade.dev/ko/api/compilemodule/)
- [`uniformStruct`](https://typeshade.dev/ko/api/uniformstruct/)
- [`storageBuffer`](https://typeshade.dev/ko/api/storagebuffer/)
- [`resource`](https://typeshade.dev/ko/api/resource/)
- [`ioStruct`](https://typeshade.dev/ko/api/iostruct/)
- [`structDecl`](https://typeshade.dev/ko/api/structdecl/)
- [`constDecl`](https://typeshade.dev/ko/api/constdecl/)
- [`fp64Guard`](https://typeshade.dev/ko/api/fp64guard/)
- [`fn`](https://typeshade.dev/ko/api/fn/)
- [`ModuleParts`](https://typeshade.dev/ko/api/moduleparts/)
- [`module`](https://typeshade.dev/ko/api/module/)
- [`constExpr`](https://typeshade.dev/ko/api/constexpr/)

## 소스

[`src/core/ir/builder.ts`, 1020행](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/ir/builder.ts#L1020), 커밋 af54ddad5 기준
