module scheme // A class has two independent layouts: version describes its instances, // whereas meta_layout_version describes the class object's own fields. struct GoopsLayout { class &GoopsClass = unsafe { nil } version u64 migrating bool allocation_owner voidptr } fn goops_same_representation_family(a GoopsRepresentation, b GoopsRepresentation) bool { return a == b || (a in [.object, .method] && b in [.object, .method]) || (a in [.generic, .generic_setter, .accessor] && b in [.generic, .generic_setter, .accessor]) } fn (value Value) goops_copy_flag() bool { return match value.goops_kind() { .goops_class { value.goops_class_pointer().migration_copy } .goops_method { value.goops_method_pointer().migration_copy } .goops_slot { value.goops_slot_pointer().migration_copy } .procedure { g := value.goops_layout_generic() !isnil(g) && g.migration_copy } else { false } } } fn (value Value) goops_set_copy_flag(flag bool) { match value.goops_kind() { .goops_class { mut o := value.goops_class_pointer() o.migration_copy = flag } .goops_method { mut o := value.goops_method_pointer() o.migration_copy = flag } .goops_slot { mut o := value.goops_slot_pointer() o.migration_copy = flag } .procedure { mut o := value.goops_layout_generic() if !isnil(o) { o.migration_copy = flag } } else {} } } fn (value Value) goops_layout_generic() &GoopsGeneric { if value.tag != .procedure { return unsafe { nil } } p := value.procedure_pointer() if p.mode in [.goops_accessor, .goops_generic_setter] { return p.env.slots[0].goops_layout_generic() } if p.mode !in [.goops_generic, .goops_make] { return unsafe { nil } } return p.env.slots[0].goops_generic_data_pointer() } fn (value Value) goops_layout() GoopsLayout { match value.goops_kind() { .goops_instance { o := value.goops_instance_pointer() return GoopsLayout{ class: o.class, version: o.layout_version, migrating: o.migrating, allocation_owner: o.physical_owner() } } .goops_class { o := value.goops_class_pointer() return GoopsLayout{ class: o.meta, version: o.meta_layout_version, migrating: o.meta_migrating, allocation_owner: o.allocation_owner } } .goops_method { o := value.goops_method_pointer() return GoopsLayout{ class: o.type_class, version: o.layout_version, migrating: o.migrating, allocation_owner: o.allocation_owner } } .goops_slot { o := value.goops_slot_pointer() return GoopsLayout{ class: o.type_class, version: o.layout_version, migrating: o.migrating, allocation_owner: o.allocation_owner } } .procedure { o := value.goops_layout_generic() if !isnil(o) { return GoopsLayout{ class: o.type_class, version: o.layout_version, migrating: o.migrating, allocation_owner: o.allocation_owner } } } else {} } return GoopsLayout{} } fn (value Value) goops_pin_layout(class &GoopsClass, migrating bool) { match value.goops_kind() { .goops_instance { mut o := value.goops_instance_pointer() o.class = class o.migrating = migrating } .goops_class { mut o := value.goops_class_pointer() o.meta = class o.meta_migrating = migrating } .goops_method { mut o := value.goops_method_pointer() o.type_class = class o.migrating = migrating } .goops_slot { mut o := value.goops_slot_pointer() o.type_class = class o.migrating = migrating } .procedure { mut o := value.goops_layout_generic() o.type_class = class o.migrating = migrating } else { panic('invalid migration object') } } } // Allocate-instance remains a public callback. Keep its public field defaults // until change-class visits each field; initializers can observe this order. fn (vm &VM) goops_check_retained_classes(owner voidptr, classes []&GoopsClass) ! { for class in classes { if !vm.goops_retains_owner(owner, class.allocation_owner) { return failure('class dependency crosses an arena boundary') } } } // Default change-class copies public field data without replaying publication // APIs on canonical methods, graph endpoints, or effective slot definitions. // User virtual setters still run through the ordinary public slot-set! path. fn goops_copy_native_slot_native(mut vm VM, args []Value) !Value { check_arity('copy-native-slot!', args.len, 4, 4)! copy := args[0] definition := vm.checked_goops_slot(args[1])! field := definition.native_field if field == .none || copy.goops_kind() == .goops_instance { return boolean(false) } value := args[2] source := args[3] layout := copy.goops_layout() vm.goops_require_mutation(layout.allocation_owner)! match field { .class_cpl { mut c := vm.checked_goops_class(copy)! classes := vm.goops_retained_class_prefix(c.allocation_owner, value, value.list_length()!)! for i, class in classes { for j in 0 .. i { if voidptr(class) == voidptr(classes[j]) { return failure('duplicate class in precedence list') } } } // A copied CPL starts with the public source identity, which is // retained at commit. Do not substitute the temporary copy. c.cpl = classes } .class_slots { mut c := vm.checked_goops_class(copy)! values := proper_list_values(value)! mut slots := []GoopsSlot{cap: values.len} for item in values { d := vm.goops_retained_slot(c.allocation_owner, item)! if d.slot.metadata.name.tag != .symbol { return failure('effective slot needs a symbol name') } for prior in slots { if prior.name == d.slot.name { return failure('duplicate effective slot ${d.slot.name}') } } slots << *d.slot } // Retargeting is deferred until the entire migration can commit. c.definitions = values c.slots = slots } .method_specializers { mut method := vm.checked_goops_method(copy)! specializers, rest_class := vm.goops_method_specializers(method.allocation_owner, value, false)! method.specializers = specializers method.rest = !isnil(rest_class) method.rest_class = rest_class } .generic_methods { mut g := vm.checked_goops_generic(copy)! methods := vm.goops_method_array(value)! for method in methods { if method.allocation_owner != g.allocation_owner { return failure('method publication crosses an arena boundary') } } g.methods = methods } .generic_extends { mut g := vm.checked_goops_generic(copy)! values := proper_list_values(value)! for parent in values { vm.goops_check_extended_relation(vm.checked_goops_generic(parent)!, g)! } g.extends = values } .generic_extended_by { mut g := vm.checked_goops_generic(copy)! values := proper_list_values(value)! for child in values { vm.goops_check_extended_relation(g, vm.checked_goops_generic(child)!)! } g.migration_children = goops_public_references(values) g.migration_has_children = true } .generic_procedure { mut g := vm.checked_goops_generic(copy)! if eqv(value, source) || eqv(value, copy) { g.procedure_override = boolean(false) } else { vm.check_callable_owner(value)! g.procedure_override = value } } else { vm.goops_native_slot_write(copy, field, value, false)! } } if copy.tag == .procedure { mut generic := copy.goops_layout_generic() if !isnil(generic) { generic.generation++ } } return boolean(true) } // Only public layout data moves. Registry membership, canonical versions, // ownership and migration scopes stay attached to each physical identity. fn (mut vm VM) goops_commit_native_layout(object Value, copy Value, old_class &GoopsClass) !Value { if object.goops_kind() != copy.goops_kind() { if object.goops_kind() in [.goops_instance, .goops_method] && copy.goops_kind() in [.goops_instance, .goops_method] { return vm.goops_exchange_roles(object, copy, old_class) } return failure('change-class across native representation families is not implemented') } match object.goops_kind() { .goops_method { mut target := vm.checked_goops_method(object)! mut next := vm.checked_goops_method(copy)! vm.goops_check_retained_classes(target.allocation_owner, next.specializers)! if !isnil(next.rest_class) { vm.goops_require_retention(target.allocation_owner, next.rest_class.allocation_owner)! } previous := *target copy_generic := next.generic target.type_class = next.type_class target.layout_version = next.layout_version target.fields = next.fields target.specializers = next.specializers target.rest = next.rest target.rest_class = next.rest_class target.procedure = next.procedure target.public_procedure = next.public_procedure target.needs_next = next.needs_next target.generic = next.generic target.formals = next.formals target.body = next.body target.source = next.source target.make_procedure = next.make_procedure target.slot_definition = next.slot_definition next.type_class = previous.type_class next.layout_version = previous.layout_version next.fields = previous.fields next.specializers = previous.specializers next.rest = previous.rest next.rest_class = previous.rest_class next.procedure = previous.procedure next.public_procedure = previous.public_procedure next.needs_next = previous.needs_next next.generic = previous.generic next.formals = previous.formals next.body = previous.body next.source = previous.source next.make_procedure = previous.make_procedure next.slot_definition = previous.slot_definition next.type_class = old_class target.generation++ vm.goops_method_changed(target, previous.generic) // A custom allocator can install and warm its result before the // private field transfer. Retain that effective procedure until // an explicit method edit, while cold calls see the new body. if !next.migration_copy || !next.installed { next.generation++ vm.goops_method_changed(next, copy_generic) } return copy } .goops_slot { mut target := vm.checked_goops_slot_definition(object)! mut next := vm.checked_goops_slot_definition(copy)! previous := *target backing := *target.slot copy_backing := *next.slot unsafe { *target.slot = GoopsSlot{ ...copy_backing native_field: backing.native_field read_only: backing.read_only hidden: backing.hidden protected: backing.protected opaque: backing.opaque class_allocated: backing.class_allocated each_subclass: backing.each_subclass virtual: backing.virtual virtual_getter: backing.virtual_getter has_virtual_getter: backing.has_virtual_getter virtual_setter: backing.virtual_setter has_virtual_setter: backing.has_virtual_setter shared: backing.shared } } unsafe { *next.slot = GoopsSlot{ ...backing native_field: copy_backing.native_field read_only: copy_backing.read_only hidden: copy_backing.hidden protected: copy_backing.protected opaque: copy_backing.opaque class_allocated: copy_backing.class_allocated each_subclass: copy_backing.each_subclass virtual: copy_backing.virtual virtual_getter: copy_backing.virtual_getter has_virtual_getter: copy_backing.has_virtual_getter virtual_setter: copy_backing.virtual_setter has_virtual_setter: copy_backing.has_virtual_setter shared: copy_backing.shared } } target.type_class = next.type_class target.layout_version = next.layout_version target.fields = next.fields target.options = next.options target.accessor_generation++ next.accessor_generation++ next.type_class = previous.type_class next.layout_version = previous.layout_version next.fields = previous.fields next.options = previous.options next.type_class = old_class return copy } .goops_class { mut target := vm.checked_goops_class(object)! mut next := vm.checked_goops_class(copy)! if target.version != next.migration_source_version { return failure('class instance layout changed during metaclass migration') } vm.goops_check_retained_classes(target.allocation_owner, next.supers)! vm.goops_check_retained_classes(target.allocation_owner, next.cpl)! if !isnil(next.redefined) { vm.goops_require_retention(target.allocation_owner, next.redefined.allocation_owner)! } for definition in next.direct_definitions { vm.goops_retained_slot(target.allocation_owner, definition)! } previous := *target methods := goops_copy_public_references(next.direct_methods) children := goops_copy_public_references(next.direct_subclasses) // Preflight every definition before the first canonical retarget. vm.goops_prepare_class_copy_definitions(mut next, target)! target.meta = next.meta target.meta_layout_version = next.meta_layout_version target.fields = next.fields target.name = next.name target.name_value = next.name_value target.supers = next.supers target.cpl = next.cpl target.direct_slots = next.direct_slots target.direct_definitions = next.direct_definitions target.redefined = next.redefined target.layout = next.layout target.flags = next.flags target.finalizer = next.finalizer target.printer = next.printer target.slots = next.slots target.definitions = next.definitions target.instance_fields = next.instance_fields target.has_init_thunks = next.has_init_thunks target.call_slot = next.call_slot target.representation = next.representation target.direct_methods = methods target.direct_subclasses = children next.meta = previous.meta next.meta_layout_version = previous.meta_layout_version next.fields = previous.fields next.name = previous.name next.name_value = previous.name_value next.supers = previous.supers next.cpl = previous.cpl next.direct_slots = previous.direct_slots next.direct_definitions = previous.direct_definitions next.redefined = previous.redefined next.layout = previous.layout next.flags = previous.flags next.finalizer = previous.finalizer next.printer = previous.printer next.slots = previous.slots next.definitions = previous.definitions next.instance_fields = previous.instance_fields next.has_init_thunks = previous.has_init_thunks next.call_slot = previous.call_slot next.representation = previous.representation next.meta = old_class next.direct_methods = previous.direct_methods next.direct_subclasses = previous.direct_subclasses target.dispatch_generation++ next.dispatch_generation++ return copy } .procedure { mut target := vm.checked_goops_generic(object)! mut next := vm.checked_goops_generic(copy)! children := proper_list_values(vm.goops_extended_by(copy)!)! old_children := proper_list_values(vm.goops_extended_by(object)!)! for method in next.methods { if method.allocation_owner != target.allocation_owner { return failure('method publication crosses an arena boundary') } } for parent in next.extends { vm.goops_check_extended_relation(vm.checked_goops_generic(parent)!, target)! } for child in children { child_generic := vm.checked_goops_generic(child)! vm.goops_check_extended_relation(target, child_generic)! if voidptr(child_generic) == voidptr(target) { return failure('generic cannot extend itself') } } previous := *target old_mode := object.procedure_pointer().mode old_name := object.procedure_pointer().name old_setter := goops_generic_presentation_setter(object) new_mode := copy.procedure_pointer().mode new_name := copy.procedure_pointer().name new_setter := goops_generic_presentation_setter(copy) target.type_class = next.type_class target.layout_version = next.layout_version target.fields = next.fields target.name = next.name target.methods = next.methods target.extends = next.extends target.procedure_override = next.procedure_override next.type_class = previous.type_class next.layout_version = previous.layout_version next.fields = previous.fields next.name = previous.name next.methods = previous.methods next.extends = previous.extends next.procedure_override = previous.procedure_override next.type_class = old_class next.migration_children = goops_public_references(old_children) next.migration_has_children = true target.migration_has_children = false target.migration_children = unsafe { nil } target.generation++ target.effective_methods = []&GoopsMethod{} target.extends_dependencies = []&GoopsGeneric{} target.extends_generations = []u64{} target.applicability_entries = []GoopsApplicabilityEntry{} next.generation++ next.effective_methods = []&GoopsMethod{} next.extends_dependencies = []&GoopsGeneric{} next.extends_generations = []u64{} next.applicability_entries = []GoopsApplicabilityEntry{} // The copied children field is public data; other endpoint effects // already performed by user initializers retain their identities. vm.goops_set_extended_children(target, children)! vm.goops_install_generic_presentation(object, target, new_mode, new_name, new_setter) vm.goops_install_generic_presentation(copy, next, old_mode, old_name, old_setter) return copy } else { return failure('expected a native GOOPS object') } } } fn goops_generic_presentation_setter(value Value) Value { p := value.procedure_pointer() return if p.mode in [.goops_accessor, .goops_generic_setter] { p.env.slots[1] } else { Value{} } } fn (vm &VM) goops_install_generic_presentation(value Value, generic &GoopsGeneric, mode ProcedureKind, name string, setter Value) { mut target := value.procedure_pointer() if target.mode == mode { if mode in [.goops_accessor, .goops_generic_setter] { container_store(mut target.env.slots, 1, setter) } unsafe { *target = Procedure{ ...*target, name: name } } return } mut env := unsafe { &Environment(nil) } if mode in [.goops_accessor, .goops_generic_setter] { getter := procedure_value(&Procedure{ owner: vm.owner mode: .goops_generic name: name env: &Environment{ slots: [payload_value(.goops_generic_data, generic)], escaped: true } }) env = &Environment{ slots: [getter, setter], escaped: true } } else { env = &Environment{ slots: [payload_value(.goops_generic_data, generic)], escaped: true } } unsafe { *target = Procedure{ ...*target, mode: mode, env: env, name: name } } } // Class construction has already established its own CPL, effective slots, // subclass registry and dispatch methods. Only copy editable nonstructural // properties and metaclass extension fields onto that new identity. fn goops_clone_slot_native(mut vm VM, args []Value) !Value { check_arity('clone-slot?', args.len, 2, 2)! if args[0].tag != .goops_class { return boolean(true) } slot := vm.checked_goops_slot(args[1])! return boolean(slot.native_field in [.none, .class_flags, .class_printer, .class_finalizer]) }