From 0363f63b0ca0e555f7f8b7abf6915d73c77cb0cf Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Fri, 22 Feb 2013 15:02:35 -0700 Subject: [PATCH] trying to get dynamic module loading in place. --- whalesong/js-assembler/get-runtime.rkt | 4 +- .../runtime-src/baselib-loadscript.js | 61 +++++++++++++++++++ whalesong/js-assembler/runtime-src/runtime.js | 57 ++++++++++++++++- whalesong/whalesong-cmd.rkt | 2 +- whalesong/whalesong-helpers.rkt | 2 +- 5 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 whalesong/js-assembler/runtime-src/baselib-loadscript.js diff --git a/whalesong/js-assembler/get-runtime.rkt b/whalesong/js-assembler/get-runtime.rkt index 6247951..3337f04 100644 --- a/whalesong/js-assembler/get-runtime.rkt +++ b/whalesong/js-assembler/get-runtime.rkt @@ -44,6 +44,8 @@ baselib.js baselib-frames.js + + baselib-loadscript.js baselib-unionfind.js baselib-equality.js @@ -104,4 +106,4 @@ files))) (define (get-runtime) - text) \ No newline at end of file + text) diff --git a/whalesong/js-assembler/runtime-src/baselib-loadscript.js b/whalesong/js-assembler/runtime-src/baselib-loadscript.js new file mode 100644 index 0000000..7d0b6e7 --- /dev/null +++ b/whalesong/js-assembler/runtime-src/baselib-loadscript.js @@ -0,0 +1,61 @@ +/*jslint unparam: true, sub: true, vars: true, white: true, plusplus: true, maxerr: 50, indent: 4 */ + +// Frame structures. +(function(baselib) { + 'use strict'; + var exports = {}; + baselib.loadscript = exports; + + ////////////////////////////////////////////////////////////////////// + /* Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Contact information: + * Dao Gottwald + * + * @version 1.6 + * @url http://design-noir.de/webdev/JS/loadScript/ + */ + + var loadScript = function(url, callback, onError) { + var f = arguments.callee; + if (!("queue" in f)) + f.queue = {}; + var queue = f.queue; + if (url in queue) { // script is already in the document + if (callback) { + if (queue[url]) // still loading + queue[url].push(callback); + else // loaded + callback(); + } + return; + } + queue[url] = callback ? [callback] : []; + var script = document.createElement("script"); + script.type = "text/javascript"; + script.onload = script.onreadystatechange = function() { + if (script.readyState && script.readyState != "loaded" && script.readyState != "complete") + return; + script.onreadystatechange = script.onload = null; + document.getElementsByTagName("head")[0].removeChild(script); + var work = queue[url]; + delete(queue[url]); + while (work.length) + work.shift()(); + }; + script.onerror = function() { + script.onreadystatechange = script.onload = null; + document.getElementsByTagName("head")[0].removeChild(script); + onError(); + }; + script.src = url; + document.getElementsByTagName("head")[0].appendChild(script); + }; + + ////////////////////////////////////////////////////////////////////// + exports.loadScript = loadScript; +}(this.plt.baselib)); diff --git a/whalesong/js-assembler/runtime-src/runtime.js b/whalesong/js-assembler/runtime-src/runtime.js index b978485..4c8f28f 100644 --- a/whalesong/js-assembler/runtime-src/runtime.js +++ b/whalesong/js-assembler/runtime-src/runtime.js @@ -6,12 +6,15 @@ // All of the values here are namespaced under "plt.runtime". /*global $*/ -(function(plt, baselib) { +(function(plt) { 'use strict'; var runtime = {}; plt.runtime = runtime; + // abbreviation, since we'll be using the basic library a lot. + var baselib = plt.baselib; + ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// @@ -1117,6 +1120,54 @@ }; + // defaultModuleLoader: Machine string (-> any) (-> any) -> void + // + // The default module loader currently doesn't do anything dynamic. + // + // Other module loader implementations may do more interesting + // things here, such as loading off the disk, or from the network. + var defaultModuleLoader = function(M, moduleName, success, fail) { + if (M.modules[moduleName] instanceof ModuleRecord) { + return success(); + } else { + return fail(); + } + }; + + + var makeLocalFileModuleLoader = function(basepath) { + var loadScript = baselib.loadscript.loadScript; + return function(M, moduleName, success, fail) { + if (M.modules[moduleName] instanceof ModuleRecord) { + return success(); + } else { + var onManifestLoaded = function() { + // The manifest should map module names to + // their files. + if (runtime.currentModuleManifest[moduleName]) { + var modulePath = + (basepath + "/" + + runtime.currentModuleManifest[moduleName]); + return loadScript(modulePath, success, fail); + } + // FILL ME IN: + // 1. Look up the path in the manifest. + // 2. Dynamically load the module in using loadScript + // 3. If that succeeds, continue, otherwise, fail. + return fail(); + + }; + return loadScript(basepath + "/manifest.js", + onManifestLoaded, + fail); + } + }; + }; + + + + + ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// @@ -1125,6 +1176,8 @@ // Exports var exports = runtime; exports['currentMachine'] = new Machine(); + exports['currentModuleLoader'] = defaultModuleLoader; + exports['currentModuleManifest'] = {}; exports['invokeMains'] = invokeMains; exports['lookupInMains'] = lookupInMains; @@ -1273,4 +1326,4 @@ exports['makeRandomNonce'] = makeRandomNonce; -}(this.plt, this.plt.baselib)); \ No newline at end of file +}(this.plt)); diff --git a/whalesong/whalesong-cmd.rkt b/whalesong/whalesong-cmd.rkt index 91a554a..13d58ac 100644 --- a/whalesong/whalesong-cmd.rkt +++ b/whalesong/whalesong-cmd.rkt @@ -63,7 +63,7 @@ #:program "whalesong" #:argv (current-command-line-arguments) "The Whalesong command-line tool for compiling Racket to JavaScript" - ["version" "Print the current version" + #;["version" "Print the current version" "Print the current version" #:args () (print-version)] diff --git a/whalesong/whalesong-helpers.rkt b/whalesong/whalesong-helpers.rkt index 70d0d8b..ee802b8 100644 --- a/whalesong/whalesong-helpers.rkt +++ b/whalesong/whalesong-helpers.rkt @@ -282,5 +282,5 @@ -(define (print-version) +#;(define (print-version) (fprintf (current-report-port) "~a\n" (this-package-version)))