# @casual-simulation/engine262

> Implementation of ECMA-262 in JavaScript

Latest version **0.0.1-4de2170374e22761996e46eb1362f4496ee57f8f** (published 2022-12-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @casual-simulation/engine262
pnpm add @casual-simulation/engine262
yarn add @casual-simulation/engine262
bun add @casual-simulation/engine262
```

Provides the command `engine262`.

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support; large bundle; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.0.1-4de2170374e22761996e46eb1362f4496ee57f8f |
| Published | 2022-12-29 |
| First published | 2022-09-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.9 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 950 |
| Author | engine262 Contributors |
| Maintainers | kallyngowdyyeti, casualsimulation |

## Links

- npm: https://www.npmjs.com/package/@casual-simulation/engine262
- Repository: https://github.com/engine262/engine262
- Homepage: https://github.com/engine262/engine262#readme
- Issues: https://github.com/engine262/engine262/issues
- npm.io page: https://npm.io/package/@casual-simulation/engine262

## Recent versions

- 0.0.1-4de2170374e22761996e46eb1362f4496ee57f8f (latest) — 2022-12-29
- 0.0.1-6fadd673dfc837c2717737a2729a1baf022566a2 — 2022-10-24
- 0.0.1-6d83cdbf886f5ed0ed77bb9f2b72d97589b4b8a7 — 2022-10-21
- 0.0.1-04ffba3d522a4c88f20f7b91f85815516837f24f — 2022-10-19
- 0.0.1-a09eb79588a3480149bb76a7409b4194724f8178 — 2022-10-14
- 0.0.1-5f234b220e83c800746bf26f1fc1618bc1a9a4c1 — 2022-10-14
- 0.0.1-a8a20e9b4160d057d90be020b3dceced95df2f37 — 2022-10-13
- 0.0.1-8a510225db4a55334db9ae0678cfcdf1df99f72c — 2022-10-13
- 0.0.1-f9008809d2e19c617b3cb36d4c2e900915f8c913 — 2022-10-12
- 0.0.1-2935487044ec948b6db4f4e6b026aa2a849ddd80 — 2022-10-12
- 0.0.1-8a49b623a5f37bcdfd4966a52621980e58b89b02 — 2022-10-12
- 0.0.1-258ce9785e7128fa42d7a1c0664aeb2bc1f15e91 — 2022-10-12
- 0.0.1-adfd2696a3afca1d7f0fb30e814b7feaf7480e21 — 2022-10-11
- 0.0.1-74ee0cd5eb186f04f40fda6ed5b65f9949b21246 — 2022-10-11
- 0.0.1-d521e75215a74518a6dc3c295dbeb4818ad6e9e7 — 2022-10-10
- … 28 more at https://npm.io/package/@casual-simulation/engine262/versions

## README

# engine262

An implementation of ECMA-262 in JavaScript

Goals
- 100% Spec Compliance
- Introspection
- Ease of modification

Non-Goals
- Speed at the expense of any of the goals

This project is bound by a [Code of Conduct][COC].

Join us in `#engine262:matrix.org`.


## Changes from Mainline

- Additional generators have been added so that it is possible to step through the evaluation by simply calling `.next()` on the returned iterator.
  - It should be possible to step into most user code, including callbacks from standard library functions.
  - Scenarios that are mostly not supported are stepping into Proxy getters/setters from within standard library functions.
    For example, it is currently not possible to step into a Proxy getter when using a proxy as the `thisValue` for standard library functions.
  - The process for enabling stepping for such features is pretty simple: you look for situations where the `unwind()` function is used and replace it with `yield*`.
    The tricky part is that using `yield*` forces you to make the containing function to be a generator function, which means that callers of that function now also need to use `yield*` or `unwind()`.

## Why this exists

While helping develop new features for JavaScript, I've found that one of the
most useful methods of finding what works and what doesn't is being able to
actually run code using the new feature. [Babel][] is fantastic for this, but
sometimes features just can't be nicely represented with it. Similarly,
implementing a feature in one of the engines is a large undertaking, involving
long compile times and annoying bugs with the optimizing compilers.

engine262 is a tool to allow JavaScript developers to have a sandbox where new
features can be quickly prototyped and explored. As an example, adding
[do expressions][] to this engine is as simple as the following 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();
     }
```

This simplicity applies to many other proposals, such as [optional chaining][],
[pattern matching][], [the pipeline operator][], and more. This engine has also
been used to find bugs in ECMA-262 and [test262][], the test suite for
conforming JavaScript implementations.

## Requirements

To run engine262 itself, a engine with support for recent ECMAScript features
is needed. Additionally, the CLI (`bin/engine262.js`) and test262 runner
(`test/test262/test262.js`) require a recent version of Node.js.

## Using engine262

Use it online: https://engine262.js.org

You can install the latest engine262 build from [GitHub Packages][].

If you install it globally, you can use the CLI like so:

`$ engine262`

Or, you can install it locally and use the 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.
```

## Testing engine262

This project can be run against [test262][], which is particularly useful
for developing new features and/or tests:
```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
```
The output will indicate counts for total tests, passing tests, failing tests, and skipped tests.

## Related Projects

Many people and organizations have attempted to write a JavaScript interpreter
in JavaScript much like engine262, with different goals. Some of them are
included here for reference, though engine262 is not based on any of them.

- https://github.com/facebook/prepack
- https://github.com/mozilla/narcissus
- https://github.com/NeilFraser/JS-Interpreter
- https://github.com/metaes/metaes
- https://github.com/Siubaak/sval

[Babel]: https://babeljs.io/
[COC]: https://github.com/engine262/engine262/blob/master/CODE_OF_CONDUCT.md
[do expressions]: https://github.com/tc39/proposal-do-expressions
[optional chaining]: https://github.com/tc39/proposal-optional-chaining
[pattern matching]: https://github.com/tc39/proposal-pattern-matching
[test262]: https://github.com/tc39/test262
[the pipeline operator]: https://github.com/tc39/proposal-pipeline-operator
[GitHub Packages]: https://github.com/engine262/engine262/packages

---
_Source: https://npm.io/package/@casual-simulation/engine262 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
