From 079e3002a7f0a52e5c7ebdba3cb158f66b891bc4 Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Fri, 1 Nov 2013 16:21:54 -0700 Subject: [PATCH] ex/tryhy: per request repl instance --- eg/tryhy/js/repl.js | 17 +++++++++++++---- eg/tryhy/main.hy | 15 +++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/eg/tryhy/js/repl.js b/eg/tryhy/js/repl.js index 91a22bb..cc07a0b 100644 --- a/eg/tryhy/js/repl.js +++ b/eg/tryhy/js/repl.js @@ -1,15 +1,24 @@ $(document).ready(function(){ + var backlog = []; $('#hy-console').console({ promptLabel: 'hy=> ', commandValidate:function(line){ - if (line == "") return false; + if (line == '') return false; else return true; }, commandHandle:function(line, report){ - $.getJSON("/eval", {code: line}, function(data) { - report([{msg : data.stdout, className:"jquery-console-message-value"}, - {msg : data.stderr, className:"jquery-console-message-error"}]); + $.ajax({ + type: 'POST', + url: '/eval', + data: JSON.stringify({code: line, env: backlog}), + contentType: 'application/json', + dataType: 'json', + success: function(data) { + report([{msg : data.stdout, className:'jquery-console-message-value'}, + {msg : data.stderr, className:'jquery-console-message-error'}]); + } }); + backlog.push(line); }, animateScroll:true, promptHistory:true, diff --git a/eg/tryhy/main.hy b/eg/tryhy/main.hy index a1ec69b..2e1404e 100644 --- a/eg/tryhy/main.hy +++ b/eg/tryhy/main.hy @@ -1,4 +1,3 @@ -(require hy.contrib.meth) (import [hy.cmdline [HyREPL]] [sys] [StringIO [StringIO]] @@ -18,9 +17,13 @@ (setv sys.stderr old-stderr) {"stdout" (fake-stdout.getvalue) "stderr" (fake-stderr.getvalue)})]]) - -(def repl (MyHyREPL)) - (def app (Flask __name__)) -(route hello "/" [name] (.format "(hello \"{0}!\")" name)) -(route eval-get "/eval" [] (json.dumps (repl.eval (get request.args "code")))) + +(with-decorator (kwapply (app.route "/eval") {"methods" ["POST"]}) + (fn [] + (let [[repl (MyHyREPL)] [input (request.get_json)]] + (foreach [expr (get input "env")] + (repl.eval expr)) + (json.dumps (repl.eval (get input "code"))) + ))) +