[plt-scheme] dynamic required modules and scheme/sandbox

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Thu Jun 5 22:47:18 EDT 2008

Hi everyone,

I chased down a problem with my use of scheme/sandbox with some other 
dynamic-required modules that I had placed in a namespace.  I wanted to 
get some module sharing going on with those, but my naive use of the 
sandbox didn't let sharing happen.  I finally fixed my problem and wanted 
to share my solution with others.  Also, if someone sees a better way to 
do this, that would also be appreciated!

#lang scheme/base
(require scheme/list)

(define libraries-to-attach
   (list '(lib "world.ss" "htdp")
         '(lib "mrpict.ss" "texpict")
         '(lib "cache-image-snip.ss" "mrlib")))

;; make-sandboxed-evaluator/dynamic: module-path -> void
;; Creates a sandboxed evaluator from a dynamic context.
;; We need to do this if we've dynamic-required the other libraries that 
;; we want to attach to.
;; Call in the dynamic context of the namespace that's attaches
;; to the libraries-to-attach modules.

(define (make-sandboxed-evaluator/dynamic language)
   (let ([sandbox-security-guard
          (dynamic-require 'scheme/sandbox 'sandbox-security-guard)]
          (dynamic-require 'scheme/sandbox 'sandbox-namespace-specs)]
          (dynamic-require 'scheme/sandbox 'make-evaluator)])
     (parameterize ([sandbox-security-guard (current-security-guard)]
                     (let-values ([(ns paths)
                                    (first (sandbox-namespace-specs))
                                    (rest (sandbox-namespace-specs)))])
                       (cons ns (append libraries-to-attach paths)))])
       (make-evaluator language))))

Posted on the users mailing list.