module scheme fn (vm &VM) checked_goops_copy_plan(value Value) !&GoopsMigrationState { if value.tag != .goops_migration { return failure('expected an instance copy plan') } state := unsafe { &GoopsMigrationState(value.pointer()) } if !state.copy_plan { return failure('expected an instance copy plan') } vm.goops_class_for(state.object)! return state } fn (vm &VM) goops_validate_copy_plan(state &GoopsMigrationState) ! { vm.goops_class_for(state.object)! object := state.object.goops_layout() if isnil(object.class) { return failure('expected a GOOPS object') } vm.goops_require_mutation(object.allocation_owner)! if state.target.version != state.version { return failure('class changed while an instance copy was suspended') } if state.snapshot.version != state.copy_source_version { return failure('source class changed during instance copy') } mut plan := unsafe { state } if !state.copy_committed && state.copy_initialized && state.object.tag == .goops_class && state.copy.tag == .goops_class { canonical := state.object.goops_class_pointer() candidate := state.copy.goops_class_pointer() if !state.copy_completed_by_callback && canonical.version != candidate.migration_source_version && voidptr(object.class) == voidptr(state.copy_class) && object.version == state.copy_version { plan.copy_completed_by_callback = true plan.copy_completion_version = canonical.version } if state.copy_completed_by_callback && canonical.version != state.copy_completion_version { return failure('class changed after its migration was completed by a callback') } } if state.copy_committed || state.copy_completed_by_callback { if state.copy_class.version != state.copy_version { return failure('class changed after an instance copy was committed') } if voidptr(object.class) != voidptr(state.copy_class) || object.version != state.copy_version { return failure('instance changed after its copy was committed') } } else { if object.version != state.copy_source_version || (voidptr(object.class) != voidptr(state.snapshot) && voidptr(object.class.live_class) != voidptr(state.snapshot)) { return failure('instance changed during instance copy') } } if state.copy_initialized { vm.goops_class_for(state.copy)! copy := state.copy.goops_layout() vm.goops_require_mutation(copy.allocation_owner)! if !state.copy_committed && (voidptr(copy.class) != voidptr(state.copy_class) || copy.version != state.copy_version || state.copy_class.version != state.copy_version) { return failure('new instance layout became obsolete during migration') } } } fn goops_prepare_copy_native(mut vm VM, args []Value) !Value { check_arity('prepare-instance-copy', args.len, 3, 3)! vm.goops_class_for(args[0])! object := args[0].goops_layout() if isnil(object.class) { return failure('expected a GOOPS object') } old_class := vm.checked_goops_class(args[1])! target := vm.checked_goops_class(args[2])! vm.goops_require_mutation(object.allocation_owner)! if !vm.goops_retains_owner(object.allocation_owner, old_class.allocation_owner) || !vm.goops_retains_owner(object.allocation_owner, target.allocation_owner) { return failure('instance class crosses an arena boundary') } if voidptr(object.class) != voidptr(old_class) || object.version != old_class.version { return failure('source instance layout became obsolete before copying') } return payload_value(.goops_migration, &GoopsMigrationState{ object: args[0] target: target snapshot: old_class version: target.version copy_plan: true copy_source_version: object.version }) } fn goops_bind_copy_native(mut vm VM, args []Value) !Value { check_arity('bind-instance-copy', args.len, 2, 2)! mut state := vm.checked_goops_copy_plan(args[0])! vm.goops_validate_copy_plan(state)! if state.copy_initialized { return failure('instance copy was already allocated') } if eqv(state.object, args[1]) { return failure('allocate-instance must return a distinct copy') } vm.goops_class_for(args[1])! copy := args[1].goops_layout() if isnil(copy.class) { return failure('allocate-instance must return a GOOPS object') } vm.goops_require_mutation(copy.allocation_owner)! if !vm.goops_retains_owner(copy.allocation_owner, copy.class.allocation_owner) { return failure('instance class crosses an arena boundary') } if copy.migrating { return failure('allocated copy is already migrating') } if copy.version != copy.class.version { return failure('allocated copy has an obsolete layout') } if state.object.tag == .goops_class && args[1].tag == .goops_class { mut next := args[1].goops_class_pointer() next.migration_source_version = state.object.goops_class_pointer().version } state.copy = args[1] state.copy_class = copy.class state.copy_version = copy.version state.copy_initialized = true return args[1] } fn goops_copy_winder_native(mut vm VM, args []Value) !Value { check_arity('instance-copy-winder', args.len, 2, 2)! state := vm.checked_goops_copy_plan(args[0])! if !state.copy_initialized { return failure('instance copy has not been allocated') } return procedure_value(&Procedure{ owner: vm.owner mode: if args[1].is_true() { .goops_migration_enter } else { .goops_migration_leave } env: &Environment{ slots: [args[0]], escaped: true } }) } fn goops_copy_target_native(mut vm VM, args []Value) !Value { check_arity('instance-copy-target', args.len, 1, 1)! state := vm.checked_goops_copy_plan(args[0])! vm.goops_validate_copy_plan(state)! if !state.copy_initialized { return failure('instance copy has not been allocated') } // A replay writes the current layout explicitly. The escaped allocated // copy stays an independent old view; it never aliases current storage. if state.copy_completed_by_callback { return boolean(false) } return if state.copy_committed { state.object } else { state.copy } } fn (mut m Machine) enter_goops_copy(vm &VM, state &GoopsMigrationState) ! { vm.goops_validate_copy_plan(state)! if !state.copy_initialized { return failure('instance copy has not been allocated') } for machine in vm.active { mut scope := machine.goops_migrations for !isnil(scope) { if scope.state.copy_plan && eqv(scope.state.object, state.object) { return failure('instance copy is already active') } scope = scope.previous } } object := state.object.goops_layout() scope := &GoopsMigrationScope{ state: state previous: m.goops_migrations previous_migrating: object.migrating previous_copy_flag: state.copy.goops_copy_flag() } m.goops_migrations = scope state.object.goops_pin_layout(object.class, true) state.copy.goops_set_copy_flag(!state.copy_committed && !state.copy_completed_by_callback) }