diff --git a/whalesong/js-assembler/assemble-perform-statement.rkt b/whalesong/js-assembler/assemble-perform-statement.rkt index 6f6ecfa..3cdc4a4 100644 --- a/whalesong/js-assembler/assemble-perform-statement.rkt +++ b/whalesong/js-assembler/assemble-perform-statement.rkt @@ -192,19 +192,21 @@ [(FinalizeModuleInvokation!? op) (define modname (symbol->string (ModuleLocator-name (FinalizeModuleInvokation!-path op)))) (string-append - (format "M.modules[~s].finalizeModuleInvokation();" - modname) - - "(function (ns) {" + "(function (selfMod,ns,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]);" - (symbol->string (ModuleProvide-external-name a-provide)) - (symbol->string (ModuleProvide-internal-name a-provide)))] + (format "ns.set(~s, M.primitives[~s]);" + (symbol->string (ModuleProvide-external-name a-provide)) + (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)))] [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].getNamespace()));" modname))])) + (format "}(M.modules[~s],M.modules[~s].getNamespace(),M.modules[~s].prefix));" modname modname modname))])) diff --git a/whalesong/js-assembler/package.rkt b/whalesong/js-assembler/package.rkt index 3d24c2b..7bec670 100644 --- a/whalesong/js-assembler/package.rkt +++ b/whalesong/js-assembler/package.rkt @@ -173,12 +173,13 @@ (for/list ([modprovide (get-provided-names bytecode)] [i (in-naturals)]) (string-append -; (format "modrec.getNamespace().set(~s,exports[~s]);\n" -; (symbol->string (ModuleProvide-internal-name modprovide)) -; (symbol->string (ModuleProvide-external-name modprovide))) - (format "modrec.prefix[~a]=exports[~s];\n" - i - (symbol->string (ModuleProvide-external-name modprovide))))))) + (format "modrec.getNamespace().set(~s,exports[~s]);\n" + (symbol->string (ModuleProvide-external-name modprovide)) + (symbol->string (ModuleProvide-internal-name modprovide))) + ;(format "modrec.prefix[~a]=exports[~s];\n" + ; i + ; (symbol->string (ModuleProvide-external-name modprovide)))) + )))) (define (get-prefix-code bytecode) (format "modrec.prefix=[~a];modrec.prefix.names=[~a];modrec.prefix.internalNames=[~a];" @@ -220,7 +221,6 @@ (function(MACHINE, EXPORTS){~a})(M, exports); ~a modrec.privateExports = exports; - modrec.finalizeModuleInvokation(); return M.c.pop().label(M);" (symbol->string name) (get-prefix-code bytecode) diff --git a/whalesong/js-assembler/runtime-src/baselib-modules.js b/whalesong/js-assembler/runtime-src/baselib-modules.js index 871d2b2..ccb2e2e 100644 --- a/whalesong/js-assembler/runtime-src/baselib-modules.js +++ b/whalesong/js-assembler/runtime-src/baselib-modules.js @@ -10,36 +10,13 @@ var Namespace = function(modrec) { this.modrec = modrec; - // string -> integer - // Returns the position within the prefix that we should be looking. + // Returns the key/value pairs of the prefix. + // mapping: string -> value this.mapping = {}; - this.prefix = void(0); - this.extra = {}; }; Namespace.prototype.get = function(name) { - if (this.mapping.hasOwnProperty(name)) { - return this.prefix[this.mapping[name]]; - } - if (this.extra.hasOwnProperty(name)) { - return this.extra[name]; - } - return void(0); - }; - - Namespace.prototype.refreshPrefixMapping = function() { - var prefix = this.modrec.prefix; - var name; - var i; - for (i = 0; i < prefix.length; i++) { - name = prefix.names[i]; - this.mapping[name] = i; - if (this.extra.hasOwnProperty(name)) { - prefix[i] = this.extra[name]; - delete this.extra[name]; - } - } - this.prefix = prefix; + return this.mapping[name]; }; Namespace.prototype.hasKey = function(name) { @@ -47,16 +24,7 @@ }; Namespace.prototype.set = function(name, value) { - if (this.mapping.hasOwnProperty(name)) { - this.prefix[this.mapping[name]] = value; - return; - }; - if (this.extra.hasOwnProperty(name)) { - this.extra[name] = value; - return; - } - this.extra[name] = value; - return; + this.mapping[name] = value; }; var ModuleRecord = function (name, label) { @@ -85,12 +53,7 @@ // Returns access to the names defined in the module. ModuleRecord.prototype.getNamespace = function () { return this.namespace; - }; - - ModuleRecord.prototype.finalizeModuleInvokation = function () { - this.namespace.refreshPrefixMapping(); - }; - + }; // External invokation of a module. ModuleRecord.prototype.invoke = function (MACHINE, succ, fail) { @@ -148,4 +111,4 @@ exports.ModuleRecord = ModuleRecord; -}(this.plt.baselib, this.plt)); \ No newline at end of file +}(this.plt.baselib, this.plt)); diff --git a/whalesong/repl-prototype/server.rkt b/whalesong/repl-prototype/server.rkt index 0e2106b..196b899 100644 --- a/whalesong/repl-prototype/server.rkt +++ b/whalesong/repl-prototype/server.rkt @@ -14,11 +14,6 @@ "../js-assembler/package.rkt" "../parser/parse-bytecode.rkt" "../compiler/compiler.rkt" - "../compiler/expression-structs.rkt" - "../compiler/il-structs.rkt" - "../compiler/lexical-structs.rkt" - "../compiler/compiler-structs.rkt" - "../compiler/optimize-il.rkt" "../js-assembler/assemble.rkt" "write-runtime.rkt" (for-syntax racket/base)) @@ -79,7 +74,7 @@ (define op (open-output-bytes)) (write raw-bytecode op) (define whalesong-bytecode (parse-bytecode (open-input-bytes (get-output-bytes op)))) - #;(pretty-print whalesong-bytecode) + (pretty-print whalesong-bytecode) (define compiled-bytecode (compile-for-repl whalesong-bytecode)) #;(pretty-print compiled-bytecode) (define assembled-op (open-output-string))