Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80713 views
1
var indexOf = require('indexof');
2
3
var Object_keys = function (obj) {
4
if (Object.keys) return Object.keys(obj)
5
else {
6
var res = [];
7
for (var key in obj) res.push(key)
8
return res;
9
}
10
};
11
12
var forEach = function (xs, fn) {
13
if (xs.forEach) return xs.forEach(fn)
14
else for (var i = 0; i < xs.length; i++) {
15
fn(xs[i], i, xs);
16
}
17
};
18
19
var defineProp = (function() {
20
try {
21
Object.defineProperty({}, '_', {});
22
return function(obj, name, value) {
23
Object.defineProperty(obj, name, {
24
writable: true,
25
enumerable: false,
26
configurable: true,
27
value: value
28
})
29
};
30
} catch(e) {
31
return function(obj, name, value) {
32
obj[name] = value;
33
};
34
}
35
}());
36
37
var globals = ['Array', 'Boolean', 'Date', 'Error', 'EvalError', 'Function',
38
'Infinity', 'JSON', 'Math', 'NaN', 'Number', 'Object', 'RangeError',
39
'ReferenceError', 'RegExp', 'String', 'SyntaxError', 'TypeError', 'URIError',
40
'decodeURI', 'decodeURIComponent', 'encodeURI', 'encodeURIComponent', 'escape',
41
'eval', 'isFinite', 'isNaN', 'parseFloat', 'parseInt', 'undefined', 'unescape'];
42
43
function Context() {}
44
Context.prototype = {};
45
46
var Script = exports.Script = function NodeScript (code) {
47
if (!(this instanceof Script)) return new Script(code);
48
this.code = code;
49
};
50
51
Script.prototype.runInContext = function (context) {
52
if (!(context instanceof Context)) {
53
throw new TypeError("needs a 'context' argument.");
54
}
55
56
var iframe = document.createElement('iframe');
57
if (!iframe.style) iframe.style = {};
58
iframe.style.display = 'none';
59
60
document.body.appendChild(iframe);
61
62
var win = iframe.contentWindow;
63
var wEval = win.eval, wExecScript = win.execScript;
64
65
if (!wEval && wExecScript) {
66
// win.eval() magically appears when this is called in IE:
67
wExecScript.call(win, 'null');
68
wEval = win.eval;
69
}
70
71
forEach(Object_keys(context), function (key) {
72
win[key] = context[key];
73
});
74
forEach(globals, function (key) {
75
if (context[key]) {
76
win[key] = context[key];
77
}
78
});
79
80
var winKeys = Object_keys(win);
81
82
var res = wEval.call(win, this.code);
83
84
forEach(Object_keys(win), function (key) {
85
// Avoid copying circular objects like `top` and `window` by only
86
// updating existing context properties or new properties in the `win`
87
// that was only introduced after the eval.
88
if (key in context || indexOf(winKeys, key) === -1) {
89
context[key] = win[key];
90
}
91
});
92
93
forEach(globals, function (key) {
94
if (!(key in context)) {
95
defineProp(context, key, win[key]);
96
}
97
});
98
99
document.body.removeChild(iframe);
100
101
return res;
102
};
103
104
Script.prototype.runInThisContext = function () {
105
return eval(this.code); // maybe...
106
};
107
108
Script.prototype.runInNewContext = function (context) {
109
var ctx = Script.createContext(context);
110
var res = this.runInContext(ctx);
111
112
forEach(Object_keys(ctx), function (key) {
113
context[key] = ctx[key];
114
});
115
116
return res;
117
};
118
119
forEach(Object_keys(Script.prototype), function (name) {
120
exports[name] = Script[name] = function (code) {
121
var s = Script(code);
122
return s[name].apply(s, [].slice.call(arguments, 1));
123
};
124
});
125
126
exports.createScript = function (code) {
127
return exports.Script(code);
128
};
129
130
exports.createContext = Script.createContext = function (context) {
131
var copy = new Context();
132
if(typeof context === 'object') {
133
forEach(Object_keys(context), function (key) {
134
copy[key] = context[key];
135
});
136
}
137
return copy;
138
};
139
140