modulevariables

This commit is contained in:
Danny Yoo 2011-05-25 22:11:34 -04:00
parent a92e6b95e4
commit 24e39ede45
6 changed files with 114 additions and 21 deletions

7
NOTES
View File

@ -566,3 +566,10 @@ I've isolated exactly what primitives I need to get racket/base up and
running. It looks like I need 231 of them. That's not that much, running. It looks like I need 231 of them. That's not that much,
actually. experiments/primitives-for-racket-base describes which ones actually. experiments/primitives-for-racket-base describes which ones
we need. we need.
----------------------------------------------------------------------
May 25, 2011
About to make modules work. Need to make sure exports can rename names.

View File

@ -430,6 +430,11 @@
[to : ModuleLocator]) [to : ModuleLocator])
#:transparent) #:transparent)
;; Given the module locator, do any finalizing operations, like
;; setting up the module namespace.
(define-struct: FinalizeModuleInvokation! ([path : ModuleLocator])
#:transparent)
(define-type PrimitiveCommand (U (define-type PrimitiveCommand (U
@ -458,6 +463,7 @@
InstallModuleEntry! InstallModuleEntry!
MarkModuleInvoked! MarkModuleInvoked!
AliasModuleName! AliasModuleName!
FinalizeModuleInvokation!
)) ))

View File

@ -2,6 +2,7 @@
(require "assemble-helpers.rkt" (require "assemble-helpers.rkt"
"../il-structs.rkt" "../il-structs.rkt"
"../lexical-structs.rkt" "../lexical-structs.rkt"
"../parameters.rkt"
racket/string) racket/string)
(provide assemble-op-statement) (provide assemble-op-statement)
@ -46,8 +47,17 @@
;; FIXME: this should be looking at the module path and getting ;; FIXME: this should be looking at the module path and getting
;; the value here! It shouldn't be looking into Primitives... ;; the value here! It shouldn't be looking into Primitives...
[(ModuleVariable? n) [(ModuleVariable? n)
(cond
[((current-kernel-module-locator?)
(ModuleVariable-module-name n))
(format "MACHINE.primitives[~s]" (format "MACHINE.primitives[~s]"
(symbol->string (ModuleVariable-name n)))])) (symbol->string (ModuleVariable-name n)))]
[else
(format "MACHINE.modules[~s].namespace[~s]"
(symbol->string
(ModuleLocator-name
(ModuleVariable-module-name n)))
(symbol->string (ModuleVariable-name n)))])]))
names) names)
",") ",")
(string-join (map (string-join (map
@ -145,4 +155,7 @@
[(AliasModuleName!? op) [(AliasModuleName!? op)
(format "MACHINE.modules[~s] = MACHINE.modules[~s];" (format "MACHINE.modules[~s] = MACHINE.modules[~s];"
(symbol->string (ModuleLocator-name (AliasModuleName!-to op))) (symbol->string (ModuleLocator-name (AliasModuleName!-to op)))
(symbol->string (ModuleLocator-name (AliasModuleName!-from op))))])) (symbol->string (ModuleLocator-name (AliasModuleName!-from op))))]
[(FinalizeModuleInvokation!? op)
(format "MACHINE.modules[~s].finalizeModuleInvokation();")]))

View File

@ -98,9 +98,22 @@
this.label = label; this.label = label;
this.isInvoked = false; this.isInvoked = false;
this.prefix = false; this.prefix = false;
this.namespace = {};
}; };
// Returns access to the names defined in the module.
ModuleRecord.prototype.getNamespace = function() {
return this.namespace;
};
ModuleRecord.prototype.finalizeModuleInvokation = function() {
var i, len = this.prefix.names.length;
for (i=0; i < len; i++) {
this.namespace[this.prefix.names[i]] = this.prefix[i];
}
};
// External invokation of a module.
ModuleRecord.prototype.invoke = function(MACHINE, succ, fail) { ModuleRecord.prototype.invoke = function(MACHINE, succ, fail) {
var oldErrorHandler = MACHINE.params['currentErrorHandler']; var oldErrorHandler = MACHINE.params['currentErrorHandler'];
var afterGoodInvoke = function(MACHINE) { var afterGoodInvoke = function(MACHINE) {
@ -113,11 +126,13 @@
} else { } else {
MACHINE.params['currentErrorHandler'] = function(MACHINE, anError) { MACHINE.params['currentErrorHandler'] = function(MACHINE, anError) {
MACHINE.params['currentErrorHandler'] = oldErrorHandler; MACHINE.params['currentErrorHandler'] = oldErrorHandler;
setTimeout(function() { fail(MACHINE, anError)}, 0); setTimeout(
function() {
fail(MACHINE, anError)
},
0);
}; };
MACHINE.control.push(new CallFrame( MACHINE.control.push(new CallFrame(afterGoodInvoke, null));
afterGoodInvoke,
null));
trampoline(MACHINE, this.label); trampoline(MACHINE, this.label);
} }
}; };

