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

https://github.com/luanrt/jinter

A tiny JavaScript interpreter written in TypeScript
https://github.com/luanrt/jinter

interpreter javascript vm

Last synced: 9 months ago
JSON representation

A tiny JavaScript interpreter written in TypeScript

Awesome Lists containing this project

README

          

[actions]: https://github.com/LuanRT/Jinter/actions

Jinter

A tiny JavaScript interpreter written in TypeScript

[![Tests](https://github.com/LuanRT/Jinter/actions/workflows/test.yml/badge.svg?branch=main)][actions]

> **Note**: This project was originally developed for use in [YouTube.js](https://github.com/LuanRT/YouTube.js).

## Table of Contents

- [Installation](#installation)
- [Usage](#usage)
- [API](#api)
- [`evaluate(input: string)`](#evaluateinput-string)
- [`visitor`](#visitor)
- [`scope`](#scope)
- [License](#license)

## Installation
```sh
npm install jintr
```

## Usage

Execute some JavaScript code:
```ts
// const Jinter = require('jintr').default;
import { Jinter } from 'jintr';

const code = `
function sayHiTo(person) {
console.log('Hi ' + person + '!');
}

sayHiTo('mom');
`

const jinter = new Jinter();
jinter.evaluate(code);
```
---
Inject your own functions, objects, etc:
```ts
import { Jinter } from 'jintr';

const jinter = new Jinter();

const code = `
console.log(new SomeClass().a);
console.log('hello'.toArray());

function myFn() {
console.log('[myFn]: Who called me?');
}

myFn();
`;

class SomeClass {
constructor() {
this.a = 'this is a test';
}
}

jinter.defineObject('SomeClass', SomeClass);

// Ex: str.toArray();
jinter.visitor.on('toArray', (node, visitor) => {
if (node.type === 'CallExpression' && node.callee.type === 'MemberExpression') {
const obj = visitor.visitNode(node.callee.object);
return obj.split('');
}
});

// Intercept function calls
jinter.visitor.on('myFn', (node) => {
if (node.type == 'CallExpression')
console.info('myFn was called!');
return '__continue_exec';
});

jinter.evaluate(code);
```

For more examples see [`/test`](https://github.com/LuanRT/Jinter/tree/main/test) and [`/examples`](https://github.com/LuanRT/Jinter/tree/main/examples).

## API
* Jinter()
* [`evaluate(input: string)`](#evaluate)
* [`visitor`](#visitor)
* [`scope`](#scope)

### `evaluate(input: string)`
Evaluates the given JavaScript code.

### `visitor`
The node visitor. This is responsible for walking the AST and executing the nodes.

### `scope`
Represents the global scope of the program.

## License
Distributed under the [MIT](https://choosealicense.com/licenses/mit/) License.


(back to top)