| 1 | module scheme |
| 2 | |
| 3 | fn (vm &VM) goops_field_pointer(object Value, index int) !&Value { |
| 4 | if index < 0 { return failure('slot has no allocated field') } |
| 5 | if object.goops_kind() == .goops_instance { |
| 6 | o := vm.checked_goops_instance(object)! |
| 7 | if index >= o.field_count { return failure('slot index exceeds instance layout') } |
| 8 | return unsafe { o.fields + index } |
| 9 | } |
| 10 | fields := match object.goops_kind() { |
| 11 | .goops_class { vm.checked_goops_class(object)!.fields } |
| 12 | .goops_slot { vm.checked_goops_slot_definition(object)!.fields } |
| 13 | .goops_method { vm.checked_goops_method(object)!.fields } |
| 14 | .procedure { vm.checked_goops_generic(object)!.fields } |
| 15 | else { return failure('object has no instance fields') } |
| 16 | } |
| 17 | if index >= fields.len { return failure('slot index exceeds metaobject layout') } |
| 18 | return unsafe { &Value(fields.data) + index } |
| 19 | } |
| 20 | |
| 21 | fn goops_unbound_fields(vm &VM, count int) []Value { |
| 22 | return filled_storage(count, vm.goops.unbound) |
| 23 | } |
| 24 | |
| 25 | fn (mut vm VM) goops_allocate_object(class &GoopsClass) !Value { |
| 26 | if !vm.goops_retains_owner(v_arena_current(), class.allocation_owner) { |
| 27 | return failure('instance class crosses an arena boundary') |
| 28 | } |
| 29 | object := vm.goops_allocate_body(class)! |
| 30 | return vm.register_goops_finalizer(class, object) |
| 31 | } |
| 32 | |
| 33 | fn (mut vm VM) goops_allocate_body(class &GoopsClass) !Value { |
| 34 | match class.representation { |
| 35 | .class { |
| 36 | return goops_class_value(&GoopsClass{ |
| 37 | owner: vm.owner |
| 38 | name: '<uninitialized>' |
| 39 | name_value: vm.goops.unbound |
| 40 | meta: class |
| 41 | meta_layout_version: class.version |
| 42 | representation: .object |
| 43 | fields: goops_unbound_fields(vm, class.instance_fields) |
| 44 | layout: vm.goops.unbound |
| 45 | finalizer: boolean(false) |
| 46 | printer: boolean(false) |
| 47 | }) |
| 48 | } |
| 49 | .slot { |
| 50 | slot := &GoopsSlot{ |
| 51 | metadata: &GoopsSlotMetadata{ name: vm.goops.unbound, allocation: keyword('instance')!, init_keyword: boolean(false), init_form: vm.goops.unbound, init_thunk: boolean(false), options: nil_value() } |
| 52 | name: '<uninitialized>' |
| 53 | init_value: vm.goops.unbound |
| 54 | init_thunk: boolean(false) |
| 55 | getter: boolean(false) |
| 56 | setter: boolean(false) |
| 57 | accessor: boolean(false) |
| 58 | raw_getter: boolean(false) |
| 59 | checked_getter: boolean(false) |
| 60 | raw_setter: boolean(false) |
| 61 | } |
| 62 | return payload_value(.goops_slot, &GoopsSlotDefinition{ owner: vm.owner, class: vm.goops.slot_class, type_class: class, layout_version: class.version, slot: slot, options: nil_value(), fields: goops_unbound_fields(vm, class.instance_fields) }) |
| 63 | } |
| 64 | .generic, .generic_setter, .accessor { |
| 65 | generic := if class.representation == .generic { |
| 66 | goops_new_generic(mut vm, boolean(false)) |
| 67 | } else { |
| 68 | goops_new_accessor(mut vm, boolean(false), boolean(false), class.representation == .accessor) |
| 69 | } |
| 70 | mut data := vm.checked_goops_generic(generic)! |
| 71 | data.type_class = class |
| 72 | data.layout_version = class.version |
| 73 | data.fields = goops_unbound_fields(vm, class.instance_fields) |
| 74 | return generic |
| 75 | } |
| 76 | .method { |
| 77 | return goops_method_value(&GoopsMethod{ owner: vm.owner, allocation_owner: v_arena_current(), type_class: class, layout_version: class.version, fields: goops_unbound_fields(vm, class.instance_fields), procedure: boolean(false), generic: boolean(false), formals: nil_value(), body: nil_value(), source: boolean(false), make_procedure: boolean(false), slot_definition: boolean(false) }) |
| 78 | } |
| 79 | .object { |
| 80 | if goops_cpl_rank(class, vm.goops.object) < 0 { |
| 81 | return failure('cannot allocate a non-object builtin class') |
| 82 | } |
| 83 | if class.instance_fields > (max_int - goops_field_offset()) / int(sizeof(Value)) { |
| 84 | return failure('class has too many fields') |
| 85 | } |
| 86 | offset := goops_field_offset() |
| 87 | data := unsafe { vcalloc(offset + class.instance_fields * int(sizeof(Value))) } |
| 88 | fields := if class.instance_fields == 0 { |
| 89 | unsafe { &Value(nil) } |
| 90 | } else { |
| 91 | unsafe { &Value(data + offset) } |
| 92 | } |
| 93 | mut instance := unsafe { &GoopsInstance(data) } |
| 94 | unsafe { *instance = GoopsInstance{ class: class, fields: fields, field_count: class.instance_fields, layout_version: class.version } } |
| 95 | for i in 0 .. class.instance_fields { |
| 96 | unsafe { fields[i] = vm.goops.unbound } |
| 97 | } |
| 98 | return payload_value(.goops_instance, instance) |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | fn (vm &VM) goops_set_direct_definitions(mut class GoopsClass, input Value) ! { |
| 104 | values := proper_list_values(input)! |
| 105 | mut slots := []GoopsSlot{cap: values.len} |
| 106 | for value in values { |
| 107 | slot := vm.goops_retained_slot(class.allocation_owner, value)!.slot |
| 108 | if slot.metadata.name.tag != .symbol { return failure('class slot needs a symbol name') } |
| 109 | for prior in slots { |
| 110 | if prior.name == slot.name { return failure('duplicate direct slot ${slot.name}') } |
| 111 | } |
| 112 | slots << *slot |
| 113 | } |
| 114 | class.direct_slots = slots |
| 115 | class.direct_definitions = values |
| 116 | } |
| 117 | |
| 118 | fn (vm &VM) goops_set_effective_definitions(mut class GoopsClass, input Value) ! { |
| 119 | return vm.goops_store_effective_definitions(mut class, proper_list_values(input)!, unsafe { nil }) |
| 120 | } |
| 121 | |
| 122 | fn goops_class_copy_owns_definition(definition &GoopsSlotDefinition, staged &GoopsClass, target &GoopsClass) bool { |
| 123 | return voidptr(definition.class) == voidptr(staged) || voidptr(definition.class) == voidptr(target) |
| 124 | || definition.storage.len == 0 |
| 125 | } |
| 126 | |
| 127 | // A copied class's public slot list can refer to another class's canonical |
| 128 | // definitions. Copy those values without retargeting their private backing. |
| 129 | fn (vm &VM) goops_prepare_class_copy_definitions(mut staged GoopsClass, target &GoopsClass) ! { |
| 130 | return vm.goops_store_effective_definitions(mut staged, staged.definitions, target) |
| 131 | } |
| 132 | |
| 133 | fn (vm &VM) goops_store_effective_definitions(mut class GoopsClass, values []Value, copy_target &GoopsClass) ! { |
| 134 | mut slots := []GoopsSlot{cap: values.len} |
| 135 | for value in values { |
| 136 | definition := vm.checked_goops_slot_definition(value)! |
| 137 | if isnil(copy_target) || goops_class_copy_owns_definition(definition, class, copy_target) { |
| 138 | vm.goops_require_mutation(definition.allocation_owner)! |
| 139 | } else if !vm.goops_retains_owner(class.allocation_owner, definition.allocation_owner) { |
| 140 | return failure('class dependency crosses an arena boundary') |
| 141 | } |
| 142 | slot := definition.slot |
| 143 | if slot.metadata.name.tag != .symbol { |
| 144 | return failure('effective slot needs a symbol name') |
| 145 | } |
| 146 | for prior in slots { |
| 147 | if prior.name == slot.name { return failure('duplicate effective slot ${slot.name}') } |
| 148 | } |
| 149 | slots << *slot |
| 150 | } |
| 151 | // All definitions are checked before the first retarget. No callback runs |
| 152 | // between this preflight and publication, so a later invalid target cannot |
| 153 | // leave an earlier definition pointing at unpublished storage. |
| 154 | // Retarget canonical definitions only after the entire storage is allocated. |
| 155 | // Every handle retains that exact array, so later class layout replacement |
| 156 | // cannot leave an escaped getter or slot handle pointing at reclaimed storage. |
| 157 | for i, value in values { |
| 158 | mut definition := value.goops_slot_pointer() |
| 159 | if !isnil(copy_target) && !goops_class_copy_owns_definition(definition, class, copy_target) { |
| 160 | continue |
| 161 | } |
| 162 | definition.slot = unsafe { &slots[i] } |
| 163 | definition.class = if isnil(copy_target) { &class } else { copy_target } |
| 164 | definition.storage = slots |
| 165 | } |
| 166 | class.slots = slots |
| 167 | class.definitions = values |
| 168 | class.call_slot = goops_slot_index(class, 'procedure') |
| 169 | if goops_cpl_rank(class, vm.goops.applicable_struct) < 0 { class.call_slot = -1 } |
| 170 | class.has_init_thunks = false |
| 171 | for i in 0 .. class.slots.len { |
| 172 | mut slot := &class.slots[i] |
| 173 | slot.read_only = vm.goops_slot_kind_flag(values[i], '<read-only-slot>') |
| 174 | slot.hidden = vm.goops_slot_kind_flag(values[i], '<hidden-slot>') |
| 175 | slot.protected = vm.goops_slot_kind_flag(values[i], '<protected-slot>') |
| 176 | slot.opaque = vm.goops_slot_kind_flag(values[i], '<opaque-slot>') |
| 177 | if !slot.class_allocated { |
| 178 | slot.has_init_value = false |
| 179 | slot.has_init_thunk = slot.metadata.init_thunk.is_true() |
| 180 | if slot.has_init_thunk && slot.metadata.init_thunk.tag == .procedure { |
| 181 | p := slot.metadata.init_thunk.procedure_pointer() |
| 182 | f := p.function |
| 183 | if p.mode == .closure && !isnil(f) && isnil(f.code) && f.params == 0 && f.instructions.len == 2 && f.constants.len == 1 && f.instructions[0].op == .constant && f.instructions[1].op == .ret && eqv(f.constants[0], slot.init_value) { |
| 184 | slot.has_init_value = true |
| 185 | slot.has_init_thunk = false |
| 186 | } |
| 187 | } |
| 188 | if slot.has_init_thunk || slot.virtual { class.has_init_thunks = true } |
| 189 | } |
| 190 | } |
| 191 | class.representation = vm.goops_representation(class) |
| 192 | } |
| 193 | |
| 194 | fn (vm &VM) goops_slot_kind_flag(value Value, name string) bool { |
| 195 | wanted := vm.goops.protocol_classes[name] or { return false } |
| 196 | definition := value.goops_slot_pointer() |
| 197 | actual := if isnil(definition.type_class) { vm.goops.slot_class } else { definition.type_class } |
| 198 | return goops_cpl_rank(actual, wanted) >= 0 |
| 199 | } |
| 200 | |
| 201 | fn (vm &VM) goops_representation(class &GoopsClass) GoopsRepresentation { |
| 202 | if goops_cpl_rank(class, vm.goops.meta) >= 0 { return .class } |
| 203 | if goops_cpl_rank(class, vm.goops.slot_class) >= 0 { return .slot } |
| 204 | if goops_cpl_rank(class, vm.goops.accessor_class) >= 0 { return .accessor } |
| 205 | if goops_cpl_rank(class, vm.goops.generic_setter_class) >= 0 { return .generic_setter } |
| 206 | if goops_cpl_rank(class, vm.goops.generic_class) >= 0 { return .generic } |
| 207 | if goops_cpl_rank(class, vm.goops.method_class) >= 0 { return .method } |
| 208 | return .object |
| 209 | } |
| 210 | |
| 211 | fn goops_allocate_native(mut vm VM, args []Value) !Value { |
| 212 | check_arity('allocate-instance', args.len, 2, 2)! |
| 213 | return vm.goops_allocate_object(vm.checked_goops_class(args[0])!) |
| 214 | } |
| 215 | |
| 216 | fn goops_std_cpl_native(mut vm VM, args []Value) !Value { |
| 217 | check_arity('compute-std-cpl', args.len, 1, 1)! |
| 218 | class := vm.checked_goops_class(args[0])! |
| 219 | for parent in class.supers { |
| 220 | if voidptr(parent) == voidptr(class) || goops_cpl_rank(parent, class) >= 0 { |
| 221 | return failure('circular class inheritance') |
| 222 | } |
| 223 | } |
| 224 | mut result := []&GoopsClass{cap: 1 + class.supers.len * 3} |
| 225 | result << class |
| 226 | result << goops_merge_cpl(class.supers)! |
| 227 | return goops_class_list(result) |
| 228 | } |
| 229 | |
| 230 | fn goops_std_slots_native(mut vm VM, args []Value) !Value { |
| 231 | check_arity('compute-slots', args.len, 1, 1)! |
| 232 | class := vm.checked_goops_class(args[0])! |
| 233 | mut values := []Value{} |
| 234 | mut names := []string{} |
| 235 | for ancestor_index := class.cpl.len - 1; ancestor_index >= 0; ancestor_index-- { |
| 236 | ancestor := class.cpl[ancestor_index] |
| 237 | for definition in ancestor.direct_definitions { |
| 238 | slot := vm.checked_goops_slot(definition)! |
| 239 | name := slot.metadata.name |
| 240 | if name.tag != .symbol { return failure('slot name must be a symbol') } |
| 241 | index := names.index(name.text()) |
| 242 | if index < 0 { |
| 243 | names << name.text() |
| 244 | values << definition |
| 245 | } else { |
| 246 | container_store_validated(mut values, index, definition) |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | return list(values) |
| 251 | } |
| 252 | |
| 253 | fn goops_direct_initargs_native(mut vm VM, args []Value) !Value { |
| 254 | check_arity('compute-direct-slot-definition-initargs', args.len, 2, 2)! |
| 255 | vm.checked_goops_class(args[0])! |
| 256 | if args[1].tag == .symbol { return list([keyword('name')!, args[1]]) } |
| 257 | if args[1].tag != .pair { return failure('slot specification must be a symbol or a list') } |
| 258 | pair := args[1].pair_pointer() |
| 259 | if pair.car.tag != .symbol { return failure('slot name must be a symbol') } |
| 260 | _ = goops_option_list_length(pair.cdr)! |
| 261 | mut options := copy_list(pair.cdr)! |
| 262 | thunk := goops_option_value(options, 'init-thunk', boolean(false)) |
| 263 | value := goops_option_value(options, 'init-value', vm.goops.unbound) |
| 264 | if !thunk.is_true() && !vm.goops_unbound(value) { |
| 265 | options = cons(keyword('init-thunk')!, cons(goops_constant_initializer(vm, value), options)) |
| 266 | } |
| 267 | return cons(keyword('name')!, cons(pair.car, options)) |
| 268 | } |
| 269 | |
| 270 | fn goops_get_keyword_native(mut _vm VM, args []Value) !Value { |
| 271 | check_arity('get-keyword', args.len, 2, 3)! |
| 272 | if args[0].tag != .keyword { return failure('get-keyword needs a keyword') } |
| 273 | _ = goops_option_list_length(args[1])! |
| 274 | return goops_option_value(args[1], args[0].text(), if args.len == 3 { |
| 275 | args[2] |
| 276 | } else { |
| 277 | boolean(false) |
| 278 | }) |
| 279 | } |
| 280 | |
| 281 | fn goops_unbound_native(mut vm VM, args []Value) !Value { |
| 282 | check_arity('unbound?', args.len, 1, 1)! |
| 283 | return boolean(vm.goops_unbound(args[0])) |
| 284 | } |
| 285 | |
| 286 | fn goops_validate_initargs_native(mut _vm VM, args []Value) !Value { |
| 287 | check_arity('validate-initargs', args.len, 1, 1)! |
| 288 | _ = goops_option_list_length(args[0])! |
| 289 | return args[0] |
| 290 | } |
| 291 | |
| 292 | fn goops_configure_slot_native(mut vm VM, args []Value) !Value { |
| 293 | check_arity('configure-slot', args.len, 2, 2)! |
| 294 | mut d := vm.checked_goops_slot_definition(args[0])! |
| 295 | vm.goops_require_mutation(d.allocation_owner)! |
| 296 | _ = goops_option_list_length(args[1])! |
| 297 | mut slot := d.slot |
| 298 | id := goops_option_value(args[1], '%native-field', boolean(false)) |
| 299 | if id.is_true() { |
| 300 | n := id.as_integer()! |
| 301 | if n <= 0 || n > int(GoopsNativeField.method_source) { |
| 302 | return failure('invalid native slot field') |
| 303 | } |
| 304 | slot.native_field = unsafe { GoopsNativeField(n) } |
| 305 | } |
| 306 | d.options = args[1] |
| 307 | slot.metadata.options = args[1] |
| 308 | vm.goops_check_init_thunk(slot.metadata.init_thunk)! |
| 309 | return args[0] |
| 310 | } |
| 311 | |
| 312 | fn goops_goops_error_native(mut _vm VM, args []Value) !Value { |
| 313 | check_arity('goops-error', args.len, 1, -1)! |
| 314 | mut message := if args[0].tag == .string { args[0].text() } else { write(args[0])! } |
| 315 | for value in args[1..] { message += ' ' + write(value)! } |
| 316 | return failure(message) |
| 317 | } |
| 318 | |