| 1 | module scheme |
| 2 | |
| 3 | @[heap] |
| 4 | struct GoopsSlotMetadata { |
| 5 | mut: |
| 6 | allocation Value |
| 7 | init_keyword Value |
| 8 | name Value |
| 9 | options Value |
| 10 | init_form Value |
| 11 | has_init_form bool |
| 12 | init_thunk Value |
| 13 | } |
| 14 | |
| 15 | @[heap] |
| 16 | struct GoopsSlotDefinition { |
| 17 | owner &Owner |
| 18 | allocation_owner voidptr = v_arena_current() |
| 19 | mut: |
| 20 | accessor_generation u64 |
| 21 | class &GoopsClass |
| 22 | type_class &GoopsClass = unsafe { nil } |
| 23 | layout_version u64 = 1 |
| 24 | migrating bool |
| 25 | migration_copy bool |
| 26 | slot &GoopsSlot |
| 27 | options Value |
| 28 | fields []Value |
| 29 | storage []GoopsSlot |
| 30 | } |
| 31 | |
| 32 | fn (value Value) goops_slot_pointer() &GoopsSlotDefinition { |
| 33 | return unsafe { &GoopsSlotDefinition(value.pointer()) } |
| 34 | } |
| 35 | |
| 36 | fn (vm &VM) checked_goops_slot_definition(value Value) !&GoopsSlotDefinition { |
| 37 | if value.tag != .goops_slot { return failure('expected a slot definition') } |
| 38 | definition := value.goops_slot_pointer() |
| 39 | if voidptr(definition.owner) != voidptr(vm.owner) { |
| 40 | return failure('slot belongs to another VM') |
| 41 | } |
| 42 | return definition |
| 43 | } |
| 44 | |
| 45 | // Reading a definition may borrow an ancestor's storage. Rebuilding or retargeting |
| 46 | // its metadata requires the same mutation domain as ordinary slot-set!. |
| 47 | fn (vm &VM) checked_goops_slot_mutation(value Value) !&GoopsSlotDefinition { |
| 48 | definition := vm.checked_goops_slot_definition(value)! |
| 49 | vm.goops_require_mutation(definition.allocation_owner)! |
| 50 | return definition |
| 51 | } |
| 52 | |
| 53 | fn (vm &VM) goops_retained_slot(owner voidptr, value Value) !&GoopsSlotDefinition { |
| 54 | definition := vm.checked_goops_slot_definition(value)! |
| 55 | vm.goops_require_retention(owner, definition.allocation_owner)! |
| 56 | return definition |
| 57 | } |
| 58 | |
| 59 | fn (vm &VM) checked_goops_slot(value Value) !&GoopsSlot { |
| 60 | return vm.checked_goops_slot_definition(value)!.slot |
| 61 | } |
| 62 | |
| 63 | fn (vm &VM) goops_unbound(value Value) bool { |
| 64 | return value.tag == .uninitialized || (value.tag == .pair && value.pointer() == vm.goops.unbound.pointer()) |
| 65 | } |
| 66 | |
| 67 | fn goops_constant_initializer(vm &VM, value Value) Value { |
| 68 | function := &Function{ |
| 69 | code: unsafe { nil } |
| 70 | params: 0 |
| 71 | slot_count: 0 |
| 72 | constants: [value] |
| 73 | instructions: [ |
| 74 | bridge_instruction(.constant, 0, 0), |
| 75 | bridge_instruction(.ret, 0, 0), |
| 76 | ] |
| 77 | } |
| 78 | return procedure_value(&Procedure{ owner: vm.owner, mode: .closure, name: 'slot initializer', function: function }) |
| 79 | } |
| 80 | |
| 81 | fn (vm &VM) install_goops_slot_definitions(mut class GoopsClass) { |
| 82 | mut direct := if class.direct_definitions.len == class.direct_slots.len { |
| 83 | class.direct_definitions |
| 84 | } else { |
| 85 | value_storage(class.direct_slots.len) |
| 86 | } |
| 87 | for i in 0 .. class.direct_slots.len { |
| 88 | if direct[i].tag == .goops_slot { continue } |
| 89 | container_store_validated(mut direct, i, payload_value(.goops_slot, &GoopsSlotDefinition{ owner: vm.owner, class: class, slot: &class.direct_slots[i], options: class.direct_slots[i].metadata.options })) |
| 90 | } |
| 91 | mut effective := value_storage(class.slots.len) |
| 92 | for i in 0 .. class.slots.len { |
| 93 | container_store_validated(mut effective, i, payload_value(.goops_slot, &GoopsSlotDefinition{ owner: vm.owner, class: class, slot: &class.slots[i], options: copy_list(class.slots[i].metadata.options) or { panic(err) } })) |
| 94 | } |
| 95 | class.direct_definitions = direct |
| 96 | class.definitions = effective |
| 97 | } |
| 98 | |
| 99 | fn goops_definition_list(definitions []Value) Value { |
| 100 | mut result := nil_value() |
| 101 | for i := definitions.len - 1; i >= 0; i-- { result = cons(definitions[i], result) } |
| 102 | return result |
| 103 | } |
| 104 | |
| 105 | fn goops_class_direct_slots_native(mut vm VM, args []Value) !Value { |
| 106 | check_arity('class-direct-slots', args.len, 1, 1)! |
| 107 | return goops_definition_list(vm.checked_goops_class(args[0])!.direct_definitions) |
| 108 | } |
| 109 | |
| 110 | fn goops_class_slots_native(mut vm VM, args []Value) !Value { |
| 111 | check_arity('class-slots', args.len, 1, 1)! |
| 112 | return goops_definition_list(vm.checked_goops_class(args[0])!.definitions) |
| 113 | } |
| 114 | |
| 115 | fn goops_class_slot_definition_native(mut vm VM, args []Value) !Value { |
| 116 | check_arity('class-slot-definition', args.len, 2, 2)! |
| 117 | class := vm.checked_goops_class(args[0])! |
| 118 | if args[1].tag != .symbol { return failure('class-slot-definition needs a symbol slot name') } |
| 119 | index := goops_slot_index(class, args[1].text()) |
| 120 | return if index < 0 { boolean(false) } else { class.definitions[index] } |
| 121 | } |
| 122 | |
| 123 | fn goops_slot_name_native(mut vm VM, args []Value) !Value { |
| 124 | check_arity('slot-definition-name', args.len, 1, 1)! |
| 125 | return vm.checked_goops_slot(args[0])!.metadata.name |
| 126 | } |
| 127 | |
| 128 | fn goops_slot_options_native(mut vm VM, args []Value) !Value { |
| 129 | check_arity('slot-definition-options', args.len, 1, 1)! |
| 130 | return vm.checked_goops_slot_definition(args[0])!.options |
| 131 | } |
| 132 | |
| 133 | fn goops_slot_allocation_native(mut vm VM, args []Value) !Value { |
| 134 | check_arity('slot-definition-allocation', args.len, 1, 1)! |
| 135 | slot := vm.checked_goops_slot(args[0])! |
| 136 | return slot.metadata.allocation |
| 137 | } |
| 138 | |
| 139 | fn goops_slot_getter_native(mut vm VM, args []Value) !Value { |
| 140 | check_arity('slot-definition-getter', args.len, 1, 1)! |
| 141 | return vm.checked_goops_slot(args[0])!.getter |
| 142 | } |
| 143 | |
| 144 | fn goops_slot_setter_native(mut vm VM, args []Value) !Value { |
| 145 | check_arity('slot-definition-setter', args.len, 1, 1)! |
| 146 | return vm.checked_goops_slot(args[0])!.setter |
| 147 | } |
| 148 | |
| 149 | fn goops_slot_accessor_native(mut vm VM, args []Value) !Value { |
| 150 | check_arity('slot-definition-accessor', args.len, 1, 1)! |
| 151 | return vm.checked_goops_slot(args[0])!.accessor |
| 152 | } |
| 153 | |
| 154 | fn goops_slot_init_value_native(mut vm VM, args []Value) !Value { |
| 155 | check_arity('slot-definition-init-value', args.len, 1, 1)! |
| 156 | slot := vm.checked_goops_slot(args[0])! |
| 157 | return slot.init_value |
| 158 | } |
| 159 | |
| 160 | fn goops_slot_init_form_native(mut vm VM, args []Value) !Value { |
| 161 | check_arity('slot-definition-init-form', args.len, 1, 1)! |
| 162 | metadata := vm.checked_goops_slot(args[0])!.metadata |
| 163 | return if metadata.has_init_form { metadata.init_form } else { vm.goops.unbound } |
| 164 | } |
| 165 | |
| 166 | fn goops_slot_init_thunk_native(mut vm VM, args []Value) !Value { |
| 167 | check_arity('slot-definition-init-thunk', args.len, 1, 1)! |
| 168 | return vm.checked_goops_slot(args[0])!.metadata.init_thunk |
| 169 | } |
| 170 | |
| 171 | fn goops_slot_init_keyword_native(mut vm VM, args []Value) !Value { |
| 172 | check_arity('slot-definition-init-keyword', args.len, 1, 1)! |
| 173 | slot := vm.checked_goops_slot(args[0])! |
| 174 | return slot.metadata.init_keyword |
| 175 | } |
| 176 | |
| 177 | fn goops_slot_init_function_native(mut vm VM, args []Value) !Value { |
| 178 | check_arity('slot-init-function', args.len, 2, 2)! |
| 179 | class := vm.checked_goops_class(args[0])! |
| 180 | if args[1].tag != .symbol { return failure('slot-init-function needs a symbol slot name') } |
| 181 | index := goops_slot_index(class, args[1].text()) |
| 182 | if index < 0 { return failure('slot not found ${args[1].text()}') } |
| 183 | return class.slots[index].metadata.init_thunk |
| 184 | } |
| 185 | |