diff --git a/whalesong/repl-prototype/htdocs/repl.js b/whalesong/repl-prototype/htdocs/repl.js index 771273b..1e0c287 100644 --- a/whalesong/repl-prototype/htdocs/repl.js +++ b/whalesong/repl-prototype/htdocs/repl.js @@ -1,23 +1,26 @@ -var COMPILED = []; - $(document).ready(function() { "use strict"; var repl = $("#repl"); var output = $("#output"); - - plt.runtime.currentMachine.params.currentDisplayer = function(MACHINE, domNode) { + + var M = plt.runtime.currentMachine; + + + // We configure output to send it to the "output" DOM node. + M.params.currentDisplayer = function(MACHINE, domNode) { $(domNode).appendTo(output); } - plt.runtime.currentMachine.params.currentErrorDisplayer = function(MACHINE, domNode) { + M.params.currentErrorDisplayer = function(MACHINE, domNode) { $(domNode).appendTo(output); } + // We then want to initialize the language module. var initializeLanguage = function(afterLanguageInitialization) { // Load up the language. - plt.runtime.currentMachine.modules['whalesong/lang/whalesong.rkt'].invoke( - plt.runtime.currentMachine, + M.modules['whalesong/lang/whalesong.rkt'].invoke( + M, function() { console.log("Environment initialized."); afterLanguageInitialization(); @@ -27,8 +30,6 @@ $(document).ready(function() { alert("uh oh!"); }); }; - - repl.attr('disabled', 'true'); repl.val('Please wait, initializing...'); initializeLanguage( @@ -50,11 +51,46 @@ $(document).ready(function() { }); + + // CPS'ed for-each. + var forEachK = function(elts, f, after) { + var n = elts.length; + var loop = function(i) { + if (i >= n) { + return after(); + } else { + return f(elts[i], function() { loop(i+1); }); + } + } + loop(0); + }; + + + // In evaluation, we'll send compilation requests to the server, + // and get back bytecode that we should evaluate. var evaluate = function(src, after) { console.log("about to eval", src); var onCompile = function(compiledResult) { - COMPILED.push(compiledResult); - after(); + // compiledResult.compiledCodes is an array of function chunks. + var compiledCodes = compiledResult.compiledCodes; + forEachK(compiledCodes, + function(code, k) { + var codeFunction = eval(code); + var onGoodEvaluation = function() { + console.log('good evaluation'); + k(); + }; + var onBadEvaluation = function(M, err) { + console.log('bad evaluation'); + console.log(err); + if (err.stack) { + console.log(err.stack); + } + after(); + }; + codeFunction(M, onGoodEvaluation, onBadEvaluation); + }, + after); //eval(compiledResult.compiled); // FIXME // plt.runtime.currentMachine.modules['whalesong/repl-prototype/anonymous-module.rkt'].invoke( diff --git a/whalesong/repl-prototype/server.rkt b/whalesong/repl-prototype/server.rkt index f47173d..58c615d 100644 --- a/whalesong/repl-prototype/server.rkt +++ b/whalesong/repl-prototype/server.rkt @@ -73,7 +73,7 @@ (define assembled (assemble/write-invoke compiled-bytecode #f assembled-op)) (cons (get-output-string assembled-op) (loop))]))) (printf "assembled codes ~s\n" assembled-codes) - (write-json (hash 'compiled-codes assembled-codes) + (write-json (hash 'compiledCodes assembled-codes) op)] [else (define program-port (open-output-string)) @@ -81,7 +81,7 @@ (read (open-input-string (string-append "#lang whalesong\n" text-src))))) #:should-follow-children? (lambda (src) #f) #:output-port program-port) - (write-json (hash 'compiled-module (get-output-string program-port)) + (write-json (hash 'compiledModule (get-output-string program-port)) op) ]) ;; Send it back as json text....