View File

@ -74,7 +74,7 @@
[self-path : Symbol] [self-path : Symbol]
[label : Symbol] [label : Symbol]
[invoked? : Boolean] [invoked? : Boolean]
[exports : (HashTable Symbol PrimitiveValue)] [namespace : (HashTable Symbol PrimitiveValue)]
[toplevel : (U False toplevel)]) [toplevel : (U False toplevel)])
#:transparent #:transparent
#:mutable) #:mutable)

View File

@ -337,16 +337,18 @@
[(ExtendEnvironment/Prefix!? op) [(ExtendEnvironment/Prefix!? op)
(env-push! m (env-push! m
(make-toplevel (ExtendEnvironment/Prefix!-names op) (make-toplevel
(ExtendEnvironment/Prefix!-names op)
(map (lambda: ([name : (U False Symbol GlobalBucket ModuleVariable)]) (map (lambda: ([name : (U False Symbol GlobalBucket ModuleVariable)])
(cond [(eq? name #f) (cond [(eq? name #f)
(make-undefined)] (make-undefined)]
[(symbol? name) [(symbol? name)
(lookup-primitive name)] (lookup-primitive name)]
[(GlobalBucket? name) [(GlobalBucket? name)
(lookup-primitive (GlobalBucket-name name))] (lookup-primitive
(GlobalBucket-name name))]
[(ModuleVariable? name) [(ModuleVariable? name)
(lookup-primitive (ModuleVariable-name name))])) (lookup-module-variable m name)]))
(ExtendEnvironment/Prefix!-names op))))] (ExtendEnvironment/Prefix!-names op))))]
[(InstallClosureValues!? op) [(InstallClosureValues!? op)
@ -482,7 +484,57 @@
(hash-set! (machine-modules m) (hash-set! (machine-modules m)
(ModuleLocator-name (AliasModuleName!-to op)) (ModuleLocator-name (AliasModuleName!-to op))
module-record) module-record)
'ok)]))) 'ok)]
[(FinalizeModuleInvokation!? op)
(let* ([mrecord
(hash-ref (machine-modules m)
(ModuleLocator-name (FinalizeModuleInvokation!-path op)))]
[ns (module-record-namespace mrecord)]
[top
(module-record-toplevel mrecord)])
(cond
[(toplevel? top)
(for-each (lambda: ([n : (U False Symbol GlobalBucket ModuleVariable)]
[v : PrimitiveValue])
(cond
[(eq? n #f)
(void)]
[(symbol? n)
(hash-set! ns n v)]
[(GlobalBucket? n)
(hash-set! ns (GlobalBucket-name n) v)]
[(ModuleVariable? n)
(hash-set! ns (ModuleVariable-name n) v)]))
(toplevel-names top)
(toplevel-vals top))
'ok]
[(eq? top #f)
;; This should never happen. But let's make sure we can see the
;; error.
(error 'FinalizeModuleInvokation
"internal error: toplevel hasn't been initialized.")]))])))
(: lookup-module-variable (machine ModuleVariable -> PrimitiveValue))
(define (lookup-module-variable m mv)
(cond
[(or (eq?
(ModuleLocator-name
(ModuleVariable-module-name mv))
'#%kernel)
(eq?
(ModuleLocator-name
(ModuleVariable-module-name mv))
'whalesong/lang/kernel.rkt))
(lookup-primitive (ModuleVariable-name mv))]
[else
(let ([mrecord
(hash-ref (machine-modules m)
(ModuleLocator-name (ModuleVariable-module-name mv)))])
(hash-ref (module-record-namespace mrecord)
(ModuleVariable-name mv)))]))