From b01447626ca2a7409539825cbcb80ce1cb4450ce Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Wed, 6 Mar 2013 16:26:06 -0700 Subject: [PATCH] assigning the external names --- whalesong/image/private/kernel.js | 2 +- .../js-assembler/assemble-perform-statement.rkt | 15 ++++++++++----- whalesong/js-assembler/package.rkt | 7 ++++++- .../js-assembler/runtime-src/baselib-modules.js | 9 ++++++++- whalesong/resource/js-impl.js | 2 +- whalesong/resource/specialize/js-impl.js | 2 +- whalesong/web-world/js-impl.js | 6 +++--- 7 files changed, 30 insertions(+), 13 deletions(-) diff --git a/whalesong/image/private/kernel.js b/whalesong/image/private/kernel.js index dc8e916..a969031 100644 --- a/whalesong/image/private/kernel.js +++ b/whalesong/image/private/kernel.js @@ -4,7 +4,7 @@ ////////////////////////////////////////////////////////////////////// -var colorNamespace = MACHINE.modules['whalesong/image/private/color.rkt'].getNamespace(); +var colorNamespace = MACHINE.modules['whalesong/image/private/color.rkt'].getExternalNamespace(); var colorStruct = colorNamespace.get('struct:color'); var makeColor = function(r,g,b,a) { return colorStruct.constructor([r,g,b,a]); }; var isColor = colorStruct.predicate; diff --git a/whalesong/js-assembler/assemble-perform-statement.rkt b/whalesong/js-assembler/assemble-perform-statement.rkt index 3cdc4a4..a887b55 100644 --- a/whalesong/js-assembler/assemble-perform-statement.rkt +++ b/whalesong/js-assembler/assemble-perform-statement.rkt @@ -192,7 +192,7 @@ [(FinalizeModuleInvokation!? op) (define modname (symbol->string (ModuleLocator-name (FinalizeModuleInvokation!-path op)))) (string-append - "(function (selfMod,ns,prefix) {" + "(function (selfMod,ns,extNs,prefix) {" (string-join (for/list: : (Listof String) ([a-provide : ModuleProvide (FinalizeModuleInvokation!-provides op)]) (cond [(kernel-module-name? (ModuleProvide-source a-provide)) (format "ns.set(~s, M.primitives[~s]);" @@ -200,13 +200,18 @@ (symbol->string (ModuleProvide-internal-name a-provide)))] [(equal? (ModuleLocator-name (ModuleProvide-source a-provide)) (ModuleLocator-name (FinalizeModuleInvokation!-path op))) - (format "ns.set(~s, prefix[selfMod.getPrefixOffset(~s)]);" - (symbol->string (ModuleProvide-internal-name a-provide)) - (symbol->string (ModuleProvide-internal-name a-provide)))] + (string-append (format "ns.set(~s, prefix[selfMod.getPrefixOffset(~s)]);" + (symbol->string (ModuleProvide-internal-name a-provide)) + (symbol->string (ModuleProvide-internal-name a-provide))) + ;; For JS-derived code, it might be inconvenient to get the bindings by internal + ;; name. We assign a separate mapping here to make it easier to access. + (format "extNs.set(~s, prefix[selfMod.getPrefixOffset(~s)]);" + (symbol->string (ModuleProvide-external-name a-provide)) + (symbol->string (ModuleProvide-internal-name a-provide))))] [else (format "ns.set(~s, M.modules[~s].getNamespace().get(~s));" (symbol->string (ModuleProvide-external-name a-provide)) (symbol->string (ModuleLocator-name (ModuleProvide-source a-provide))) (symbol->string (ModuleProvide-internal-name a-provide)))])) "") - (format "}(M.modules[~s],M.modules[~s].getNamespace(),M.modules[~s].prefix));" modname modname modname))])) + (format "}(M.modules[~s],M.modules[~s].getNamespace(),M.modules[~s].getExternalNamespace(),M.modules[~s].prefix));" modname modname modname modname))])) diff --git a/whalesong/js-assembler/package.rkt b/whalesong/js-assembler/package.rkt index 6c62a55..3edf552 100644 --- a/whalesong/js-assembler/package.rkt +++ b/whalesong/js-assembler/package.rkt @@ -173,9 +173,12 @@ (for/list ([modprovide (get-provided-names bytecode)] [i (in-naturals)]) (string-append - (format "modrec.getNamespace().set(~s,exports[~s]);\n" + (format "ns.set(~s,exports[~s]);\n" (symbol->string (ModuleProvide-internal-name modprovide)) (symbol->string (ModuleProvide-external-name modprovide))) + (format "extNs.set(~s,exports[~s]);\n" + (symbol->string (ModuleProvide-external-name modprovide)) + (symbol->string (ModuleProvide-external-name modprovide))) (format "modrec.prefix[~a]=exports[~s];\n" i (symbol->string (ModuleProvide-external-name modprovide))))))) @@ -214,6 +217,8 @@ (format " if(--M.cbt<0) { throw arguments.callee; } var modrec = M.modules[~s]; + var ns = modrec.getNamespace(); + var extNs = modrec.getExternalNamespace(); ~a var exports = {}; modrec.isInvoked = true; diff --git a/whalesong/js-assembler/runtime-src/baselib-modules.js b/whalesong/js-assembler/runtime-src/baselib-modules.js index ccb2e2e..fa3e57b 100644 --- a/whalesong/js-assembler/runtime-src/baselib-modules.js +++ b/whalesong/js-assembler/runtime-src/baselib-modules.js @@ -33,6 +33,7 @@ this.isInvoked = false; this.prefix = false; this.namespace = new Namespace(this); + this.externalNamespace = new Namespace(this); // JavaScript-implemented code will assign privateExports // with all of the exported identifiers. @@ -50,11 +51,17 @@ return void(0); }; - // Returns access to the names defined in the module. + // Returns access to the names provided in the module. + // Note that the names are the names internal to the module. ModuleRecord.prototype.getNamespace = function () { return this.namespace; }; + // Returns access to the names defined with their external names. + ModuleRecord.prototype.getExternalNamespace = function() { + return this.externalNamespace; + }; + // External invokation of a module. ModuleRecord.prototype.invoke = function (MACHINE, succ, fail) { this._invoke(false, MACHINE, succ, fail); diff --git a/whalesong/resource/js-impl.js b/whalesong/resource/js-impl.js index 27aca0f..9727eb9 100644 --- a/whalesong/resource/js-impl.js +++ b/whalesong/resource/js-impl.js @@ -1,4 +1,4 @@ -var resourceType = MACHINE.modules['whalesong/resource/structs.rkt'].getNamespace().get('struct:resource'); +var resourceType = MACHINE.modules['whalesong/resource/structs.rkt'].getExternalNamespace().get('struct:resource'); var makePrimitiveProcedure = plt.baselib.functions.makePrimitiveProcedure; diff --git a/whalesong/resource/specialize/js-impl.js b/whalesong/resource/specialize/js-impl.js index a5d449c..742b37c 100644 --- a/whalesong/resource/specialize/js-impl.js +++ b/whalesong/resource/specialize/js-impl.js @@ -1,4 +1,4 @@ -var resourceType = MACHINE.modules['whalesong/resource/structs.rkt'].getNamespace().get('struct:resource'); +var resourceType = MACHINE.modules['whalesong/resource/structs.rkt'].getExternalNamespace().get('struct:resource'); var makeClosure = plt.baselib.functions.makeClosure; var finalizeClosureCall = plt.baselib.functions.finalizeClosureCall; diff --git a/whalesong/web-world/js-impl.js b/whalesong/web-world/js-impl.js index 48acf90..1b069fd 100644 --- a/whalesong/web-world/js-impl.js +++ b/whalesong/web-world/js-impl.js @@ -34,10 +34,10 @@ var resourceStructType = - MACHINE.modules['whalesong/resource/structs.rkt'].getNamespace().get('struct:resource'); + MACHINE.modules['whalesong/resource/structs.rkt'].getExternalNamespace().get('struct:resource'); var eventStructType = - MACHINE.modules['whalesong/web-world/event.rkt'].getNamespace().get('struct:event'); + MACHINE.modules['whalesong/web-world/event.rkt'].getExternalNamespace().get('struct:event'); @@ -2287,4 +2287,4 @@ EXPORTS['EventHandler'] = EventHandler; EXPORTS['wrapFunction'] = wrapFunction; ////////////////////////////////////////////////////////////////////// -}()); \ No newline at end of file +}());