module scheme import reader @[heap] struct GoopsMigrationState { object Value target &GoopsClass snapshot &GoopsClass version u64 copy_plan bool copy_source_version u64 mut: copy Value copy_class &GoopsClass = unsafe { nil } copy_version u64 copy_initialized bool copy_committed bool copy_completed_by_callback bool copy_completion_version u64 } // One registration per dynamic entry, rather than mutable state in a captured // continuation. The object only borrows this registration while it is active. @[heap] struct GoopsMigrationScope { state &GoopsMigrationState previous &GoopsMigrationScope = unsafe { nil } previous_migrating bool previous_copy_flag bool } const goops_migration_bridge_code = [ Instruction{ op: .goops_migration_start, origin: reader.no_node } Instruction{ op: .goops_migration_retry, origin: reader.no_node } ]! const goops_migration_bridge = Function{ code: unsafe { nil } params: 3 slot_count: 3 instructions: unsafe { static_bridge_instructions(&goops_migration_bridge_code[0], goops_migration_bridge_code.len) } } @[inline] fn goops_needs_migration(value Value) bool { if value.goops_kind() == .goops_instance { object := value.goops_instance_pointer() return !object.migrating && object.layout_version != object.class.version } layout := value.goops_layout() return !isnil(layout.class) && !layout.migrating && layout.version != layout.class.version } fn (mut m Machine) prepare_goops_migration(vm &VM, value Value, base int) ! { object := value.goops_layout() if isnil(object.class) { return failure('expected a GOOPS object') } vm.goops_class_for(value)! vm.goops_require_mutation(object.allocation_owner)! mut snapshot := unsafe { &GoopsClass(nil) } for entry in object.class.history { if entry.version == object.version { snapshot = entry.snapshot break } } if isnil(snapshot) { return failure('instance layout history is unavailable') } state := &GoopsMigrationState{ object: value, target: object.class, snapshot: snapshot, version: object.class.version } procedure := m.stack[base] args := list(m.arguments(base + 1)) m.truncate(base) m.push(procedure_value(&Procedure{ owner: vm.owner, mode: .closure, name: 'instance migration', function: &goops_migration_bridge })) m.push(procedure) m.push(args) m.push(payload_value(.goops_migration, state)) } fn (mut m Machine) start_goops_migration(mut vm VM) ! { state_value := m.env.slots[2] state := unsafe { &GoopsMigrationState(state_value.pointer()) } m.push(vm.goops.migration_procedure) m.push(state.object) m.push(goops_class_value(state.target)) for mode in [ProcedureKind.goops_migration_enter, .goops_migration_leave] { m.push(procedure_value(&Procedure{ owner: vm.owner, mode: mode, env: &Environment{ slots: [state_value], escaped: true } })) } m.apply(mut vm, 4, false)! } fn (mut m Machine) retry_goops_migration(mut vm VM) ! { m.pop_value() state := unsafe { &GoopsMigrationState(m.env.slots[2].pointer()) } object := state.object.goops_layout() if object.version == state.snapshot.version && state.target.version == state.version { return failure('change-class did not update the obsolete instance') } procedure := m.env.slots[0] args := m.env.slots[1] count := args.list_length()! m.push(procedure) mut cursor := args for _ in 0 .. count { pair := cursor.pair_pointer() m.push(pair.car) cursor = pair.cdr } m.apply(mut vm, count, true)! } fn (mut m Machine) enter_goops_migration(vm &VM, state &GoopsMigrationState) ! { if state.copy_plan { return m.enter_goops_copy(vm, state) } object := state.object.goops_layout() vm.goops_class_for(state.object)! vm.goops_require_mutation(object.allocation_owner)! if state.target.version != state.version { return failure('class changed while an instance migration was suspended') } if object.migrating { return failure('instance migration is already active') } if voidptr(object.class) != voidptr(state.target) && voidptr(object.class) != voidptr(state.snapshot) { return failure('instance changed class while its migration was suspended') } if object.version != state.version && object.version != state.snapshot.version { return failure('instance changed while its migration was suspended') } scope := &GoopsMigrationScope{ state: state, previous: m.goops_migrations } m.goops_migrations = scope state.object.goops_pin_layout(if object.version != state.version { state.snapshot } else { state.target }, true) } fn (mut m Machine) leave_goops_migration(state &GoopsMigrationState) { if isnil(m.goops_migrations) || voidptr(m.goops_migrations.state) != voidptr(state) { return } m.release_goops_migration() } fn (mut m Machine) release_goops_migration() { scope := m.goops_migrations object := scope.state.object.goops_layout() if scope.state.copy_plan { class := if !scope.previous_migrating && !isnil(object.class.live_class) { object.class.live_class } else { object.class } scope.state.object.goops_pin_layout(class, scope.previous_migrating) // A native old view remains historical for the update hook. Once // its dynamic extent ends, normal access can adopt the current class. if scope.state.copy_committed && (scope.state.copy.goops_kind() != .goops_instance || scope.state.object.goops_kind() == .goops_method) { copy_layout := scope.state.copy.goops_layout() if !isnil(copy_layout.class) && !isnil(copy_layout.class.live_class) { scope.state.copy.goops_pin_layout(copy_layout.class.live_class, copy_layout.migrating) } } scope.state.copy.goops_set_copy_flag(scope.previous_copy_flag) m.goops_migrations = scope.previous return } if object.migrating { class := if voidptr(object.class) == voidptr(scope.state.snapshot) || voidptr(object.class.live_class) == voidptr(scope.state.target) { scope.state.target } else { object.class } scope.state.object.goops_pin_layout(class, false) } m.goops_migrations = scope.previous } fn (m &Machine) goops_obsolete_argument(base int, argc int) Value { for index in 0 .. argc { value := m.stack[base + 1 + index] if goops_needs_migration(value) { return value } } return Value{} } fn (m &Machine) goops_native_obsolete_argument(mut vm VM, p &Procedure, base int, argc int) !Value { if argc == 0 { return Value{} } if ((p.native == goops_class_of_native || p.native == goops_instance_predicate) && argc == 1) || ((p.native == goops_is_a_native || p.native == goops_slot_exists_native) && argc == 2) { value := m.stack[base + 1] return if goops_needs_migration(value) { value } else { Value{} } } if p.native == goops_compute_methods_native || p.native == goops_find_method_native || p.native == goops_sort_methods_native { g := m.stack[base + 1] if goops_needs_migration(g) { return g } generic := g.goops_layout_generic() if !isnil(generic) { view := vm.goops_all_methods(generic)! obsolete := goops_obsolete_method(view.values) view.release() if obsolete.tag != .unspecified { return obsolete } } if p.native == goops_sort_methods_native && argc == 3 { mut methods := m.stack[base + 2] _ = methods.list_length()! for methods.tag == .pair { pair := methods.pair_pointer() if goops_needs_migration(pair.car) { return pair.car } methods = pair.cdr } } } index := if (p.native == goops_compute_methods_native || p.native == goops_find_method_native) && argc == 2 { 2 } else if p.native == goops_sort_methods_native && argc == 3 { 3 } else { return Value{} } mut cursor := m.stack[base + index] _ = cursor.list_length()! for cursor.tag == .pair { pair := cursor.pair_pointer() if goops_needs_migration(pair.car) { return pair.car } cursor = pair.cdr } return Value{} } fn (mut m Machine) cleanup_goops_migrations() { for !isnil(m.goops_migrations) { m.release_goops_migration() } } // Heap identities can die independently. An interior pointer into the other // identity would retain its header and any obsolete inline values it contains. fn (mut object GoopsInstance) detach_inline_fields() { if object.field_count == 0 || voidptr(object.fields) != unsafe { voidptr(&u8(&object) + goops_field_offset()) } { return } bytes := object.field_count * int(sizeof(Value)) inline_fields := object.fields object.fields = unsafe { &Value(vcalloc(bytes)) } unsafe { vmemcpy(object.fields, inline_fields, bytes) vmemset(inline_fields, 0, bytes) } } fn goops_exchange_ordinary_layout(mut object GoopsInstance, mut proposed GoopsInstance) { if voidptr(&object) == voidptr(&proposed) { return } if isnil(object.physical_owner()) { object.detach_inline_fields() proposed.detach_inline_fields() } previous_class := object.class previous_fields := object.fields previous_count := object.field_count previous_version := object.layout_version object.class = proposed.class object.fields = proposed.fields object.field_count = proposed.field_count object.layout_version = proposed.layout_version proposed.class = previous_class proposed.fields = previous_fields proposed.field_count = previous_count proposed.layout_version = previous_version } // Exchange logical layouts only. Each physical allocation retains its owner // and dynamic migration flag, and the allocator's copy becomes the old view. fn goops_commit_instance_native(mut vm VM, args []Value) !Value { check_arity('commit-instance', args.len, 3, 3)! mut state := vm.checked_goops_copy_plan(args[2])! if !state.copy_initialized || !eqv(args[0], state.object) || !eqv(args[1], state.copy) { return failure('instance copy does not match its plan') } vm.goops_validate_copy_plan(state)! if state.copy_committed || state.copy_completed_by_callback { return boolean(false) } object_layout := state.object.goops_layout() proposed_layout := state.copy.goops_layout() if migration := vm.goops_current_migration(args[0]) { if migration.target.version != migration.version { return failure('class changed during instance migration') } if voidptr(proposed_layout.class) != voidptr(migration.target) || (voidptr(object_layout.class) != voidptr(migration.target) && voidptr(object_layout.class) != voidptr(migration.snapshot)) { return failure('instance changed class during migration') } } if state.object.goops_kind() != .goops_instance || state.copy.goops_kind() != .goops_instance { previous := vm.goops_commit_native_layout(state.object, state.copy, state.snapshot)! state.copy_committed = true state.copy.goops_set_copy_flag(false) return previous } mut object := vm.checked_goops_instance(state.object)! mut proposed := vm.checked_goops_instance(state.copy)! goops_exchange_ordinary_layout(mut object, mut proposed) state.copy_committed = true return state.copy } fn (vm &VM) goops_current_migration(value Value) ?&GoopsMigrationState { for index := vm.active.len - 1; index >= 0; index-- { mut scope := vm.active[index].goops_migrations for !isnil(scope) { if !scope.state.copy_plan && eqv(scope.state.object, value) { return scope.state } scope = scope.previous } } return none } fn (mut m Machine) redefine_goops_binding(mut vm VM, depth int, slot int) ! { mut previous := Value{ tag: .uninitialized } if depth >= 0 { previous = m.env.ancestor(depth).slots[slot] } else { mut global_slot := m.function.global_slots[slot] if global_slot < 0 { if found := vm.global_index_in(m.function.code.namespace, m.function.names[slot]) { global_slot = found m.function.global_slots[slot] = found } } if global_slot >= 0 { previous = vm.globals[global_slot] } } if previous.tag == .uninitialized { return } created := m.pop_value() m.push(vm.goops.class_redefinition) m.push(previous) m.push(created) m.apply(mut vm, 2, false)! } fn goops_obsolete_method(methods []&GoopsMethod) Value { for method in methods { value := goops_method_value(method) if goops_needs_migration(value) { return value } } return Value{} }