Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

jintr

LuanRT142.9kMIT3.3.1TypeScript support: included

A tiny JavaScript interpreter written in TypeScript.

js, vm, typescript, javascript, interpreter

readme

Jinter

A tiny JavaScript interpreter written in TypeScript

Tests

Note: This project was originally developed for use in YouTube.js.

Table of Contents

Installation

npm install jintr

Usage

Execute some JavaScript code:

// 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:

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 and /examples.

API

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 License.

(back to top)

changelog

Changelog

3.3.1 (2025-04-23)

Bug Fixes

  • CallExpression: Properly parse computed properties in call expressions (4d6c8d1)

3.3.0 (2025-03-24)

Features

  • Allow custom options in parseScript method (16bf448)

3.2.1 (2025-02-21)

Bug Fixes

  • statements: Use prefixed strings to avoid internal conflicts (#29) (dfeed9b)

3.2.0 (2024-12-12)

Features

3.1.0 (2024-12-05)

Features

  • nodes: Add EmptyStatement (c5d3f9d)

3.0.2 (2024-10-28)

⚠ BREAKING CHANGES

  • Drop CJS support
  • convert to ESModule (#4)
  • overhaul interpreter (#3)

Features

Bug Fixes

  • for-stmts: do not return if body type is ExpressionStatement (7e43562)
  • package: add extra file configuration to release-please-config.json (f2d08b7)
  • short-circuiting not working correctly (8b610d1)
  • Stop using global augmentation and add LICENSE file (94b6bb0)

Miscellaneous Chores

Code Refactoring

3.0.1 (2024-10-28)

Bug Fixes

  • Stop using global augmentation and add LICENSE file (94b6bb0)

3.0.0 (2024-10-28)

⚠ BREAKING CHANGES

  • Drop CJS support

Code Refactoring

2.1.1 (2024-07-31)

Miscellaneous Chores

2.1.0 (2024-07-31)

Miscellaneous Chores

1.2.0 (2024-07-31)

Features

1.1.0 (2023-07-09)

Features

  • add ForOfStatement node (203d3ae)

1.0.0 (2023-04-08)

⚠ BREAKING CHANGES

  • convert to ESModule (#4)
  • overhaul interpreter (#3)

Features

Bug Fixes

  • for-stmts: do not return if body type is ExpressionStatement (7e43562)
  • short-circuiting not working correctly (8b610d1)

Code Refactoring