From de009f04d4814c06806400b0086cf4ad1e1ce90c Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Fri, 22 Feb 2013 16:46:05 -0700 Subject: [PATCH] trying to find why dynamic module loading isn't working. --- whalesong/js-assembler/package.rkt | 10 ++++- whalesong/js-assembler/runtime-src/runtime.js | 42 +++++++++---------- whalesong/whalesong-helpers.rkt | 25 +++++++++-- 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/whalesong/js-assembler/package.rkt b/whalesong/js-assembler/package.rkt index 9e402bc..fdf6c95 100644 --- a/whalesong/js-assembler/package.rkt +++ b/whalesong/js-assembler/package.rkt @@ -29,7 +29,8 @@ (prefix-in resource-query: "../resource/query.rkt") (prefix-in runtime: "get-runtime.rkt") (prefix-in racket: racket/base) - racket/runtime-path) + racket/runtime-path + json) @@ -597,7 +598,8 @@ EOF (define (get-html-template js-files #:manifest (manifest #f) #:with-legacy-ie-support? (with-legacy-ie-support? #t) - #:title (title "")) + #:title (title "") + #:module-mappings (module-mappings (make-hash))) (format #< @@ -610,6 +612,9 @@ EOF ~a ~a ~a + @@ -628,6 +633,7 @@ EOF (format " \n" js)) js-files) "") + (jsexpr->string module-mappings) invoke-main-module-code)) diff --git a/whalesong/js-assembler/runtime-src/runtime.js b/whalesong/js-assembler/runtime-src/runtime.js index 7d9187c..b8d8d3c 100644 --- a/whalesong/js-assembler/runtime-src/runtime.js +++ b/whalesong/js-assembler/runtime-src/runtime.js @@ -1127,7 +1127,6 @@ // 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) { - console.log("request to load", moduleName); if (M.modules[moduleName] instanceof ModuleRecord) { return success(); } else { @@ -1136,31 +1135,25 @@ }; - var makeLocalFileModuleLoader = function(basepath) { + // makeLocalFileModuleLoader: (hashof string string) -> moduleLoader + // Given the manifest mapping module names to the files that implement them, + // produces a module loader that uses loadscript to get these modules + // into memory. + var makeLocalFileModuleLoader = function(moduleManifest) { var loadScript = baselib.loadscript.loadScript; return function(M, moduleName, success, fail) { + console.log("request to load", moduleName); + 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); + // The manifest should map module names to + // their files. + if (moduleManifest[moduleName]) { + var modulePath = moduleManifest[moduleName]; + return loadScript(modulePath, success, fail); + } + return fail(); } }; }; @@ -1177,8 +1170,10 @@ // Exports var exports = runtime; exports['currentMachine'] = new Machine(); + exports['currentModuleLoader'] = defaultModuleLoader; - exports['currentModuleManifest'] = {}; + exports['makeLocalFileModuleLoader'] = makeLocalFileModuleLoader; + exports['invokeMains'] = invokeMains; exports['lookupInMains'] = lookupInMains; @@ -1194,6 +1189,9 @@ exports['setReadyTrue'] = setReadyTrue; exports['setReadyFalse'] = setReadyFalse; + + + exports['Machine'] = Machine; exports['Frame'] = Frame; exports['CallFrame'] = CallFrame; diff --git a/whalesong/whalesong-helpers.rkt b/whalesong/whalesong-helpers.rkt index 564abe4..22d2040 100644 --- a/whalesong/whalesong-helpers.rkt +++ b/whalesong/whalesong-helpers.rkt @@ -220,11 +220,16 @@ (fprintf (current-report-port) "Creating destination directory ~s\n" (current-output-dir)) (make-directory* (current-output-dir))) + + (define main-module-path (make-output-js-filename #f)) ;; Write out the main module and its other module dependencies. (parameterize ([current-on-resource on-resource]) (call-with-output-file* (make-output-js-filename #f) (lambda (op) - (display (get-runtime) op) + (display (get-runtime) op)) + #:exists 'replace) + (call-with-output-file* main-module-path + (lambda (op) (display (get-inert-code (make-MainModuleSource (normalize-path (build-path f))) make-output-js-filename) @@ -236,12 +241,22 @@ (fprintf (current-report-port) (format "Writing html ~s\n" (build-path (current-output-dir) output-html-filename))) + (define dynamically-loaded-modules + (remove (file-name-from-path main-module-path) + (for/list ([(key path) module-mappings]) + (file-name-from-path path)))) + (displayln dynamically-loaded-modules) (call-with-output-file* (build-path (current-output-dir) output-html-filename) (lambda (op) (display (get-html-template - (map file-name-from-path (reverse written-js-paths)) + (for*/list ([p (reverse written-js-paths)] + [name (in-value (file-name-from-path p))] + #:unless (member name dynamically-loaded-modules)) + name) #:title title - #:manifest output-manifest-filename) + #:manifest output-manifest-filename + #:module-mappings (for/hash ([(key path) module-mappings]) + (values key (path->string (file-name-from-path path))))) op)) #:exists 'replace) @@ -256,6 +271,7 @@ (fprintf op "~a\n" js-name)) (for [(resource-name written-resources)] (fprintf op "~a\n" resource-name)) + (fprintf op "~a\n" output-js-module-manifest-filename) (fprintf op "\n# All other resources (e.g. sites) require the user to be online.\nNETWORK:\n*\n")) #:exists 'replace) @@ -268,7 +284,8 @@ (fprintf op "plt.runtime.currentModuleManifest=") (write-json (for/hash ([(key path) module-mappings]) (values key (path->string (file-name-from-path path)))) - op)) + op) + (fprintf op ";\n")) #:exists 'replace)