[racket] Evaluating pict to string

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Sun Apr 21 21:43:26 EDT 2013

I think the issue you're seeing is that you've instantiated two copies of
slideshow/pict (as in the example program below), one inside the sandbox
and one outside so the functions on picts from the outside don't work on
picts that are produced by code inside.

I think that probably the right fix is to add some code to expressions that
are evaluated that does the translation inside the sandbox (so very big
picts turn into out of memory errors inside the sandbox instead of outside,
for example).

Robby

#lang racket
(require racket/sandbox)
(define evaluator
  (parameterize ([sandbox-output 'string]
                 [sandbox-error-output 'string]
                 [sandbox-path-permissions '((exists #rx#"")
                                             (read #rx#""))])
    (call-with-limits #f #f
                      (lambda () (make-evaluator 'slideshow)))))


(define (run-code ev str)
  (define res (ev str))
  (define out (get-output ev))
  (define err (get-error-output ev))
  (list res out err))

(require slideshow/pict)

(define not-an-outside-pict (run-code evaluator (format "~s" '(circle 10))))
(pict? not-an-outside-pict)

(run-code evaluator (format "~s" '(pict? (circle 10))))




On Sun, Apr 21, 2013 at 8:26 PM, manu d <th3rac25 at gmail.com> wrote:

> Hello
>
> I am trying to port TryClojure (https://github.com/Raynes/tryclojure), a
> web-based REPL, to Racket.
>
> I would like the tutorial part to be based on Racket's 'Quick' tutorial (
> http://docs.racket-lang.org/quick/)
>
> To do that, I need to evaluate pict expressions, like (circle 10), to a
> string I can return to the browser for rendering.
>
> Right now, I evaluate the strings coming from the browser with the
> following code:
>
> ;;--------------------------------
>
> (define evaluator
>     (parameterize ([sandbox-output 'string]
>                           [sandbox-error-output 'string] ...)
>       (call-with-limits #f #f
>                         (lambda () (make-evaluator 'slideshow)))))
>
>
> (define (run-code ev str)
>   (define res (ev str))
>   (define out (get-output ev))
>   (define err (get-error-output ev))
>   (list res out err))
>
> ;;--------------------------------
>
> I know I can convert a pict structure like this: (convert a-pict
> 'png-bytes)
> but (evaluator "(circle 10)") does not evaluate to a pict structure, so I
> can't use convert here.
>
> I also cannot see how to use bitmap% save-file method here, because I
> cannot get my hands on the bitmap object
>
> Is there a way to parameterize how picts are rendered so I can end up with
> a PNG bytestring ?
>
> Thank you
>
> Manu
>
>
>
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130421/521f16e1/attachment.html>

Posted on the users mailing list.