Binding a submodule into a sandbox

63 views
Skip to first unread message

Vincent Lee

unread,
Jan 30, 2021, 12:29:49 AM1/30/21
to Racket Users

Hi all,

I'm trying to create a sandbox to which I expose a limited set of functions and values from the outside. So far I have this:

#lang racket

(require racket/sandbox)

(module* sub racket
  (provide foo)
  (define foo "foo"))

However if I now run

(make-evaluator 'racket #:requires 'sub)

I get an error. I also get an error if I try

(make-evaluator 'racket #:requires (submod "." 'sub))

I expect this to work and for `foo` to be available in the sandbox.

A normal `(require 'sub)` works fine, but according to the docs #:requires takes the exact same form arguments as `require`, so I'm not sure what I'm missing.

Alternatively, what's the cleanest way to achieve my goal of providing a fixed set of functions and variables into a sandbox?

Thanks in advance.

Matthew Flatt

unread,
Jan 30, 2021, 10:07:16 AM1/30/21
to Vincent Lee, Racket Users
I think the main problem is that `make-evaluator` is a function, which
means that it doesn't know what module it's called in. So, relative
paths like 'sub or (submod "." 'sub) don't work.

You could use `quote-module-path` from `syntax/location` like this:

#lang racket
(require racket/sandbox
syntax/location)

(module* sub racket
(provide foo)
(define foo "foo"))

(make-evaluator 'racket #:requires (list (quote-module-path sub)))

This can work because the `quote-module-path` form expands to an
expression at compile time that identifies the enclosing module at run
time.
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to racket-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/53fc8114-3348-4d7b-b915-cb37dac1b
> d25n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages