| 1 | module scheme |
| 2 | |
| 3 | // A class has two independent layouts: version describes its instances, |
| 4 | // whereas meta_layout_version describes the class object's own fields. |
| 5 | struct GoopsLayout { |
| 6 | class &GoopsClass = unsafe { nil } |
| 7 | version u64 |
| 8 | migrating bool |
| 9 | allocation_owner voidptr |
| 10 | } |
| 11 | |
| 12 | fn goops_same_representation_family(a GoopsRepresentation, b GoopsRepresentation) bool { |
| 13 | return a == b || (a in [.object, .method] && b in [.object, .method]) |
| 14 | || (a in [.generic, .generic_setter, .accessor] |
| 15 | && b in [.generic, .generic_setter, .accessor]) |
| 16 | } |
| 17 | |
| 18 | fn (value Value) goops_copy_flag() bool { |
| 19 | return match value.goops_kind() { |
| 20 | .goops_class { value.goops_class_pointer().migration_copy } |
| 21 | .goops_method { value.goops_method_pointer().migration_copy } |
| 22 | .goops_slot { value.goops_slot_pointer().migration_copy } |
| 23 | .procedure { |
| 24 | g := value.goops_layout_generic() |
| 25 | !isnil(g) && g.migration_copy |
| 26 | } |
| 27 | else { false } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | fn (value Value) goops_set_copy_flag(flag bool) { |
| 32 | match value.goops_kind() { |
| 33 | .goops_class { |
| 34 | mut o := value.goops_class_pointer() |
| 35 | o.migration_copy = flag |
| 36 | } |
| 37 | .goops_method { |
| 38 | mut o := value.goops_method_pointer() |
| 39 | o.migration_copy = flag |
| 40 | } |
| 41 | .goops_slot { |
| 42 | mut o := value.goops_slot_pointer() |
| 43 | o.migration_copy = flag |
| 44 | } |
| 45 | .procedure { |
| 46 | mut o := value.goops_layout_generic() |
| 47 | if !isnil(o) { o.migration_copy = flag } |
| 48 | } |
| 49 | else {} |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | fn (value Value) goops_layout_generic() &GoopsGeneric { |
| 54 | if value.tag != .procedure { return unsafe { nil } } |
| 55 | p := value.procedure_pointer() |
| 56 | if p.mode in [.goops_accessor, .goops_generic_setter] { |
| 57 | return p.env.slots[0].goops_layout_generic() |
| 58 | } |
| 59 | if p.mode !in [.goops_generic, .goops_make] { return unsafe { nil } } |
| 60 | return p.env.slots[0].goops_generic_data_pointer() |
| 61 | } |
| 62 | |
| 63 | fn (value Value) goops_layout() GoopsLayout { |
| 64 | match value.goops_kind() { |
| 65 | .goops_instance { |
| 66 | o := value.goops_instance_pointer() |
| 67 | return GoopsLayout{ class: o.class, version: o.layout_version, migrating: o.migrating, allocation_owner: o.physical_owner() } |
| 68 | } |
| 69 | .goops_class { |
| 70 | o := value.goops_class_pointer() |
| 71 | return GoopsLayout{ class: o.meta, version: o.meta_layout_version, migrating: o.meta_migrating, allocation_owner: o.allocation_owner } |
| 72 | } |
| 73 | .goops_method { |
| 74 | o := value.goops_method_pointer() |
| 75 | return GoopsLayout{ class: o.type_class, version: o.layout_version, migrating: o.migrating, allocation_owner: o.allocation_owner } |
| 76 | } |
| 77 | .goops_slot { |
| 78 | o := value.goops_slot_pointer() |
| 79 | return GoopsLayout{ class: o.type_class, version: o.layout_version, migrating: o.migrating, allocation_owner: o.allocation_owner } |
| 80 | } |
| 81 | .procedure { |
| 82 | o := value.goops_layout_generic() |
| 83 | if !isnil(o) { |
| 84 | return GoopsLayout{ class: o.type_class, version: o.layout_version, migrating: o.migrating, allocation_owner: o.allocation_owner } |
| 85 | } |
| 86 | } |
| 87 | else {} |
| 88 | } |
| 89 | return GoopsLayout{} |
| 90 | } |
| 91 | |
| 92 | fn (value Value) goops_pin_layout(class &GoopsClass, migrating bool) { |
| 93 | match value.goops_kind() { |
| 94 | .goops_instance { |
| 95 | mut o := value.goops_instance_pointer() |
| 96 | o.class = class |
| 97 | o.migrating = migrating |
| 98 | } |
| 99 | .goops_class { |
| 100 | mut o := value.goops_class_pointer() |
| 101 | o.meta = class |
| 102 | o.meta_migrating = migrating |
| 103 | } |
| 104 | .goops_method { |
| 105 | mut o := value.goops_method_pointer() |
| 106 | o.type_class = class |
| 107 | o.migrating = migrating |
| 108 | } |
| 109 | .goops_slot { |
| 110 | mut o := value.goops_slot_pointer() |
| 111 | o.type_class = class |
| 112 | o.migrating = migrating |
| 113 | } |
| 114 | .procedure { |
| 115 | mut o := value.goops_layout_generic() |
| 116 | o.type_class = class |
| 117 | o.migrating = migrating |
| 118 | } |
| 119 | else { panic('invalid migration object') } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Allocate-instance remains a public callback. Keep its public field defaults |
| 124 | // until change-class visits each field; initializers can observe this order. |
| 125 | |
| 126 | fn (vm &VM) goops_check_retained_classes(owner voidptr, classes []&GoopsClass) ! { |
| 127 | for class in classes { |
| 128 | if !vm.goops_retains_owner(owner, class.allocation_owner) { |
| 129 | return failure('class dependency crosses an arena boundary') |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Default change-class copies public field data without replaying publication |
| 135 | // APIs on canonical methods, graph endpoints, or effective slot definitions. |
| 136 | // User virtual setters still run through the ordinary public slot-set! path. |
| 137 | fn goops_copy_native_slot_native(mut vm VM, args []Value) !Value { |
| 138 | check_arity('copy-native-slot!', args.len, 4, 4)! |
| 139 | copy := args[0] |
| 140 | definition := vm.checked_goops_slot(args[1])! |
| 141 | field := definition.native_field |
| 142 | if field == .none || copy.goops_kind() == .goops_instance { return boolean(false) } |
| 143 | value := args[2] |
| 144 | source := args[3] |
| 145 | layout := copy.goops_layout() |
| 146 | vm.goops_require_mutation(layout.allocation_owner)! |
| 147 | match field { |
| 148 | .class_cpl { |
| 149 | mut c := vm.checked_goops_class(copy)! |
| 150 | classes := vm.goops_retained_class_prefix(c.allocation_owner, value, value.list_length()!)! |
| 151 | for i, class in classes { |
| 152 | for j in 0 .. i { |
| 153 | if voidptr(class) == voidptr(classes[j]) { |
| 154 | return failure('duplicate class in precedence list') |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | // A copied CPL starts with the public source identity, which is |
| 159 | // retained at commit. Do not substitute the temporary copy. |
| 160 | c.cpl = classes |
| 161 | } |
| 162 | .class_slots { |
| 163 | mut c := vm.checked_goops_class(copy)! |
| 164 | values := proper_list_values(value)! |
| 165 | mut slots := []GoopsSlot{cap: values.len} |
| 166 | for item in values { |
| 167 | d := vm.goops_retained_slot(c.allocation_owner, item)! |
| 168 | if d.slot.metadata.name.tag != .symbol { |
| 169 | return failure('effective slot needs a symbol name') |
| 170 | } |
| 171 | for prior in slots { |
| 172 | if prior.name == d.slot.name { |
| 173 | return failure('duplicate effective slot ${d.slot.name}') |
| 174 | } |
| 175 | } |
| 176 | slots << *d.slot |
| 177 | } |
| 178 | // Retargeting is deferred until the entire migration can commit. |
| 179 | c.definitions = values |
| 180 | c.slots = slots |
| 181 | } |
| 182 | .method_specializers { |
| 183 | mut method := vm.checked_goops_method(copy)! |
| 184 | specializers, rest_class := vm.goops_method_specializers(method.allocation_owner, value, false)! |
| 185 | method.specializers = specializers |
| 186 | method.rest = !isnil(rest_class) |
| 187 | method.rest_class = rest_class |
| 188 | } |
| 189 | .generic_methods { |
| 190 | mut g := vm.checked_goops_generic(copy)! |
| 191 | methods := vm.goops_method_array(value)! |
| 192 | for method in methods { |
| 193 | if method.allocation_owner != g.allocation_owner { |
| 194 | return failure('method publication crosses an arena boundary') |
| 195 | } |
| 196 | } |
| 197 | g.methods = methods |
| 198 | } |
| 199 | .generic_extends { |
| 200 | mut g := vm.checked_goops_generic(copy)! |
| 201 | values := proper_list_values(value)! |
| 202 | for parent in values { |
| 203 | vm.goops_check_extended_relation(vm.checked_goops_generic(parent)!, g)! |
| 204 | } |
| 205 | g.extends = values |
| 206 | } |
| 207 | .generic_extended_by { |
| 208 | mut g := vm.checked_goops_generic(copy)! |
| 209 | values := proper_list_values(value)! |
| 210 | for child in values { |
| 211 | vm.goops_check_extended_relation(g, vm.checked_goops_generic(child)!)! |
| 212 | } |
| 213 | g.migration_children = goops_public_references(values) |
| 214 | g.migration_has_children = true |
| 215 | } |
| 216 | .generic_procedure { |
| 217 | mut g := vm.checked_goops_generic(copy)! |
| 218 | if eqv(value, source) || eqv(value, copy) { |
| 219 | g.procedure_override = boolean(false) |
| 220 | } else { |
| 221 | vm.check_callable_owner(value)! |
| 222 | g.procedure_override = value |
| 223 | } |
| 224 | } |
| 225 | else { vm.goops_native_slot_write(copy, field, value, false)! } |
| 226 | } |
| 227 | if copy.tag == .procedure { |
| 228 | mut generic := copy.goops_layout_generic() |
| 229 | if !isnil(generic) { generic.generation++ } |
| 230 | } |
| 231 | return boolean(true) |
| 232 | } |
| 233 | |
| 234 | // Only public layout data moves. Registry membership, canonical versions, |
| 235 | // ownership and migration scopes stay attached to each physical identity. |
| 236 | fn (mut vm VM) goops_commit_native_layout(object Value, copy Value, old_class &GoopsClass) !Value { |
| 237 | if object.goops_kind() != copy.goops_kind() { |
| 238 | if object.goops_kind() in [.goops_instance, .goops_method] && copy.goops_kind() in [.goops_instance, .goops_method] { |
| 239 | return vm.goops_exchange_roles(object, copy, old_class) |
| 240 | } |
| 241 | return failure('change-class across native representation families is not implemented') |
| 242 | } |
| 243 | match object.goops_kind() { |
| 244 | .goops_method { |
| 245 | mut target := vm.checked_goops_method(object)! |
| 246 | mut next := vm.checked_goops_method(copy)! |
| 247 | vm.goops_check_retained_classes(target.allocation_owner, next.specializers)! |
| 248 | if !isnil(next.rest_class) { |
| 249 | vm.goops_require_retention(target.allocation_owner, next.rest_class.allocation_owner)! |
| 250 | } |
| 251 | previous := *target |
| 252 | copy_generic := next.generic |
| 253 | target.type_class = next.type_class |
| 254 | target.layout_version = next.layout_version |
| 255 | target.fields = next.fields |
| 256 | target.specializers = next.specializers |
| 257 | target.rest = next.rest |
| 258 | target.rest_class = next.rest_class |
| 259 | target.procedure = next.procedure |
| 260 | target.public_procedure = next.public_procedure |
| 261 | target.needs_next = next.needs_next |
| 262 | target.generic = next.generic |
| 263 | target.formals = next.formals |
| 264 | target.body = next.body |
| 265 | target.source = next.source |
| 266 | target.make_procedure = next.make_procedure |
| 267 | target.slot_definition = next.slot_definition |
| 268 | next.type_class = previous.type_class |
| 269 | next.layout_version = previous.layout_version |
| 270 | next.fields = previous.fields |
| 271 | next.specializers = previous.specializers |
| 272 | next.rest = previous.rest |
| 273 | next.rest_class = previous.rest_class |
| 274 | next.procedure = previous.procedure |
| 275 | next.public_procedure = previous.public_procedure |
| 276 | next.needs_next = previous.needs_next |
| 277 | next.generic = previous.generic |
| 278 | next.formals = previous.formals |
| 279 | next.body = previous.body |
| 280 | next.source = previous.source |
| 281 | next.make_procedure = previous.make_procedure |
| 282 | next.slot_definition = previous.slot_definition |
| 283 | next.type_class = old_class |
| 284 | target.generation++ |
| 285 | vm.goops_method_changed(target, previous.generic) |
| 286 | // A custom allocator can install and warm its result before the |
| 287 | // private field transfer. Retain that effective procedure until |
| 288 | // an explicit method edit, while cold calls see the new body. |
| 289 | if !next.migration_copy || !next.installed { |
| 290 | next.generation++ |
| 291 | vm.goops_method_changed(next, copy_generic) |
| 292 | } |
| 293 | return copy |
| 294 | } |
| 295 | .goops_slot { |
| 296 | mut target := vm.checked_goops_slot_definition(object)! |
| 297 | mut next := vm.checked_goops_slot_definition(copy)! |
| 298 | previous := *target |
| 299 | backing := *target.slot |
| 300 | copy_backing := *next.slot |
| 301 | unsafe { |
| 302 | *target.slot = GoopsSlot{ |
| 303 | ...copy_backing |
| 304 | native_field: backing.native_field |
| 305 | read_only: backing.read_only |
| 306 | hidden: backing.hidden |
| 307 | protected: backing.protected |
| 308 | opaque: backing.opaque |
| 309 | class_allocated: backing.class_allocated |
| 310 | each_subclass: backing.each_subclass |
| 311 | virtual: backing.virtual |
| 312 | virtual_getter: backing.virtual_getter |
| 313 | has_virtual_getter: backing.has_virtual_getter |
| 314 | virtual_setter: backing.virtual_setter |
| 315 | has_virtual_setter: backing.has_virtual_setter |
| 316 | shared: backing.shared |
| 317 | } |
| 318 | } |
| 319 | unsafe { |
| 320 | *next.slot = GoopsSlot{ |
| 321 | ...backing |
| 322 | native_field: copy_backing.native_field |
| 323 | read_only: copy_backing.read_only |
| 324 | hidden: copy_backing.hidden |
| 325 | protected: copy_backing.protected |
| 326 | opaque: copy_backing.opaque |
| 327 | class_allocated: copy_backing.class_allocated |
| 328 | each_subclass: copy_backing.each_subclass |
| 329 | virtual: copy_backing.virtual |
| 330 | virtual_getter: copy_backing.virtual_getter |
| 331 | has_virtual_getter: copy_backing.has_virtual_getter |
| 332 | virtual_setter: copy_backing.virtual_setter |
| 333 | has_virtual_setter: copy_backing.has_virtual_setter |
| 334 | shared: copy_backing.shared |
| 335 | } |
| 336 | } |
| 337 | target.type_class = next.type_class |
| 338 | target.layout_version = next.layout_version |
| 339 | target.fields = next.fields |
| 340 | target.options = next.options |
| 341 | target.accessor_generation++ |
| 342 | next.accessor_generation++ |
| 343 | next.type_class = previous.type_class |
| 344 | next.layout_version = previous.layout_version |
| 345 | next.fields = previous.fields |
| 346 | next.options = previous.options |
| 347 | next.type_class = old_class |
| 348 | return copy |
| 349 | } |
| 350 | .goops_class { |
| 351 | mut target := vm.checked_goops_class(object)! |
| 352 | mut next := vm.checked_goops_class(copy)! |
| 353 | if target.version != next.migration_source_version { |
| 354 | return failure('class instance layout changed during metaclass migration') |
| 355 | } |
| 356 | vm.goops_check_retained_classes(target.allocation_owner, next.supers)! |
| 357 | vm.goops_check_retained_classes(target.allocation_owner, next.cpl)! |
| 358 | if !isnil(next.redefined) { |
| 359 | vm.goops_require_retention(target.allocation_owner, next.redefined.allocation_owner)! |
| 360 | } |
| 361 | for definition in next.direct_definitions { |
| 362 | vm.goops_retained_slot(target.allocation_owner, definition)! |
| 363 | } |
| 364 | previous := *target |
| 365 | methods := goops_copy_public_references(next.direct_methods) |
| 366 | children := goops_copy_public_references(next.direct_subclasses) |
| 367 | // Preflight every definition before the first canonical retarget. |
| 368 | vm.goops_prepare_class_copy_definitions(mut next, target)! |
| 369 | target.meta = next.meta |
| 370 | target.meta_layout_version = next.meta_layout_version |
| 371 | target.fields = next.fields |
| 372 | target.name = next.name |
| 373 | target.name_value = next.name_value |
| 374 | target.supers = next.supers |
| 375 | target.cpl = next.cpl |
| 376 | target.direct_slots = next.direct_slots |
| 377 | target.direct_definitions = next.direct_definitions |
| 378 | target.redefined = next.redefined |
| 379 | target.layout = next.layout |
| 380 | target.flags = next.flags |
| 381 | target.finalizer = next.finalizer |
| 382 | target.printer = next.printer |
| 383 | target.slots = next.slots |
| 384 | target.definitions = next.definitions |
| 385 | target.instance_fields = next.instance_fields |
| 386 | target.has_init_thunks = next.has_init_thunks |
| 387 | target.call_slot = next.call_slot |
| 388 | target.representation = next.representation |
| 389 | target.direct_methods = methods |
| 390 | target.direct_subclasses = children |
| 391 | next.meta = previous.meta |
| 392 | next.meta_layout_version = previous.meta_layout_version |
| 393 | next.fields = previous.fields |
| 394 | next.name = previous.name |
| 395 | next.name_value = previous.name_value |
| 396 | next.supers = previous.supers |
| 397 | next.cpl = previous.cpl |
| 398 | next.direct_slots = previous.direct_slots |
| 399 | next.direct_definitions = previous.direct_definitions |
| 400 | next.redefined = previous.redefined |
| 401 | next.layout = previous.layout |
| 402 | next.flags = previous.flags |
| 403 | next.finalizer = previous.finalizer |
| 404 | next.printer = previous.printer |
| 405 | next.slots = previous.slots |
| 406 | next.definitions = previous.definitions |
| 407 | next.instance_fields = previous.instance_fields |
| 408 | next.has_init_thunks = previous.has_init_thunks |
| 409 | next.call_slot = previous.call_slot |
| 410 | next.representation = previous.representation |
| 411 | next.meta = old_class |
| 412 | next.direct_methods = previous.direct_methods |
| 413 | next.direct_subclasses = previous.direct_subclasses |
| 414 | target.dispatch_generation++ |
| 415 | next.dispatch_generation++ |
| 416 | return copy |
| 417 | } |
| 418 | .procedure { |
| 419 | mut target := vm.checked_goops_generic(object)! |
| 420 | mut next := vm.checked_goops_generic(copy)! |
| 421 | children := proper_list_values(vm.goops_extended_by(copy)!)! |
| 422 | old_children := proper_list_values(vm.goops_extended_by(object)!)! |
| 423 | for method in next.methods { |
| 424 | if method.allocation_owner != target.allocation_owner { |
| 425 | return failure('method publication crosses an arena boundary') |
| 426 | } |
| 427 | } |
| 428 | for parent in next.extends { |
| 429 | vm.goops_check_extended_relation(vm.checked_goops_generic(parent)!, target)! |
| 430 | } |
| 431 | for child in children { |
| 432 | child_generic := vm.checked_goops_generic(child)! |
| 433 | vm.goops_check_extended_relation(target, child_generic)! |
| 434 | if voidptr(child_generic) == voidptr(target) { |
| 435 | return failure('generic cannot extend itself') |
| 436 | } |
| 437 | } |
| 438 | previous := *target |
| 439 | old_mode := object.procedure_pointer().mode |
| 440 | old_name := object.procedure_pointer().name |
| 441 | old_setter := goops_generic_presentation_setter(object) |
| 442 | new_mode := copy.procedure_pointer().mode |
| 443 | new_name := copy.procedure_pointer().name |
| 444 | new_setter := goops_generic_presentation_setter(copy) |
| 445 | target.type_class = next.type_class |
| 446 | target.layout_version = next.layout_version |
| 447 | target.fields = next.fields |
| 448 | target.name = next.name |
| 449 | target.methods = next.methods |
| 450 | target.extends = next.extends |
| 451 | target.procedure_override = next.procedure_override |
| 452 | next.type_class = previous.type_class |
| 453 | next.layout_version = previous.layout_version |
| 454 | next.fields = previous.fields |
| 455 | next.name = previous.name |
| 456 | next.methods = previous.methods |
| 457 | next.extends = previous.extends |
| 458 | next.procedure_override = previous.procedure_override |
| 459 | next.type_class = old_class |
| 460 | next.migration_children = goops_public_references(old_children) |
| 461 | next.migration_has_children = true |
| 462 | target.migration_has_children = false |
| 463 | target.migration_children = unsafe { nil } |
| 464 | target.generation++ |
| 465 | target.effective_methods = []&GoopsMethod{} |
| 466 | target.extends_dependencies = []&GoopsGeneric{} |
| 467 | target.extends_generations = []u64{} |
| 468 | target.applicability_entries = []GoopsApplicabilityEntry{} |
| 469 | next.generation++ |
| 470 | next.effective_methods = []&GoopsMethod{} |
| 471 | next.extends_dependencies = []&GoopsGeneric{} |
| 472 | next.extends_generations = []u64{} |
| 473 | next.applicability_entries = []GoopsApplicabilityEntry{} |
| 474 | // The copied children field is public data; other endpoint effects |
| 475 | // already performed by user initializers retain their identities. |
| 476 | vm.goops_set_extended_children(target, children)! |
| 477 | vm.goops_install_generic_presentation(object, target, new_mode, new_name, new_setter) |
| 478 | vm.goops_install_generic_presentation(copy, next, old_mode, old_name, old_setter) |
| 479 | return copy |
| 480 | } |
| 481 | else { return failure('expected a native GOOPS object') } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | fn goops_generic_presentation_setter(value Value) Value { |
| 486 | p := value.procedure_pointer() |
| 487 | return if p.mode in [.goops_accessor, .goops_generic_setter] { p.env.slots[1] } else { Value{} } |
| 488 | } |
| 489 | |
| 490 | fn (vm &VM) goops_install_generic_presentation(value Value, generic &GoopsGeneric, mode ProcedureKind, name string, setter Value) { |
| 491 | mut target := value.procedure_pointer() |
| 492 | if target.mode == mode { |
| 493 | if mode in [.goops_accessor, .goops_generic_setter] { |
| 494 | container_store(mut target.env.slots, 1, setter) |
| 495 | } |
| 496 | unsafe { *target = Procedure{ ...*target, name: name } } |
| 497 | return |
| 498 | } |
| 499 | mut env := unsafe { &Environment(nil) } |
| 500 | if mode in [.goops_accessor, .goops_generic_setter] { |
| 501 | getter := procedure_value(&Procedure{ |
| 502 | owner: vm.owner |
| 503 | mode: .goops_generic |
| 504 | name: name |
| 505 | env: &Environment{ slots: [payload_value(.goops_generic_data, generic)], escaped: true } |
| 506 | }) |
| 507 | env = &Environment{ slots: [getter, setter], escaped: true } |
| 508 | } else { |
| 509 | env = &Environment{ slots: [payload_value(.goops_generic_data, generic)], escaped: true } |
| 510 | } |
| 511 | unsafe { *target = Procedure{ ...*target, mode: mode, env: env, name: name } } |
| 512 | } |
| 513 | |
| 514 | // Class construction has already established its own CPL, effective slots, |
| 515 | // subclass registry and dispatch methods. Only copy editable nonstructural |
| 516 | // properties and metaclass extension fields onto that new identity. |
| 517 | fn goops_clone_slot_native(mut vm VM, args []Value) !Value { |
| 518 | check_arity('clone-slot?', args.len, 2, 2)! |
| 519 | if args[0].tag != .goops_class { return boolean(true) } |
| 520 | slot := vm.checked_goops_slot(args[1])! |
| 521 | return boolean(slot.native_field in [.none, .class_flags, .class_printer, .class_finalizer]) |
| 522 | } |
| 523 | |