-
Notifications
You must be signed in to change notification settings - Fork 10
/
circumference.js
83 lines (63 loc) · 2.97 KB
/
circumference.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var
sys = require("sys"),
jefe = new require("../lib/jefe"), // change me as needed
elJefe = new jefe.Jefe({ recycleAfterN: 20 }),
scriptName = "circumference";
// Create the script once. This will compile it and cache it. This script will
// calculate the circumference of a circle (how exciting!) of radius R. Note
// that while untrusted scripts do not have access to the Node.js API, module
// system, etc. they do have access to standard JavaScript as implemented by V8.
// This includes built-in objects like Object, String, Array, Math, etc.
// If we provided an invalid script (e.g. syntax error), this `compile` call
// would throw.
try {
elJefe.compile(scriptName, "C = 2 * Math.PI * R");
} catch (e) {
sys.puts("problem compiling script: " + e);
process.exit(1);
}
// If we provided an invalid script (e.g. syntax error), this `compile` call
// would throw, like this:
try {
elJefe.compile(scriptName, "C = 2 * ; Math.PI * R");
} catch (e) { // ^-- oops!
sys.puts("problem compiling script: " + e);
sys.puts("It's OK! We were expecting that.");
}
// BTW, compiling a script with the same name will replace any existing script with
// that name in Jefe.
// With the script compiled, let us run it N times, each with a distinct
// sandbox. Since an untrusted script only has r/w access to the the sandbox
// we give it, we have a simple way of "passing" arguments and "returning"
// results. To pass a parameter, set a value in the sandbox. *Be careful what
// you set!* The script looks for a "global" to find the argument(s). To
// "return" a result from the script back to the caller, the script does
// nothing special; it just sets one or more "globals" (really just set in the
// sandbox object).
var
nDone = 0,
nCalls = 100;
for (var i = 0; i < nCalls; ++i) {
elJefe.run(scriptName, { R:i+1 }, function (error, sandboxIn, sandboxOut) {
// If there's a problem with Jefe (bug; someone else killed the child
// process from the outside; etc.) then `error` will be a message
// indicating what went wrong. If `error == jefe.ERR_TOO_MUCH_TIME` then
// the script took too long to finish. If `error == jefe.ERR_TOO_MUCH_MEMORY`
// then the script used too much memory. Otherwise, if `error` is non-null
// then the script threw an exception. If `error === null` then the
// `sandboxOut` contains the contents of the sandbox at script end.
if (error)
throw new Error("Problem running script: " + error);
sys.puts("The circumference of a circle with radius " + sandboxIn.R + " equals " + sandboxOut.C);
if (++nDone == nCalls) finalize();
});
}
function finalize() {
// You can fetch statistics about the runs for each script.
var stats = elJefe.getScriptStats(scriptName);
sys.p(stats);
var meanElapsed = stats.totalRunTime / (stats.runs || 1);
sys.puts("mean elapsed time per run of '" + scriptName + "' was " + meanElapsed + " ms");
sys.puts("all done!");
process.exit(0);
}