
# CLI

Phasis 在 `bin/` 中提供三个主要可执行文件：

- **`bin/phasis`** — 运行 JavaScript 文件，求值命令行传入的源代码，转储 AST，并渲染 JSX 组件。
- **`bin/test262`** — 针对 Phasis 运行官方 ECMAScript 一致性测试套件。
- **`bin/wpt`** — 针对 Phasis 运行 Web Platform Tests 测试用例（URL、编码、fetch、标头、Blob、AbortSignal、流等）。

`composer require phasis/phasis` 后，运行时 CLI 会链接到 `vendor/bin/phasis`。开发运行器需要源代码检出版本，因此不包含在 Composer 归档中。

## bin/phasis

```
phasis [--node] <file.js|file.mjs|file.jsx|file.ts|file.tsx> [args] | phasis [--node] -e '<source>' | phasis --ast <file> | phasis --render <component.jsx> [--props '<json>']
```

`--node` 标志会启用与 Node 兼容的软件包和主机 I/O。不提供 REPL，也不支持从 stdin 输入源代码。

### 运行文件

```bash
./vendor/bin/phasis script.js
./vendor/bin/phasis entry.ts
```

将 `script.js` 作为经典脚本执行。执行结束后，脚本通过 `console.log`（以及 `console.error`、`console.warn`、…）写入的所有内容都会一次性输出到 stdout。如果脚本抛出异常，错误消息会输出到 stderr，退出状态码为 1。

