module scheme // Ordinary instances keep the physical owner inline. A role record takes // over that word on the first transition and carries the same owner. @[inline] fn (o &GoopsInstance) role() &GoopsRole { return unsafe { &GoopsRole(o.owner_or_role) } } @[inline] fn (o &GoopsInstance) physical_owner() voidptr { return if o.has_role { o.role().allocation_owner } else { o.owner_or_role } } // Allocate the other body only when an identity first crosses the method/object // boundary. A method table can retain its stable method pointer across either // transition. Public values always refer to the original identity. @[heap] struct GoopsRole { public Value object &GoopsInstance method &GoopsMethod allocation_owner voidptr mut: kind Kind } @[inline] fn (v Value) goops_kind() Kind { if v.tag == .goops_instance { o := unsafe { &GoopsInstance(v.pointer()) } if o.has_role { return o.role().kind } } else if v.tag == .goops_method { m := unsafe { &GoopsMethod(v.pointer()) } if !isnil(m.role) { return m.role.kind } } return v.tag } fn (v Value) ensure_goops_role(owner &Owner, class &GoopsClass) &GoopsRole { mut object := unsafe { &GoopsInstance(nil) } mut method := unsafe { &GoopsMethod(nil) } if v.tag == .goops_instance { object = unsafe { &GoopsInstance(v.pointer()) } if object.has_role { return object.role() } method = &GoopsMethod{ owner: owner, allocation_owner: object.physical_owner() } } else { method = unsafe { &GoopsMethod(v.pointer()) } if !isnil(method.role) { return method.role } object = &GoopsInstance{ class: class } } role := &GoopsRole{ public: v, object: object, method: method, kind: v.tag, allocation_owner: method.allocation_owner } object.owner_or_role = voidptr(role) object.has_role = true method.role = role return role } fn (vm &VM) goops_exchange_roles(object Value, copy Value, old_class &GoopsClass) !Value { a := object.goops_layout() b := copy.goops_layout() vm.goops_require_mutation(a.allocation_owner)! vm.goops_require_mutation(b.allocation_owner)! ordinary := object.goops_kind() == .goops_instance instance := if ordinary { object.goops_instance_pointer() } else { copy.goops_instance_pointer() } method := if ordinary { copy.goops_method_pointer() } else { object.goops_method_pointer() } // A raw field pointer can point inside either public header, including // after an earlier ordinary exchange. Detach it before changing roles. mut fields := unsafe { &Value(nil) } bytes := instance.field_count * int(sizeof(Value)) if bytes != 0 { fields = unsafe { &Value(vcalloc(bytes)) } unsafe { vmemcpy(fields, instance.fields, bytes) } } mut left := object.ensure_goops_role(vm.owner, old_class) mut right := copy.ensure_goops_role(vm.owner, old_class) mut from := if ordinary { left } else { right } mut to := if ordinary { right } else { left } owner := a.allocation_owner unsafe { *to.object = GoopsInstance{ has_role: true owner_or_role: voidptr(to) class: if ordinary { old_class } else { instance.class } layout_version: instance.layout_version field_count: instance.field_count fields: fields migrating: if ordinary { b.migrating } else { a.migrating } } *from.method = GoopsMethod{ ...*method role: from type_class: if ordinary { method.type_class } else { old_class } migrating: if ordinary { a.migrating } else { b.migrating } migration_copy: false registered: from.method.registered installed: false specializer_tickets: [] generation: from.method.generation + 1 } // Keep already-built effective procedures valid for their recorded // classes. Fresh applicability sees the new role and reports an error. *to.method = GoopsMethod{ owner: vm.owner allocation_owner: owner role: to registered: method.registered generation: method.generation } if bytes != 0 { vmemset(instance.fields, 0, bytes) } *from.object = GoopsInstance{ class: vm.goops.object, has_role: true, owner_or_role: voidptr(from) } } from.kind = .goops_method to.kind = .goops_instance return copy }