fix bug in deserializer; also change scribble eval to not use sandbox module mode

svn: r8280
This commit is contained in:
Matthew Flatt 2008-01-10 19:47:13 +00:00
parent 9f8b439c5c
commit 2d08c17725
5 changed files with 87 additions and 21 deletions

View File

@ -349,7 +349,7 @@
(deserial-shell (unbox v) mod-map (not-ready-fixup sv) n) (deserial-shell (unbox v) mod-map (not-ready-fixup sv) n)
(deserialize-one v share mod-map))]) (deserialize-one v share mod-map))])
(vector-set! share n val) (vector-set! share n val)
v) val)
sv))) sv)))
(define (deserialize-one v share mod-map) (define (deserialize-one v share mod-map)
@ -426,7 +426,7 @@
v0)] v0)]
[(h) [(h)
;; Hash table ;; Hash table
(let ([ht0 (make-hash-table)]) (let ([ht0 (apply make-hash-table (cdr v))])
(vector-set! fixup n (lambda (ht) (vector-set! fixup n (lambda (ht)
(hash-table-for-each (hash-table-for-each
ht ht

View File

@ -209,7 +209,7 @@
[sandbox-error-output 'string] [sandbox-error-output 'string]
[sandbox-eval-limits #f] [sandbox-eval-limits #f]
[sandbox-make-inspector current-inspector]) [sandbox-make-inspector current-inspector])
(make-evaluator 'scheme/base))) (make-evaluator '(begin (require scheme/base)))))
(define (do-plain-eval ev s catching-exns?) (define (do-plain-eval ev s catching-exns?)
(call-with-values (lambda () (call-with-values (lambda ()
@ -223,6 +223,8 @@
[(module . _rest) [(module . _rest)
(syntax->datum s)] (syntax->datum s)]
[_else s])] [_else s])]
[(bytes? s)
`(begin ,s)]
[(string? s) [(string? s)
`(begin ,s)] `(begin ,s)]
[else s])))) [else s]))))

View File

@ -1,9 +1,11 @@
#lang scribble/doc #lang scribble/doc
@require[scribble/manual] @(require scribble/manual
@require[scribble/struct] scribble/struct
@require[scribble/eval] scribble/eval
@require[mzlib/process] mzlib/process
@require["guide-utils.ss"] "guide-utils.ss"
(for-label scheme/tcp
scheme/serialize))
@(define io-eval (make-base-eval)) @(define io-eval (make-base-eval))
@ -51,7 +53,17 @@ examples:
(close-input-port in) (close-input-port in)
] ]
@interaction-eval[#:eval io-eval (when (file-exists? "data") (delete-file "data"))] If a file exists already, then @scheme[open-output-file] raises an
exception by default. Supply an option like @scheme[#:exists
'truncate] or @scheme[#:exists 'update] to re-write or update the
file:
@examples[
#:eval io-eval
(define out (open-output-file "data" #:exists 'truncate))
(display "howdy" out)
(close-output-port out)
]
Instead of having to match @scheme[open-input-file] and Instead of having to match @scheme[open-input-file] and
@scheme[open-output-file] calls, most Scheme programmers will instead @scheme[open-output-file] calls, most Scheme programmers will instead
@ -61,6 +73,7 @@ with the output port; when the function returns, the port is closed.
@examples[ @examples[
#:eval io-eval #:eval io-eval
(call-with-output-file "data" (call-with-output-file "data"
#:exists 'truncate
(lambda (out) (lambda (out)
(display "hello" out))) (display "hello" out)))
(call-with-input-file "data" (call-with-input-file "data"
@ -107,7 +120,7 @@ with the output port; when the function returns, the port is closed.
@;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@item{@bold{Process Pipes:} The @scheme[process] function runs a new @item{@bold{Process Pipes:} The @scheme[subprocess] function runs a new
process at the OS level and returns ports that correspond to the process at the OS level and returns ports that correspond to the
subprocess's stdin, stdout, and stderr. (The first three arguments subprocess's stdin, stdout, and stderr. (The first three arguments
can be certain kinds of existing ports to connect directly to the can be certain kinds of existing ports to connect directly to the
@ -207,6 +220,8 @@ ways to print an instance of a built-in value:
} }
Here are some examples using each:
@twocolumn[ @twocolumn[
@interaction[ @interaction[
@ -243,8 +258,8 @@ text. In the format string supplied to @scheme[printf], @litchar{~a}
(deliver "John" "string") (deliver "John" "string")
] ]
An advantage of @scheme[write] is that many forms of data can be An advantage of @scheme[write], as opposed to @scheme[display], is
read back in using @scheme[read]. that many forms of data can be read back in using @scheme[read].
@examples[ @examples[
#:eval io-eval #:eval io-eval
@ -258,8 +273,56 @@ read back in using @scheme[read].
] ]
@; ---------------------------------------------------------------------- @; ----------------------------------------------------------------------
@subsection{Serialization} @section{Datatypes and Serialization}
New datatypes created by @scheme[define-struct] by default
@scheme[write] either using @schemeresultfont{#<....>} notation (for
opaque structure types) or using @schemeresultfont{#(....)} vector
notation (for transparent structure types). In neither can can the
result be read back in as an instance of the structure type.
@examples[
(define-struct posn (x y))
(write (make-posn 1 2))
(define-values (in out) (make-pipe))
(write (make-posn 1 2) out)
(read in)
]
@interaction[
(define-struct posn (x y) #:transparent)
(write (make-posn 1 2))
(define-values (in out) (make-pipe))
(write (make-posn 1 2) out)
(define v (read in))
v
(posn? v)
(vector? v)
]
The @scheme[define-serializable-struct] form defines a structure type
that can be @scheme[serialize]d to a value that can be printed using
@scheme[write] and restored via @scheme[read]. The @scheme[serialize]d
result can be @scheme[deserialize]d to get back an instance of the
original structure type. The serialization form an functions are
provided by the @schememodname[scheme/serialize] library.
@examples[
(require scheme/serialize)
(define-serializable-struct posn (x y) #:transparent)
(deserialize (serialize (make-posn 1 2)))
(write (serialize (make-posn 1 2)))
(define-values (in out) (make-pipe))
(write (serialize (make-posn 1 2)) out)
(deserialize (read in))
]
In addition to the names bound by @scheme[define-struct],
@scheme[define-serializable-struct] binds an identifier with
deserialization information, and it automatically @scheme[provide]s
the deserialization identifier from a module context. This
deserialization identifier is accessed reflectively when a value is
deserialized.
@; ---------------------------------------------------------------------- @; ----------------------------------------------------------------------
@section{Bytes versus Characters} @section{Bytes versus Characters}

View File

@ -148,7 +148,7 @@
[more (apply tt more)])) [more (apply tt more)]))
(define cppi cpp) (define cppi cpp)
(define cppdef (lambda (x) (as-cpp-defn x (as-index (cpp x))))) (define cppdef (lambda (x) (as-cpp-defn x (cpp x))))
(define *var italic) (define *var italic)
(define mzc (exec "mzc")) (define mzc (exec "mzc"))

View File

@ -60,7 +60,7 @@ escapes:
@itemize{ @itemize{
@item{@FmtMark{n} or @FmtMark{%} prints a newline} @item{@FmtMark{n} or @FmtMark{%} prints a newline, the same as @litchar{\n}}
@item{@FmtMark{a} or @FmtMark{A} @scheme[display]s the next argument @item{@FmtMark{a} or @FmtMark{A} @scheme[display]s the next argument
among the @scheme[v]s} among the @scheme[v]s}
@ -92,12 +92,13 @@ escapes:
@item{@FmtMark{~} prints a tilde.} @item{@FmtMark{~} prints a tilde.}
@item{@FmtMark{}@nonterm{w}, where @nonterm{w} is a whitespace character, @item{@FmtMark{}@nonterm{w}, where @nonterm{w} is a whitespace
skips characters in @scheme[form] until a non-whitespace character (see @scheme[char-whitespace?]), skips characters in
character is encountered or until a second end-of-line is @scheme[form] until a non-whitespace character is encountered or
encountered (whichever happens first). An end-of-line is either until a second end-of-line is encountered (whichever happens
@scheme[#\return], @scheme[#\newline], or @scheme[#\return] followed first). On all platforms, an end-of-line can be @scheme[#\return],
immediately by @scheme[#\newline] (on all platforms).} @scheme[#\newline], or @scheme[#\return] followed immediately by
@scheme[#\newline].}
} }