| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import context |
| 5 | |
| 6 | fn native_migration_expect(mut vm VM, source string, expected string) { |
| 7 | value := vm.eval(source) or { panic(err) } |
| 8 | actual := write(value) or { panic(err) } |
| 9 | if actual != expected { panic('expected ${expected}, got ${actual}') } |
| 10 | } |
| 11 | |
| 12 | fn test_native_method_layout_migration_preserves_installed_identity_and_dispatch() { |
| 13 | mut vm := new() |
| 14 | native_migration_expect(mut vm, '(define c (make-class (list <method>) |
| 15 | (quote ((x #:init-value 1))) #:metaclass <redefinable-class>)) |
| 16 | (define m (make c #:specializers (list <integer>) #:procedure (lambda (v) v))) |
| 17 | (define retained m) (define g (make-generic (quote g))) (add-method! g m) |
| 18 | (class-redefinition c (make-class (list <method>) |
| 19 | (quote ((x #:init-value 2) (y #:init-value 3))) #:metaclass <redefinable-class>)) |
| 20 | (list (eq? m retained) (eq? (class-of m) c) (slot-ref m (quote x)) |
| 21 | (slot-ref m (quote y)) (g 42) (eq? (method-generic-function m) g) |
| 22 | (eq? m (car (generic-function-methods g))))', '(#t #t 1 3 42 #t #t)') |
| 23 | } |
| 24 | |
| 25 | fn test_native_slot_layout_migration_preserves_name_and_canonical_definition() { |
| 26 | mut vm := new() |
| 27 | native_migration_expect(mut vm, '(define c (make-class (list <slot>) |
| 28 | (quote ((x #:init-value 1))) #:metaclass <redefinable-class>)) |
| 29 | (define s (make c #:name (quote field) #:init-value 9)) (define retained s) |
| 30 | (class-redefinition c (make-class (list <slot>) |
| 31 | (quote ((x #:init-value 2) (y #:init-value 3))) #:metaclass <redefinable-class>)) |
| 32 | (list (eq? retained s) (eq? (class-of s) c) (slot-ref s (quote x)) |
| 33 | (slot-ref s (quote y)) (slot-definition-name s))', '(#t #t 1 3 field)') |
| 34 | } |
| 35 | |
| 36 | fn test_native_metaclass_layout_migration_preserves_own_instance_layout_and_slots() { |
| 37 | mut vm := new() |
| 38 | native_migration_expect(mut vm, '(define meta (make-class (list <class>) |
| 39 | (quote ((x #:init-value 1))) #:metaclass <redefinable-class>)) |
| 40 | (define c (make-class (quote ()) (quote ((value #:init-value 42))) #:metaclass meta)) |
| 41 | (define object (make c)) (define retained c) |
| 42 | (define definition (class-slot-definition c (quote value))) |
| 43 | (class-redefinition meta (make-class (list <class>) |
| 44 | (quote ((x #:init-value 2) (y #:init-value 3))) #:metaclass <redefinable-class>)) |
| 45 | (list (eq? c retained) (eq? (class-of c) meta) (slot-ref c (quote x)) |
| 46 | (slot-ref c (quote y)) (eq? (class-of object) c) (slot-ref object (quote value)) |
| 47 | (eq? definition (class-slot-definition c (quote value))) |
| 48 | (eq? c (car (class-precedence-list c))))', '(#t #t 1 3 #t 42 #t #t)') |
| 49 | } |
| 50 | |
| 51 | fn test_native_generic_layout_migration_preserves_methods_and_backlinks() { |
| 52 | mut vm := new() |
| 53 | native_migration_expect(mut vm, '(define c (make-class (list <generic>) |
| 54 | (quote ((x #:init-value 1))) #:metaclass <redefinable-class>)) |
| 55 | (define g (make c)) (define retained g) |
| 56 | (add-method! g (method ((v <integer>)) v)) |
| 57 | (define m (car (generic-function-methods g))) |
| 58 | (class-redefinition c (make-class (list <generic>) |
| 59 | (quote ((x #:init-value 2) (y #:init-value 3))) #:metaclass <redefinable-class>)) |
| 60 | (list (eq? g retained) (eq? (class-of g) c) (slot-ref g (quote x)) |
| 61 | (slot-ref g (quote y)) (length (generic-function-methods g)) |
| 62 | (eq? (method-generic-function m) g) (eq? m (car (generic-function-methods g))) |
| 63 | (g 42))', '(#t #t 1 3 1 #t #t 42)') |
| 64 | } |
| 65 | |
| 66 | fn test_native_selected_method_and_generic_calls_migrate_before_application() { |
| 67 | mut vm := new() |
| 68 | native_migration_expect(mut vm, '(define updates 0) |
| 69 | (define c (make-class (list <method>) (quote ()) #:metaclass <redefinable-class>)) |
| 70 | (define-method (update-instance-for-different-class (old <method>) (new <method>)) |
| 71 | (set! updates (+ updates 1)) new) |
| 72 | (define m (make c #:specializers (list <integer>) #:procedure (lambda (v) updates))) |
| 73 | (define g (make-generic (quote g))) (add-method! g m) |
| 74 | (class-redefinition c (make-class (list <method>) (quote ((added #:init-value 3))) |
| 75 | #:metaclass <redefinable-class>)) |
| 76 | (list (g 42) updates (slot-ref m (quote added)))', '(1 1 3)') |
| 77 | native_migration_expect(mut vm, '(define gc (make-class (list <generic>) |
| 78 | (quote ()) #:metaclass <redefinable-class>)) |
| 79 | (define-method (update-instance-for-different-class (old <generic>) (new <generic>)) |
| 80 | (set! updates (+ updates 1)) new) |
| 81 | (define h (make gc)) (add-method! h (method ((v <integer>)) updates)) |
| 82 | (class-redefinition gc (make-class (list <generic>) (quote ((added #:init-value 4))) |
| 83 | #:metaclass <redefinable-class>)) |
| 84 | (list (h 42) updates (slot-ref h (quote added)))', '(2 2 4)') |
| 85 | } |
| 86 | |
| 87 | fn test_native_canonical_slot_migration_keeps_escaped_raw_accessors_attached() { |
| 88 | mut vm := new() |
| 89 | native_migration_expect(mut vm, '(define c (make-class (list <slot>) |
| 90 | (quote ((x #:init-value 1))) #:metaclass <redefinable-class>)) |
| 91 | (define holder (make-class (quote ()) |
| 92 | (list (list (quote value) #:class c #:init-value 7)))) |
| 93 | (define s (class-slot-definition holder (quote value))) |
| 94 | (define raw (slot-ref s (quote slot-ref/raw))) (define object (make holder)) |
| 95 | (class-redefinition c (make-class (list <slot>) |
| 96 | (quote ((y #:init-value 3) (x #:init-value 2))) #:metaclass <redefinable-class>)) |
| 97 | (slot-ref s (quote y)) (slot-set! s (quote init-value) 12) |
| 98 | (list (eq? s (class-slot-definition holder (quote value))) (raw object) |
| 99 | (slot-ref (make holder) (quote value)) (slot-ref s (quote x)))', '(#t 7 12 1)') |
| 100 | } |
| 101 | |
| 102 | fn native_migration_setup(family string) string { |
| 103 | creation := match family { |
| 104 | '<method>' { '(make c #:specializers (list <integer>) #:procedure (lambda (v) v))' } |
| 105 | '<slot>' { '(make c #:name (quote value))' } |
| 106 | '<class>' { '(make-class (quote ()) (quote ((value #:init-value 42))) #:metaclass c)' } |
| 107 | else { '(make c)' } |
| 108 | } |
| 109 | return '(define c (make-class (list ${family}) (quote ((x #:init-value 1))) |
| 110 | #:metaclass <redefinable-class>)) (define object ${creation})' |
| 111 | } |
| 112 | |
| 113 | fn test_native_layout_continuation_escape_replay_and_stale_target_are_safe() { |
| 114 | for family in ['<method>', '<slot>', '<class>', '<generic>'] { |
| 115 | mut vm := new() |
| 116 | vm.eval(native_migration_setup(family)) or { panic(err) } |
| 117 | vm.eval('(define saved #f) (define escape #f) (define updates 0) |
| 118 | (define-method (update-instance-for-different-class (old <object>) (new <object>)) |
| 119 | (set! updates (+ updates 1)) new) |
| 120 | (class-redefinition c (make-class (list ${family}) |
| 121 | (list (list (quote added) #:init-thunk |
| 122 | (lambda () (call/cc (lambda (k) (set! saved k) (escape (quote paused)))))) |
| 123 | (quote (x #:init-value 2))) #:metaclass <redefinable-class>))') or { panic(err) } |
| 124 | native_migration_expect(mut vm, '(call/cc (lambda (out) (set! escape out) |
| 125 | (slot-ref object (quote added))))', 'paused') |
| 126 | object := vm.lookup('object') or { panic(err) } |
| 127 | if object.goops_layout().migrating || vm.active.len != 0 { |
| 128 | panic('escaped native migration retained its guard') |
| 129 | } |
| 130 | for value in [7, 8] { native_migration_expect(mut vm, '(saved ${value})', '${value}') } |
| 131 | native_migration_expect(mut vm, '(list (slot-ref object (quote x)) |
| 132 | (slot-ref object (quote added)) updates)', '(1 8 1)') |
| 133 | vm.eval('(class-redefinition c (make-class (list ${family}) |
| 134 | (quote ((latest #:init-value 42))) #:metaclass <redefinable-class>))') or { panic(err) } |
| 135 | if _ := vm.eval('(saved 9)') { |
| 136 | panic('accepted a stale native migration continuation') |
| 137 | } else { |
| 138 | if !err.msg().contains('class changed') { panic(err) } |
| 139 | } |
| 140 | native_migration_expect(mut vm, '(slot-ref object (quote latest))', '42') |
| 141 | if object.goops_layout().migrating || vm.active.len != 0 { |
| 142 | panic('stale continuation retained native state') |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | fn test_native_layout_control_failure_rolls_back_and_reuses_the_vm() { |
| 148 | for family in ['<method>', '<slot>', '<class>', '<generic>'] { |
| 149 | mut vm := new() |
| 150 | vm.eval(native_migration_setup(family)) or { panic(err) } |
| 151 | vm.eval('(define fail? #t) |
| 152 | (class-redefinition c (make-class (list ${family}) |
| 153 | (list (quote (x #:init-value 2)) |
| 154 | (list (quote added) #:init-thunk (lambda () (if fail? (let loop () (loop)) 9)))) |
| 155 | #:metaclass <redefinable-class>))') or { panic(err) } |
| 156 | object := vm.lookup('object') or { panic(err) } |
| 157 | before := object.goops_layout() |
| 158 | vm.options = Options{ ...vm.options, max_steps: 1000 } |
| 159 | if _ := vm.eval('(slot-ref object (quote added))') { |
| 160 | panic('unbounded initializer ignored the step budget') |
| 161 | } else { |
| 162 | if err !is EvalError || !err.msg().contains('step limit') { panic(err) } |
| 163 | } |
| 164 | after := object.goops_layout() |
| 165 | if after.migrating || after.version != before.version || voidptr(after.class) != voidptr(before.class) |
| 166 | || vm.active.len != 0 { |
| 167 | panic('failed native initialization changed the live layout') |
| 168 | } |
| 169 | vm.options = Options{ ...vm.options, max_steps: 5000000 } |
| 170 | native_migration_expect(mut vm, '(set! fail? #f) |
| 171 | (list (slot-ref object (quote added)) (slot-ref object (quote x)))', '(9 1)') |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | fn test_native_metadata_migration_copies_dispatch_fields_and_retains_registration_effects() { |
| 176 | mut vm := new() |
| 177 | native_migration_expect(mut vm, '(define c (make-class (list <extended-generic>) |
| 178 | (quote ((x #:init-value 1))) #:metaclass <redefinable-class>)) |
| 179 | (define-method (parent (v <number>)) 1) |
| 180 | (define g (make c #:extends (list parent))) (define child (make-extended-generic g)) |
| 181 | (define old-child-list (slot-ref g (quote extended-by))) (define added #f) |
| 182 | (class-redefinition c (make-class (list <extended-generic>) |
| 183 | (list (quote (x #:init-value 2)) |
| 184 | (list (quote added) #:init-thunk |
| 185 | (lambda () (set! added (method ((v <integer>)) 2)) (add-method! g added) 3))) |
| 186 | #:metaclass <redefinable-class>)) |
| 187 | (list (slot-ref g (quote added)) (g 42) (parent 42) (child 42) |
| 188 | (eq? g (car (slot-ref child (quote extends)))) |
| 189 | (eq? child (car (slot-ref g (quote extended-by)))) |
| 190 | (eq? g (car (slot-ref parent (quote extended-by)))) |
| 191 | (length (slot-ref g (quote methods))) (length (generic-function-methods g)) |
| 192 | (eq? g (method-generic-function added)) |
| 193 | (if (memq added (class-direct-methods <integer>)) #t #f))', '(3 1 1 1 #t #t #t 0 1 #t #t)') |
| 194 | } |
| 195 | |
| 196 | fn test_native_current_layout_calls_remain_bounded_after_all_four_migrations() { |
| 197 | mut region := arena.new() |
| 198 | region.push() |
| 199 | mut vm := new_with_options(Options{ max_steps: 10000000 }) or { panic(err) } |
| 200 | for index, family in ['<method>', '<slot>', '<class>', '<generic>'] { |
| 201 | vm.eval(native_migration_setup(family)) or { panic(err) } |
| 202 | vm.eval('(define o${index} object) |
| 203 | (class-redefinition c (make-class (list ${family}) |
| 204 | (quote ((added #:init-value 2) (x #:init-value 3))) #:metaclass <redefinable-class>)) |
| 205 | (slot-ref object (quote added))') or { panic(err) } |
| 206 | } |
| 207 | vm.eval('(define (run n acc) (if (= n 0) acc |
| 208 | (begin (class-of o0) (class-of o1) (class-of o2) (class-of o3) |
| 209 | (run (- n 1) (+ acc (slot-ref o0 (quote x)) (slot-ref o1 (quote x)) |
| 210 | (slot-ref o2 (quote x)) (slot-ref o3 (quote x))))))) (run 50 0)') or { panic(err) } |
| 211 | vm.options = Options{ ...vm.options, max_frames: 8 } |
| 212 | run := vm.lookup('run') or { panic(err) } |
| 213 | args := [integer(50000), integer(0)] |
| 214 | before := region.used() |
| 215 | answer := vm.call(run, args) or { panic(err) } |
| 216 | delta := region.used() - before |
| 217 | if answer.whole() != 200000 { panic('incorrect native metadata tail loop') } |
| 218 | if delta > 65536 { panic('native metadata loop allocated ${delta} bytes') } |
| 219 | region.pop() |
| 220 | region.free() |
| 221 | } |
| 222 | |
| 223 | fn test_native_migration_initializers_can_change_native_policy_before_selection() { |
| 224 | mut vm := new() |
| 225 | native_migration_expect(mut vm, '(define c (make-class (list <method>) (quote ()) |
| 226 | #:metaclass <redefinable-class>)) |
| 227 | (define m (make c #:specializers (list <string>) #:procedure (lambda (v) 42))) |
| 228 | (define g (make-generic (quote g))) (add-method! g m) |
| 229 | (class-redefinition c (make-class (list <method>) |
| 230 | (list (list (quote policy) #:allocation #:virtual #:init-value 0 |
| 231 | #:slot-ref (lambda (object) 0) |
| 232 | #:slot-set! (lambda (object value) (slot-set! object (quote specializers) (list <integer>))))) |
| 233 | #:metaclass <redefinable-class>)) |
| 234 | (list (g 7) (eq? (car (method-specializers m)) <integer>))', '(42 #t)') |
| 235 | for family in ['<class>', '<generic>'] { |
| 236 | mut other := new() |
| 237 | other.eval(native_migration_setup(family)) or { panic(err) } |
| 238 | native_migration_expect(mut other, '(class-redefinition c (make-class (list ${family}) |
| 239 | (list (quote (x #:init-value 2)) |
| 240 | (list (quote rename) #:allocation #:virtual #:init-value 0 |
| 241 | #:slot-ref (lambda (object) 0) |
| 242 | #:slot-set! (lambda (object value) (slot-set! object (quote name) (quote renamed))))) |
| 243 | #:metaclass <redefinable-class>)) |
| 244 | (list (slot-ref object (quote x)) (slot-ref object (quote name)))', '(1 renamed)') |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | fn test_native_hook_supplied_method_migrates_without_repeating_applicability_effects() { |
| 249 | mut vm := new() |
| 250 | native_migration_expect(mut vm, '(define calls 0) (define updates 0) |
| 251 | (define-class <external-dispatch> (<generic>)) |
| 252 | (define c (make-class (list <method>) (quote ()) #:metaclass <redefinable-class>)) |
| 253 | (define m (make c #:specializers (list <integer>) #:procedure (lambda (v) updates))) |
| 254 | (define-method (compute-applicable-methods (g <external-dispatch>) args) |
| 255 | (set! calls (+ calls 1)) (list m)) |
| 256 | (define-method (update-instance-for-different-class (old <method>) (new <method>)) |
| 257 | (set! updates (+ updates 1)) new) |
| 258 | (define g (make <external-dispatch>)) |
| 259 | (class-redefinition c (make-class (list <method>) (quote ((added #:init-value 3))) |
| 260 | #:metaclass <redefinable-class>)) |
| 261 | (list (g 7) (g 8) calls updates)', '(1 1 1 1)') |
| 262 | native_migration_expect(mut vm, '(slot-set! m (quote procedure) (lambda (v) 42)) |
| 263 | (list (g 9) calls)', '(42 2)') |
| 264 | } |
| 265 | |
| 266 | fn test_native_prepared_copy_dynamic_extent_survives_escape_and_multishot_reentry() { |
| 267 | mut vm := new() |
| 268 | vm.eval('(define c (make-class (list <generic>) (quote ()) #:metaclass <redefinable-class>)) |
| 269 | (define object (make c)) (define prepared #f) (define saved #f) (define escape #f) |
| 270 | (class-redefinition c (make-class (list <generic>) |
| 271 | (list (list (quote pause) #:allocation #:virtual #:init-value 0 |
| 272 | #:slot-ref (lambda (object) 0) |
| 273 | #:slot-set! (lambda (copy value) |
| 274 | (set! prepared copy) |
| 275 | (slot-set! copy (quote name) |
| 276 | (call/cc (lambda (k) (set! saved k) (escape (quote paused)))))))) |
| 277 | #:metaclass <redefinable-class>))') or { panic(err) } |
| 278 | native_migration_expect(mut vm, '(call/cc (lambda (out) (set! escape out) |
| 279 | (class-of object)))', 'paused') |
| 280 | prepared := vm.lookup('prepared') or { panic(err) } |
| 281 | if prepared.goops_copy_flag() { panic('escaped prepared copy retained its staging flag') } |
| 282 | vm.eval('(saved 7)') or { panic(err) } |
| 283 | native_migration_expect(mut vm, '(slot-ref object (quote name))', '7') |
| 284 | if prepared.goops_copy_flag() { panic('committed copy retained its staging flag') } |
| 285 | vm.eval('(saved 8)') or { panic(err) } |
| 286 | native_migration_expect(mut vm, '(slot-ref object (quote name))', '7') |
| 287 | if prepared.goops_copy_flag() { panic('replayed copy retained its staging flag') } |
| 288 | } |
| 289 | |
| 290 | fn test_native_migration_host_cancellation_restores_each_family() { |
| 291 | for family in ['<method>', '<slot>', '<class>', '<generic>'] { |
| 292 | mut vm := new() |
| 293 | mut background := context.background() |
| 294 | mut ctx, cancel := context.with_cancel(mut background) |
| 295 | defer { cancel() } |
| 296 | vm.register('stop-native-migration', fn [cancel] (mut _vm VM, _args []Value) !Value { |
| 297 | cancel() |
| 298 | return integer(9) |
| 299 | }) or { panic(err) } |
| 300 | vm.eval(native_migration_setup(family)) or { panic(err) } |
| 301 | vm.eval('(class-redefinition c (make-class (list ${family}) |
| 302 | (list (quote (x #:init-value 2)) |
| 303 | (list (quote added) #:init-thunk (lambda () (stop-native-migration)))) |
| 304 | #:metaclass <redefinable-class>))') or { panic(err) } |
| 305 | if _ := vm.with_context(mut ctx, fn (mut vm VM) !Value { |
| 306 | return vm.eval('(class-of object)') |
| 307 | }) { |
| 308 | panic('native migration ignored cancellation') |
| 309 | } else { |
| 310 | if err !is CancellationRequest { panic(err) } |
| 311 | } |
| 312 | object := vm.lookup('object') or { panic(err) } |
| 313 | if object.goops_layout().migrating || vm.active.len != 0 { |
| 314 | panic('cancellation retained native migration state') |
| 315 | } |
| 316 | native_migration_expect(mut vm, '(list (slot-ref object (quote x)) |
| 317 | (slot-ref object (quote added)))', '(1 9)') |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | fn test_native_migration_and_allocation_reject_unsafe_borrowed_owners() { |
| 322 | for family in ['<method>', '<slot>', '<class>', '<generic>'] { |
| 323 | mut vm := new() |
| 324 | vm.eval(native_migration_setup(family)) or { panic(err) } |
| 325 | vm.eval('(class-redefinition c (make-class (list ${family}) |
| 326 | (quote ((x #:init-value 2) (added #:init-value 3))) #:metaclass <redefinable-class>))') or { panic(err) } |
| 327 | object := vm.lookup('object') or { panic(err) } |
| 328 | before := object.goops_layout() |
| 329 | mut child := arena.new() |
| 330 | child.push() |
| 331 | if _ := vm.eval('(slot-ref object (quote added))') { |
| 332 | panic('migrated an ancestor-owned native object from a child arena') |
| 333 | } else { |
| 334 | if !err.msg().contains('arena boundary') { panic(err) } |
| 335 | } |
| 336 | if object.goops_layout().migrating || object.goops_layout().version != before.version { |
| 337 | panic('owner rejection changed the native layout') |
| 338 | } |
| 339 | child.pop() |
| 340 | child.free() |
| 341 | native_migration_expect(mut vm, '(slot-ref object (quote added))', '3') |
| 342 | mut temporary := arena.new() |
| 343 | temporary.push() |
| 344 | class_value := vm.eval('(make-class (list ${family}) (quote ()))') or { panic(err) } |
| 345 | temporary.pop() |
| 346 | make := vm.lookup('make') or { panic(err) } |
| 347 | if _ := vm.call(make, [class_value]) { |
| 348 | panic('published a parent-owned instance of a child-arena class') |
| 349 | } else { |
| 350 | if !err.msg().contains('arena boundary') { panic(err) } |
| 351 | } |
| 352 | temporary.free() |
| 353 | native_migration_expect(mut vm, '(slot-ref object (quote x))', '1') |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | fn test_native_method_migration_copies_public_procedure_before_initializer_effect() { |
| 358 | mut vm := new() |
| 359 | native_migration_expect(mut vm, '(define c (make-class (list <method>) |
| 360 | (quote ((x #:init-value 1))) #:metaclass <redefinable-class>)) |
| 361 | (define object (make c #:specializers (list <integer>) #:procedure (lambda (v) 1))) |
| 362 | (class-redefinition c (make-class (list <method>) |
| 363 | (list (quote (x #:init-value 2)) |
| 364 | (list (quote policy) #:allocation #:virtual #:init-value 0 |
| 365 | #:slot-ref (lambda (o) 0) #:slot-set! (lambda (copy value) |
| 366 | (slot-set! copy (quote specializers) (list <number>)) |
| 367 | (slot-set! object (quote procedure) (lambda (v) 42))))) |
| 368 | #:metaclass <redefinable-class>)) |
| 369 | (class-of object) |
| 370 | (list (map class-name (method-specializers object)) ((method-procedure object) 7))', |
| 371 | '((<number>) 1)') |
| 372 | } |
| 373 | |
| 374 | fn test_native_generic_migration_copies_empty_dispatch_list_and_new_procedure() { |
| 375 | mut vm := new() |
| 376 | native_migration_expect(mut vm, '(define c (make-class (list <generic>) |
| 377 | (quote ((x #:init-value 1))) #:metaclass <redefinable-class>)) |
| 378 | (define object (make c)) (define added #f) |
| 379 | (class-redefinition c (make-class (list <generic>) |
| 380 | (list (quote (x #:init-value 2)) |
| 381 | (list (quote policy) #:allocation #:virtual #:init-value 0 |
| 382 | #:slot-ref (lambda (o) 0) #:slot-set! (lambda (copy value) |
| 383 | (slot-set! copy (quote procedure) (lambda (v) 42)) |
| 384 | (set! added (make <method> #:specializers (list <integer>) #:procedure (lambda (v) 2))) |
| 385 | (add-method! object added)))) #:metaclass <redefinable-class>)) |
| 386 | (class-of object) |
| 387 | (list (length (generic-function-methods object)) ((slot-ref object (quote procedure)) 7) |
| 388 | (eq? (method-generic-function added) object) |
| 389 | (if (memq added (class-direct-methods <integer>)) #t #f))', '(0 42 #t #t)') |
| 390 | } |
| 391 | |
| 392 | fn test_native_generic_migration_does_not_retarget_shared_method_backlinks() { |
| 393 | mut vm := new() |
| 394 | native_migration_expect(mut vm, '(define c (make-class (list <generic>) |
| 395 | (quote ((x #:init-value 1))) #:metaclass <redefinable-class>)) |
| 396 | (define object (make c)) (define other (make-generic (quote other))) |
| 397 | (define m (method ((v <integer>)) v)) (add-method! object m) (add-method! other m) |
| 398 | (class-redefinition c (make-class (list <generic>) |
| 399 | (quote ((x #:init-value 2) (added #:init-value 3))) #:metaclass <redefinable-class>)) |
| 400 | (class-of object) |
| 401 | (list (eq? (method-generic-function m) other) |
| 402 | (eq? m (car (generic-function-methods object))) |
| 403 | (eq? m (car (generic-function-methods other))))', '(#t #t #t)') |
| 404 | } |
| 405 | |
| 406 | fn test_native_metaclass_migration_retains_reentrant_own_class_redefinition() { |
| 407 | mut vm := new() |
| 408 | native_migration_expect(mut vm, '(define meta (make-class (list <redefinable-class>) |
| 409 | (quote ((x #:init-value 1))) #:metaclass <redefinable-class>)) |
| 410 | (define c (make-class (quote ()) (quote ((value #:init-value 42))) #:metaclass meta)) |
| 411 | (define object (make c)) (define once #t) |
| 412 | (class-redefinition meta (make-class (list <redefinable-class>) |
| 413 | (list (quote (x #:init-value 2)) |
| 414 | (list (quote added) #:init-thunk (lambda () |
| 415 | (if once (begin (set! once #f) |
| 416 | (class-redefinition c (make-class (quote ()) |
| 417 | (quote ((value #:init-value 99) (new #:init-value 100))) #:metaclass meta)))) 3))) |
| 418 | #:metaclass <redefinable-class>)) |
| 419 | (class-of c) |
| 420 | (list (eq? (class-of c) meta) (slot-ref c (quote x)) (slot-ref c (quote added)) |
| 421 | (slot-ref object (quote value)) (slot-ref object (quote new)) |
| 422 | (slot-ref (make c) (quote value)))', '(#t 2 3 42 100 99)') |
| 423 | c := vm.lookup('c') or { panic(err) } |
| 424 | if c.goops_class_pointer().meta_migrating || vm.active.len != 0 { |
| 425 | panic('reentrant class redefinition retained a migration registration') |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | fn test_native_class_snapshot_keeps_derived_arena_dependencies_weak() { |
| 430 | mut owner := arena.new() |
| 431 | owner.push() |
| 432 | mut vm := new() |
| 433 | vm.eval('(define meta (make-class (list <class>) (quote ((x #:init-value 1))) |
| 434 | #:metaclass <redefinable-class>)) |
| 435 | (define c (make-class (quote ()) (quote ()) #:metaclass meta))') or { panic(err) } |
| 436 | mut child := arena.new() |
| 437 | child.push() |
| 438 | vm.eval('(let ((derived (make-class (list c) (quote ()))) |
| 439 | (g (make-generic (quote temporary)))) |
| 440 | (add-method! g (make <method> #:specializers (list c) #:procedure (lambda (v) 1))) |
| 441 | (list derived g))') or { panic(err) } |
| 442 | child.pop() |
| 443 | // The child remains alive during migration and all positive API checks. |
| 444 | native_migration_expect(mut vm, '(class-redefinition meta (make-class (list <class>) |
| 445 | (quote ((x #:init-value 2) (added #:init-value 3))) #:metaclass <redefinable-class>)) |
| 446 | (list (slot-ref c (quote added)) (length (class-direct-subclasses c)) |
| 447 | (length (class-direct-methods c)))', '(3 1 1)') |
| 448 | class := (vm.lookup('c') or { panic(err) }).goops_class_pointer() |
| 449 | if isnil(class.direct_subclasses) || isnil(class.direct_methods) |
| 450 | || goops_public_reference_values(class.direct_subclasses).len != 1 |
| 451 | || goops_public_reference_values(class.direct_methods).len != 1 { |
| 452 | panic('native class snapshot did not retain live weak dependencies') |
| 453 | } |
| 454 | child.free() |
| 455 | // Only the registry's public filtered query runs after child teardown. |
| 456 | // No expired child Value is retained or dereferenced by this fixture. |
| 457 | native_migration_expect(mut vm, '(list (length (class-direct-subclasses c)) |
| 458 | (length (class-direct-methods c)) (slot-ref c (quote x)))', '(0 0 1)') |
| 459 | owner.pop() |
| 460 | owner.free() |
| 461 | } |
| 462 | |
| 463 | fn test_native_class_public_method_snapshot_keeps_explicit_registration_effects() { |
| 464 | mut vm := new() |
| 465 | native_migration_expect(mut vm, '(define meta (make-class (list <class>) |
| 466 | (quote ((x #:init-value 1))) #:metaclass <redefinable-class>)) |
| 467 | (define c (make-class (quote ()) (quote ()) #:metaclass meta)) |
| 468 | (define g (make-generic (quote g))) (define m #f) |
| 469 | (class-redefinition meta (make-class (list <class>) |
| 470 | (list (quote (x #:init-value 2)) (list (quote added) #:init-thunk (lambda () |
| 471 | (set! m (make <method> #:specializers (list c) #:procedure (lambda (v) 42))) |
| 472 | (add-method! g m) 3))) #:metaclass <redefinable-class>)) |
| 473 | (class-of c) |
| 474 | (define copied (list (slot-ref c (quote added)) (length (class-direct-methods c)) |
| 475 | (eq? (method-generic-function m) g) (eq? m (car (generic-function-methods g))))) |
| 476 | (add-method! g m) |
| 477 | (list copied (length (class-direct-methods c)) (g (make c)))', '((3 0 #t #t) 1 42)') |
| 478 | } |
| 479 | |
| 480 | fn test_native_class_failed_copy_keeps_public_metadata_and_private_dependencies() { |
| 481 | mut vm := new() |
| 482 | vm.eval('(define meta (make-class (list <class>) (quote ((x #:init-value 1))) |
| 483 | #:metaclass <redefinable-class>)) |
| 484 | (define c (make-class (quote ()) (quote ()) #:metaclass meta)) |
| 485 | (define derived (make-class (list c) (quote ()))) |
| 486 | (define g (make-generic (quote g))) |
| 487 | (define m (make <method> #:specializers (list c) #:procedure (lambda (v) 42))) |
| 488 | (add-method! g m) (define fail-copy #t) |
| 489 | (class-redefinition meta (make-class (list <class>) |
| 490 | (list (quote (x #:init-value 2)) |
| 491 | (list (quote policy) #:allocation #:virtual #:init-value 0 |
| 492 | #:slot-ref (lambda (o) 0) #:slot-set! (lambda (copy value) |
| 493 | (slot-set! copy (quote direct-methods) (quote ())) |
| 494 | (slot-set! copy (quote direct-subclasses) (quote ())) |
| 495 | (if fail-copy (car 0))))) |
| 496 | #:metaclass <redefinable-class>))') or { panic(err) } |
| 497 | c := (vm.lookup('c') or { panic(err) }).goops_class_pointer() |
| 498 | methods := c.direct_methods |
| 499 | subclasses := c.direct_subclasses |
| 500 | version := c.meta_layout_version |
| 501 | if _ := vm.eval('(class-of c)') { |
| 502 | panic('failed metadata copy committed') |
| 503 | } else { |
| 504 | if !err.msg().contains('pair') { panic(err) } |
| 505 | } |
| 506 | if c.meta_layout_version != version || c.meta_migrating || vm.active.len != 0 |
| 507 | || voidptr(c.direct_methods) != voidptr(methods) |
| 508 | || voidptr(c.direct_subclasses) != voidptr(subclasses) |
| 509 | || goops_public_reference_values(c.direct_methods).len != 1 |
| 510 | || goops_public_reference_values(c.direct_subclasses).len != 1 { |
| 511 | panic('failed metadata copy changed the source public view') |
| 512 | } |
| 513 | native_migration_expect(mut vm, '(set! fail-copy #f) (class-of c) |
| 514 | (list (length (class-direct-methods c)) (length (class-direct-subclasses c)) |
| 515 | (slot-ref c (quote x)) (g (make c)))', '(0 0 1 42)') |
| 516 | if vm.goops_direct_methods(c).len != 1 || vm.goops_direct_subclasses(c).len != 1 { |
| 517 | panic('copied public views removed private dependencies') |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | fn test_native_class_previous_view_stays_independent_of_later_publication() { |
| 522 | mut vm := new() |
| 523 | native_migration_expect(mut vm, '(define meta (make-class (list <class>) |
| 524 | (quote ((x #:init-value 1))) #:metaclass <redefinable-class>)) |
| 525 | (define c (make-class (quote ()) (quote ()) #:metaclass meta)) |
| 526 | (define g (make-generic (quote g))) (define h (make-generic (quote h))) |
| 527 | (define m (make <method> #:specializers (list c) #:procedure (lambda (v) 42))) |
| 528 | (add-method! g m) (define previous #f) |
| 529 | (define-method (update-instance-for-different-class (old <class>) (new <class>)) |
| 530 | (set! previous old) new) |
| 531 | (class-redefinition meta (make-class (list <class>) |
| 532 | (quote ((x #:init-value 2) (added #:init-value 3))) #:metaclass <redefinable-class>)) |
| 533 | (class-of c) |
| 534 | (add-method! h (make <method> #:specializers (list c) #:procedure (lambda (v) 99))) |
| 535 | (slot-set! c (quote x) 9) |
| 536 | (list (eq? previous c) (length (class-direct-methods previous)) |
| 537 | (length (class-direct-methods c)) (eq? m (car (class-direct-methods previous))) |
| 538 | (slot-ref previous (quote x)) (slot-exists? previous (quote added)) |
| 539 | (slot-ref c (quote added)))', '(#f 1 2 #t 1 #t 3)') |
| 540 | } |
| 541 | |
| 542 | fn test_native_allocated_copies_are_the_old_views_and_keep_their_owners() { |
| 543 | for family in ['<method>', '<slot>', '<class>', '<generic>'] { |
| 544 | mut owner := arena.new() |
| 545 | owner.push() |
| 546 | mut vm := new() |
| 547 | vm.eval('(define allocating-meta (make-class (list <redefinable-class>) (quote ())))') or { panic(err) } |
| 548 | vm.eval(native_migration_setup(family).replace('#:metaclass <redefinable-class>', '#:metaclass allocating-meta')) or { panic(err) } |
| 549 | object := vm.lookup('object') or { panic(err) } |
| 550 | physical_owner := object.goops_layout().allocation_owner |
| 551 | vm.eval('(define prepared #f) (define previous #f) |
| 552 | (define-method (allocate-instance (class allocating-meta) args) |
| 553 | (let ((copy (next-method))) (set! prepared copy) copy)) |
| 554 | (define-method (update-instance-for-different-class (old <object>) (new <object>)) |
| 555 | (set! previous old) new) |
| 556 | (define d (make-class (list ${family}) |
| 557 | (quote ((x #:init-value 2) (y #:init-value 3))) #:metaclass allocating-meta))') or { panic(err) } |
| 558 | native_migration_expect(mut vm, '(change-class object d) (slot-ref object (quote y)) |
| 559 | (slot-set! prepared (quote x) 9) |
| 560 | (list (eq? prepared previous) (slot-ref object (quote x)) |
| 561 | (slot-ref prepared (quote x)) (slot-exists? prepared (quote y)) |
| 562 | (slot-ref object (quote y)))', '(#t 1 9 #f 3)') |
| 563 | copy := vm.lookup('prepared') or { panic(err) } |
| 564 | if physical_owner == unsafe { nil } || object.goops_layout().allocation_owner != physical_owner |
| 565 | || copy.goops_layout().allocation_owner != physical_owner || object.goops_layout().migrating |
| 566 | || copy.goops_layout().migrating || copy.goops_copy_flag() || vm.active.len != 0 { |
| 567 | panic('native copy exchange changed a physical owner or retained dynamic state') |
| 568 | } |
| 569 | owner.pop() |
| 570 | owner.free() |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | fn test_native_prepared_generic_children_use_public_views_and_invalidate_cache() { |
| 575 | mut vm := new() |
| 576 | native_migration_expect(mut vm, "(define allocating-meta (make-class (list <redefinable-class>) '())) |
| 577 | (define c (make-class (list <extended-generic>) '() #:metaclass allocating-meta)) |
| 578 | (define object (make c #:extends '())) |
| 579 | (define child (make-extended-generic object)) |
| 580 | (add-method! object (method ((v <number>)) 1)) |
| 581 | (add-method! child (method ((v <integer>)) 3)) |
| 582 | (define (selected g) ((method-procedure (car (compute-applicable-methods g (list 7)))) 7)) |
| 583 | (define prepared #f) (define snapshots '()) |
| 584 | (define-method (allocate-instance (class allocating-meta) args) |
| 585 | (let ((copy (next-method))) (set! prepared copy) copy)) |
| 586 | (define d (make-class (list <extended-generic>) |
| 587 | (list (list 'policy #:allocation #:virtual #:init-value 0 |
| 588 | #:slot-ref (lambda (o) 0) #:slot-set! (lambda (copy value) |
| 589 | (set! snapshots (list (selected copy))) |
| 590 | (slot-set! copy 'extended-by '()) |
| 591 | (set! snapshots (append snapshots (list (selected copy)))) |
| 592 | (slot-set! copy 'extended-by (list child)) |
| 593 | (set! snapshots (append snapshots (list (selected copy))))))) |
| 594 | #:metaclass allocating-meta)) |
| 595 | (change-class object d) |
| 596 | (define before (list snapshots (selected object) (selected prepared) (prepared 3/2))) |
| 597 | (add-method! child (method ((v <fraction>)) 6)) |
| 598 | (define after-child-edit (prepared 3/2)) |
| 599 | (slot-set! prepared 'extended-by '()) |
| 600 | (append before (list after-child-edit (selected prepared) (selected object))) |
| 601 | ", '((3 1 3) 3 3 1 6 1 3)') |
| 602 | } |
| 603 | |
| 604 | fn test_native_method_copy_fields_and_explicit_installation_are_independent() { |
| 605 | mut vm := new() |
| 606 | native_migration_expect(mut vm, "(define meta (make-class (list <redefinable-class>) '())) |
| 607 | (define c (make-class (list <method>) '((x #:init-value 1)) #:metaclass meta)) |
| 608 | (define object (make c #:specializers (list <number>) #:procedure (lambda (v) 1))) |
| 609 | (define h (make-generic 'h)) (define prepared #f) (define previous #f) (define warm #f) |
| 610 | (define-method (allocate-instance (class meta) args) |
| 611 | (let ((copy (next-method))) |
| 612 | (initialize copy (list #:specializers (list <integer>) #:procedure (lambda (v) 99))) |
| 613 | (add-method! h copy) |
| 614 | (set! warm (h 7)) (set! prepared copy) copy)) |
| 615 | (define-method (update-instance-for-different-class (old <object>) (new <object>)) |
| 616 | (set! previous old) new) |
| 617 | (define d (make-class (list <method>) '((x #:init-value 2) (y #:init-value 3)) #:metaclass meta)) |
| 618 | (change-class object d) |
| 619 | (list warm (h 7) (h 2.5) ((method-procedure prepared) 7) ((method-procedure object) 7) (eq? prepared previous) |
| 620 | (method-generic-function prepared) (eq? prepared (car (generic-function-methods h)))) |
| 621 | ", '(99 99 1 1 1 #t #f #t)') |
| 622 | } |
| 623 | |
| 624 | fn test_native_method_previous_view_adopts_after_the_update_hook() { |
| 625 | mut vm := new() |
| 626 | native_migration_expect(mut vm, '(define allocating-meta (make-class (list <redefinable-class>) (quote ()))) |
| 627 | (define c (make-class (list <method>) (quote ((x #:init-value 1))) #:metaclass allocating-meta)) |
| 628 | (define object (make c #:specializers (list <integer>) #:procedure (lambda (v) v))) |
| 629 | (define prepared #f) (define previous #f) |
| 630 | (define-method (allocate-instance (class allocating-meta) args) |
| 631 | (let ((copy (next-method))) (set! prepared copy) copy)) |
| 632 | (define-method (update-instance-for-different-class (old <object>) (new <object>)) |
| 633 | (set! previous old) new) |
| 634 | (class-redefinition c (make-class (list <method>) (quote ((x #:init-value 2) (y #:init-value 3))) #:metaclass allocating-meta)) |
| 635 | (define observed (slot-ref object (quote y))) |
| 636 | (define old prepared) |
| 637 | (define same (eq? old previous)) |
| 638 | (define current-class (eq? (class-of old) c)) |
| 639 | (slot-set! old (quote x) 9) |
| 640 | (list observed same current-class (slot-ref old (quote y)) |
| 641 | (slot-ref old (quote x)) (slot-ref object (quote x))) |
| 642 | ', '(3 #t #t 3 9 1)') |
| 643 | } |
| 644 | |
| 645 | fn test_native_slot_previous_view_adopts_after_the_update_hook() { |
| 646 | mut vm := new() |
| 647 | native_migration_expect(mut vm, '(define allocating-meta (make-class (list <redefinable-class>) (quote ()))) |
| 648 | (define c (make-class (list <slot>) (quote ((x #:init-value 1))) #:metaclass allocating-meta)) |
| 649 | (define object (make c #:name (quote value))) |
| 650 | (define prepared #f) (define previous #f) |
| 651 | (define-method (allocate-instance (class allocating-meta) args) |
| 652 | (let ((copy (next-method))) (set! prepared copy) copy)) |
| 653 | (define-method (update-instance-for-different-class (old <object>) (new <object>)) |
| 654 | (set! previous old) new) |
| 655 | (class-redefinition c (make-class (list <slot>) (quote ((x #:init-value 2) (y #:init-value 3))) #:metaclass allocating-meta)) |
| 656 | (define observed (slot-ref object (quote y))) |
| 657 | (define old prepared) |
| 658 | (define same (eq? old previous)) |
| 659 | (define current-class (eq? (class-of old) c)) |
| 660 | (slot-set! old (quote x) 9) |
| 661 | (list observed same current-class (slot-ref old (quote y)) |
| 662 | (slot-ref old (quote x)) (slot-ref object (quote x))) |
| 663 | ', '(3 #t #t 3 9 1)') |
| 664 | } |
| 665 | |
| 666 | fn test_native_class_previous_view_adopts_after_the_update_hook() { |
| 667 | mut vm := new() |
| 668 | native_migration_expect(mut vm, '(define allocating-meta (make-class (list <redefinable-class>) (quote ()))) |
| 669 | (define c (make-class (list <class>) (quote ((x #:init-value 1))) #:metaclass allocating-meta)) |
| 670 | (define object (make-class (quote ()) (quote ((value #:init-value 42))) #:metaclass c)) |
| 671 | (define prepared #f) (define previous #f) |
| 672 | (define-method (allocate-instance (class allocating-meta) args) |
| 673 | (let ((copy (next-method))) (set! prepared copy) copy)) |
| 674 | (define-method (update-instance-for-different-class (old <object>) (new <object>)) |
| 675 | (set! previous old) new) |
| 676 | (class-redefinition c (make-class (list <class>) (quote ((x #:init-value 2) (y #:init-value 3))) #:metaclass allocating-meta)) |
| 677 | (define observed (slot-ref object (quote y))) |
| 678 | (define old prepared) |
| 679 | (define same (eq? old previous)) |
| 680 | (define current-class (eq? (class-of old) c)) |
| 681 | (slot-set! old (quote x) 9) |
| 682 | (list observed same current-class (slot-ref old (quote y)) |
| 683 | (slot-ref old (quote x)) (slot-ref object (quote x))) |
| 684 | ', '(3 #t #t 3 9 1)') |
| 685 | canonical := (vm.lookup('object') or { panic(err) }).goops_class_pointer() |
| 686 | definition := (vm.eval('(class-slot-definition object (quote value))') or { panic(err) }).goops_slot_pointer() |
| 687 | index := goops_slot_index(canonical, 'value') |
| 688 | if index < 0 || voidptr(definition.class) != voidptr(canonical) |
| 689 | || voidptr(definition.slot) != unsafe { voidptr(&canonical.slots[index]) } { |
| 690 | panic('adopting the copied class view retargeted another class canonical definition') |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | fn test_native_generic_previous_view_adopts_after_the_update_hook() { |
| 695 | mut vm := new() |
| 696 | native_migration_expect(mut vm, '(define allocating-meta (make-class (list <redefinable-class>) (quote ()))) |
| 697 | (define c (make-class (list <generic>) (quote ((x #:init-value 1))) #:metaclass allocating-meta)) |
| 698 | (define object (make c)) |
| 699 | (define prepared #f) (define previous #f) |
| 700 | (define-method (allocate-instance (class allocating-meta) args) |
| 701 | (let ((copy (next-method))) (set! prepared copy) copy)) |
| 702 | (define-method (update-instance-for-different-class (old <object>) (new <object>)) |
| 703 | (set! previous old) new) |
| 704 | (class-redefinition c (make-class (list <generic>) (quote ((x #:init-value 2) (y #:init-value 3))) #:metaclass allocating-meta)) |
| 705 | (define observed (slot-ref object (quote y))) |
| 706 | (define old prepared) |
| 707 | (define same (eq? old previous)) |
| 708 | (define current-class (eq? (class-of old) c)) |
| 709 | (slot-set! old (quote x) 9) |
| 710 | (list observed same current-class (slot-ref old (quote y)) |
| 711 | (slot-ref old (quote x)) (slot-ref object (quote x))) |
| 712 | ', '(3 #t #t 3 9 1)') |
| 713 | } |
| 714 | |
| 715 | fn test_native_previous_class_view_stays_historical_inside_the_update_hook() { |
| 716 | mut vm := new() |
| 717 | native_migration_expect(mut vm, '(define allocating-meta (make-class (list <redefinable-class>) (quote ()))) |
| 718 | (define c (make-class (list <class>) (quote ((x #:init-value 1))) #:metaclass allocating-meta)) |
| 719 | (define object (make-class (quote ()) (quote ((value #:init-value 42))) #:metaclass c)) |
| 720 | (define prepared #f) (define previous #f) (define inspected #f) (define seen #f) |
| 721 | (define-method (allocate-instance (class allocating-meta) args) |
| 722 | (let ((copy (next-method))) (set! prepared copy) copy)) |
| 723 | (define-method (update-instance-for-different-class (old <object>) (new <object>)) |
| 724 | (if (not inspected) (begin (set! inspected #t) (set! previous old) |
| 725 | (set! seen (list (eq? (class-of old) c) (slot-exists? old (quote y)))))) new) |
| 726 | (class-redefinition c (make-class (list <class>) (quote ((x #:init-value 2) (y #:init-value 3))) #:metaclass allocating-meta)) |
| 727 | (define observed (slot-ref object (quote y))) |
| 728 | (list observed seen (eq? (class-of previous) c)) |
| 729 | ', '(3 (#f #f) #t)') |
| 730 | } |
| 731 | |