
Security News
npm ‘is’ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
@engine262/engine262
Advanced tools
Implementation of ECMA-262 in JavaScript
An implementation of ECMA-262 in JavaScript
Goals
Non-Goals
This project is bound by a Code of Conduct.
Join us in #engine262:matrix.org
.
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:
--- a/src/evaluator.mts
+++ b/src/evaluator.mts
@@ -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.mts
+++ b/src/parser/ExpressionParser.mts
@@ -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<ParseNode.DoExpression>();
+ 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.
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.
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:
import { Agent, setSurroundingAgent, ManagedRealm, Value, CreateDataProperty, inspect, CreateBuiltinFunction, skipDebugger } from '@engine262/engine262';
const agent = new Agent({
// onDebugger() {},
// ensureCanCompileStrings() {},
// hasSourceTextAvailable() {},
// loadImportedModule() {},
// onNodeEvaluation() {},
// features: [],
});
setSurroundingAgent(agent);
const realm = new ManagedRealm({
// promiseRejectionTracker() {},
// getImportMetaProperties() {},
// finalizeImportMeta() {},
// randomSeed() {},
});
realm.scope(() => {
// Add print function from host
const print = CreateBuiltinFunction((args) => {
console.log(...args.map((tmp) => inspect(tmp)));
return Value.undefined;
}, 1, Value('print'), []);
skipDebugger(CreateDataProperty(realm.GlobalObject, 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.
This project can be run against test262, which is particularly useful for developing new features and/or tests:
$ # build engine262
$ npm run 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.
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.
FAQs
Implementation of ECMA-262 in JavaScript
The npm package @engine262/engine262 receives a total of 255 weekly downloads. As such, @engine262/engine262 popularity was classified as not popular.
We found that @engine262/engine262 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.