> ## Documentation Index
> Fetch the complete documentation index at: https://docs.projectreal.gg/llms.txt
> Use this file to discover all available pages before exploring further.

# hookfunction

> 用 replacement function hook 一个 target function，并返回 original target closure 的 cloned copy。两个 arguments 都必须是 functions。function 首先 flush pending C function swaps，通过 function cloning cloning target，将 replacement closure 或它的 mapped newcclosure target 添加到 hidden stack tracking，对 target 和 cloned original 调用 FixCallstack，然后通过 C closure, Luau closure 或 newcclosure-specific path patch target closure。如果 target 尚未在 hook records 中，则 cloned original 会存储在那里供 restorefunction 使用。returned function 是 cloned original closure。

<div className="real-function-page">
  ```lua theme={null}
  hookfunction(target: function, replacement: function): function
  ```

  ## 别名

  * `hookfunc`
  * `replaceclosure`
  * `detourfunction`
  * `detour_function`

  ## 参数

  <ParamField path={"target"} type={"function"} required>
    要 patch 的 Function。传入 non-function 会产生 type error。
  </ParamField>

  <ParamField path={"replacement"} type={"function"} required>
    其 function 会 install 或 map 到 target 的 Function。传入 non-function 会产生 type error。
  </ParamField>

  ## 返回值

  <ResponseField name={"original"} type={"function"}>
    在 hook install 前创建的 original target closure 的 cloned copy。
  </ResponseField>

  ## 示例

  Patch a function closure and keep a cloned original.

  ```lua theme={null}
  local function target(value)
      return value + 1
  end

  local original
  original = hookfunction(target, function(value)
      return original(value) * 2
  end)
  ```
</div>
