-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
42 lines (35 loc) · 979 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const ivm = require('isolated-vm');
const index = {};
/**
* @param {function}fn
* @return {string}
*/
index.stringifyAndRun = (fn) => `(${fn.toString()})();`;
/**
* @param {number} memoryLimit
* @param {boolean} inspector
* @return {{
* isolate: module:isolated-vm.Isolate,
* context: Context,
* jail: Reference<Object>
* }}
*/
index.createIsolate = ({ memoryLimit, inspector } = {}) => {
const isolate = new ivm.Isolate({ memoryLimit, inspector });
const context = isolate.createContextSync({ inspector });
const jail = context.globalReference();
jail.setSync('global', jail.derefInto());
return {
isolate,
context,
jail
};
};
index.makeInspectorFileName = () => {
const stack = (new Error()).stack;
const lines = stack.split('\n');
const callerLine = lines[3];
const [/*match*/, /*caller*/, file, line, /*column*/] = /at (.*?) \((.*?)\:(\d+):(\d+)\)/.exec(callerLine);
return `${file}:${line}`;
};
module.exports = index;