# @casual-simulation/engine262 中文文档

规范地址：https://npmcn.com/package/%40casual-simulation/engine262/

版本：0\.0\.1-4de2170374e22761996e46eb1362f4496ee57f8f
npm 元数据更新时间：2022-12-29T22:21:53.408Z
中文译文更新时间：2026-11-09T04:58:28.092Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40casual-simulation/engine262
原文对照：https://npmcn.com/package/%40casual-simulation/engine262/?lang=original
项目主页：https://github.com/engine262/engine262#readme
源码仓库：https://github.com/engine262/engine262

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：12.5 MB，30 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2022-09-29T07:39:01.701Z
- 最新版本发布：2022-12-29T22:21:53.226Z
- 近 30 天下载量：11430（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：43（近 12 个月 0 个）

## 中文简介

在JavaScript中实施ECMA-262

## 安装

```sh
npm install @casual-simulation/engine262
```

也可以使用：`pnpm add @casual-simulation/engine262`、`yarn add @casual-simulation/engine262`、`bun add @casual-simulation/engine262`

## README

# 发动机262

在JavaScript中实施ECMA-262

目标

-   100%的光谱遵守率
-   回顾
-   修改的便利

非目标

-   以牺牲任何目标为代价的速度

这个项目受 [行为守则](https://github.com/engine262/engine262/blob/master/CODE_OF_CONDUCT.md)。 。 。 .

加入我们 `#engine262:matrix.org`。 。 。 .

## 从主线更改

-   增加了更多的发电机,以便通过仅仅拨打 `.next()` 在返回的传动器上.
    -   应该可以步入大多数用户代码,包括标准库函数的回调.
    -   大多得不到支持的情景正在从标准库函数内步入代理获取器/发送器. 例如,目前无法在使用代理服务器时步入代理获取器 `thisValue` 用于标准库函数.
    -   使这些特性能够步入的过程相当简单:你寻找一些情况 `unwind()` 函数被使用并替换为 `yield*`。 。 。 . 棘手的部分是使用 `yield*` 强制您使包含的函数成为生成函数,这意味着该函数的调用者现在也需要使用 `yield*` 或者说 `unwind()`。 。 。 .

## 为什么会这样

在帮助开发JavaScript的新功能的同时,我发现了其中之一 最有用的方法 找出什么是可行的什么是不能 实际使用新特性运行代码. [宝贝](https://babeljs.io/) 这真是太棒了 但是 有时候,一些特征只是不能 很好地代表它。 同样, 实施其中一种发动机的特性是一项大型工作,涉及 长编译时间和有优化编译器的烦扰错误.

引擎262是一个工具,允许 JavaScript 开发者在新位置有一个沙盒 特性可以迅速被原型和探索. 例如,添加: [执行表达式](https://github.com/tc39/proposal-do-expressions) 至此引擎与以下 diff 一样简单:

```diff
--- a/src/evaluator.mjs
+++ b/src/evaluator.mjs
@@ -232,6 +232,8 @@ export function* Evaluate(node) {
     case 'GeneratorBody':
     case 'AsyncGeneratorBody':
       return yield* Evaluate_AnyFunctionBody(node);
+    case 'DoExpression':
+      return yield* Evaluate_Block(node.Block);
     default:
       throw new OutOfRange('Evaluate', node);
   }
--- a/src/parser/ExpressionParser.mjs
+++ b/src/parser/ExpressionParser.mjs
@@ -579,6 +579,12 @@ export class ExpressionParser extends FunctionParser {
         return this.parseRegularExpressionLiteral();
       case Token.LPAREN:
         return this.parseParenthesizedExpression();
+      case Token.DO: {
+        const node = this.startNode();
+        this.next();
+        node.Block = this.parseBlock();
+        return this.finishNode(node, 'DoExpression');
+      }
       default:
         return this.unexpected();
     }
```

这种简单适用于许多其他建议,例如: [可选链条](https://github.com/tc39/proposal-optional-chaining), (中文(简体) ), [模式匹配](https://github.com/tc39/proposal-pattern-matching), (中文(简体) ), [管道操作员](https://github.com/tc39/proposal-pipeline-operator)还有更多 这个引擎也有 用于在ECMA-262和 [测试262](https://github.com/tc39/test262),用于 符合 JavaScript 执行.

## 所需资源

要运行引擎262本身, 支持最近 ECMAScript 特性的引擎 这是需要的。 此外,《国家行动倡议》`bin/engine262.js`)和试验262跑道 (单位:千美元)`test/test262/test262.js`)需要最新版本的Node.js.

## 使用引擎262

在线使用 : [https://engine262.js.org (英语)](https://engine262.js.org/)

您可以从 [GitHub 软件包](https://github.com/engine262/engine262/packages)。 。 。 .

如果您在全球安装, 您可以像这样使用 CLI :

`$ engine262`

或者,你可以在本地安装并使用API:

```js
'use strict';

const {
  Agent,
  setSurroundingAgent,
  ManagedRealm,
  Value,

  CreateDataProperty,

  inspect,
} = require('engine262');

const agent = new Agent({
  // onDebugger() {},
  // ensureCanCompileStrings() {},
  // hasSourceTextAvailable() {},
  // onNodeEvaluation() {},
  // features: [],
});
setSurroundingAgent(agent);

const realm = new ManagedRealm({
  // promiseRejectionTracker() {},
  // resolveImportedModule() {},
  // getImportMetaProperties() {},
  // finalizeImportMeta() {},
  // randomSeed() {},
});

realm.scope(() => {
  // Add print function from host
  const print = new Value((args) => {
    console.log(...args.map((tmp) => inspect(tmp)));
    return Value.undefined;
  });
  CreateDataProperty(realm.GlobalObject, new Value('print'), print);
});

realm.evaluateScript(`
'use strict';

async function* numbers() {
  let i = 0;
  while (true) {
    const n = await Promise.resolve(i++);
    yield n;
  }
}

(async () => {
  for await (const item of numbers()) {
    print(item);
  }
})();
`);

// a stream of numbers fills your console. it fills you with determination.
```

## 测试引擎262

此项目可针对 [测试262](https://github.com/tc39/test262),这特别有用 用于开发新的特性和/或测试:

```sh
$ # build engine262
$ npm build

$ # update local test262 in test/test262/test262
$ git submodule update --init --recursive

$ # update local test262 to a pull request
$ pushd test/test262/test262
$ git fetch origin refs/pull/$PR_NUMBER/head && git checkout FETCH_HEAD
$ popd

$ # run specific tests
$ npm run test:test262 built-ins/AsyncGenerator*

$ # run all tests
$ npm run test:test262
```

产出将显示全部测试、通过测试、失败测试和跳过测试的计数.

## 相关项目

许多人和组织试图撰写 JavaScript 解释器 在JavaScript中很像引擎262,目标不同. 其中一些是 虽然引擎262并非基于其中任何一种,但在此列出以供参考.

-   [https://github.com/facebook/prepack 页面存档备份,存于互联网档案馆](https://github.com/facebook/prepack)
-   [https://github.com/mozilla/narcissus 页面存档备份,存于互联网档案馆](https://github.com/mozilla/narcissus)
-   [https://github.com/NeilFraser/JS-解释器](https://github.com/NeilFraser/JS-Interpreter)
-   [https://github.com/metaes/metaes (英语)](https://github.com/metaes/metaes)
-   [https://github.com/锡巴克/斯瓦尔](https://github.com/Siubaak/sval)

## 常见问题

### @casual-simulation/engine262 是什么？

在JavaScript中实施ECMA-262

### 如何安装 @casual-simulation/engine262？

在项目目录运行 npm install @casual-simulation/engine262，或使用 pnpm add / yarn add / bun add。它还提供命令行工具 engine262，可以用 npx engine262 直接运行。

### @casual-simulation/engine262 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @casual-simulation/engine262 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @casual-simulation/engine262 可以商用吗？

@casual-simulation/engine262 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @casual-simulation/engine262 的下载量有多少？

根据 npm 官方下载统计，@casual-simulation/engine262 在 2026/10/09 – 2026/11/07 这 30 天内被下载 11,430 次，日均约 381 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @casual-simulation/engine262 还在维护吗？

最新版本 0\.0\.1-4de2170374e22761996e46eb1362f4496ee57f8f 发布于 2022/12/30（3 年前）；至今共发布 43 个版本，其中过去 12 个月发布了 0 个；首次发布于 2022/09/29。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

