Skip to content

A sandboxed JavaScript interpreter in JavaScript written in TypeScript.

License

Notifications You must be signed in to change notification settings

arcatdmz/JS-Interpreter

 
 

Repository files navigation

JS-Interpreter

A sandboxed JavaScript interpreter written in TypeScript, packed with Webpack or tsc for use with Node.js or within a browser (either loaded statically or dynamically). Execute arbitrary JavaScript code line by line in isolation and safety.

This interpreter is a TypeScript port of the original JavaScript version.

There are several library files that serve different use cases. See below for more concrete instructions.

  • dist/interpreter.js ... packed with tsc (npm run build) and can be loaded from Node.js or a browser with RequireJS. acorn needs to be loaded separately.
  • dist/interpreter.d.ts ... TypeScript type definition
  • dist/acorn_interpreter.js ... packed with Webpack (npm run build-with-acorn) as a library and can be loaded with a <script> tag from a browser. acorn is bundled.
  • dist/interpreter.global.js ... packed with Webpack (npm run build-global) as a library and can be loaded with a <script> tag from a browser. acorn needs to be loaded separately.

Live demo: https://junkato.jp/JS-Interpreter

More demos: https://junkato.jp/JS-Interpreter/demos

Documentation: This README file ... or the original documentation at https://neil.fraser.name/software/JS-Interpreter/docs.html

Node.js Basic Usage

npm install to install the latest code.

npm install acorn https://github.com/arcatdmz/JS-Interpreter --save
npm install @types/acorn --save-dev

Write typed code with perfect auto completion!

import acorn = require('acorn');
import Interpreter = require('JS-Interpreter');
Interpreter.acorn = acorn;

// Test the interpreter.
var interpreter = new Interpreter("var a = 1, b = 2; a + b;");
interpreter.run();
console.log('1 + 2 = ' + interpreter.value);
// shows '1 + 2 = 3' in the console

Browser Basic Usage

Load dist/acorn_interpreter.js statically and use the globally-declared Interpreter class.

<script src="https://cdn.rawgit.com/arcatdmz/JS-Interpreter/39cbd828/dist/acorn_interpreter.js"></script>
<script>
var interpreter = new Interpreter("var a = 1, b = 2; a + b;");
interpreter.run();
alert('1 + 2 = ' + interpreter.value);
// shows an alert '1 + 2 = 3'
</script>

Dynamic loading with RequireJS

Load dist/acorn.js and dist/interpreter.js dynamically with RequireJS and use the Interpreter class. No global scope pollution!

<script src="https://cdn.rawgit.com/arcatdmz/JS-Interpreter/39cbd828/lib/require.js"></script>
<script>
requirejs(['https://cdn.rawgit.com/arcatdmz/JS-Interpreter/39cbd828/lib/acorn.js', 'https://cdn.rawgit.com/arcatdmz/JS-Interpreter/39cbd828/dist/interpreter.js'], function (acorn, Interpreter) {
  Interpreter.acorn = acorn; // Manually pass acorn runtime to the Interpreter
  var interpreter = new Interpreter("var a = 1, b = 2; a + b;");
  interpreter.run();
  alert('1 + 2 = ' + interpreter.value);
  // shows an alert '1 + 2 = 3'
});
</script>

(c) 2013-2017 Google Inc. and Jun Kato

About

A sandboxed JavaScript interpreter in JavaScript written in TypeScript.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 59.6%
  • JavaScript 33.9%
  • HTML 6.5%