-
Notifications
You must be signed in to change notification settings - Fork 2
/
clos-dumper.lisp
253 lines (201 loc) · 8.62 KB
/
clos-dumper.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
(in-package :mt)
#| ######################################################################
Structure dumper.
Copyright (c) 1994-2011 Michael Travers
Permission is given to use and modify this code
as long as the copyright notice is preserved.
Send questions, comments, and fixes to [email protected].
-------------------------------------------------------------------------
What this does: Saves a structure of CLOS objects (and other lisp objects) as an executable
form that recreates the structure when loaded.
How it works: Recursively calls DUMP-FORM on an object. The default DUMP-FORM for CLOS
objects calls SLOT-DUMP-FORMS, which should return an init list that will be passed
to make-instance when the object is re-created. The macro MAKE-SLOT-DUMPER is the
easiest way to specify which slots of a class are to be dumped.
Use the function DUMP-TO-FILE to create a dump file which can be read back with LOAD.
Objects are only dumped once, so "tangled trees" are handled properly. But
circularities are NOT handled. If you have back-pointers in your structure they should
not be dumped, instead arrange to have them set up automatically when the object is
initialized. For example, if you were saving a structure of nested views, you would
dump either the VIEW-CONTAINER slot or the VIEW-SUBVIEWS slot, but not both.
History:
1/10/97 18:57 Revived, fixed a sequence problem for MCL4
###################################################################### |#
(export '(dump dump-form slot-dump-forms dump-to-file dump-to-stream dump-to-string dump-copy
make-slot-dumper *dump-top-thing*))
(defvar *dump-ht*) ; Bound
(defvar *prepass* nil)
(defvar *prelude-vars*)
(defvar *dumper-gensym-counter*)
(defvar *dump-top-thing*) ;bound to toplevel object; dump methods can examine this to change their behavior.
;;; a short main name makes dumps smaller
(defvar *dump-temp-package* (make-package "MTDT" :nicknames '("DUMP-TEMP")))
(defun dump-var (var)
`(setq ,var (dump (symbol-value var))))
(defun dump (thing)
(let ((*dump-top-thing* thing)
(*dump-ht* (make-hash-table :test 'eq))
(*prelude-vars* nil)
(*dumper-gensym-counter* 0))
(let ((*prepass* t))
(dump-form thing))
(let ((form (dump-form thing)))
(values `(let ,*prelude-vars* ,form)
*dump-ht*))))
;;; There's no common superclass for standard objects and defstructs, so we kludge it to avoid code duplication
(defmethod dump-form :around ((r t))
(if (or (typep r 'standard-object)
(typep r 'structure-object))
(if *prepass*
(let ((hash-result (gethash r *dump-ht*)))
(if hash-result
(case (car hash-result)
(:one-ref
(setf (gethash r *dump-ht*) (cons :multi-ref (cdr hash-result)))
(cdr hash-result))
(:in-progress (warn "losing circularity on ~A" r)))
;; this could be a case clause but MCL has a bug
(progn
(setf (gethash r *dump-ht*) '(:in-progress))
(let ((result (call-next-method)))
(setf (gethash r *dump-ht*) (cons :one-ref result))
result))))
;; Second pass
(let ((hash-result (gethash r *dump-ht*)))
(case (car hash-result)
;; Just one ref, no prob
(:one-ref (call-next-method))
;; Mult ref, first time
(:multi-ref
(let ((sym (intern (concatenate 'string "T" (princ-to-string (incf *dumper-gensym-counter*)))
*dump-temp-package*)))
(push sym *prelude-vars*)
(setf (gethash r *dump-ht*)
`(:in-progress ,sym))
(prog1
`(setf ,sym ,(call-next-method))
(setf (gethash r *dump-ht*)
`(:second-ref ,sym)))))
(:in-progress
(error ";;; shit this can't work"))
(:second-ref
(cadr hash-result))
(t (error "ugh")))))
(call-next-method)))
;;; Default is to dump as self
(defmethod dump-form ((d t))
d)
;;; Perhaps this should be default
(defmethod dump-form ((d symbol))
`(quote ,d))
(defmethod dump-form ((d null))
nil)
(defmethod dump-form ((l list))
(cond ;; nil handled by separate method
((eq 'quote (car l))
l)
((null (cdr (last l)))
`(list ,@(mapcar #'dump-form l))) ; regular lists
(t
`(cons ,(dump-form (car l)) ,(dump-form (cdr l)))))) ; dotted lists
;;; Pretty inefficient, since it translates sequences to/from lists
(defmethod dump-form ((s sequence))
`(rebuild-sequence ',(type-of s) ,(length s) ,@(mapcar #'dump-form (coerce s 'list))))
(defmethod dump-form ((d standard-object))
`(make-instance ',(class-name (class-of d)) ,@(slot-dump-forms d)))
(defmethod dump-form ((d structure-object))
`(,(symbol-conc 'make- (type-of d))
,@(slot-dump-forms d)))
(defmethod slot-dump-forms nconc ((d structure-object))
nil)
(defmethod dump-form ((ht hash-table))
`(undump-ht ',(hash-table-test ht) ',(ht-contents ht)))
(defun undump-ht (test contents)
(let ((ht (make-hash-table :test test)))
(dolist (elt contents)
(setf (gethash (car elt) ht) (cadr elt)))
ht))
(defgeneric slot-dump-forms (d)
(:method-combination nconc))
(defmethod slot-dump-forms nconc ((d standard-object))
nil)
;;; defaults to all initable slots
;;; +++ this may lose if class isn't properly initalized at macroexpand time
;;; version that can deal with unbound slots
(defmacro make-slot-dumper (class &rest slots)
#+CCL (unless slots (setf slots (ccl::class-make-instance-initargs class)))
`(defmethod slot-dump-forms nconc ((x ,class))
(mt:collecting
(dolist (spec ',slots)
(let ((slot (if (listp spec) (car spec) spec))
(default (and (listp spec) (cadr spec)))
value)
(unless (not (slot-boundp x slot))
(setq value (slot-value x slot))
(when (or (not (listp spec))
(not (equal value default)))
(mt:collect (mt:keywordize slot))
(mt:collect (dump-form value)))))))))
#| this may work better in MCL4, types are now length specific and LENGTH does not return the right thing sometimes
(defmethod dump-form ((s sequence))
`(rebuild-sequence ',(type-of s) ,(array-dimension s 0) ,@(mapcar #'dump-form (coerce s 'list))))
|#
;;; Strings dump as themselves.
(defmethod dump-form ((d string))
d)
(defun rebuild-sequence (type size &rest elements)
(let ((seq (make-sequence type size)))
(do ((rest elements (cdr rest))
(n 0 (1+ n)))
((null rest) seq)
(setf (aref seq n) (car rest)))))
;;; if an object lives in a global variable, mix this in.
(defclass globally-named-object-mixin ()
((global-name :initarg :global-name :initform nil)))
(defmethod dump-form :around ((o globally-named-object-mixin))
(with-slots (global-name) o
(if global-name
`(setf ,global-name ,(call-next-method))
(call-next-method))))
(defmethod slot-dump-forms nconc ((o globally-named-object-mixin))
`(:global-name ',(slot-value o 'global-name)))
;;; File interface
;;; Note: it would be nice to dump directly to a compiled form, but MCL doesn't
;;; have hooks for this.
(defvar *dumping-to-file* nil) ;+++ doesn't do anything...I suppose dump funs can examine
(defun dump-to-file (thing file &key (compile t) prelude (package *package*) pretty?)
(let ((*package* (if package (find-package package) *package*))
(*dumping-to-file* (pathname file)))
(with-open-file (stream file :direction :output :if-exists :supersede)
(when package (print `(in-package ,(package-name *package*)) stream))
(when prelude (print prelude stream))
(dump-to-stream thing stream :pretty? pretty?))
(when compile
(compile-file file))))
;;; *print-pretty* turns on ' notation, but of course also lards it up with whitespace. There
;;; doesn't seem to be any easy way to get one without the other
(defun dump-to-stream (object stream &key pretty?)
(let ((*print-pretty* pretty?)
(*print-miser-width* nil)
(*print-readably* t))
(prin1 (dump object) stream)))
(defun dump-to-string (object)
(with-output-to-string (s)
(dump-to-stream object s)))
;;; Create a new object EQUAL (loosely speaking) but not EQ to the argument
(defun dump-copy (object)
(eval (dump object)))
;;; SIMPLY? can be used if value is serializable without going through dump (that is, for lists of strings, numbers, etc)
(defun dump-var-to-file (var file &key (compile t) prelude (package *package*) simply?)
(let ((*package* (find-package package))
(*dumping-to-file* (pathname file)))
(with-open-file (stream file :direction :output :if-exists :supersede)
(print `(in-package ,(package-name *package*)) stream)
(print prelude stream)
(let ((*print-array* t))
(print `(setq ,var ,(if simply?
(list 'quote (symbol-value var))
(dump (symbol-value var))))
stream)))
(when compile
(compile-file file))))