I've got a situation where I'd like to be able to pass certain variables into and out of a Coffeescript evaluation environment. The situation looks like this:
coffee = require('coffee-script');
exports.run = function(req,res){
var program = req.query.program;
var inputs = req.query.inputs;
var outputs = coffee.eval(program);
res.json(outputs);
};
Basically, inputs will be an object listing input parameters for the user's program, and outputs will pass those back to the web interface for prominent display.
How can I expose variables and functions to the sandboxed Coffeescript environment?
I have a workaround that I'm using for the time being. I'm making the user's Coffeescript program return a function that then gets evaluated.
Inputs:
{ x:5, y:7 }
Server side:
outputs = coffee.eval(program + '\nmain')(inputs,outputs);
User code:
main = (i,o) ->
o.z = i.x*5 + i.y
A little messy, but it works for the time being.