• Stars
    star
    22
  • Rank 1,048,934 (Top 21 %)
  • Language
    TypeScript
  • Created about 2 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A tiny JavaScript interpreter written in TypeScript

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(code);
jinter.interpret();

Inject your own functions and variables into the interpreter:

// ...

jinter.visitor.on('println', (node, visitor) => {
  if (node.type === 'CallExpression' && node.callee.type === 'MemberExpression') {
    const args = node.arguments.map((arg) => visitor.visitNode(arg));
    return console.log(...args);
  }
});

// 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('');    
  }  
});

// Or you can just intercept access to specific nodes;
jinter.visitor.on('myFn', (node, visitor) => {
  console.info('MyFn node just got accessed:', node);
  return 'proceed'; // tells the interpreter to continue execution 
});

jinter.interpret();

For more examples see /test and /examples.

API

interpret()

Interprets the code passed to the constructor.

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)