/usr/share/guile/2.0/oop/goops
Edit: /usr/share/guile/2.0/oop/goops/save.scm (24542B)
;;; installed-scm-file
;;;; Copyright (C) 2000,2001,2002, 2006, 2009, 2010, 2013 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
;;;;
(define-module (oop goops save)
:use-module (oop goops internal)
:use-module (oop goops util)
:re-export (make-unbound)
:export (save-objects load-objects restore
enumerate! enumerate-component!
write-readably write-component write-component-procedure
literal? readable make-readable))
;;;
;;; save-objects ALIST PORT [EXCLUDED] [USES]
;;;
;;; ALIST ::= ((NAME . OBJECT) ...)
;;;
;;; Save OBJECT ... to PORT so that when the data is read and evaluated
;;; OBJECT ... are re-created under names NAME ... .
;;; Exclude any references to objects in the list EXCLUDED.
;;; Add a (use-modules . USES) line to the top of the saved text.
;;;
;;; In some instances, when `save-object' doesn't know how to produce
;;; readable syntax for an object, you can explicitly register read
;;; syntax for an object using the special form `readable'.
;;;
;;; Example:
;;;
;;; The function `foo' produces an object of obscure structure.
;;; Only `foo' can construct such objects. Because of this, an
;;; object such as
;;;
;;; (define x (vector 1 (foo)))
;;;
;;; cannot be saved by `save-objects'. But if you instead write
;;;
;;; (define x (vector 1 (readable (foo))))
;;;
;;; `save-objects' will happily produce the necessary read syntax.
;;;
;;; To add new read syntax, hang methods on `enumerate!' and
;;; `write-readably'.
;;;
;;; enumerate! OBJECT ENV
;;; Should call `enumerate-component!' (which takes same args) on
;;; each component object. Should return #t if the composite object
;;; can be written as a literal. (`enumerate-component!' returns #t
;;; if the component is a literal.
;;;
;;; write-readably OBJECT PORT ENV
;;; Should write a readable representation of OBJECT to PORT.
;;; Should use `write-component' to print each component object.
;;; Use `literal?' to decide if a component is a literal.
;;;
;;; Utilities:
;;;
;;; enumerate-component! OBJECT ENV
;;;
;;; write-component OBJECT PATCHER PORT ENV
;;; PATCHER is an expression which, when evaluated, stores OBJECT
;;; into its current location.
;;;
;;; Example:
;;;
;;; (write-component (car ls) `(set-car! ,ls ,(car ls)) file env)
;;;
;;; write-component is a macro.
;;;
;;; literal? COMPONENT ENV
;;;
(define-method (immediate? (o
)) #f)
(define-method (immediate? (o )) #t)
(define-method (immediate? (o )) #t)
(define-method (immediate? (o )) #t)
(define-method (immediate? (o )) #t)
(define-method (immediate? (o )) #t)
(define-method (immediate? (o )) #t)
;;; enumerate! OBJECT ENVIRONMENT
;;;
;;; Return #t if object is a literal.
;;;
(define-method (enumerate! (o ) env) #t)
(define-method (write-readably (o ) file env)
;;(goops-error "No read-syntax defined for object `~S'" o)
(write o file) ;doesn't catch bugs, but is much more flexible
)
;;;
;;; Readables
;;;
(define readables (make-weak-key-hash-table 61))
(define-macro (readable exp)
`(make-readable ,exp ',(copy-tree exp)))
(define (make-readable obj expr)
(hashq-set! readables obj expr)
obj)
(define (readable-expression obj)
`(readable ,(hashq-ref readables obj)))
;; FIXME: if obj is nil or false, this can return a false value. OTOH
;; usually this is only for non-immediates.
(define (readable? obj)
(hashq-ref readables obj))
;;;
;;; Writer helpers
;;;
(define (write-component-procedure o file env)
"Return #f if circular reference"
(cond ((immediate? o) (write o file) #t)
((readable? o) (write (readable-expression o) file) #t)
((excluded? o env) (display #f file) #t)
(else
(let ((info (object-info o env)))
(cond ((not (binding? info)) (write-readably o file env) #t)
((not (eq? (visiting info) #:defined)) #f) ;forward reference
(else (display (binding info) file) #t))))))
;;; write-component OBJECT PATCHER FILE ENV
;;;
(define-macro (write-component object patcher file env)
`(or (write-component-procedure ,object ,file ,env)
(begin
(display #f ,file)
(add-patcher! ,patcher ,env))))
;;;
;;; Strings
;;;
(define-method (enumerate! (o ) env) #f)
;;;
;;; Vectors
;;;
(define-method (enumerate! (o ) env)
(or (not (vector? o))
(let ((literal? #t))
(array-for-each (lambda (o)
(if (not (enumerate-component! o env))
(set! literal? #f)))
o)
literal?)))
(define-method (write-readably (o ) file env)
(if (not (vector? o))
(write o file)
(let ((n (vector-length o)))
(if (zero? n)
(display "#()" file)
(let ((not-literal? (not (literal? o env))))
(display (if not-literal?
"(vector "
"#(")
file)
(if (and not-literal?
(literal? (vector-ref o 0) env))
(display #\' file))
(write-component (vector-ref o 0)
`(vector-set! ,o 0 ,(vector-ref o 0))
file
env)
(do ((i 1 (+ 1 i)))
((= i n))
(display #\space file)
(if (and not-literal?
(literal? (vector-ref o i) env))
(display #\' file))
(write-component (vector-ref o i)
`(vector-set! ,o ,i ,(vector-ref o i))
file
env))
(display #\) file))))))
;;;
;;; Arrays
;;;
(define-method (enumerate! (o ) env)
(enumerate-component! (shared-array-root o) env))
(define (make-mapper array)
(let* ((n (array-rank array))
(indices (reverse (if (<= n 11)
(list-tail '(t s r q p n m l k j i) (- 11 n))
(let loop ((n n)
(ls '()))
(if (zero? n)
ls
(loop (- n 1)
(cons (gensym "i") ls))))))))
`(lambda ,indices
(+ ,(shared-array-offset array)
,@(map (lambda (ind dim inc)
`(* ,inc ,(if (pair? dim) `(- ,ind ,(car dim)) ind)))
indices
(array-dimensions array)
(shared-array-increments array))))))
(define (write-array prefix o not-literal? file env)
(letrec ((inner (lambda (n indices)
(if (not (zero? n))
(let ((el (apply array-ref o
(reverse (cons 0 indices)))))
(if (and not-literal?
(literal? el env))
(display #\' file))
(write-component
el
`(array-set! ,o ,el ,@indices)
file
env)))
(do ((i 1 (+ 1 i)))
((= i n))
(display #\space file)
(let ((el (apply array-ref o
(reverse (cons i indices)))))
(if (and not-literal?
(literal? el env))
(display #\' file))
(write-component
el
`(array-set! ,o ,el ,@indices)
file
env))))))
(display prefix file)
(let loop ((dims (array-dimensions o))
(indices '()))
(cond ((null? (cdr dims))
(inner (car dims) indices))
(else
(let ((n (car dims)))
(do ((i 0 (+ 1 i)))
((= i n))
(if (> i 0)
(display #\space file))
(display prefix file)
(loop (cdr dims) (cons i indices))
(display #\) file))))))
(display #\) file)))
(define-method (write-readably (o ) file env)
(let ((root (shared-array-root o)))
(cond ((literal? o env)
(if (not (vector? root))
(write o file)
(begin
(display #\# file)
(display (array-rank o) file)
(write-array #\( o #f file env))))
((binding? root env)
(display "(make-shared-array " file)
(if (literal? root env)
(display #\' file))
(write-component root
(goops-error "write-readably(): internal error")
file
env)
(display #\space file)
(display (make-mapper o) file)
(for-each (lambda (dim)
(display #\space file)
(display dim file))
(array-dimensions o))
(display #\) file))
(else
(display "(list->uniform-array " file)
(display (array-rank o) file)
(display " '() " file)
(write-array "(list " o #f file env)))))
;;;
;;; Pairs
;;;
;;; These methods have more complex structure than is required for
;;; most objects, since they take over some of the logic of
;;; `write-component'.
;;;
(define-method (enumerate! (o ) env)
(let ((literal? (enumerate-component! (car o) env)))
(and (enumerate-component! (cdr o) env)
literal?)))
(define-method (write-readably (o ) file env)
(let ((proper? (let loop ((ls o))
(or (null? ls)
(and (pair? ls)
(not (binding? (cdr ls) env))
(loop (cdr ls))))))
(1? (or (not (pair? (cdr o)))
(binding? (cdr o) env)))
(not-literal? (not (literal? o env)))
(infos '())
(refs (ref-stack env)))
(display (cond ((not not-literal?) #\()
(proper? "(list ")
(1? "(cons ")
(else "(cons* "))
file)
(if (and not-literal?
(literal? (car o) env))
(display #\' file))
(write-component (car o) `(set-car! ,o ,(car o)) file env)
(do ((ls (cdr o) (cdr ls))
(prev o ls))
((or (not (pair? ls))
(binding? ls env))
(if (not (null? ls))
(begin
(if (not not-literal?)
(display " ." file))
(display #\space file)
(if (and not-literal?
(literal? ls env))
(display #\' file))
(write-component ls `(set-cdr! ,prev ,ls) file env)))
(display #\) file))
(display #\space file)
(set! infos (cons (object-info ls env) infos))
(push-ref! ls env) ;*fixme* optimize
(set! (visiting? (car infos)) #t)
(if (and not-literal?
(literal? (car ls) env))
(display #\' file))
(write-component (car ls) `(set-car! ,ls ,(car ls)) file env)
)
(for-each (lambda (info)
(set! (visiting? info) #f))
infos)
(set! (ref-stack env) refs)
))
;;;
;;; Objects
;;;
;;; Doesn't yet handle unbound slots
;; Don't export this function! This is all very temporary.
;;
(define (get-set-for-each proc class)
(for-each (lambda (slotdef g-n-s)
(let ((g-n-s (cddr g-n-s)))
(cond ((integer? g-n-s)
(proc (standard-get g-n-s) (standard-set g-n-s)))
((not (memq (slot-definition-allocation slotdef)
'(#:class #:each-subclass)))
(proc (car g-n-s) (cadr g-n-s))))))
(class-slots class)
(slot-ref class 'getters-n-setters)))
(define (access-for-each proc class)
(for-each (lambda (slotdef g-n-s)
(let ((g-n-s (cddr g-n-s))
(a (slot-definition-accessor slotdef)))
(cond ((integer? g-n-s)
(proc (slot-definition-name slotdef)
(and a (generic-function-name a))
(standard-get g-n-s)
(standard-set g-n-s)))
((not (memq (slot-definition-allocation slotdef)
'(#:class #:each-subclass)))
(proc (slot-definition-name slotdef)
(and a (generic-function-name a))
(car g-n-s)
(cadr g-n-s))))))
(class-slots class)
(slot-ref class 'getters-n-setters)))
(define-macro (restore class slots . exps)
"(restore CLASS (SLOT-NAME1 ...) EXP1 ...)"
`(let ((o ((@@ (oop goops) %allocate-instance) ,class '())))
(for-each (lambda (name val)
(slot-set! o name val))
',slots
(list ,@exps))
o))
(define-method (enumerate! (o