vile / scheme / goops_copy.v
162 lines · 155 sloc · 6.65 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3fn (vm &VM) checked_goops_copy_plan(value Value) !&GoopsMigrationState {
4 if value.tag != .goops_migration { return failure('expected an instance copy plan') }
5 state := unsafe { &GoopsMigrationState(value.pointer()) }
6 if !state.copy_plan { return failure('expected an instance copy plan') }
7 vm.goops_class_for(state.object)!
8 return state
9}
10
11fn (vm &VM) goops_validate_copy_plan(state &GoopsMigrationState) ! {
12 vm.goops_class_for(state.object)!
13 object := state.object.goops_layout()
14 if isnil(object.class) { return failure('expected a GOOPS object') }
15 vm.goops_require_mutation(object.allocation_owner)!
16 if state.target.version != state.version {
17 return failure('class changed while an instance copy was suspended')
18 }
19 if state.snapshot.version != state.copy_source_version {
20 return failure('source class changed during instance copy')
21 }
22 mut plan := unsafe { state }
23 if !state.copy_committed && state.copy_initialized
24 && state.object.tag == .goops_class && state.copy.tag == .goops_class {
25 canonical := state.object.goops_class_pointer()
26 candidate := state.copy.goops_class_pointer()
27 if !state.copy_completed_by_callback
28 && canonical.version != candidate.migration_source_version
29 && voidptr(object.class) == voidptr(state.copy_class) && object.version == state.copy_version {
30 plan.copy_completed_by_callback = true
31 plan.copy_completion_version = canonical.version
32 }
33 if state.copy_completed_by_callback && canonical.version != state.copy_completion_version {
34 return failure('class changed after its migration was completed by a callback')
35 }
36 }
37 if state.copy_committed || state.copy_completed_by_callback {
38 if state.copy_class.version != state.copy_version {
39 return failure('class changed after an instance copy was committed')
40 }
41 if voidptr(object.class) != voidptr(state.copy_class)
42 || object.version != state.copy_version {
43 return failure('instance changed after its copy was committed')
44 }
45 } else {
46 if object.version != state.copy_source_version
47 || (voidptr(object.class) != voidptr(state.snapshot)
48 && voidptr(object.class.live_class) != voidptr(state.snapshot)) {
49 return failure('instance changed during instance copy')
50 }
51 }
52 if state.copy_initialized {
53 vm.goops_class_for(state.copy)!
54 copy := state.copy.goops_layout()
55 vm.goops_require_mutation(copy.allocation_owner)!
56 if !state.copy_committed && (voidptr(copy.class) != voidptr(state.copy_class)
57 || copy.version != state.copy_version || state.copy_class.version != state.copy_version) {
58 return failure('new instance layout became obsolete during migration')
59 }
60 }
61}
62
63fn goops_prepare_copy_native(mut vm VM, args []Value) !Value {
64 check_arity('prepare-instance-copy', args.len, 3, 3)!
65 vm.goops_class_for(args[0])!
66 object := args[0].goops_layout()
67 if isnil(object.class) { return failure('expected a GOOPS object') }
68 old_class := vm.checked_goops_class(args[1])!
69 target := vm.checked_goops_class(args[2])!
70 vm.goops_require_mutation(object.allocation_owner)!
71 if !vm.goops_retains_owner(object.allocation_owner, old_class.allocation_owner)
72 || !vm.goops_retains_owner(object.allocation_owner, target.allocation_owner) {
73 return failure('instance class crosses an arena boundary')
74 }
75 if voidptr(object.class) != voidptr(old_class) || object.version != old_class.version {
76 return failure('source instance layout became obsolete before copying')
77 }
78 return payload_value(.goops_migration, &GoopsMigrationState{
79 object: args[0]
80 target: target
81 snapshot: old_class
82 version: target.version
83 copy_plan: true
84 copy_source_version: object.version
85 })
86}
87
88fn goops_bind_copy_native(mut vm VM, args []Value) !Value {
89 check_arity('bind-instance-copy', args.len, 2, 2)!
90 mut state := vm.checked_goops_copy_plan(args[0])!
91 vm.goops_validate_copy_plan(state)!
92 if state.copy_initialized { return failure('instance copy was already allocated') }
93 if eqv(state.object, args[1]) {
94 return failure('allocate-instance must return a distinct copy')
95 }
96 vm.goops_class_for(args[1])!
97 copy := args[1].goops_layout()
98 if isnil(copy.class) { return failure('allocate-instance must return a GOOPS object') }
99 vm.goops_require_mutation(copy.allocation_owner)!
100 if !vm.goops_retains_owner(copy.allocation_owner, copy.class.allocation_owner) {
101 return failure('instance class crosses an arena boundary')
102 }
103 if copy.migrating { return failure('allocated copy is already migrating') }
104 if copy.version != copy.class.version {
105 return failure('allocated copy has an obsolete layout')
106 }
107 if state.object.tag == .goops_class && args[1].tag == .goops_class {
108 mut next := args[1].goops_class_pointer()
109 next.migration_source_version = state.object.goops_class_pointer().version
110 }
111 state.copy = args[1]
112 state.copy_class = copy.class
113 state.copy_version = copy.version
114 state.copy_initialized = true
115 return args[1]
116}
117
118fn goops_copy_winder_native(mut vm VM, args []Value) !Value {
119 check_arity('instance-copy-winder', args.len, 2, 2)!
120 state := vm.checked_goops_copy_plan(args[0])!
121 if !state.copy_initialized { return failure('instance copy has not been allocated') }
122 return procedure_value(&Procedure{
123 owner: vm.owner
124 mode: if args[1].is_true() { .goops_migration_enter } else { .goops_migration_leave }
125 env: &Environment{ slots: [args[0]], escaped: true }
126 })
127}
128
129fn goops_copy_target_native(mut vm VM, args []Value) !Value {
130 check_arity('instance-copy-target', args.len, 1, 1)!
131 state := vm.checked_goops_copy_plan(args[0])!
132 vm.goops_validate_copy_plan(state)!
133 if !state.copy_initialized { return failure('instance copy has not been allocated') }
134 // A replay writes the current layout explicitly. The escaped allocated
135 // copy stays an independent old view; it never aliases current storage.
136 if state.copy_completed_by_callback { return boolean(false) }
137 return if state.copy_committed { state.object } else { state.copy }
138}
139
140fn (mut m Machine) enter_goops_copy(vm &VM, state &GoopsMigrationState) ! {
141 vm.goops_validate_copy_plan(state)!
142 if !state.copy_initialized { return failure('instance copy has not been allocated') }
143 for machine in vm.active {
144 mut scope := machine.goops_migrations
145 for !isnil(scope) {
146 if scope.state.copy_plan && eqv(scope.state.object, state.object) {
147 return failure('instance copy is already active')
148 }
149 scope = scope.previous
150 }
151 }
152 object := state.object.goops_layout()
153 scope := &GoopsMigrationScope{
154 state: state
155 previous: m.goops_migrations
156 previous_migrating: object.migrating
157 previous_copy_flag: state.copy.goops_copy_flag()
158 }
159 m.goops_migrations = scope
160 state.object.goops_pin_layout(object.class, true)
161 state.copy.goops_set_copy_flag(!state.copy_committed && !state.copy_completed_by_callback)
162}
163