Skip to content

Support Node.js/RequireJS/TypeScript #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
3bc0116
a tiny bit of acceleration.
arcatdmz Dec 9, 2015
a1a9a8e
Revert "a tiny bit of acceleration."
arcatdmz Aug 12, 2017
76494d3
Merge branch 'master' into forked-master
arcatdmz Aug 12, 2017
96a60dc
Naive rewriting of interpreter.js into TypeScript.
arcatdmz Aug 12, 2017
68221cc
Remove dependency on requirejs; fix license name so that it matches t…
arcatdmz Aug 12, 2017
02840d2
Fix build errors; compile *.ts into *.js
arcatdmz Aug 12, 2017
4dc5ef2
Explicitly declare Interpreter fields; add @types/estree
arcatdmz Aug 12, 2017
2760e1d
remove dependency on the ES6 feature (Array.from)
arcatdmz Aug 12, 2017
de53566
Add types to MyState fields.
arcatdmz Aug 12, 2017
de41073
Add types to Interpreter methods.
arcatdmz Aug 12, 2017
9c3550b
Add some workaround for producing raw JS code (not AMD/ES6 modules)
arcatdmz Aug 12, 2017
c599b2d
Add Interpreter.Object and Interpreter.State for API compatibility
arcatdmz Aug 12, 2017
16ff726
Start using webpack for bundling and compressing JS files.
arcatdmz Aug 12, 2017
6c74e0e
Add RequireJS and an example of dynamically loading interpreter.js; c…
arcatdmz Aug 12, 2017
db7d294
Add build scripts; add simple demo code in TypeScript; change to dyna…
arcatdmz Aug 12, 2017
a5999e7
Some refactoring in the building pipeline
arcatdmz Aug 12, 2017
39cbd82
Add TypeScript type declaration; add browser/Node.js examples; remove…
arcatdmz Aug 12, 2017
a401153
Remove unnecessary map files.
arcatdmz Aug 12, 2017
27a8f26
Update Node.js example to use GitHub-hosted JS-Interpreter code.
arcatdmz Aug 12, 2017
83b3134
Update README.md and bump version to 0.1.1.
arcatdmz Aug 12, 2017
bb8028b
Aww... fix typo in my email address.
arcatdmz Aug 12, 2017
55f7159
Fix browser sample code (acorn path).
arcatdmz Aug 12, 2017
2487dac
Minor document updates.
arcatdmz Aug 17, 2017
76b28cd
Find methods in Interpreter prototype rather than the current instance.
arcatdmz Aug 19, 2017
321b906
Bump version to 0.1.2.
arcatdmz Aug 19, 2017
1151ed3
add MyDescriptor interface
arcatdmz Aug 19, 2017
5ade519
Recover stateStack before throwing realError for improved debugging e…
arcatdmz Aug 19, 2017
e7fce5a
Update compiled files.
arcatdmz Aug 19, 2017
8c87044
Merge branch 'fork-master'
arcatdmz Aug 19, 2017
5fcfcb6
Update compiled files (incorporate NeilFraser/JS-Interpreter@8882421b…
arcatdmz Aug 19, 2017
33ccea2
Merge branch 'fork-master'
arcatdmz Aug 22, 2017
68316ce
Update compiled files (incorporate NeilFraser/JS-Interpreter@a689749f…
arcatdmz Aug 22, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

*.komodoproject
node_modules/
78 changes: 74 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,81 @@
JS-Interpreter
==============

A sandboxed JavaScript interpreter in JavaScript. Execute arbitrary JavaScript
code line by line in isolation and safety.
A sandboxed JavaScript interpreter written in TypeScript, packed with `Webpack` or `tsc` for use with Node.js or within a browser (either loaded [statically](https://junkato.jp/JS-Interpreter/) or [dynamically](https://junkato.jp/JS-Interpreter/?required)). Execute arbitrary JavaScript code line by line in isolation and safety.

This interpreter is a TypeScript port of [the original JavaScript version](https://github.com/NeilFraser/JS-Interpreter).

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://neil.fraser.name/software/JS-Interpreter/
[https://junkato.jp/JS-Interpreter](https://junkato.jp/JS-Interpreter)

More demos:
[https://junkato.jp/JS-Interpreter/demos](https://junkato.jp/JS-Interpreter/demos)

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

## Node.js Basic Usage

`npm install` to install the latest code.

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

Write typed code with perfect auto completion!

```typescript
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.

```html
<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](//requirejs.org) and use the `Interpreter` class.
No global scope pollution!

```html
<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](https://junkato.jp)
134 changes: 0 additions & 134 deletions acorn_interpreter.js

This file was deleted.

4 changes: 2 additions & 2 deletions demos/async.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<meta charset="utf-8">
<title>JS-Interpreter Async Demo</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="../acorn.js"></script>
<script src="../interpreter.js"></script>
<script src="../lib/acorn.js"></script>
<script src="../dist/interpreter.global.js"></script>
<script>
var myInterpreter;
function initAlert(interpreter, scope) {
Expand Down
14 changes: 14 additions & 0 deletions demos/browser/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html><html lang="en">
<head>
<meta charset="utf-8" />
<title>JS-Interpreter TypeScript demo page</title>
<link rev="made" href="mailto:i@junkato.jp" />
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Droid+Sans" />
<link href="//junkato.jp/css/bootstrap.min.css" rel="stylesheet">
<link href="//junkato.jp/css/main.css" rel="stylesheet">
</head>
<body>
<script>var require = { paths: { "acorn": "../../lib/acorn" } };</script>
<script src="../../lib/require.js" data-main="index.js"></script>
</body>
</html>
9 changes: 9 additions & 0 deletions demos/browser/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define(["require", "exports", "acorn", "../../dist/interpreter"], function (require, exports, acorn, Interpreter) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Interpreter.acorn = acorn;
// Test 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'
});
9 changes: 9 additions & 0 deletions demos/browser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

import acorn = require('acorn');
import Interpreter = require('../../dist/interpreter');
Interpreter.acorn = acorn;

// Test 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'
4 changes: 2 additions & 2 deletions demos/json.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<meta charset="utf-8">
<title>JS-Interpreter JSON Demo</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="../acorn.js"></script>
<script src="../interpreter.js"></script>
<script src="../lib/acorn.js"></script>
<script src="../dist/interpreter.js"></script>
<script>
var myInterpreter;
function initAlert(interpreter, scope) {
Expand Down
9 changes: 9 additions & 0 deletions demos/nodejs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var acorn = require("acorn");
var 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
9 changes: 9 additions & 0 deletions demos/nodejs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

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
39 changes: 39 additions & 0 deletions demos/nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions demos/nodejs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "js-interpreter-demo-nodejs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "tsc index.ts --typeRoots ./node_modules/@types"
},
"author": "Jun Kato <i@junkato.jp> (https://junkato.jp)",
"license": "ISC",
"devDependencies": {
"@types/acorn": "^4.0.2",
"@types/node": "^8.0.20"
},
"dependencies": {
"acorn": "^0.4.2",
"JS-Interpreter": "git+https://github.com/arcatdmz/JS-Interpreter.git"
}
}
4 changes: 2 additions & 2 deletions demos/serialize.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<meta charset="utf-8">
<title>JS-Interpreter Serialization Demo</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="../acorn.js"></script>
<script src="../interpreter.js"></script>
<script src="../lib/acorn.js"></script>
<script src="../dist/interpreter.global.js"></script>
<script src="serialize.js"></script>
<script>
var myInterpreter;
Expand Down
4 changes: 2 additions & 2 deletions demos/thread.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<meta charset="utf-8">
<title>JS-Interpreter Thread Demo</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="../acorn.js"></script>
<script src="../interpreter.js"></script>
<script src="../lib/acorn.js"></script>
<script src="../dist/interpreter.js"></script>
<script>
function initAlert(interpreter, scope) {
var wrapper = function(text) {
Expand Down
20 changes: 20 additions & 0 deletions dist/acorn_interpreter.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/acorn_interpreter.js.map

Large diffs are not rendered by default.

Loading