Skip to content

Instantly share code, notes, and snippets.

@shesek
Created March 14, 2013 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shesek/5165195 to your computer and use it in GitHub Desktop.
Save shesek/5165195 to your computer and use it in GitHub Desktop.
serialize = (fn) -> JSON.stringify body: ''+fn, context: fn._context?() or {}
unserialize = (str) ->
{ body, context } = JSON.parse str
args = (Object.keys context).join(', ')
vals = (v for k, v of context).map(JSON.stringify).join(', ')
eval "(function(#{args}) { return #{body}; })(#{vals})"
ctx = (ctx_fn, fn) -> fn._context = ctx_fn; fn
sum = (a, b) -> ctx (->{a,b}), (c) -> a+b+c
alert (unserialize serialize (sum 2, 4)) 5 # 11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment