[racket] compiling interactions?

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Fri Jan 14 15:51:04 EST 2011

Hi everyone,


I'm trying to use the following to get the bytecodes for a non-module
interaction:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#lang racket/base
(define (get-interaction-bytecode x #:language-module
                                  (language-module 'racket/base))
  (let ([module-namespace
         (parameterize ([current-namespace (make-base-empty-namespace)])
           (dynamic-require language-module #f)
           (module->namespace language-module))])
    (parameterize ([current-namespace module-namespace])
      (serialize-compiled-code
       (compile (namespace-syntax-introduce
                 (datum->syntax #f (cons '#%top-interaction x))))))))

;; serialize-compiled-code: compiled-code -> bytes
(define (serialize-compiled-code a-compiled-code)
  (let ([op (open-output-bytes)])
    (write a-compiled-code op)
    (get-output-bytes op)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



I have a separate, restricted language called wescheme-interaction.rkt
that doesn't provide a 'require' special form:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#lang s-exp "base.rkt"
(require "wescheme.rkt")
(provide (except-out (all-from-out "wescheme.rkt")
		     require))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


When I compile simple interactions, I get the expected bytecode:

> (get-interaction-bytecode '(printf "hello world\n") #:language-module '(file "/home/dyoo/work/js-vm/lang/wescheme-interaction.rkt"))
#"#~\0055.0.1\1\0\0\0\1\0\0\0\0\332\0\0\0\237%\24i\237$\20\1\36\35\6\n\nkernel.rkt\35\6\b\bbase.rkt\35\6\f\fwescheme.rkt\[email protected]\00633/home/dyoo/work/js-vm/lang/wescheme-interaction.rkt\vBprintf\224\3\20\0\24\16\237\1'moby-stack-record-continuation-mark-key\25\20\6^BsyntaxDoperator\6\2\2#f\v\v\v$\370P\237%$&\6\f\fhello
world\n"


This actually looks ok.  However, when I try to get the system to
properly error out when the user tries to pass in a 'require',

    (get-interaction-bytecode '(require racket) #:language-module
'(file "/home/dyoo/work/js-vm/lang/wescheme-interaction.rkt"))


then I don't get the expected error: it looks like it just uses
require, even though I've excluded it from my language.


Posted on the users mailing list.