Skip to content

Commit 6f49d19

Browse files
authored
Prepare proper module dependency graph when taking module from stdin (#222)
Currently, we relied on a stub "nothing.rkt", but we should instead look at the imported modules itself. This should fix the the 'undefined identifier' issues we keep getting in Playground. FIXES #134
1 parent ab35976 commit 6f49d19

5 files changed

Lines changed: 32 additions & 38 deletions

File tree

racketscript-compiler/racketscript/compiler/expand.rkt

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@
310310
nom-src-mod-path-orig
311311
mod-src-id))]))
312312

313-
;; If the moduele is renamed use the id name used at the importing
313+
;; If the module is renamed use the id name used at the importing
314314
;; module rather than defining module. Since renamed, module currently
315315
;; are #%kernel which we write ourselves in JS we prefer original name.
316316
;; TODO: We potentially might have clashes, but its unlikely.
@@ -458,7 +458,7 @@
458458
(define (to-absyn/top stx)
459459
(to-absyn stx))
460460

461-
(define (do-expand stx in-path)
461+
(define (do-expand stx)
462462
;; error checking
463463
(syntax-parse stx
464464
[((~and mod-datum (~datum module)) n:id lang:expr . rest)
@@ -490,21 +490,12 @@
490490
(read-accept-lang #t)
491491
(define full-path (path->complete-path (actual-module-path in-path)))
492492
(parameterize ([current-directory (path-only full-path)])
493-
(do-expand (open-read-module in-path) in-path)))
493+
(do-expand (open-read-module in-path))))
494494

495495
(define (read-and-expand-module input)
496496
(read-accept-reader #t)
497497
(read-accept-lang #t)
498-
;; Just give it any name for now
499-
(define full-path
500-
(match (object-name input)
501-
['stdin (main-source-file)]
502-
[v (path->complete-path v)]))
503-
(define new-cwd (if full-path
504-
(path-only full-path)
505-
(current-directory)))
506-
(parameterize ([current-directory new-cwd])
507-
(do-expand (read-syntax (object-name input) input) full-path)))
498+
(do-expand (read-syntax (object-name input) input)))
508499

509500
;;;----------------------------------------------------------------------------
510501
;;; Flatten Phases in Module
@@ -994,4 +985,3 @@
994985

995986
(check-equal? (map syntax-e (get-quoted-bindings test-mod-1))
996987
'(internal-func))))
997-

racketscript-compiler/racketscript/compiler/main.rkt

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -368,25 +368,30 @@
368368
(log-rjs-info "RacketScript root directory: ~a" racketscript-dir))
369369

370370
(unless (input-from-stdin?)
371-
;; Initialize global-export-graph so that we can import each
372-
;; module as an object and follow identifier's from there.
373-
;; For stdin builds, we have to defer this operation.
371+
;; Initialize global-export-graph so that we can import each module as an
372+
;; object and follow identifier's from there. For stdin builds, we have to
373+
;; defer this operation.
374374
(unless (equal? (build-mode) 'js)
375375
;; As 'js mode prints output to stdout, we don't want to mix
376376
(log-rjs-info "Resolving module dependencies and identifiers... "))
377-
(global-export-graph (get-export-tree source)))
377+
(global-export-graph (get-export-tree (list source))))
378378

379379
(define (expanded-module)
380380
(cond
381381
[(input-from-stdin?)
382-
;; HACK: Just make an stupid guess that all that we will
383-
;; ever use will come from standard library. Since we
384-
;; need stdin from playground, its fine for now.
385-
;; TODO: Figure out a way to compile this syntax to
386-
;; module code bytecode
387-
(global-export-graph (get-export-tree (build-path racketscript-compiler-dir
388-
"nothing.rkt")))
389-
(read-and-expand-module (current-input-port))]
382+
(parameterize ([current-namespace (make-base-namespace)])
383+
(define expanded-mod (read-and-expand-module (current-input-port)))
384+
(eval expanded-mod)
385+
386+
;; Prepare the module graph for each import. We don't care about, exports
387+
;; from here, as this module will never be imported.
388+
(match (module->imports ''anonymous-module)
389+
[`((0 ,mods ...) rst ...)
390+
;; TODO: Do we need to look at other phase imports?
391+
(global-export-graph (get-export-tree (map resolve-module-path-index mods)))]
392+
[_ (error "unexpected form returned by module->imports")])
393+
394+
expanded-mod)]
390395
[else
391396
(quick-expand source)]))
392397

racketscript-compiler/racketscript/compiler/moddeps.rkt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@
7171
[#f (result src* id*)]
7272
['() #f]))))
7373

74-
;; ModulePath -> ExportTree
74+
;; (Listof ModulePath) -> ExportTree
7575
;; Return whole tree of exports with its source starting
76-
;; from mod-name (ModulePath)
77-
(define (get-export-tree mod-name)
76+
;; with given list of modules 'mods'.
77+
(define (get-export-tree mods)
7878
(define modules (filter-not symbol? (module-deps/tsort-inv
79-
(get-module-deps mod-name))))
79+
(get-module-deps mods))))
8080
(for/hash ([m (append (set->list primitive-modules) modules)])
8181
(values m (get-exports/modpath m))))
8282

@@ -129,9 +129,9 @@
129129
(transpose _)
130130
(tsort _)))
131131

132-
;; Path -> (Map Path (Listof Path))
133-
;; Returns a adjecency map of module imports
134-
(define (get-module-deps mod-path)
132+
;; (Listof Path) -> (Map Path (Listof Path))
133+
;; Returns a adjacency map of module imports.
134+
(define (get-module-deps mod-paths)
135135
(define graph (make-hash))
136136
(define (build-graph mod-path)
137137
(define path (resolve-module-path mod-path #f))
@@ -150,5 +150,7 @@
150150
(hash-update! graph path (λ (v) (cons new-mod v)))
151151
(unless (hash-ref graph new-mod #f)
152152
(build-graph new-mod))]))))
153-
(build-graph mod-path)
153+
(for ([path mod-paths])
154+
(build-graph path))
155+
154156
graph)

racketscript-compiler/racketscript/compiler/nothing.rkt

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/fixture.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
;; Path-String -> ExportTree
106106
(define get-cached-export-tree
107107
(memoized-λ (test-fpath)
108-
(get-export-tree test-fpath)))
108+
(get-export-tree (list test-fpath))))
109109

110110
;; Path-String -> Void
111111
;; Compile test-case in `fpath` to JavaScript

0 commit comments

Comments
 (0)