diff --git a/get-module-bytecode.rkt b/get-module-bytecode.rkt index cdb930c..c894541 100644 --- a/get-module-bytecode.rkt +++ b/get-module-bytecode.rkt @@ -1,5 +1,7 @@ #lang racket/base -(require racket/contract racket/path) +(require racket/contract + racket/path + syntax/modcode) (provide/contract [get-module-bytecode ((or/c string? path? input-port?) . -> . bytes?)]) @@ -9,21 +11,34 @@ (define (get-module-bytecode x) (let ([compiled-code (cond + ;; Assumed to be a path string [(string? x) - (call-with-input-file x - (lambda (ip) - (get-compiled-code-from-port ip)))] + (get-compiled-code-from-path (normalize-path (build-path x)))] + [(path? x) - (call-with-input-file x - (lambda (ip) - (get-compiled-code-from-port ip)))] + (get-compiled-code-from-path x)] + + ;; Input port is assumed to contain the text of a module. + [(input-port? x) + (get-compiled-code-from-port x)] + [else - (get-compiled-code-from-port x)])]) + (error 'get-module-bytecode)])]) (let ([op (open-output-bytes)]) (write compiled-code op) (get-output-bytes op)))) +;; Tries to use get-module-code to grab at module bytecode. Sometimes this fails +;; because it appears get-module-code tries to write to compiled/. +(define (get-compiled-code-from-path p) + (with-handlers ([void (lambda (exn) + ;; Failsafe: try to do it from scratch + (call-with-input-file* p + (lambda (ip) + (get-compiled-code-from-port ip))))]) + (get-module-code p))) + (define (get-compiled-code-from-port ip) (parameterize ([read-accept-reader #t] diff --git a/tests/test-parse-bytecode-on-collects.rkt b/tests/test-parse-bytecode-on-collects.rkt index e0a3ac1..aa0d481 100644 --- a/tests/test-parse-bytecode-on-collects.rkt +++ b/tests/test-parse-bytecode-on-collects.rkt @@ -1,5 +1,11 @@ #lang racket/base +;; Tries to parse all the files in collects and sees how long it takes. +;; +;; TODO: figure out why it fails to get the module bytecode for +;; collects/tests/matrix-test.rkt. I'm seeing the following: +;; read-syntax: cannot load snip-class reader + (require "../parse-bytecode.rkt" racket/path) @@ -14,9 +20,10 @@ p])))) -(for ([path (in-directory)]) +(for ([path (in-directory collects-dir)]) (when (regexp-match? #rx"[.]rkt$" path) (printf "parsing file: ~a... " path) + (flush-output) (let ([start-time (current-inexact-milliseconds)]) (void (parse-bytecode path)) (let ([end-time (current-inexact-milliseconds)])