1 prototypes define: #Segment
3 {#root. "The root object to trace from."
4 #imports -> ExtensibleArray new. "The symbol table for the output."
5 #relocations -> ExtensibleArray new. "The relocations for the output."
6 #output -> ExtensibleArray new. "The output representations."
7 #objects -> IdentityDictionary new. "Maps from object to it's particular represenation."}.
8 "A Segment builds up a definition of a group of objects that can be extracted
9 and serialized in binary form and then reloaded using a group of linking
12 Segment traits define: #Representation
13 "Object representation in the Segment's output."
15 {#offset -> 0. "The allocated offset for the representation in the output."}.
17 Segment traits define: #Object &parents: {Segment Representation}
18 &slots: {#map "The map which defines the structure.".
19 #slotValues -> ExtensibleArray new "The values of various slots in the object."}.
21 rep@(Segment Object traits) new
22 [rep clone `>> [slotValues := rep slotValues new. ]].
24 rep@(Segment Object traits) newFrom: obj
25 [rep new `>> [slotValues addAllLast: obj slotValues. ]].
27 Segment traits define: #Array &parents: {Segment Object}
28 &slots: {#elements -> ExtensibleArray new}.
30 a@(Segment Array traits) new
31 [resend `>> [elements := a elements new. ]].
33 a@(Segment Array traits) newFrom: obj
34 [resend `>> [elements addAllLast: obj. ]].
36 Segment traits define: #ByteArray &parents: {Segment Array}.
38 Segment traits define: #Method &parents: {Segment Representation}
39 &slots: {#roles -> {} "Roles is a collection containing, for each argument position, an array of the objects upon which a role must be defined."}.
41 Segment Method traits define: #Primitive &parents: {Segment Method}
42 &slots: {#index "The index in the primitive table of the repspective primitive"}.
44 rep@(Segment Method Primitive traits) newFrom: obj
45 [rep clone `>> [selector := obj selector. index := obj index. ]].
47 Segment Method traits define: #Compiled &parents: {Segment Method}
48 &slots: {#sourceCode "The source code which generated the particular compiled method."}.
50 rep@(Segment Method Compiled traits) newFrom: obj
53 [selector := obj selector.
54 roles := ((0 below: obj arity) collect: [| :_ | ExtensibleArray new]). ]
57 "TODO: Handle Closure objects."
59 Segment traits define: #Map &parents: {Segment Representation}
60 &slots: {#delegates -> ExtensibleArray new.
63 rep@(Segment Map traits) new
64 [resend `>> [delegates := rep delegates new. slots := rep slots new. ]].
66 rep@(Segment Map traits) newFrom: obj
70 (obj delegateNames with: obj delegates do:
71 [| :slot :val | newRep delegates addLast: slot -> val]).
72 rep slots := (Array newSize: obj slotCount).
73 0 below: obj slotTable size by: 2 do:
75 (obj slotTable at: index)
78 offset := (obj slotTable at: index + 1) >> 2 - 2.
79 newRep slots at: offset put: name]]. ]
82 Segment traits define: #Import.
83 "Imported values that must be evaluted when the image segment is loaded."
84 [| :sym | Segment traits define: sym &parents: {Segment Import}]
85 for: #{#Path. #Symbol}.
87 Segment traits define: #Relocation
88 "Relocation against Imports"
90 {#import. "The imported value this relocation takes as its value."
91 #object. "The object being relocated against."
92 #slot "The slot whose value needs to be relocated."}.
94 s@(Segment traits) newOn: root
98 imports := s imports new.
99 relocations := s relocations new.
100 output := s output new.
101 objects := s objects new. ]
104 s@(Segment traits) represents: obj
105 "Whether the Segment has recorded a representation for the particular object."
106 [s objects includesKey: obj].
108 s@(Segment traits) represent: obj as: rep
109 "Ensures that the Segment has a representation of the object made from the given
111 [s objects at: obj ifAbsentPut: [rep newFrom: obj]].
113 s@(Segment traits) trace: obj@(Root traits)
114 "By default, trace objects as any standard thing with named slots, and then
117 s represent: obj as: s Object.
121 s@(Segment traits) trace: obj@(Array traits)
123 s represent: obj as: s Array.
127 s@(Segment traits) trace: obj@(ByteArray traits)
129 s represent: obj as: s ByteArray.
133 s@(Segment traits) trace: obj@(Map traits)
135 s represent: obj as: s Map.
138 s@(Segment traits) trace: obj@(PrimitiveMethod traits)
140 s represent: obj as: s Method Primitive.
143 s@(Segment traits) trace: obj@(CompiledMethod traits)
145 s represent: obj as: s Method Compiled.
148 s@(Segment traits) trace
151 s@(Segment traits) writeOn: out
155 s@(Segment traits) readFrom: in
159 s@(Segment traits) linkContents
163 prototypes define: #SavedSegment &parents: {Segment}
164 &slots: {#file -> File}.
166 s@(Segment traits) saveToFile: filename
168 (File newNamed: filename &mode: CreateWrite) sessionDo:
169 [| :f | s writeOn: f writer]
172 s@(SavedSegment traits) saveToFile: filename
173 [s file := (s file newNamed: filename)].
175 s@(Segment traits) loadFromFile: filename
176 "Create a new SavedSegment for the file, and load and answer it."
177 [SavedSegment clone `>> [| :ss | file := (ss file newNamed: filename). load. ]].
179 s@(SavedSegment traits) save
180 "Write the segment into its file."
181 [s file sessionDo: [| :f | s writeOn: f writer] &mode: s file Write].
183 s@(SavedSegment traits) load
184 "(Re-)load the segment from its file."
185 [s file sessionDo: [| :f | s readFrom: f reader] &mode: s file Read].