An open API service indexing awesome lists of open source software.

https://github.com/xgheaven/jsscript

A flexible ESNext modern JavaScript engine running on another JavaScript engine.
https://github.com/xgheaven/jsscript

javascript-vm quickjs v8

Last synced: 11 months ago
JSON representation

A flexible ESNext modern JavaScript engine running on another JavaScript engine.

Awesome Lists containing this project

README

          

# jsscript

![All](https://img.shields.io/endpoint?url=https%3A%2F%2Fgist.githubusercontent.com%2FXGHeaven%2Feb8c89fdddb8545a94c59c88b687fad5%2Fraw%2Fmain-all.json)
![Strict mode](https://img.shields.io/endpoint?url=https%3A%2F%2Fgist.githubusercontent.com%2FXGHeaven%2Feb8c89fdddb8545a94c59c88b687fad5%2Fraw%2Fmain-strict%2520mode.json)

A JavaScript Runtime implement in JavaScript.

Inspired by [QuickJS](https://github.com/bellard/quickjs).

## Goal

- Provide a easy way to learning JavaScript engine. No needs to learn C.
- Only support `strict mode` and modern JavaScript feature.
- Such as `let`, `const`, `async`, `await`, `arrow function`, `class`, etc.
- Not implement `with`, `eval`, `var`, `label` etc.
- Please do not using this project for your production without any tested.
- Flexable, you can only use the part of JavaScript.
- Some prototype function of Builtin Object depends on the host environmnet.
- For example, if you want to use `Array.prototype.at` in vm, you need make sure it can been used in host vm. Or you need to load ployfill.

## How to use

### CLI

```bash
# instal as global
npm install @xgheaven/jsscript -g
```

It's like `Node.js` cli without special module.

```bash
jsscript run your-file.js
```

### API

```bash
# install
npm install @xgheaven/jsscript
```

```typescript
import { Runtime, Features, parseScript, parseBytecode, toHostValue } from 'jsscript'

const runtime = new Runtime({
features: [
// inject builtin features
new Features.ECMA262Feature(), // inject All ECMA262 Spec object, like `Promise`/`RegExp`/...
// you can write your self feature
],
})
const context = runtime.newContext()

const script = ``;

// parse it to function object
const fn = parseScript(context, script)
// or compile it bytecode then parse it
const bc = compileToBytecode(script)
const fn = parseBytecode(context, bc)

// run it
const ret = context.run(fn)

// convert js value of vm to host vm
console.log(toHostValue(ret))
```

#### Error Handle

TODO

#### Define Feature

TODO

# Benchmark(TODO)

> The performance is not the core of jsscript. But It's still important for using.
> Using [v8-7 version](https://github.com/mozilla/arewefastyet/tree/master/benchmarks/v8-v7) benchmark test. (It's so old, only support es5. I will find more newer test cases in the future to replace it)

| | jsscript | [sablejs](https://github.com/sablejs/sablejs) | [eval5](https://github.com/bplok20010/eval5) |
| --- | --- | --- | --- |
| *Language* | TypeScript | JavaScript | TypeScript |
| *Support Version* | ES6(target) | ES5 | ES5 |
| *Version* | see `package.json` | `1.1.0` | `1.4.7` |
| `crypto` Score | - | - | - |
| `deltablue` Score | - | - | - |
| `earley-boyer` Score | - | - | - |
| `navier-stokes` Score | - | - | - |
| `raytrace` Score | - | - | - |
| `regexp` Score | - | - | - |
| `richards` Score | - | - | - |
| `splay` Score | - | - | - |
| **Total Score** | - | - | - |

## More

I want to use `jsscript` as package name.
But it too similar to the package `js-script` which published 10+ years ago.
I have to add the personal scope in the package name.

## Same package

- [engine262](https://github.com/engine262/engine262)