`.mjs` 文件和 JSX 文件（`.jsx`）通过模块加载器作为 ES 模块运行，因此 `import` / `export`、顶层 `await` 和 `import.meta` 都可正常使用，模块说明符也会像打包器一样解析（参见 [模块](https://phasis.inline0.dev/zh/docs/modules/)）。[TypeScript 文件](https://phasis.inline0.dev/zh/docs/typescript/)也会自动正常运行：`.ts`、`.tsx`、`.mts`和 `.cts`。其他文件（包括 `.js`）会按经典脚本解析，因此，包含 `import` / `export` 或顶层 `await` 的 `.js` 文件会导致 SyntaxError。

### 执行源码

```bash
./vendor/bin/phasis -e 'console.log(Math.PI * 2)'
# 6.283185307179586
```

参数会作为完整脚本执行。只会打印 `console` 输出；最后一个表达式的值不会打印，因此 `phasis -e '1 + 2'` 不会输出任何内容。

### 运行与 Node 兼容的服务器

```bash
./vendor/bin/phasis --node server.cjs --port 3000
./vendor/bin/phasis --node -e 'console.log(require("node:path").basename(process.cwd()))'
```

Node 模式会从 `node_modules` 解析软件包，提供 `process.argv` 和一份主机环境副本，并在仍有活动引用句柄时运行真实的事件循环。`console.log`/`info`/`debug` 会立即写入 stdout；`console.warn`/`error` 会立即写入 stderr。`process.stdout` 和 `process.stderr` 接受原始字节。任务完成或失败时，CLI 会关闭其拥有的资源。`process.exitCode` 和 `process.exit(code)` 用于设置进程退出状态。有关模块覆盖范围和主机限制，请参见[Node 兼容性](https://phasis.inline0.dev/zh/docs/modules/#node-compatibility)。

### 导出 AST

```bash
./vendor/bin/phasis --ast script.js
./vendor/bin/phasis -e 'a + b' --ast
```

以 JSON 格式打印解析树（缩进两个空格）。适用于调试解析器问题，或基于 Phasis 构建工具。对于 `.jsx` 和 `.tsx` 文件，解析树会保留 JSX 节点（`JsxMode::Preserve`）；TypeScript 类型会被擦除。
如需以编程方式访问，请使用 [解析器 API](https://phasis.inline0.dev/zh/docs/parser/)。

### 渲染组件

```bash
./vendor/bin/phasis --render components/Card.jsx --props '{"title":"Hello"}'
```

使用 `Engine::render()` 渲染模块的默认导出，并打印 HTML。`--props` 是作为组件 props 传入的 JSON 对象。参见 [JSX](https://phasis.inline0.dev/zh/docs/jsx/)。

## bin/test262

`bin/test262` 会对 Phasis 运行 [官方 test262 测试套件](https://github.com/tc39/test262)。为项目贡献更改时，它可用作回归测试框架。`COMPAT.md` 中的完整套件快照由相关的 `bin/compat-report` 生成，手动启动的 CI 矩阵会分片运行该套件。

### 运行单个类别

```bash
./bin/test262 --category built-ins/Array
```

### 运行完整套件

```bash
./bin/test262 --jobs 4
```

### 配置

运行器会从项目根目录读取 `config/support.php`。其中的 `test262_skipped_features` 列表为空：不会跳过任何 test262 功能，包括 `tail-call-optimization`。该文件还列出了类别以及必须隔离运行的测试。

套件本身是 `test262` git 子模块；克隆后请运行 `git submodule update --init test262`。如果缺少该子模块，运行器会退出并显示“test262 suite not found”。

### 其他实用标志

| 标志 | 用途 |
|---|---|
| `--failures` | 每个失败的测试打印一行 |
| `--limit N` | 跑完前 N 个测试后停止 |
| `--jobs N` | 并行运行 N 个测试 |
| `--files-from file` | 只运行 `file` 中列出的测试 |
| `--json` | 在 stdout 输出机器可读的摘要 |
| `--no-progress` | 不显示进度行 |

最新快照请参阅[兼容性部分](https://phasis.inline0.dev/zh/docs/compatibility/)。

## bin/wpt

`bin/wpt` 会运行导入到 `tests/Wpt/fixtures/` 中的 Web Platform Tests 测试样例。测试集涵盖 URL、编码、atob/btoa、structuredClone、performance、Headers、Blob/File、AbortController/Signal、EventTarget、FormData、WHATWG Streams、完整的 `crypto`/`SubtleCrypto` 接口、`fetch`/`Request`/`Response`、`XMLHttpRequest`/`XMLHttpRequestUpload` 以及 `WebSocket`。各领域的通过率表请参阅[Web API](https://phasis.inline0.dev/zh/docs/compatibility/web-apis/)。

### 运行所有导入的测试样例

```bash
./bin/wpt
```

### 运行单个类别

```bash
./bin/wpt --category headers
./bin/wpt --category streams
./bin/wpt --category blob
```

### 按子测试显示输出

```bash
./bin/wpt --category abort --verbose
```

打印每个测试样例中的所有子测试（PASS / FAIL），并在失败时显示断言消息。

### JSON 输出

```bash
./bin/wpt --json
```

供 CI 流水线或脚本分析使用的机器可读通过/失败摘要。

### 备注

- 会触发 Fetch 的测试样例会在 `127.0.0.1:8765` 上自动启动捆绑的 WPT 测试服务器 `tests/Wpt/fetch-server.php`，并在完成后关闭。
- 位于 `tests/Wpt/testharness-shim.js` 的测试框架垫片仅实现导入测试样例所用的 WPT API 接口（约 400 行，而完整的 `testharness.js` 约有 3K 行）。
- 源测试样例位于 `tests/Wpt/upstream/`（[web-platform-tests/wpt](https://github.com/web-platform-tests/wpt) 的 git 忽略稀疏检出目录）。要导入更多样例，请将 `.any.js` 文件从 `upstream/<area>/` 复制到 `fixtures/<category>/`，运行器便会自动识别。

## 退出代码

在默认模式下，`bin/phasis` 会以以下两种代码之一退出：

| 状态码 | 含义 |
|---|---|
| `0` | 成功 — 脚本已运行完毕（包括在 `--ast` 之后） |
| `1` | 其他任何情况：未捕获的 JS 异常、语法错误、文件缺失或参数无效 |

脚本中的错误会将错误消息打印到 stderr。参数无效或文件缺失时，会将用法说明行打印到 stderr。

Node 模式还会返回程序明确指定的 `process.exitCode` 或
`process.exit(code)` 状态。

## 资源限制

`bin/phasis` 使用引擎默认设置，且没有用于设置限制的标志：4,096 帧的 JS 调用栈上限、用于转译闭包的 8,192 帧保护，以及默认 `maxLoopIterations` 100,000。这些设置适用于解释器、字节码 VM 和转译闭包（参见 [API：setLimit](https://phasis.inline0.dev/zh/docs/api/#setlimit)）。没有时间限制；如果脚本可能无法终止，请将命令包装在 `timeout` 中。
