-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshovel.coffee
More file actions
48 lines (45 loc) · 1.22 KB
/
shovel.coffee
File metadata and controls
48 lines (45 loc) · 1.22 KB
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
util = require("util")
vm = require("vm")
({
isEvaluating: false
logs: []
init: ->
sandbox = @createSandbox()
process.on "message", (data)=>
@isEvaluating = true
try
@logs.push(util.inspect(vm.runInNewContext(data, sandbox)))
catch err
@logs.push(err.stack)
process.send(@logs.join("\n"))
@logs = []
@isEvaluating = false
createSandbox: ->
tids = []
env =
_: require("underscore")
ls: require("prelude-ls")
async: require("async")
CoffeeScript: require("coffee-script")
setTimeout: (callback, delay=1000, args...)->
if delay < 1000
delay = 1000
tids.push(setTimeout.apply(null, [callback, delay].concat(args)))
tids[tids.length-1]
clearTimeout: clearTimeout
getTimeouts: -> tids.slice(0)
console:
log: =>
str = Array.prototype.map.call(arguments, (v)->
if typeof v is "string"
then v
else util.inspect(v)
).join(", ")
if @isEvaluating
@logs.push(str)
else
process.send(str)
undefined
env.global = env
env
}).init()