jintr
Version:
A tiny JavaScript interpreter written in TypeScript.
116 lines (84 loc) • 2.45 kB
Markdown
[]: https://github.com/LuanRT/Jinter/actions
<h1 align=center>Jinter</h1>
<p align=center>A tiny JavaScript interpreter written in TypeScript
<div align="center">
[][actions]
</div>
> **Note**: This project was originally developed for use in [YouTube.js](https://github.com/LuanRT/YouTube.js).
- [Installation](
- [Usage](
- [API](
- [`evaluate(input: string)`](
- [`visitor`](
- [`scope`](
- [License](
```sh
npm install jintr
```
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).
* Jinter()
* [`evaluate(input: string)`](
* [`visitor`](
* [`scope`](
Evaluates the given JavaScript code.
The node visitor. This is responsible for walking the AST and executing the nodes.
Represents the global scope of the program.
Distributed under the [MIT](https://choosealicense.com/licenses/mit/) License.
<p align="right">
(<a href="#top">back to top</a>)
</p>