> For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt.

# WebAssembly

3.8
从 Lynx 3.8 开始，Lynx 基于 PrimJS 集成为 Android 提供了基础的 WebAssembly 支持。

这项能力面向 Lynx 应用中常见的 WebAssembly 加载和执行场景。目前这项能力仅支持后台线程，主线程暂不支持。当前实现重点在于提供 JavaScript 与 WebAssembly 之间可用的互操作能力，而不是完整覆盖整个 WebAssembly JavaScript API。

## 当前支持

目前 Lynx 支持以下核心流程：

- 使用 `new WebAssembly.Module(...)` 创建模块
- 使用 `new WebAssembly.Instance(...)` 实例化模块
- 在 JavaScript 中调用 WebAssembly 导出的函数
- 在常见场景下使用 `WebAssembly.Memory`、`WebAssembly.Table` 和 `WebAssembly.Global`
- 在实例化时传入 function、memory、table 和 global 类型的 imports

## 当前限制

当前支持范围是有边界的：

- 目前该能力可在 Lynx 3.8 的 Android 平台中使用
- WebAssembly 当前仅支持在后台线程中使用
- 主线程暂不支持 WebAssembly
- 目前尚未提供完整的 WebAssembly JavaScript API
- `WebAssembly` 上的一些静态 API，例如 `compile`、`instantiate`、`validate` 以及 streaming 相关变体，当前暂未支持
- 一些反射类 API 和更高级的行为仍然是部分支持

在实践中，推荐优先使用基于 `WebAssembly.Module` 和 `WebAssembly.Instance` 的构造式用法。

## 示例

```js
const module = new WebAssembly.Module(wasmBytes);
const instance = new WebAssembly.Instance(module, imports);

const result = instance.exports.add(1, 2);
```

可以通过下面这个完整的 Lynx 页面示例查看同样的调用流程：

**下面是一个示例: webassembly**

**Entry:** `src`
**Bundle:** `dist/main.lynx.bundle`

```tsx
import { useCallback, useState } from "@lynx-js/react";

import { addWithWasm } from "./addWasm.js";

import "./App.css";

export function App() {
  const [result, setResult] = useState<number | null>(null);
  const [error, setError] = useState<string | null>(null);

  const calculate = useCallback(() => {
    "background-only";
    setError(null);

    try {
      setResult(addWithWasm(1, 2));
    } catch (err) {
      setResult(null);
      setError(err instanceof Error ? err.message : "Failed to run WebAssembly");
    }
  }, []);

  return (
    <page>
      <view className="App">
        <view className="Panel">
          <text className="Eyebrow">WebAssembly on Lynx</text>
          <text className="Title">1 + 2</text>
          <text className="Formula">Calculated by a Wasm add function</text>

          <view className="Button" bindtap={calculate}>
            <text className="ButtonText">Run add(1, 2)</text>
          </view>

          <view className="ResultBox">
            <text className="ResultLabel">Result</text>
            <text className="ResultValue">
              {error ?? (result == null ? "Tap to calculate" : String(result))}
            </text>
          </view>
        </view>
      </view>
    </page>
  );
}

```

v0.3.0



更多底层运行时支持的说明，可参考 [PrimJS 的 WebAssembly 文档](https://github.com/lynx-family/primjs/blob/develop/docs/wasm.md)。

## 兼容性

**Compatibility Table**

**Query:** `lynx-api.global.WebAssembly`

[Source](https://github.com/lynx-family/lynx-website/tree/main/packages/lynx-compat-data/lynx-api/global/WebAssembly.json) · [MDN](https://developer.mozilla.org/zh-CN/docs/WebAssembly/Reference/JavaScript_interface)

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| WebAssembly | Partial (3.8) | Unknown | Unknown | Unknown | Unknown | Yes |

**`WebAssembly`**

<code>WebAssembly</code>

**Status:** Experimental

**Specifications:**
- [https://webassembly.github.io/spec/js-api/#webassembly-namespace](https://webassembly.github.io/spec/js-api/#webassembly-namespace)

- **Android — From 3.8:** Partial support; Available on the background thread only, not on the main thread, via the PrimJS runtime. Supports common module loading and execution flows such as `new WebAssembly.Module()` and `new WebAssembly.Instance()`. The complete WebAssembly JavaScript API is not exposed; static methods such as `WebAssembly.compile()`, `WebAssembly.instantiate()`, `WebAssembly.validate()`, and the streaming variants are not supported yet.

