vile / scheme / goops_redefinition.v
213 lines · 208 sloc · 7.13 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3fn goops_redefinition_preflight_native(mut vm VM, args []Value) !Value {
4 check_arity('class-redefinition preflight', args.len, 2, 2)!
5 old := vm.checked_goops_class(args[0])!
6 fresh := vm.checked_goops_class(args[1])!
7 vm.goops_require_mutation(fresh.allocation_owner)!
8 mut pending := [old]
9 mut seen := map[voidptr]bool{}
10 defer {
11 unsafe {
12 pending.free()
13 seen.free()
14 }
15 }
16 for pending.len > 0 {
17 class := pending.pop()
18 if seen[voidptr(class)] { continue }
19 seen[voidptr(class)] = true
20 vm.goops_require_mutation(class.allocation_owner)!
21 for child in vm.goops_direct_subclasses(class) { pending << child }
22 // Public MOP links may add traversal targets, while the private
23 // registry also covers dependencies omitted by a public overwrite.
24 for child in goops_public_reference_values(class.direct_subclasses) {
25 pending << child.goops_class_pointer()
26 }
27 }
28 vm.goops_prune_references()
29 mut node := vm.goops.references
30 for !isnil(node) {
31 if node.kind == .method {
32 if value := node.reference.get() {
33 method := value.goops_method_pointer()
34 if method.installed {
35 mut affected := seen[voidptr(method.rest_class)] || voidptr(method.rest_class) == voidptr(fresh)
36 for class in method.specializers {
37 if seen[voidptr(class)] || voidptr(class) == voidptr(fresh) {
38 affected = true
39 break
40 }
41 }
42 if affected {
43 vm.goops_require_mutation(method.allocation_owner)!
44 generic := vm.checked_goops_generic(method.generic)!
45 vm.goops_require_mutation(generic.allocation_owner)!
46 }
47 }
48 }
49 }
50 node = node.next
51 }
52 return args[0]
53}
54
55fn (vm &VM) goops_is_accessor_method(method &GoopsMethod) bool {
56 return !isnil(method.type_class)
57 && goops_cpl_rank(method.type_class, vm.goops_protocol_class('<accessor-method>')) >= 0
58}
59
60fn (mut vm VM) goops_redefine_class(old_value Value, fresh_value Value) !Value {
61 mut old := vm.checked_goops_class(old_value)!
62 mut fresh := vm.checked_goops_class(fresh_value)!
63 vm.goops_require_mutation(old.allocation_owner)!
64 vm.goops_require_mutation(fresh.allocation_owner)!
65 if voidptr(old) == voidptr(fresh) { return old_value }
66 if !goops_same_representation_family(old.representation, fresh.representation) {
67 return failure('class redefinition across native representation families is not implemented')
68 }
69 if old.version == ~u64(0) { return failure('class layout version exhausted') }
70 if goops_cpl_rank(fresh, old) >= 0 { return failure('circular class redefinition') }
71 // Validate every affected publication before changing a header. User class
72 // construction and descendant hooks still run on the Scheme machine.
73 _ = goops_redefinition_preflight_native(mut vm, [old_value, fresh_value])!
74 mut methods := []&GoopsMethod{}
75 mut retired_accessors := []&GoopsMethod{}
76 defer {
77 unsafe {
78 methods.free()
79 retired_accessors.free()
80 }
81 }
82 mut node := vm.goops.references
83 for !isnil(node) {
84 if node.kind == .method {
85 if value := node.reference.get() {
86 method := value.goops_method_pointer()
87 if method.installed {
88 mut belongs_old := false
89 mut belongs_fresh := voidptr(method.rest_class) == voidptr(fresh)
90 for class in method.specializers {
91 if voidptr(class) == voidptr(old) { belongs_old = true }
92 if voidptr(class) == voidptr(fresh) { belongs_fresh = true }
93 }
94 if belongs_old && vm.goops_is_accessor_method(method) {
95 retired_accessors << method
96 }
97 if belongs_fresh { methods << method }
98 }
99 }
100 }
101 node = node.next
102 }
103 // Detach the CPLs before retargeting self pointers: historical layouts and
104 // escaped slot definitions must retain their exact previous storage.
105 mut previous_cpl := old.cpl.clone()
106 mut next_cpl := fresh.cpl.clone()
107 mut history := old.history.clone()
108 mut direct_methods := if isnil(old.direct_methods) {
109 vm.goops_direct_methods(old)
110 } else {
111 goops_public_reference_values(old.direct_methods)
112 }
113 fresh_methods := if isnil(fresh.direct_methods) {
114 vm.goops_direct_methods(fresh)
115 } else {
116 goops_public_reference_values(fresh.direct_methods)
117 }
118 for value in fresh_methods {
119 mut found := false
120 for previous in direct_methods {
121 if eqv(previous, value) {
122 found = true
123 break
124 }
125 }
126 if !found { direct_methods << value }
127 }
128 previous_supers := old.supers
129 next_supers := fresh.supers
130
131 history << GoopsClassHistory{ version: old.version, snapshot: fresh }
132 for mut class in previous_cpl { if voidptr(class) == voidptr(old) { class = fresh } }
133 for mut class in next_cpl { if voidptr(class) == voidptr(fresh) { class = old } }
134 old_header := *old
135 unsafe {
136 *old = *fresh
137 *fresh = old_header
138 }
139 old.meta_migrating = old_header.meta_migrating
140 old.cpl = next_cpl
141 old.version = old_header.version + 1
142 old.history = history
143 old.registered = true
144 old.retired = false
145 old.live_class = unsafe { nil }
146 old.direct_subclasses = old_header.direct_subclasses
147 old.direct_methods = goops_public_references(direct_methods)
148 fresh.direct_subclasses = goops_copy_public_references(old_header.direct_subclasses)
149 fresh.direct_methods = goops_copy_public_references(old_header.direct_methods)
150 for parent in previous_supers { goops_public_remove(parent.direct_subclasses, old_value) }
151 for parent in next_supers {
152 goops_public_remove(parent.direct_subclasses, fresh_value)
153 goops_public_add(parent.direct_subclasses, old_value)
154 }
155 fresh.cpl = previous_cpl
156 fresh.retired = true
157 fresh.live_class = old
158 fresh.redefined = old
159 for definition in old.definitions {
160 mut data := definition.goops_slot_pointer()
161 data.class = old
162 }
163 for definition in fresh.definitions {
164 mut data := definition.goops_slot_pointer()
165 data.class = fresh
166 }
167 for mut method in retired_accessors {
168 mut generic := vm.checked_goops_generic(method.generic)!
169 for index, candidate in generic.methods {
170 if voidptr(candidate) == voidptr(method) {
171 generic.methods.delete(index)
172 break
173 }
174 }
175 goops_unpublish_method_fields(method)
176 method.installed = false
177 method.generation++
178 vm.goops_method_changed(method, method.generic)
179 generic.generation++
180 }
181
182 for mut method in methods {
183 goops_unpublish_method_fields(method)
184 method.specializers = method.specializers.clone()
185 for mut class in method.specializers { if voidptr(class) == voidptr(fresh) { class = old } }
186 if voidptr(method.rest_class) == voidptr(fresh) { method.rest_class = old }
187 goops_publish_method_fields(method)
188 method.generation++
189 vm.goops_method_changed(method, method.generic)
190 mut generic := vm.checked_goops_generic(method.generic)!
191 generic.generation++
192 }
193 // A native callback can redefine this class while an outer instance update
194 // hook is active. Pin that instance to its actual stored layout until the
195 // migration scope exits; nested reads must never use new field indices.
196 for machine in vm.active {
197 mut scope := machine.goops_migrations
198 for !isnil(scope) {
199 object := scope.state.object.goops_layout()
200 if voidptr(object.class) == voidptr(old) && object.version != old.version {
201 for entry in old.history {
202 if entry.version == object.version {
203 scope.state.object.goops_pin_layout(entry.snapshot, object.migrating)
204 break
205 }
206 }
207 }
208 scope = scope.previous
209 }
210 }
211 vm.goops.has_redefinitions = true
212 return old_value
213}
214