[racket] Evaluating pict to string
Thanks Robby and Eli
I now have the following, to convert a value inside the sandbox:
;;------------------------------------------------
#lang racket
(require racket/sandbox)
(define evaluator
(parameterize ([sandbox-output 'string]
[sandbox-error-output 'string]
[sandbox-path-permissions '((exists #rx#"")
(read #rx#""))])
(call-with-trusted-sandbox-configuration
(lambda ()
(make-evaluator 'slideshow
#:requires '(file/convertible
net/base64))))))
(define (run-code ev str)
(define exp
(parameterize
([current-input-port (open-input-string str)])
`(let ([e ,(read)])
(if (convertible? e)
(base64-encode (convert e 'png-bytes) #"")
e))))
(define res (ev exp))
(define out (get-output ev))
(define err (get-error-output ev))
(list res out err))
(run-code evaluator "(circle 10)")
;;------------------------------------------------
To add the conversion code to the expressions coming from the browser, I do
the following
(define exp
(parameterize
([current-input-port (open-input-string str)])
`(let ([e ,(read)])
(if (convertible? e)
(base64-encode (convert e 'png-bytes) #"")
e))))
Would this work ?
Thank you
Manu
On Mon, Apr 22, 2013 at 1:43 PM, Robby Findler
<robby at eecs.northwestern.edu>wrote:
> 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/20130422/7e319d2b/attachment-0001.html>