trying to get at the namespace of a javscript-implemented module.

This commit is contained in:
Danny Yoo 2011-06-09 13:42:32 -04:00
parent f79886060c
commit 47f668832e
8 changed files with 80 additions and 19 deletions

View File

@ -5,7 +5,9 @@
"../make/make.rkt" "../make/make.rkt"
"../make/make-structs.rkt" "../make/make-structs.rkt"
"../parameters.rkt" "../parameters.rkt"
"../lang/js/query.rkt" "../parser/path-rewriter.rkt"
racket/match
(prefix-in query: "../lang/js/query.rkt")
(planet dyoo/closure-compile:1:1) (planet dyoo/closure-compile:1:1)
(prefix-in runtime: "get-runtime.rkt") (prefix-in runtime: "get-runtime.rkt")
(prefix-in racket: racket/base)) (prefix-in racket: racket/base))
@ -23,6 +25,23 @@
write-runtime) write-runtime)
;; notify: string (listof any)* -> void
;; Print out log message during the build process.
(define (notify msg . args)
(displayln (apply format msg args)))
(define-struct js-impl (name ;; symbol
real-path ;; path
src ;; string
)
#:transparent)
;; Packager: produce single .js files to be included to execute a ;; Packager: produce single .js files to be included to execute a
;; program. ;; program.
@ -39,19 +58,53 @@
;; source-is-javascript-module?: Source -> (or Source #f) ;; source-is-javascript-module?: Source -> boolean
;; Returns true if the source looks like a Javascript-implemented module.
(define (source-is-javascript-module? src) (define (source-is-javascript-module? src)
(cond (cond
[(StatementsSource? src) [(StatementsSource? src)
src] #f]
[(MainModuleSource? src) [(MainModuleSource? src)
src] (source-is-javascript-module? (MainModuleSource-source src))]
[(ModuleSource? src) [(ModuleSource? src)
src] (query:has-javascript-implementation? `(file ,(path->string (ModuleSource-path src))))]
[(SexpSource? src) [(SexpSource? src)
src] #f]
[(UninterpretedSource? src) [(UninterpretedSource? src)
src])) #f]))
;; get-javascript-implementation: source -> UninterpretedSource
(define (get-javascript-implementation src)
(cond
[(StatementsSource? src)
(error 'get-javascript-implementation src)]
[(MainModuleSource? src)
(get-javascript-implementation (MainModuleSource-source src))]
[(ModuleSource? src)
(let ([name (rewrite-path (ModuleSource-path src))]
[text (query:query `(file ,(path->string (ModuleSource-path src))))])
(make-UninterpretedSource
(format "
MACHINE.modules[~s] =
new plt.runtime.ModuleRecord(~s,
function(MACHINE) {
if(--MACHINE.callsBeforeTrampoline<0) { throw arguments.callee; }
MACHINE.modules[~s].isInvoked = true;
(function(MACHINE, EXPORTS){~a})(MACHINE, MACHINE.modules[~s].namespace);
return MACHINE.control.pop().label(MACHINE);
});
"
(symbol->string name)
(symbol->string name)
(symbol->string name)
text
(symbol->string name))))]
[(SexpSource? src)
(error 'get-javascript-implementation)]
[(UninterpretedSource? src)
(error 'get-javascript-implementation)]))
@ -70,14 +123,16 @@
#:should-follow-children? should-follow? #:should-follow-children? should-follow?
#:output-port op) #:output-port op)
;; wrap-source: source -> source ;; wrap-source: source -> source
;; Translate all JavaScript-implemented sources into uninterpreted sources;
;; we'll leave its interpretation to on-visit-src.
(define (wrap-source src) (define (wrap-source src)
(printf "adding ~s\n" src)
(cond (cond
[(source-is-javascript-module? src) [(source-is-javascript-module? src)
=> (notify "the module ~s has a javascript implementation.\n"
(lambda (wrapped-src) src)
wrapped-src)] (get-javascript-implementation src)]
[else [else
src])) src]))
@ -85,8 +140,7 @@
(define (on-visit-src src ast stmts) (define (on-visit-src src ast stmts)
(cond (cond
[(UninterpretedSource? src) [(UninterpretedSource? src)
;; FIXME (fprintf op (UninterpretedSource-datum src))]
(void)]
[else [else
(assemble/write-invoke stmts op) (assemble/write-invoke stmts op)
(fprintf op "(MACHINE, function() { ")])) (fprintf op "(MACHINE, function() { ")]))
@ -95,7 +149,6 @@
(define (after-visit-src src ast stmts) (define (after-visit-src src ast stmts)
(cond (cond
[(UninterpretedSource? src) [(UninterpretedSource? src)
;; FIXME
(void)] (void)]
[else [else
(fprintf op " }, FAIL, PARAMS);")])) (fprintf op " }, FAIL, PARAMS);")]))

View File

@ -180,6 +180,9 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
}; };
// A generic frame just holds marks. // A generic frame just holds marks.
var Frame = function() { var Frame = function() {
// The set of continuation marks. // The set of continuation marks.

View File

@ -25,7 +25,7 @@
#:transparent) #:transparent)
(define-struct: SexpSource ([sexp : Any]) (define-struct: SexpSource ([sexp : Any])
#:transparent) #:transparent)
(define-struct: UninterpretedSource ([datum : Any]) (define-struct: UninterpretedSource ([datum : String])
#:transparent) #:transparent)

View File

@ -202,4 +202,4 @@ colorDb.put("DIMGRAY", types.color(105, 105, 105));
colorDb.put("BLACK", types.color(0, 0, 0)); colorDb.put("BLACK", types.color(0, 0, 0));
EXPORTS['colorDb'] = colorDb; EXPORTS['_colorDb'] = colorDb;

View File

@ -1,5 +1,5 @@
#lang s-exp "../lang/js/js.rkt" #lang s-exp "../lang/js/js.rkt"
(declare-implementation #:racket "world.rkt" (declare-implementation #:racket "racket-impl.rkt"
#:javascript ("colordb.js" #:javascript ("colordb.js"
"world.js")) "kernel.js"))

6
world/racket-impl.rkt Normal file
View File

@ -0,0 +1,6 @@
#lang s-exp "../lang/base.rkt"
(provide is-color?)
(define (is-color? x)
true)

View File

@ -1 +0,0 @@
#lang s-exp "../lang/base.rkt"