| 1 | module scheme |
| 2 | |
| 3 | fn (mut vm VM) goops_make_low_accessors(value Value) !Value { |
| 4 | return vm.install_goops_low_accessors(value, true) |
| 5 | } |
| 6 | |
| 7 | fn (mut vm VM) install_goops_low_accessors(value Value, return_pair bool) !Value { |
| 8 | mut definition := vm.checked_goops_slot_mutation(value)! |
| 9 | env := &Environment{ slots: [value], escaped: true } |
| 10 | raw := procedure_value(&Procedure{ owner: vm.owner, mode: .goops_low_get, name: 'slot-ref/raw', env: env }) |
| 11 | checked := procedure_value(&Procedure{ owner: vm.owner, mode: .goops_low_checked_get, name: 'slot-ref', env: env }) |
| 12 | setter := procedure_value(&Procedure{ owner: vm.owner, mode: .goops_low_set, name: 'slot-set!', env: env }) |
| 13 | result := if return_pair { list([raw, setter]) } else { unspecified() } |
| 14 | definition.slot.raw_getter = raw |
| 15 | definition.slot.checked_getter = checked |
| 16 | definition.slot.raw_setter = setter |
| 17 | definition.accessor_generation++ |
| 18 | return result |
| 19 | } |
| 20 | |
| 21 | fn (mut vm VM) goops_slot_raw_read(object Value, slot &GoopsSlot) !Value { |
| 22 | if slot.virtual { return vm.call_small(slot.virtual_getter, object, Value{}, 1) } |
| 23 | if slot.class_allocated { |
| 24 | if isnil(slot.shared) { return failure('shared slot has no storage') } |
| 25 | return unsafe { *slot.shared } |
| 26 | } |
| 27 | if slot.native_field != .none { return vm.goops_native_slot_read(object, slot.native_field) } |
| 28 | return unsafe { *vm.goops_field_pointer(object, slot.instance_index)! } |
| 29 | } |
| 30 | |
| 31 | fn (mut m Machine) apply_goops_low(mut vm VM, p &Procedure, base int, argc int, tail bool) !int { |
| 32 | setter := p.mode == .goops_low_set |
| 33 | check_arity(p.name, argc, if setter { 2 } else { 1 }, if setter { 2 } else { 1 })! |
| 34 | if setter { goops_check_slot_value(m.stack[base + 2])! } |
| 35 | definition := vm.checked_goops_slot_definition(p.env.slots[0])! |
| 36 | mut slot := definition.slot |
| 37 | object := m.stack[base + 1] |
| 38 | if !isnil(definition.class.live_class) && object.goops_kind() == .goops_instance |
| 39 | && voidptr(object.goops_instance_pointer().class) != voidptr(definition.class) { |
| 40 | // A retained raw accessor still names its slot after layout replacement. |
| 41 | // Resolve only this historical case; ordinary access keeps its index. |
| 42 | slot = vm.goops_slot_definition(object, slot.name)! |
| 43 | } |
| 44 | if slot.virtual { |
| 45 | if setter { |
| 46 | container_store_validated(mut m.stack, base, slot.virtual_setter) |
| 47 | return 2 |
| 48 | } |
| 49 | if p.mode == .goops_low_checked_get { |
| 50 | container_store_validated(mut m.stack, base, vm.goops.virtual_read) |
| 51 | container_store_validated(mut m.stack, base + 1, slot.virtual_getter) |
| 52 | m.push(object) |
| 53 | m.push(slot.metadata.name) |
| 54 | return 3 |
| 55 | } |
| 56 | container_store_validated(mut m.stack, base, slot.virtual_getter) |
| 57 | return 1 |
| 58 | } |
| 59 | result := if setter { |
| 60 | value := m.stack[base + 2] |
| 61 | if slot.read_only && !vm.goops_unbound(vm.goops_slot_raw_read(object, slot)!) { |
| 62 | return failure('cannot modify a read-only slot') |
| 63 | } |
| 64 | vm.goops_slot_write_validated(object, slot, value)! |
| 65 | } else { |
| 66 | value := vm.goops_slot_raw_read(object, slot)! |
| 67 | if vm.goops_unbound(value) && p.mode == .goops_low_checked_get { |
| 68 | return m.prepare_unbound_slot(vm, base, object, slot.metadata.name, slot.class_allocated) |
| 69 | } |
| 70 | if vm.goops_unbound(value) { vm.goops.unbound } else { value } |
| 71 | } |
| 72 | if tail { |
| 73 | m.finish_call(result) |
| 74 | } else { |
| 75 | m.truncate(base + 1) |
| 76 | container_store_validated(mut m.stack, base, result) |
| 77 | } |
| 78 | return 0 |
| 79 | } |
| 80 | |
| 81 | fn goops_low_accessors_native(mut vm VM, args []Value) !Value { |
| 82 | check_arity('slot-accessors', args.len, 1, 1)! |
| 83 | return vm.goops_make_low_accessors(args[0]) |
| 84 | } |
| 85 | |
| 86 | fn goops_share_slot_native(mut vm VM, args []Value) !Value { |
| 87 | check_arity('share-slot', args.len, 2, 2)! |
| 88 | goops_check_slot_value(args[1])! |
| 89 | definition := vm.checked_goops_slot_mutation(args[0])! |
| 90 | mut slot := definition.slot |
| 91 | slot.class_allocated = true |
| 92 | slot.virtual = false |
| 93 | cell := unsafe { &Value(vcalloc(int(sizeof(Value)))) } |
| 94 | unsafe { *cell = args[1] } |
| 95 | slot.shared = cell |
| 96 | return vm.goops_make_low_accessors(args[0]) |
| 97 | } |
| 98 | |
| 99 | fn goops_inherit_slot_native(mut vm VM, args []Value) !Value { |
| 100 | check_arity('inherit-slot', args.len, 2, 2)! |
| 101 | definition := vm.checked_goops_slot_mutation(args[0])! |
| 102 | mut slot := definition.slot |
| 103 | original := vm.goops_retained_slot(definition.allocation_owner, args[1])!.slot |
| 104 | slot.class_allocated = original.class_allocated |
| 105 | slot.virtual = original.virtual |
| 106 | slot.shared = original.shared |
| 107 | slot.virtual_getter = original.virtual_getter |
| 108 | slot.virtual_setter = original.virtual_setter |
| 109 | return vm.goops_make_low_accessors(args[0]) |
| 110 | } |
| 111 | |
| 112 | fn goops_static_slot_native(mut vm VM, args []Value) !Value { |
| 113 | check_arity('static-slot?', args.len, 1, 1)! |
| 114 | return boolean(vm.checked_goops_slot(args[0])!.native_field != .none) |
| 115 | } |
| 116 | |
| 117 | fn goops_install_slot_native(mut vm VM, args []Value) !Value { |
| 118 | check_arity('install-slot-access', args.len, 3, 3)! |
| 119 | class := vm.checked_goops_class(args[0])! |
| 120 | definition := vm.checked_goops_slot_mutation(args[1])! |
| 121 | mut slot := definition.slot |
| 122 | allocation := slot.metadata.allocation |
| 123 | slot.class_allocated = allocation.tag == .keyword && allocation.text() in ['class', 'each-subclass'] |
| 124 | slot.each_subclass = allocation.tag == .keyword && allocation.text() == 'each-subclass' |
| 125 | if args[2].tag == .integer { |
| 126 | index := args[2].as_integer()! |
| 127 | if index < 0 || index >= class.instance_fields { |
| 128 | return failure('slot allocator returned an invalid index') |
| 129 | } |
| 130 | slot.instance_index = int(index) |
| 131 | slot.virtual = false |
| 132 | return vm.goops_make_low_accessors(args[1]) |
| 133 | } |
| 134 | pair := proper_list_values(args[2])! |
| 135 | if pair.len != 2 { return failure('slot allocator must return an index or getter/setter pair') } |
| 136 | vm.check_callable_owner(pair[0])! |
| 137 | vm.check_callable_owner(pair[1])! |
| 138 | native := pair[0].tag == .procedure && pair[0].procedure_pointer().mode in [.goops_low_get, .goops_low_checked_get] && eqv(pair[0].procedure_pointer().env.slots[0], args[1]) |
| 139 | if !native { |
| 140 | slot.virtual = true |
| 141 | slot.virtual_getter = pair[0] |
| 142 | slot.virtual_setter = pair[1] |
| 143 | } |
| 144 | return vm.goops_make_low_accessors(args[1]) |
| 145 | } |
| 146 | |
| 147 | fn (mut vm VM) goops_fast_make(args []Value) !(Value, bool) { |
| 148 | if !vm.goops.protocols_ready || args.len == 0 || args[0].tag != .goops_class { |
| 149 | return Value{}, false |
| 150 | } |
| 151 | if vm.goops.make_binding < 0 || !eqv(vm.globals[vm.goops.make_binding], vm.goops.make_instance) || !eqv(vm.globals[vm.goops.initialize_binding], vm.goops.initialize) || !eqv(vm.globals[vm.goops.allocate_binding], vm.goops.allocate_instance) { |
| 152 | return Value{}, false |
| 153 | } |
| 154 | if vm.checked_goops_generic(vm.goops.make_instance)!.generation != vm.goops.make_generation || vm.checked_goops_generic(vm.goops.initialize)!.generation != vm.goops.initialize_generation || vm.checked_goops_generic(vm.goops.allocate_instance)!.generation != vm.goops.allocate_generation { |
| 155 | return Value{}, false |
| 156 | } |
| 157 | class := vm.checked_goops_class(args[0])! |
| 158 | if class.representation != .object || class.has_init_thunks { return Value{}, false } |
| 159 | goops_check_initargs(args[1..], 'make')! |
| 160 | object := vm.goops_allocate_object(class)! |
| 161 | for index in 0 .. class.slots.len { |
| 162 | slot := &class.slots[index] |
| 163 | value := if slot.has_init_keyword { |
| 164 | goops_initarg(args[1..], slot.init_keyword, vm.goops.unbound) |
| 165 | } else { |
| 166 | vm.goops.unbound |
| 167 | } |
| 168 | actual := if !vm.goops_unbound(value) { |
| 169 | value |
| 170 | } else if !slot.class_allocated && slot.has_init_value { |
| 171 | slot.init_value |
| 172 | } else { |
| 173 | continue |
| 174 | } |
| 175 | if actual.tag == .multiple { return failure('slot initializer requires one value') } |
| 176 | vm.goops_slot_write_validated(object, slot, actual)! |
| 177 | } |
| 178 | return object, true |
| 179 | } |
| 180 | |