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.
- Host: GitHub
- URL: https://github.com/xgheaven/jsscript
- Owner: XGHeaven
- Created: 2023-11-06T16:42:57.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-17T14:57:39.000Z (over 1 year ago)
- Last Synced: 2025-02-22T00:31:45.518Z (about 1 year ago)
- Topics: javascript-vm, quickjs, v8
- Language: JavaScript
- Homepage: https://jsscript.github-docs.xgheaven.com/
- Size: 434 KB
- Stars: 40
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# jsscript


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)