Skip to content

XGHeaven/jsscript

Repository files navigation

jsscript

All Strict mode

A JavaScript Runtime implement in JavaScript.

Inspired by 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

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

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

jsscript run your-file.js

API

# install
npm install @xgheaven/jsscript
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 = `<your-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

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.

About

A flexible JavaScript engine running on another JavaScript engine.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published