| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | |
| 5 | // Keep setup and verification outside assertions so -prod executes the oracle. |
| 6 | fn test_goops_production_redefinition_in_vile_mode_preserves_class_and_instance_identity() { |
| 7 | mut vm := new() |
| 8 | vm.eval('(define base (make-class (quote ()) (quote ()) #:metaclass <redefinable-class>))') or { panic(err) } |
| 9 | value := vm.eval_alt('define-class(local_class, (base,), (x, #:init-value, 1)) |
| 10 | retained := local_class |
| 11 | object := make(local_class) |
| 12 | define-class(local_class, (base,), (added, #:init-value, 2), (x, #:init-value, 3)) |
| 13 | list(eq?(retained, local_class), eq?(class-of(object), retained), |
| 14 | slot-ref(object, quote(x)), slot-ref(object, quote(added)))') or { panic(err) } |
| 15 | actual := write(value) or { panic(err) } |
| 16 | if actual != '(#t #t 1 2)' { panic('wrong Vile class migration: ${actual}') } |
| 17 | } |
| 18 | |
| 19 | fn test_goops_production_current_layout_calls_keep_bounded_arena_usage_after_redefinition() { |
| 20 | mut region := arena.new() |
| 21 | region.push() |
| 22 | mut vm := new_with_options(Options{ max_steps: 5000000 }) or { panic(err) } |
| 23 | vm.eval('(define get-x (make-accessor)) |
| 24 | (define a (make-class (quote ()) |
| 25 | (list (list (quote x) #:init-value 2 #:accessor get-x)) #:metaclass <redefinable-class>)) |
| 26 | (define object (make a)) |
| 27 | (class-redefinition a (make-class (quote ()) |
| 28 | (list (quote (added #:init-value 3)) (list (quote x) #:init-value 5 #:accessor get-x)) |
| 29 | #:metaclass <redefinable-class>)) |
| 30 | (get-x object) |
| 31 | (define (run n acc) |
| 32 | (if (= n 0) acc (begin (class-of object) (run (- n 1) (+ acc (get-x object)))))) |
| 33 | (run 50 0)') or { panic(err) } |
| 34 | vm.options = Options{ ...vm.options, max_frames: 6 } |
| 35 | run := vm.lookup('run') or { panic(err) } |
| 36 | args := [integer(50000), integer(0)] |
| 37 | before := region.used() |
| 38 | answer := vm.call(run, args) or { panic(err) } |
| 39 | delta := region.used() - before |
| 40 | if answer.whole() != 100000 { panic('wrong current-layout loop result') } |
| 41 | if delta > 65536 { panic('current-layout calls allocated ${delta} bytes') } |
| 42 | region.pop() |
| 43 | region.free() |
| 44 | } |
| 45 | |
| 46 | fn test_goops_production_initializer_replay_and_stale_target_rejection() { |
| 47 | mut vm := new() |
| 48 | vm.eval('(define a (make-class (quote ()) (quote ((x #:init-value 1))) #:metaclass <redefinable-class>)) |
| 49 | (define object (make a)) (define saved #f) (define escape #f) (define updates 0) |
| 50 | (define-method (update-instance-for-different-class (old <object>) (new <object>)) |
| 51 | (set! updates (+ updates 1)) new) |
| 52 | (class-redefinition a (make-class (quote ()) |
| 53 | (list (list (quote added) #:init-thunk |
| 54 | (lambda () (call/cc (lambda (k) (set! saved k) (escape (quote escaped)))))) |
| 55 | (quote (x #:init-value 0))) #:metaclass <redefinable-class>))') or { panic(err) } |
| 56 | left := vm.eval('(call/cc (lambda (out) (set! escape out) (slot-ref object (quote added))))') or { panic(err) } |
| 57 | if (write(left) or { panic(err) }) != 'escaped' { panic('initializer did not escape') } |
| 58 | saved := vm.lookup('saved') or { panic(err) } |
| 59 | for value in [7, 8] { |
| 60 | answer := vm.call(saved, [integer(value)]) or { panic(err) } |
| 61 | if answer.whole() != value { panic('wrong initializer continuation result') } |
| 62 | object := vm.lookup('object') or { panic(err) }.goops_instance_pointer() |
| 63 | if object.migrating || object.layout_version != object.class.version { |
| 64 | panic('initializer replay left an obsolete or active instance') |
| 65 | } |
| 66 | } |
| 67 | values := vm.eval('(list (slot-ref object (quote x)) (slot-ref object (quote added)) updates)') or { panic(err) } |
| 68 | if (write(values) or { panic(err) }) != '(1 8 1)' { |
| 69 | panic('initializer replay replaced current storage') |
| 70 | } |
| 71 | vm.eval('(class-redefinition a (make-class (quote ()) (quote ((latest #:init-value 42))) |
| 72 | #:metaclass <redefinable-class>))') or { panic(err) } |
| 73 | if _ := vm.call(saved, [integer(9)]) { |
| 74 | panic('accepted a stale migration continuation') |
| 75 | } else { |
| 76 | if err !is EvalError || !err.msg().contains('class changed') { panic(err) } |
| 77 | } |
| 78 | answer := vm.eval('(slot-ref object (quote latest))') or { panic(err) } |
| 79 | if answer.whole() != 42 || vm.active.len != 0 { panic('stale migration left an unusable VM') } |
| 80 | } |
| 81 | |